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
17package com.android.packageinstaller;
18
19import android.app.Activity;
20import android.content.ActivityNotFoundException;
21import android.content.Intent;
22import android.content.pm.ApplicationInfo;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25import android.net.Uri;
26import android.os.Bundle;
27import android.support.annotation.Nullable;
28import android.util.Log;
29import android.widget.Button;
30
31import java.io.File;
32import java.util.List;
33
34/**
35 * Finish installation: Return status code to the caller or display "success" UI to user
36 */
37public class InstallSuccess extends Activity {
38    private static final String LOG_TAG = InstallSuccess.class.getSimpleName();
39
40    @Override
41    protected void onCreate(@Nullable Bundle savedInstanceState) {
42        super.onCreate(savedInstanceState);
43
44        if (getIntent().getBooleanExtra(Intent.EXTRA_RETURN_RESULT, false)) {
45            // Return result if requested
46            Intent result = new Intent();
47            result.putExtra(Intent.EXTRA_INSTALL_RESULT, PackageManager.INSTALL_SUCCEEDED);
48            setResult(Activity.RESULT_OK, result);
49            finish();
50        } else {
51            Intent intent = getIntent();
52            ApplicationInfo appInfo =
53                    intent.getParcelableExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO);
54            Uri packageURI = intent.getData();
55
56            setContentView(R.layout.install_success);
57
58            // Set header icon and title
59            PackageUtil.AppSnippet as;
60            PackageManager pm = getPackageManager();
61
62            if ("package".equals(packageURI.getScheme())) {
63                as = new PackageUtil.AppSnippet(pm.getApplicationLabel(appInfo),
64                        pm.getApplicationIcon(appInfo));
65            } else {
66                File sourceFile = new File(packageURI.getPath());
67                as = PackageUtil.getAppSnippet(this, appInfo, sourceFile);
68            }
69
70            PackageUtil.initSnippetForNewApp(this, as, R.id.app_snippet);
71
72            // Set up "done" button
73            findViewById(R.id.done_button).setOnClickListener(view -> {
74                if (appInfo.packageName != null) {
75                    Log.i(LOG_TAG, "Finished installing " + appInfo.packageName);
76                }
77                finish();
78            });
79
80            // Enable or disable "launch" button
81            Intent launchIntent = getPackageManager().getLaunchIntentForPackage(
82                    appInfo.packageName);
83            boolean enabled = false;
84            if (launchIntent != null) {
85                List<ResolveInfo> list = getPackageManager().queryIntentActivities(launchIntent,
86                        0);
87                if (list != null && list.size() > 0) {
88                    enabled = true;
89                }
90            }
91
92            Button launchButton = (Button)findViewById(R.id.launch_button);
93            if (enabled) {
94                launchButton.setOnClickListener(view -> {
95                    try {
96                        startActivity(launchIntent);
97                    } catch (ActivityNotFoundException | SecurityException e) {
98                        Log.e(LOG_TAG, "Could not start activity", e);
99                    }
100                    finish();
101                });
102            } else {
103                launchButton.setEnabled(false);
104            }
105        }
106    }
107}
108