UninstallerActivity.java revision 2fa4ad8141ed0cb686c7acab27f01e8c2bc2877a
1/*
2**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17package com.android.packageinstaller;
18
19import com.android.packageinstaller.R;
20import android.app.Activity;
21import android.app.AlertDialog;
22import android.app.Dialog;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageManager;
27import android.net.Uri;
28import android.os.Bundle;
29import android.util.Log;
30import android.view.View;
31import android.view.Window;
32import android.view.View.OnClickListener;
33import android.widget.Button;
34import android.content.pm.PackageManager.NameNotFoundException;
35
36/*
37 * This activity presents UI to uninstall an application. Usually launched with intent
38 * Intent.ACTION_UNINSTALL_PKG_COMMAND and attribute
39 * com.android.packageinstaller.PackageName set to the application package name
40 */
41public class UninstallerActivity extends Activity implements OnClickListener {
42    private static final String TAG = "UninstallerActivity";
43    private boolean localLOGV = false;
44    // States indicating status of ui display when uninstalling application
45    private static final int UNINSTALL_CONFIRM = 1;
46    private static final int UNINSTALL_PROGRESS = 2;
47    private static final int UNINSTALL_DONE = 3;
48    private int mCurrentState = UNINSTALL_CONFIRM;
49    PackageManager mPm;
50    private ApplicationInfo mAppInfo;
51    private Button mOk;
52    private Button mCancel;
53
54    // Dialog identifiers used in showDialog
55    private static final int DLG_BASE = 0;
56    private static final int DLG_APP_NOT_FOUND = DLG_BASE + 1;
57    private static final int DLG_UNINSTALL_FAILED = DLG_BASE + 2;
58
59    private void showDialogInner(int id) {
60            showDialog(id);
61    }
62
63    @Override
64    public Dialog onCreateDialog(int id) {
65        switch (id) {
66        case DLG_APP_NOT_FOUND :
67            return new AlertDialog.Builder(this)
68                    .setTitle(R.string.app_not_found_dlg_title)
69                    .setIcon(com.android.internal.R.drawable.ic_dialog_alert)
70                    .setMessage(R.string.app_not_found_dlg_text)
71                    .setNeutralButton(getString(R.string.dlg_ok),
72                            new DialogInterface.OnClickListener() {
73                                public void onClick(DialogInterface dialog, int which) {
74                                    finish();
75                                }})
76                    .create();
77        case DLG_UNINSTALL_FAILED :
78            // Guaranteed not to be null. will default to package name if not set by app
79           CharSequence appTitle = mPm.getApplicationLabel(mAppInfo);
80           String dlgText = getString(R.string.uninstall_failed_msg,
81                    appTitle.toString());
82            // Display uninstall failed dialog
83            return new AlertDialog.Builder(this)
84                    .setTitle(R.string.uninstall_failed)
85                    .setIcon(com.android.internal.R.drawable.ic_dialog_alert)
86                    .setMessage(dlgText)
87                    .setNeutralButton(getString(R.string.dlg_ok),
88                            new DialogInterface.OnClickListener() {
89                                public void onClick(DialogInterface dialog, int which) {
90                                    finish();
91                                }})
92                    .create();
93        }
94        return null;
95    }
96
97    private void startUninstallProgress() {
98        Intent newIntent = new Intent(Intent.ACTION_VIEW);
99        newIntent.putExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO,
100                                                  mAppInfo);
101        newIntent.setClass(this, UninstallAppProgress.class);
102        startActivityForResult(newIntent, UNINSTALL_PROGRESS);
103    }
104
105    private void startUninstallDone() {
106        Intent newIntent = new Intent(Intent.ACTION_VIEW);
107        newIntent.putExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO,
108                                                  mAppInfo);
109        newIntent.putExtra(PackageUtil.INTENT_ATTR_INSTALL_STATUS, true);
110        newIntent.setClass(this, UninstallAppDone.class);
111        startActivityForResult(newIntent, UNINSTALL_DONE);
112    }
113
114    @Override
115    public void onCreate(Bundle icicle) {
116        super.onCreate(icicle);
117        //get intent information
118        final Intent intent = getIntent();
119        Uri packageURI = intent.getData();
120        String packageName = packageURI.getEncodedSchemeSpecificPart();
121        if(packageName == null) {
122            Log.e(TAG, "Invalid package name:"+packageName);
123            showDialog(DLG_APP_NOT_FOUND);
124            return;
125        }
126        //initialize package manager
127        mPm = getPackageManager();
128        boolean errFlag = false;
129        try {
130            mAppInfo = mPm.getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
131        } catch (NameNotFoundException e) {
132            errFlag = true;
133        }
134        if(mAppInfo == null || errFlag) {
135            Log.e(TAG, "Invalid application:"+packageName);
136            showDialog(DLG_APP_NOT_FOUND);
137        } else {
138            requestWindowFeature(Window.FEATURE_NO_TITLE);
139            //set view
140            setContentView(R.layout.uninstall_confirm);
141            PackageUtil.initAppSnippet(this, mAppInfo, R.id.app_snippet);
142            //initialize ui elements
143            mOk = (Button)findViewById(R.id.ok_button);
144            mCancel = (Button)findViewById(R.id.cancel_button);
145            mOk.setOnClickListener(this);
146            mCancel.setOnClickListener(this);
147        }
148    }
149
150    @Override
151    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
152        boolean finish = true;
153        switch(requestCode) {
154        case UNINSTALL_PROGRESS:
155            finish = false;
156            mCurrentState = UNINSTALL_DONE;
157            //start the next screen to show final status of installation
158            if (resultCode==UninstallAppProgress.SUCCEEDED) {
159                startUninstallDone();
160            } else {
161                showDialogInner(DLG_UNINSTALL_FAILED);
162            }
163            break;
164        case UNINSTALL_DONE:
165            //neednt check for result code here
166            break;
167        default:
168            break;
169        }
170        if(finish) {
171            //finish off this activity to return to the previous activity that launched it
172            Log.i(TAG, "Finishing off activity");
173            finish();
174        }
175    }
176
177    private void finishAndReturn() {
178        finish();
179    }
180
181    public void onClick(View v) {
182        if(v == mOk) {
183            //initiate next screen
184            startUninstallProgress();
185        } else if(v == mCancel) {
186            finishAndReturn();
187        }
188    }
189}
190