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