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