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.app.Activity;
20 import android.content.DialogInterface;
21 import android.content.Intent;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.view.View;
28 import android.widget.Button;
29 
30 import androidx.annotation.Nullable;
31 
32 import java.util.List;
33 
34 /**
35  * Finish installation: Return status code to the caller or display "success" UI to user
36  */
37 public class InstallSuccess extends AlertActivity {
38     private static final String LOG_TAG = InstallSuccess.class.getSimpleName();
39 
40     @Nullable
41     private PackageUtil.AppSnippet mAppSnippet;
42 
43     @Nullable
44     private String mAppPackageName;
45 
46     @Nullable
47     private Intent mLaunchIntent;
48 
49     @Override
onCreate(@ullable Bundle savedInstanceState)50     protected void onCreate(@Nullable Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52 
53         setFinishOnTouchOutside(true);
54 
55         if (getIntent().getBooleanExtra(Intent.EXTRA_RETURN_RESULT, false)) {
56             // Return result if requested
57             Intent result = new Intent();
58             result.putExtra(Intent.EXTRA_INSTALL_RESULT, PackageManager.INSTALL_SUCCEEDED);
59             setResult(Activity.RESULT_OK, result);
60             finish();
61         } else {
62             Intent intent = getIntent();
63             ApplicationInfo appInfo =
64                     intent.getParcelableExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO);
65             mAppPackageName = appInfo.packageName;
66             mAppSnippet = intent.getParcelableExtra(PackageInstallerActivity.EXTRA_APP_SNIPPET,
67                     PackageUtil.AppSnippet.class);
68 
69             mLaunchIntent = getPackageManager().getLaunchIntentForPackage(mAppPackageName);
70 
71             bindUi();
72         }
73     }
74 
75     @Override
onResume()76     protected void onResume() {
77         super.onResume();
78         bindUi();
79     }
80 
bindUi()81     private void bindUi() {
82         if (mAppSnippet == null) {
83             return;
84         }
85 
86         mAlert.setIcon(mAppSnippet.icon);
87         mAlert.setTitle(mAppSnippet.label);
88         mAlert.setView(R.layout.install_content_view);
89         mAlert.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.launch), null,
90                 null);
91         mAlert.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.done),
92                 (ignored, ignored2) -> {
93                     if (mAppPackageName != null) {
94                         Log.i(LOG_TAG, "Finished installing " + mAppPackageName);
95                     }
96                     finish();
97                 }, null);
98         setupAlert();
99         requireViewById(R.id.install_success).setVisibility(View.VISIBLE);
100         // Enable or disable "launch" button
101         boolean enabled = false;
102         if (mLaunchIntent != null) {
103             List<ResolveInfo> list = getPackageManager().queryIntentActivities(mLaunchIntent,
104                     0);
105             if (list != null && list.size() > 0) {
106                 enabled = true;
107             }
108         }
109 
110         Button launchButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
111         if (enabled) {
112             launchButton.setOnClickListener(view -> {
113                 setResult(Activity.RESULT_OK, mLaunchIntent);
114                 finish();
115             });
116         } else {
117             launchButton.setEnabled(false);
118         }
119     }
120 }
121