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.content.pm.ActivityInfo; 24import android.os.Bundle; 25import android.os.Parcelable; 26import android.util.Log; 27import android.util.Slog; 28 29public class ChooserActivity extends ResolverActivity { 30 private static final String TAG = "ChooserActivity"; 31 32 private Bundle mReplacementExtras; 33 private IntentSender mChosenComponentSender; 34 35 @Override 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); 41 finish(); 42 super.onCreate(null); 43 return; 44 } 45 Intent target = (Intent)targetParcelable; 46 if (target != null) { 47 modifyTargetIntent(target); 48 } 49 mReplacementExtras = intent.getBundleExtra(Intent.EXTRA_REPLACEMENT_EXTRAS); 50 CharSequence title = intent.getCharSequenceExtra(Intent.EXTRA_TITLE); 51 int defaultTitleRes = 0; 52 if (title == null) { 53 defaultTitleRes = com.android.internal.R.string.chooseActivity; 54 } 55 Parcelable[] pa = intent.getParcelableArrayExtra(Intent.EXTRA_INITIAL_INTENTS); 56 Intent[] initialIntents = null; 57 if (pa != 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]); 62 finish(); 63 super.onCreate(null); 64 return; 65 } 66 final Intent in = (Intent) pa[i]; 67 modifyTargetIntent(in); 68 initialIntents[i] = in; 69 } 70 } 71 mChosenComponentSender = intent.getParcelableExtra( 72 Intent.EXTRA_CHOSEN_COMPONENT_INTENT_SENDER); 73 setSafeForwardingMode(true); 74 super.onCreate(savedInstanceState, target, title, defaultTitleRes, initialIntents, 75 null, false); 76 } 77 78 @Override 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); 86 } 87 } 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)); 92 } 93 return result; 94 } 95 96 @Override 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); 102 try { 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); 107 } 108 } 109 } 110 } 111 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); 118 } 119 } 120} 121