2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.internal.app;
19 import android.app.Activity;
20 import android.content.ComponentName;
21 import android.content.Intent;
22 import android.content.IntentSender;
23 import android.content.pm.ActivityInfo;
24 import android.os.Bundle;
25 import android.os.Parcelable;
26 import android.util.Log;
27 import android.util.Slog;
29 public class ChooserActivity extends ResolverActivity {
30 private static final String TAG = "ChooserActivity";
32 private Bundle mReplacementExtras;
33 private IntentSender mChosenComponentSender;
36 protected void onCreate(Bundle savedInstanceState) {
37 Intent intent = getIntent();
38 Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT);
39 if (!(targetParcelable instanceof Intent)) {
40 Log.w("ChooserActivity", "Target is not an intent: " + targetParcelable);
45 Intent target = (Intent)targetParcelable;
47 modifyTargetIntent(target);
49 mReplacementExtras = intent.getBundleExtra(Intent.EXTRA_REPLACEMENT_EXTRAS);
50 CharSequence title = intent.getCharSequenceExtra(Intent.EXTRA_TITLE);
51 int defaultTitleRes = 0;
53 defaultTitleRes = com.android.internal.R.string.chooseActivity;
55 Parcelable[] pa = intent.getParcelableArrayExtra(Intent.EXTRA_INITIAL_INTENTS);
56 Intent[] initialIntents = null;
58 initialIntents = new Intent[pa.length];
59 for (int i=0; i<pa.length; i++) {
60 if (!(pa[i] instanceof Intent)) {
61 Log.w("ChooserActivity", "Initial intent #" + i + " not an Intent: " + pa[i]);
66 final Intent in = (Intent) pa[i];
67 modifyTargetIntent(in);
68 initialIntents[i] = in;
71 mChosenComponentSender = intent.getParcelableExtra(
72 Intent.EXTRA_CHOSEN_COMPONENT_INTENT_SENDER);
73 setSafeForwardingMode(true);
74 super.onCreate(savedInstanceState, target, title, defaultTitleRes, initialIntents,
79 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
80 Intent result = defIntent;
81 if (mReplacementExtras != null) {
82 final Bundle replExtras = mReplacementExtras.getBundle(aInfo.packageName);
83 if (replExtras != null) {
84 result = new Intent(defIntent);
85 result.putExtras(replExtras);
88 if (aInfo.name.equals(IntentForwarderActivity.FORWARD_INTENT_TO_USER_OWNER)
89 || aInfo.name.equals(IntentForwarderActivity.FORWARD_INTENT_TO_MANAGED_PROFILE)) {
90 result = Intent.createChooser(result,
91 getIntent().getCharSequenceExtra(Intent.EXTRA_TITLE));
97 public void onActivityStarted(Intent intent) {
98 if (mChosenComponentSender != null) {
99 final ComponentName target = intent.getComponent();
100 if (target != null) {
101 final Intent fillIn = new Intent().putExtra(Intent.EXTRA_CHOSEN_COMPONENT, target);
103 mChosenComponentSender.sendIntent(this, Activity.RESULT_OK, fillIn, null, null);
104 } catch (IntentSender.SendIntentException e) {
105 Slog.e(TAG, "Unable to launch supplied IntentSender to report "
106 + "the chosen component: " + e);
112 private void modifyTargetIntent(Intent in) {
113 final String action = in.getAction();
114 if (Intent.ACTION_SEND.equals(action) ||
115 Intent.ACTION_SEND_MULTIPLE.equals(action)) {
116 in.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
117 Intent.FLAG_ACTIVITY_MULTIPLE_TASK);