ChooserActivity.java revision 0b3c11260ae5ea09c7e702802b39f77afc74f35f
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.android.internal.app;
18
19import android.app.Activity;
20import android.content.ComponentName;
21import android.content.Intent;
22import android.content.IntentSender;
23import android.os.Bundle;
24import android.os.Parcelable;
25import android.util.Log;
26import android.util.Slog;
27
28public class ChooserActivity extends ResolverActivity {
29    private static final String TAG = "ChooserActivity";
30
31    private Bundle mReplacementExtras;
32    private IntentSender mChosenComponentSender;
33
34    @Override
35    protected void onCreate(Bundle savedInstanceState) {
36        Intent intent = getIntent();
37        Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT);
38        if (!(targetParcelable instanceof Intent)) {
39            Log.w("ChooserActivity", "Target is not an intent: " + targetParcelable);
40            finish();
41            super.onCreate(null);
42            return;
43        }
44        Intent target = (Intent)targetParcelable;
45        if (target != null) {
46            modifyTargetIntent(target);
47        }
48        mReplacementExtras = intent.getBundleExtra(Intent.EXTRA_REPLACEMENT_EXTRAS);
49        CharSequence title = intent.getCharSequenceExtra(Intent.EXTRA_TITLE);
50        int defaultTitleRes = 0;
51        if (title == null) {
52            defaultTitleRes = com.android.internal.R.string.chooseActivity;
53        }
54        Parcelable[] pa = intent.getParcelableArrayExtra(Intent.EXTRA_INITIAL_INTENTS);
55        Intent[] initialIntents = null;
56        if (pa != null) {
57            initialIntents = new Intent[pa.length];
58            for (int i=0; i<pa.length; i++) {
59                if (!(pa[i] instanceof Intent)) {
60                    Log.w("ChooserActivity", "Initial intent #" + i + " not an Intent: " + pa[i]);
61                    finish();
62                    super.onCreate(null);
63                    return;
64                }
65                final Intent in = (Intent) pa[i];
66                modifyTargetIntent(in);
67                initialIntents[i] = in;
68            }
69        }
70        mChosenComponentSender = intent.getParcelableExtra(
71                Intent.EXTRA_CHOSEN_COMPONENT_INTENT_SENDER);
72        setSafeForwardingMode(true);
73        super.onCreate(savedInstanceState, target, title, defaultTitleRes, initialIntents,
74                null, false);
75    }
76
77    @Override
78    public Intent getReplacementIntent(String packageName, Intent defIntent) {
79        if (mReplacementExtras != null) {
80            final Bundle replExtras = mReplacementExtras.getBundle(packageName);
81            if (replExtras != null) {
82                final Intent result = new Intent(defIntent);
83                result.putExtras(replExtras);
84                return result;
85            }
86        }
87        return defIntent;
88    }
89
90    @Override
91    public void onActivityStarted(Intent intent) {
92        if (mChosenComponentSender != null) {
93            final ComponentName target = intent.getComponent();
94            if (target != null) {
95                final Intent fillIn = new Intent().putExtra(Intent.EXTRA_CHOSEN_COMPONENT, target);
96                try {
97                    mChosenComponentSender.sendIntent(this, Activity.RESULT_OK, fillIn, null, null);
98                } catch (IntentSender.SendIntentException e) {
99                    Slog.e(TAG, "Unable to launch supplied IntentSender to report "
100                            + "the chosen component: " + e);
101                }
102            }
103        }
104    }
105
106    private void modifyTargetIntent(Intent in) {
107        final String action = in.getAction();
108        if (Intent.ACTION_SEND.equals(action) ||
109                Intent.ACTION_SEND_MULTIPLE.equals(action)) {
110            in.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
111                    Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
112        }
113    }
114}
115