Home | History | Annotate | Download | only in app
      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.ActivityManager;
     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.ImageView;
     36 import 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  */
     43 public 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         TypedValue out = new TypedValue();
     88         getTheme().resolveAttribute(android.R.attr.alertDialogIcon, out, true);
     89         getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
     90                 out.resourceId);
     91     }
     92 
     93     void setText(int id, CharSequence text) {
     94         ((TextView)findViewById(id)).setText(text);
     95     }
     96 
     97     void setDrawable(int id, Drawable dr) {
     98         if (dr != null) {
     99             ((ImageView)findViewById(id)).setImageDrawable(dr);
    100         }
    101     }
    102 
    103     void setIconAndText(int iconId, int actionId, int descriptionId,
    104             String packageName, int actionStr, int descriptionStr) {
    105         CharSequence appName = "";
    106         Drawable appIcon = null;
    107         if (mCurApp != null) {
    108             try {
    109                 ApplicationInfo info = getPackageManager().getApplicationInfo(
    110                         packageName, 0);
    111                 appName = info.loadLabel(getPackageManager());
    112                 appIcon = info.loadIcon(getPackageManager());
    113             } catch (PackageManager.NameNotFoundException e) {
    114             }
    115         }
    116 
    117         setDrawable(iconId, appIcon);
    118         setText(actionId, getString(actionStr, appName));
    119         setText(descriptionId, getText(descriptionStr));
    120     }
    121 
    122     private OnClickListener mSwitchOldListener = new OnClickListener() {
    123         public void onClick(View v) {
    124             try {
    125                 ActivityManager.getService().moveTaskToFront(mCurTask, 0, null);
    126             } catch (RemoteException e) {
    127             }
    128             finish();
    129         }
    130     };
    131 
    132     private OnClickListener mSwitchNewListener = new OnClickListener() {
    133         public void onClick(View v) {
    134             try {
    135                 ActivityManager.getService().finishHeavyWeightApp();
    136             } catch (RemoteException e) {
    137             }
    138             try {
    139                 if (mHasResult) {
    140                     startIntentSenderForResult(mStartIntent, -1, null,
    141                             Intent.FLAG_ACTIVITY_FORWARD_RESULT,
    142                             Intent.FLAG_ACTIVITY_FORWARD_RESULT, 0);
    143                 } else {
    144                     startIntentSenderForResult(mStartIntent, -1, null, 0, 0, 0);
    145                 }
    146             } catch (IntentSender.SendIntentException ex) {
    147                 Log.w("HeavyWeightSwitcherActivity", "Failure starting", ex);
    148             }
    149             finish();
    150         }
    151     };
    152 
    153     private OnClickListener mCancelListener = new OnClickListener() {
    154         public void onClick(View v) {
    155             finish();
    156         }
    157     };
    158 }
    159