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     @Override
onCreate(@ullable Bundle savedInstanceState)45     protected void onCreate(@Nullable Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47 
48         if (getIntent().getBooleanExtra(Intent.EXTRA_RETURN_RESULT, false)) {
49             // Return result if requested
50             Intent result = new Intent();
51             result.putExtra(Intent.EXTRA_INSTALL_RESULT, PackageManager.INSTALL_SUCCEEDED);
52             setResult(Activity.RESULT_OK, result);
53             finish();
54         } else {
55             Intent intent = getIntent();
56             ApplicationInfo appInfo =
57                     intent.getParcelableExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO);
58             Uri packageURI = intent.getData();
59 
60             // Set header icon and title
61             PackageUtil.AppSnippet as;
62             PackageManager pm = getPackageManager();
63 
64             if ("package".equals(packageURI.getScheme())) {
65                 as = new PackageUtil.AppSnippet(pm.getApplicationLabel(appInfo),
66                         pm.getApplicationIcon(appInfo));
67             } else {
68                 File sourceFile = new File(packageURI.getPath());
69                 as = PackageUtil.getAppSnippet(this, appInfo, sourceFile);
70             }
71 
72             mAlert.setIcon(as.icon);
73             mAlert.setTitle(as.label);
74             mAlert.setView(R.layout.install_content_view);
75             mAlert.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.launch), null,
76                     null);
77             mAlert.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.done),
78                     (ignored, ignored2) -> {
79                         if (appInfo.packageName != null) {
80                             Log.i(LOG_TAG, "Finished installing " + appInfo.packageName);
81                         }
82                         finish();
83                     }, null);
84             setupAlert();
85             requireViewById(R.id.install_success).setVisibility(View.VISIBLE);
86             // Enable or disable "launch" button
87             Intent launchIntent = getPackageManager().getLaunchIntentForPackage(
88                     appInfo.packageName);
89             boolean enabled = false;
90             if (launchIntent != null) {
91                 List<ResolveInfo> list = getPackageManager().queryIntentActivities(launchIntent,
92                         0);
93                 if (list != null && list.size() > 0) {
94                     enabled = true;
95                 }
96             }
97 
98             Button launchButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
99             if (enabled) {
100                 launchButton.setOnClickListener(view -> {
101                     try {
102                         startActivity(launchIntent);
103                     } catch (ActivityNotFoundException | SecurityException e) {
104                         Log.e(LOG_TAG, "Could not start activity", e);
105                     }
106                     finish();
107                 });
108             } else {
109                 launchButton.setEnabled(false);
110             }
111         }
112     }
113 }
114