• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.packageinstaller;
18 
19 import android.annotation.Nullable;
20 import android.app.Activity;
21 import android.content.ActivityNotFoundException;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.pm.ApplicationInfo;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.util.Log;
30 import android.view.View;
31 import android.widget.Button;
32 
33 import com.android.internal.app.AlertActivity;
34 
35 import java.io.File;
36 import java.util.List;
37 
38 /**
39  * Finish installation: Return status code to the caller or display "success" UI to user
40  */
41 public class InstallSuccess extends AlertActivity {
42     private static final String LOG_TAG = InstallSuccess.class.getSimpleName();
43 
44     @Nullable
45     private PackageUtil.AppSnippet mAppSnippet;
46 
47     @Nullable
48     private String mAppPackageName;
49 
50     @Nullable
51     private Intent mLaunchIntent;
52 
53     @Override
onCreate(@ullable Bundle savedInstanceState)54     protected void onCreate(@Nullable Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56 
57         if (getIntent().getBooleanExtra(Intent.EXTRA_RETURN_RESULT, false)) {
58             // Return result if requested
59             Intent result = new Intent();
60             result.putExtra(Intent.EXTRA_INSTALL_RESULT, PackageManager.INSTALL_SUCCEEDED);
61             setResult(Activity.RESULT_OK, result);
62             finish();
63         } else {
64             Intent intent = getIntent();
65             ApplicationInfo appInfo =
66                     intent.getParcelableExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO);
67             mAppPackageName = appInfo.packageName;
68             Uri packageURI = intent.getData();
69 
70             // Set header icon and title
71             PackageManager pm = getPackageManager();
72 
73             if ("package".equals(packageURI.getScheme())) {
74                 mAppSnippet = new PackageUtil.AppSnippet(pm.getApplicationLabel(appInfo),
75                         pm.getApplicationIcon(appInfo));
76             } else {
77                 File sourceFile = new File(packageURI.getPath());
78                 mAppSnippet = PackageUtil.getAppSnippet(this, appInfo, sourceFile);
79             }
80 
81             mLaunchIntent = getPackageManager().getLaunchIntentForPackage(mAppPackageName);
82 
83             bindUi();
84         }
85     }
86 
87     @Override
onResume()88     protected void onResume() {
89         super.onResume();
90         bindUi();
91     }
92 
bindUi()93     private void bindUi() {
94         if (mAppSnippet == null) {
95             return;
96         }
97 
98         mAlert.setIcon(mAppSnippet.icon);
99         mAlert.setTitle(mAppSnippet.label);
100         mAlert.setView(R.layout.install_content_view);
101         mAlert.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.launch), null,
102                 null);
103         mAlert.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.done),
104                 (ignored, ignored2) -> {
105                     if (mAppPackageName != null) {
106                         Log.i(LOG_TAG, "Finished installing " + mAppPackageName);
107                     }
108                     finish();
109                 }, null);
110         setupAlert();
111         requireViewById(R.id.install_success).setVisibility(View.VISIBLE);
112         // Enable or disable "launch" button
113         boolean enabled = false;
114         if (mLaunchIntent != null) {
115             List<ResolveInfo> list = getPackageManager().queryIntentActivities(mLaunchIntent,
116                     0);
117             if (list != null && list.size() > 0) {
118                 enabled = true;
119             }
120         }
121 
122         Button launchButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
123         if (enabled) {
124             launchButton.setOnClickListener(view -> {
125                 try {
126                     startActivity(mLaunchIntent);
127                 } catch (ActivityNotFoundException | SecurityException e) {
128                     Log.e(LOG_TAG, "Could not start activity", e);
129                 }
130                 finish();
131             });
132         } else {
133             launchButton.setEnabled(false);
134         }
135     }
136 }
137