ChooserActivity.java revision 13420f2311757554c314f620c83cb55153b67612
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.content.Intent;
20import android.os.Bundle;
21import android.os.Parcelable;
22import android.util.Log;
23
24public class ChooserActivity extends ResolverActivity {
25    private Bundle mReplacementExtras;
26
27    @Override
28    protected void onCreate(Bundle savedInstanceState) {
29        Intent intent = getIntent();
30        Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT);
31        if (!(targetParcelable instanceof Intent)) {
32            Log.w("ChooserActivity", "Target is not an intent: " + targetParcelable);
33            finish();
34            super.onCreate(null);
35            return;
36        }
37        Intent target = (Intent)targetParcelable;
38        if (target != null) {
39            modifyTargetIntent(target);
40        }
41        mReplacementExtras = intent.getBundleExtra(Intent.EXTRA_REPLACEMENT_EXTRAS);
42        CharSequence title = intent.getCharSequenceExtra(Intent.EXTRA_TITLE);
43        int defaultTitleRes = 0;
44        if (title == null) {
45            defaultTitleRes = com.android.internal.R.string.chooseActivity;
46        }
47        Parcelable[] pa = intent.getParcelableArrayExtra(Intent.EXTRA_INITIAL_INTENTS);
48        Intent[] initialIntents = null;
49        if (pa != null) {
50            initialIntents = new Intent[pa.length];
51            for (int i=0; i<pa.length; i++) {
52                if (!(pa[i] instanceof Intent)) {
53                    Log.w("ChooserActivity", "Initial intent #" + i + " not an Intent: " + pa[i]);
54                    finish();
55                    super.onCreate(null);
56                    return;
57                }
58                final Intent in = (Intent) pa[i];
59                modifyTargetIntent(in);
60                initialIntents[i] = in;
61            }
62        }
63        super.onCreate(savedInstanceState, target, title, defaultTitleRes, initialIntents,
64                null, false);
65    }
66
67    public Intent getReplacementIntent(String packageName, Intent defIntent) {
68        if (mReplacementExtras != null) {
69            final Bundle replExtras = mReplacementExtras.getBundle(packageName);
70            if (replExtras != null) {
71                final Intent result = new Intent(defIntent);
72                result.putExtras(replExtras);
73                return result;
74            }
75        }
76        return defIntent;
77    }
78
79    private void modifyTargetIntent(Intent in) {
80        final String action = in.getAction();
81        if (Intent.ACTION_SEND.equals(action) ||
82                Intent.ACTION_SEND_MULTIPLE.equals(action)) {
83            in.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
84                    Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
85        }
86    }
87}
88