UninstallerActivity.java revision 7672c9941137c4664f5a2c64eb15a335b3c4789d
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.widget.TextView;
35import android.content.pm.PackageManager.NameNotFoundException;
36
37/*
38 * This activity presents UI to uninstall an application. Usually launched with intent
39 * Intent.ACTION_UNINSTALL_PKG_COMMAND and attribute
40 * com.android.packageinstaller.PackageName set to the application package name
41 */
42public class UninstallerActivity extends Activity implements OnClickListener,
43        DialogInterface.OnCancelListener {
44    private static final String TAG = "UninstallerActivity";
45    private boolean localLOGV = false;
46    // Request code
47    private static final int UNINSTALL_PROGRESS = 1;
48    PackageManager mPm;
49    private ApplicationInfo mAppInfo;
50    private Button mOk;
51    private Button mCancel;
52
53    // Dialog identifiers used in showDialog
54    private static final int DLG_BASE = 0;
55    private static final int DLG_APP_NOT_FOUND = DLG_BASE + 1;
56    private static final int DLG_UNINSTALL_FAILED = DLG_BASE + 2;
57
58    private void showDialogInner(int id) {
59        showDialog(id);
60    }
61
62    @Override
63    public Dialog onCreateDialog(int id) {
64        switch (id) {
65        case DLG_APP_NOT_FOUND :
66            return new AlertDialog.Builder(this)
67                    .setTitle(R.string.app_not_found_dlg_title)
68                    .setIcon(com.android.internal.R.drawable.ic_dialog_alert)
69                    .setMessage(R.string.app_not_found_dlg_text)
70                    .setNeutralButton(getString(R.string.dlg_ok),
71                            new DialogInterface.OnClickListener() {
72                                public void onClick(DialogInterface dialog, int which) {
73                                    finish();
74                                }})
75                    .create();
76        case DLG_UNINSTALL_FAILED :
77            // Guaranteed not to be null. will default to package name if not set by app
78           CharSequence appTitle = mPm.getApplicationLabel(mAppInfo);
79           String dlgText = getString(R.string.uninstall_failed_msg,
80                    appTitle.toString());
81            // Display uninstall failed dialog
82            return new AlertDialog.Builder(this)
83                    .setTitle(R.string.uninstall_failed)
84                    .setIcon(com.android.internal.R.drawable.ic_dialog_alert)
85                    .setMessage(dlgText)
86                    .setNeutralButton(getString(R.string.dlg_ok),
87                            new DialogInterface.OnClickListener() {
88                                public void onClick(DialogInterface dialog, int which) {
89                                    finish();
90                                }})
91                    .create();
92        }
93        return null;
94    }
95
96    private void startUninstallProgress() {
97        Intent newIntent = new Intent(Intent.ACTION_VIEW);
98        newIntent.putExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO,
99                                                  mAppInfo);
100        newIntent.setClass(this, UninstallAppProgress.class);
101        startActivityForResult(newIntent, UNINSTALL_PROGRESS);
102    }
103
104    @Override
105    public void onCreate(Bundle icicle) {
106        super.onCreate(icicle);
107        //get intent information
108        final Intent intent = getIntent();
109        Uri packageURI = intent.getData();
110        String packageName = packageURI.getEncodedSchemeSpecificPart();
111        if(packageName == null) {
112            Log.e(TAG, "Invalid package name:"+packageName);
113            showDialog(DLG_APP_NOT_FOUND);
114            return;
115        }
116        //initialize package manager
117        mPm = getPackageManager();
118        boolean errFlag = false;
119        try {
120            mAppInfo = mPm.getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
121        } catch (NameNotFoundException e) {
122            errFlag = true;
123        }
124        if(mAppInfo == null || errFlag) {
125            Log.e(TAG, "Invalid application:"+packageName);
126            showDialog(DLG_APP_NOT_FOUND);
127        } else {
128            requestWindowFeature(Window.FEATURE_NO_TITLE);
129            //set view
130            setContentView(R.layout.uninstall_confirm);
131            TextView question = (TextView) findViewById(R.id.uninstall_question);
132            TextView confirm = (TextView) findViewById(R.id.uninstall_confirm_text);
133            if ((mAppInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
134                question.setText(R.string.uninstall_update_question);
135                confirm.setText(R.string.uninstall_update_text);
136            } else {
137                question.setText(R.string.uninstall_application_question);
138                confirm.setText(R.string.uninstall_application_text);
139            }
140            PackageUtil.initSnippetForInstalledApp(this, mAppInfo, R.id.app_snippet);
141            //initialize ui elements
142            mOk = (Button)findViewById(R.id.ok_button);
143            mCancel = (Button)findViewById(R.id.cancel_button);
144            mOk.setOnClickListener(this);
145            mCancel.setOnClickListener(this);
146        }
147    }
148
149    @Override
150    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
151        if (requestCode != UNINSTALL_PROGRESS) {
152            return;
153        }
154        // Start the next screen to show final status of installation
155        if (resultCode != UninstallAppProgress.SUCCEEDED) {
156            showDialogInner(DLG_UNINSTALL_FAILED);
157        } else {
158            // Finish off this activity
159            if (localLOGV) Log.i(TAG, "Finishing off activity");
160            finish();
161        }
162    }
163
164    public void onClick(View v) {
165        if(v == mOk) {
166            //initiate next screen
167            startUninstallProgress();
168        } else if(v == mCancel) {
169            finish();
170        }
171    }
172
173    public void onCancel(DialogInterface dialog) {
174        finish();
175    }
176}
177