1 /* 2 * Copyright (C) 2010 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 17 package com.android.internal.app; 18 19 import android.app.Activity; 20 import android.app.ActivityManager; 21 import android.app.ActivityTaskManager; 22 import android.app.ActivityThread; 23 import android.app.IApplicationThread; 24 import android.content.Intent; 25 import android.content.IntentSender; 26 import android.content.pm.ApplicationInfo; 27 import android.content.pm.PackageManager; 28 import android.graphics.drawable.Drawable; 29 import android.os.Bundle; 30 import android.os.RemoteException; 31 import android.util.Log; 32 import android.view.View; 33 import android.view.View.OnClickListener; 34 import android.view.Window; 35 import android.widget.ImageView; 36 import android.widget.TextView; 37 38 import com.android.internal.R; 39 40 /** 41 * This activity is displayed when the system attempts to start an Intent for 42 * which there is more than one matching activity, allowing the user to decide 43 * which to go to. It is not normally used directly by application developers. 44 */ 45 public class HeavyWeightSwitcherActivity extends Activity { 46 /** The PendingIntent of the new activity being launched. */ 47 public static final String KEY_INTENT = "intent"; 48 /** Set if the caller is requesting a result. */ 49 public static final String KEY_HAS_RESULT = "has_result"; 50 /** Package of current heavy-weight app. */ 51 public static final String KEY_CUR_APP = "cur_app"; 52 /** Task that current heavy-weight activity is running in. */ 53 public static final String KEY_CUR_TASK = "cur_task"; 54 /** Package of newly requested heavy-weight app. */ 55 public static final String KEY_NEW_APP = "new_app"; 56 57 IntentSender mStartIntent; 58 boolean mHasResult; 59 String mCurApp; 60 int mCurTask; 61 String mNewApp; 62 63 @Override onCreate(Bundle savedInstanceState)64 protected void onCreate(Bundle savedInstanceState) { 65 super.onCreate(savedInstanceState); 66 67 requestWindowFeature(Window.FEATURE_NO_TITLE); 68 69 mStartIntent = (IntentSender)getIntent().getParcelableExtra(KEY_INTENT); 70 mHasResult = getIntent().getBooleanExtra(KEY_HAS_RESULT, false); 71 mCurApp = getIntent().getStringExtra(KEY_CUR_APP); 72 mCurTask = getIntent().getIntExtra(KEY_CUR_TASK, 0); 73 mNewApp = getIntent().getStringExtra(KEY_NEW_APP); 74 75 setContentView(com.android.internal.R.layout.heavy_weight_switcher); 76 77 setIconAndText(R.id.old_app_icon, R.id.old_app_action, 0, 78 mCurApp, mNewApp, R.string.old_app_action, 0); 79 setIconAndText(R.id.new_app_icon, R.id.new_app_action, R.id.new_app_description, 80 mNewApp, mCurApp, R.string.new_app_action, R.string.new_app_description); 81 82 View button = findViewById((R.id.switch_old)); 83 button.setOnClickListener(mSwitchOldListener); 84 button = findViewById((R.id.switch_new)); 85 button.setOnClickListener(mSwitchNewListener); 86 } 87 setText(int id, CharSequence text)88 void setText(int id, CharSequence text) { 89 ((TextView)findViewById(id)).setText(text); 90 } 91 setDrawable(int id, Drawable dr)92 void setDrawable(int id, Drawable dr) { 93 if (dr != null) { 94 ((ImageView)findViewById(id)).setImageDrawable(dr); 95 } 96 } 97 setIconAndText(int iconId, int actionId, int descriptionId, String packageName, String otherPackageName, int actionStr, int descriptionStr)98 void setIconAndText(int iconId, int actionId, int descriptionId, 99 String packageName, String otherPackageName, int actionStr, int descriptionStr) { 100 CharSequence appName = packageName; 101 Drawable appIcon = null; 102 if (packageName != null) { 103 try { 104 ApplicationInfo info = getPackageManager().getApplicationInfo( 105 packageName, 0); 106 appName = info.loadLabel(getPackageManager()); 107 appIcon = info.loadIcon(getPackageManager()); 108 } catch (PackageManager.NameNotFoundException e) { 109 } 110 } 111 112 setDrawable(iconId, appIcon); 113 setText(actionId, getString(actionStr, appName)); 114 if (descriptionId != 0) { 115 CharSequence otherAppName = otherPackageName; 116 if (otherPackageName != null) { 117 try { 118 ApplicationInfo info = getPackageManager().getApplicationInfo( 119 otherPackageName, 0); 120 otherAppName = info.loadLabel(getPackageManager()); 121 } catch (PackageManager.NameNotFoundException e) { 122 } 123 } 124 setText(descriptionId, getString(descriptionStr, otherAppName)); 125 } 126 } 127 128 private OnClickListener mSwitchOldListener = new OnClickListener() { 129 public void onClick(View v) { 130 try { 131 ActivityThread thread = ActivityThread.currentActivityThread(); 132 IApplicationThread appThread = thread.getApplicationThread(); 133 ActivityTaskManager.getService().moveTaskToFront(appThread, getPackageName(), 134 mCurTask, 0, null); 135 } catch (RemoteException e) { 136 } 137 finish(); 138 } 139 }; 140 141 private OnClickListener mSwitchNewListener = new OnClickListener() { 142 public void onClick(View v) { 143 try { 144 ActivityManager.getService().finishHeavyWeightApp(); 145 } catch (RemoteException e) { 146 } 147 try { 148 if (mHasResult) { 149 startIntentSenderForResult(mStartIntent, -1, null, 150 Intent.FLAG_ACTIVITY_FORWARD_RESULT, 151 Intent.FLAG_ACTIVITY_FORWARD_RESULT, 0); 152 } else { 153 startIntentSenderForResult(mStartIntent, -1, null, 0, 0, 0); 154 } 155 } catch (IntentSender.SendIntentException ex) { 156 Log.w("HeavyWeightSwitcherActivity", "Failure starting", ex); 157 } 158 finish(); 159 } 160 }; 161 } 162