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