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_NO_TITLE);
     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, 0,
     76                 mCurApp, mNewApp, R.string.old_app_action, 0);
     77         setIconAndText(R.id.new_app_icon, R.id.new_app_action, R.id.new_app_description,
     78                 mNewApp, mCurApp, 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     }
     85 
     86     void setText(int id, CharSequence text) {
     87         ((TextView)findViewById(id)).setText(text);
     88     }
     89 
     90     void setDrawable(int id, Drawable dr) {
     91         if (dr != null) {
     92             ((ImageView)findViewById(id)).setImageDrawable(dr);
     93         }
     94     }
     95 
     96     void setIconAndText(int iconId, int actionId, int descriptionId,
     97             String packageName, String otherPackageName, int actionStr, int descriptionStr) {
     98         CharSequence appName = packageName;
     99         Drawable appIcon = null;
    100         if (packageName != null) {
    101             try {
    102                 ApplicationInfo info = getPackageManager().getApplicationInfo(
    103                         packageName, 0);
    104                 appName = info.loadLabel(getPackageManager());
    105                 appIcon = info.loadIcon(getPackageManager());
    106             } catch (PackageManager.NameNotFoundException e) {
    107             }
    108         }
    109 
    110         setDrawable(iconId, appIcon);
    111         setText(actionId, getString(actionStr, appName));
    112         if (descriptionId != 0) {
    113             CharSequence otherAppName = otherPackageName;
    114             if (otherPackageName != null) {
    115                 try {
    116                     ApplicationInfo info = getPackageManager().getApplicationInfo(
    117                             otherPackageName, 0);
    118                     otherAppName = info.loadLabel(getPackageManager());
    119                 } catch (PackageManager.NameNotFoundException e) {
    120                 }
    121             }
    122             setText(descriptionId, getString(descriptionStr, otherAppName));
    123         }
    124     }
    125 
    126     private OnClickListener mSwitchOldListener = new OnClickListener() {
    127         public void onClick(View v) {
    128             try {
    129                 ActivityManager.getService().moveTaskToFront(mCurTask, 0, null);
    130             } catch (RemoteException e) {
    131             }
    132             finish();
    133         }
    134     };
    135 
    136     private OnClickListener mSwitchNewListener = new OnClickListener() {
    137         public void onClick(View v) {
    138             try {
    139                 ActivityManager.getService().finishHeavyWeightApp();
    140             } catch (RemoteException e) {
    141             }
    142             try {
    143                 if (mHasResult) {
    144                     startIntentSenderForResult(mStartIntent, -1, null,
    145                             Intent.FLAG_ACTIVITY_FORWARD_RESULT,
    146                             Intent.FLAG_ACTIVITY_FORWARD_RESULT, 0);
    147                 } else {
    148                     startIntentSenderForResult(mStartIntent, -1, null, 0, 0, 0);
    149                 }
    150             } catch (IntentSender.SendIntentException ex) {
    151                 Log.w("HeavyWeightSwitcherActivity", "Failure starting", ex);
    152             }
    153             finish();
    154         }
    155     };
    156 }
    157