1cc94118f978146761cfb697091da5401cbec6a1bgoneil/*
2cc94118f978146761cfb697091da5401cbec6a1bgoneil * Copyright (C) 2018 The Android Open Source Project
3cc94118f978146761cfb697091da5401cbec6a1bgoneil *
4cc94118f978146761cfb697091da5401cbec6a1bgoneil * Licensed under the Apache License, Version 2.0 (the "License");
5cc94118f978146761cfb697091da5401cbec6a1bgoneil * you may not use this file except in compliance with the License.
6cc94118f978146761cfb697091da5401cbec6a1bgoneil * You may obtain a copy of the License at
7cc94118f978146761cfb697091da5401cbec6a1bgoneil *
8cc94118f978146761cfb697091da5401cbec6a1bgoneil *      http://www.apache.org/licenses/LICENSE-2.0
9cc94118f978146761cfb697091da5401cbec6a1bgoneil *
10cc94118f978146761cfb697091da5401cbec6a1bgoneil * Unless required by applicable law or agreed to in writing, software
11cc94118f978146761cfb697091da5401cbec6a1bgoneil * distributed under the License is distributed on an "AS IS" BASIS,
12cc94118f978146761cfb697091da5401cbec6a1bgoneil * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cc94118f978146761cfb697091da5401cbec6a1bgoneil * See the License for the specific language governing permissions and
14cc94118f978146761cfb697091da5401cbec6a1bgoneil * limitations under the License.
15cc94118f978146761cfb697091da5401cbec6a1bgoneil */
16cc94118f978146761cfb697091da5401cbec6a1bgoneil
17cc94118f978146761cfb697091da5401cbec6a1bgoneilpackage com.android.internal.telephony.uicc;
18cc94118f978146761cfb697091da5401cbec6a1bgoneil
19cc94118f978146761cfb697091da5401cbec6a1bgoneilimport android.app.Activity;
20cc94118f978146761cfb697091da5401cbec6a1bgoneilimport android.content.ComponentName;
21cc94118f978146761cfb697091da5401cbec6a1bgoneilimport android.content.Context;
22cc94118f978146761cfb697091da5401cbec6a1bgoneilimport android.content.Intent;
23cc94118f978146761cfb697091da5401cbec6a1bgoneilimport android.content.res.Resources;
24cc94118f978146761cfb697091da5401cbec6a1bgoneilimport android.os.Bundle;
25cc94118f978146761cfb697091da5401cbec6a1bgoneilimport android.provider.Settings;
262d75c5b4baeaceef006efe888a64777cfc329d48goneilimport android.text.TextUtils;
27cc94118f978146761cfb697091da5401cbec6a1bgoneilimport android.util.Log;
28cc94118f978146761cfb697091da5401cbec6a1bgoneil
29cc94118f978146761cfb697091da5401cbec6a1bgoneilimport java.util.concurrent.TimeUnit;
30cc94118f978146761cfb697091da5401cbec6a1bgoneil
31cc94118f978146761cfb697091da5401cbec6a1bgoneil/**
32cc94118f978146761cfb697091da5401cbec6a1bgoneil * Trampoline activity used to start the full screen dialog that is shown when a SIM is inserted
33cc94118f978146761cfb697091da5401cbec6a1bgoneil * and requires a carrier app download
34cc94118f978146761cfb697091da5401cbec6a1bgoneil */
35cc94118f978146761cfb697091da5401cbec6a1bgoneilpublic class InstallCarrierAppTrampolineActivity extends Activity {
36cc94118f978146761cfb697091da5401cbec6a1bgoneil    private static final String LOG_TAG = "CarrierAppInstall";
37cc94118f978146761cfb697091da5401cbec6a1bgoneil    private static final int INSTALL_CARRIER_APP_DIALOG_REQUEST = 1;
38cc94118f978146761cfb697091da5401cbec6a1bgoneil
392d75c5b4baeaceef006efe888a64777cfc329d48goneil    // TODO(b/73648962): Move DOWNLOAD_RESULT and CARRIER_NAME to a shared location
40cc94118f978146761cfb697091da5401cbec6a1bgoneil    /**
41cc94118f978146761cfb697091da5401cbec6a1bgoneil     * This must remain in sync with
42cc94118f978146761cfb697091da5401cbec6a1bgoneil     * {@link com.android.simappdialog.InstallCarrierAppActivity#DOWNLOAD_RESULT}
43cc94118f978146761cfb697091da5401cbec6a1bgoneil     */
44cc94118f978146761cfb697091da5401cbec6a1bgoneil    private static final int DOWNLOAD_RESULT = 2;
45cc94118f978146761cfb697091da5401cbec6a1bgoneil
462d75c5b4baeaceef006efe888a64777cfc329d48goneil    /**
472d75c5b4baeaceef006efe888a64777cfc329d48goneil     * This must remain in sync with
482d75c5b4baeaceef006efe888a64777cfc329d48goneil     * {@link com.android.simappdialog.InstallCarrierAppActivity#BUNDLE_KEY_CARRIER_NAME}
492d75c5b4baeaceef006efe888a64777cfc329d48goneil     */
502d75c5b4baeaceef006efe888a64777cfc329d48goneil    private static final String CARRIER_NAME = "carrier_name";
512d75c5b4baeaceef006efe888a64777cfc329d48goneil
52cc94118f978146761cfb697091da5401cbec6a1bgoneil    /** Bundle key for the name of the package to be downloaded */
53cc94118f978146761cfb697091da5401cbec6a1bgoneil    private static final String BUNDLE_KEY_PACKAGE_NAME = "package_name";
54cc94118f978146761cfb697091da5401cbec6a1bgoneil
55cc94118f978146761cfb697091da5401cbec6a1bgoneil    /** Returns intent used to start this activity */
56cc94118f978146761cfb697091da5401cbec6a1bgoneil    public static Intent get(Context context, String packageName) {
57cc94118f978146761cfb697091da5401cbec6a1bgoneil        Intent intent = new Intent(context, InstallCarrierAppTrampolineActivity.class);
58cc94118f978146761cfb697091da5401cbec6a1bgoneil        intent.putExtra(BUNDLE_KEY_PACKAGE_NAME, packageName);
59cc94118f978146761cfb697091da5401cbec6a1bgoneil        return intent;
60cc94118f978146761cfb697091da5401cbec6a1bgoneil    }
61cc94118f978146761cfb697091da5401cbec6a1bgoneil
62cc94118f978146761cfb697091da5401cbec6a1bgoneil    private String mPackageName;
63cc94118f978146761cfb697091da5401cbec6a1bgoneil
64cc94118f978146761cfb697091da5401cbec6a1bgoneil    @Override
65cc94118f978146761cfb697091da5401cbec6a1bgoneil    protected void onCreate(Bundle savedInstanceState) {
66cc94118f978146761cfb697091da5401cbec6a1bgoneil        super.onCreate(savedInstanceState);
67cc94118f978146761cfb697091da5401cbec6a1bgoneil        Intent intent = getIntent();
68cc94118f978146761cfb697091da5401cbec6a1bgoneil        if (intent != null) {
69cc94118f978146761cfb697091da5401cbec6a1bgoneil            mPackageName = intent.getStringExtra(BUNDLE_KEY_PACKAGE_NAME);
70cc94118f978146761cfb697091da5401cbec6a1bgoneil        }
71cc94118f978146761cfb697091da5401cbec6a1bgoneil
72cc94118f978146761cfb697091da5401cbec6a1bgoneil        // If this is the first activity creation, show notification after delay regardless of
73cc94118f978146761cfb697091da5401cbec6a1bgoneil        // result code, but only if the app is not installed.
74cc94118f978146761cfb697091da5401cbec6a1bgoneil        if (savedInstanceState == null) {
75cc94118f978146761cfb697091da5401cbec6a1bgoneil            long sleepTimeMillis = Settings.Global.getLong(getContentResolver(),
76cc94118f978146761cfb697091da5401cbec6a1bgoneil                    Settings.Global.INSTALL_CARRIER_APP_NOTIFICATION_SLEEP_MILLIS,
77cc94118f978146761cfb697091da5401cbec6a1bgoneil                    TimeUnit.HOURS.toMillis(24));
78cc94118f978146761cfb697091da5401cbec6a1bgoneil            Log.d(LOG_TAG, "Sleeping carrier app install notification for : " + sleepTimeMillis
79cc94118f978146761cfb697091da5401cbec6a1bgoneil                    + " millis");
80cc94118f978146761cfb697091da5401cbec6a1bgoneil            InstallCarrierAppUtils.showNotificationIfNotInstalledDelayed(
81cc94118f978146761cfb697091da5401cbec6a1bgoneil                    this,
82cc94118f978146761cfb697091da5401cbec6a1bgoneil                    mPackageName,
83cc94118f978146761cfb697091da5401cbec6a1bgoneil                    sleepTimeMillis);
84cc94118f978146761cfb697091da5401cbec6a1bgoneil        }
85cc94118f978146761cfb697091da5401cbec6a1bgoneil
86cc94118f978146761cfb697091da5401cbec6a1bgoneil        // Display dialog activity if available
87cc94118f978146761cfb697091da5401cbec6a1bgoneil        Intent showDialogIntent = new Intent();
88cc94118f978146761cfb697091da5401cbec6a1bgoneil        ComponentName dialogComponentName = ComponentName.unflattenFromString(
89cc94118f978146761cfb697091da5401cbec6a1bgoneil                Resources.getSystem().getString(
90cc94118f978146761cfb697091da5401cbec6a1bgoneil                        com.android.internal.R.string.config_carrierAppInstallDialogComponent));
91cc94118f978146761cfb697091da5401cbec6a1bgoneil        showDialogIntent.setComponent(dialogComponentName);
922d75c5b4baeaceef006efe888a64777cfc329d48goneil        String appName = InstallCarrierAppUtils.getAppNameFromPackageName(this, mPackageName);
932d75c5b4baeaceef006efe888a64777cfc329d48goneil        if (!TextUtils.isEmpty(appName)) {
942d75c5b4baeaceef006efe888a64777cfc329d48goneil            showDialogIntent.putExtra(CARRIER_NAME, appName);
952d75c5b4baeaceef006efe888a64777cfc329d48goneil        }
96cc94118f978146761cfb697091da5401cbec6a1bgoneil
97cc94118f978146761cfb697091da5401cbec6a1bgoneil        if (showDialogIntent.resolveActivity(getPackageManager()) == null) {
98cc94118f978146761cfb697091da5401cbec6a1bgoneil            Log.d(LOG_TAG, "Could not resolve activity for installing the carrier app");
99cc94118f978146761cfb697091da5401cbec6a1bgoneil            finishNoAnimation();
100cc94118f978146761cfb697091da5401cbec6a1bgoneil        } else {
101cc94118f978146761cfb697091da5401cbec6a1bgoneil            startActivityForResult(showDialogIntent, INSTALL_CARRIER_APP_DIALOG_REQUEST);
102cc94118f978146761cfb697091da5401cbec6a1bgoneil        }
103cc94118f978146761cfb697091da5401cbec6a1bgoneil    }
104cc94118f978146761cfb697091da5401cbec6a1bgoneil
105cc94118f978146761cfb697091da5401cbec6a1bgoneil    @Override
106cc94118f978146761cfb697091da5401cbec6a1bgoneil    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
107cc94118f978146761cfb697091da5401cbec6a1bgoneil        super.onActivityResult(requestCode, resultCode, data);
108cc94118f978146761cfb697091da5401cbec6a1bgoneil        if (requestCode == INSTALL_CARRIER_APP_DIALOG_REQUEST) {
109cc94118f978146761cfb697091da5401cbec6a1bgoneil            if (resultCode == DOWNLOAD_RESULT) {
110cc94118f978146761cfb697091da5401cbec6a1bgoneil                startActivity(InstallCarrierAppUtils.getPlayStoreIntent(mPackageName));
111cc94118f978146761cfb697091da5401cbec6a1bgoneil            }
112cc94118f978146761cfb697091da5401cbec6a1bgoneil            finishNoAnimation();
113cc94118f978146761cfb697091da5401cbec6a1bgoneil        }
114cc94118f978146761cfb697091da5401cbec6a1bgoneil    }
115cc94118f978146761cfb697091da5401cbec6a1bgoneil
116cc94118f978146761cfb697091da5401cbec6a1bgoneil    private void finishNoAnimation() {
117cc94118f978146761cfb697091da5401cbec6a1bgoneil        finish();
118cc94118f978146761cfb697091da5401cbec6a1bgoneil        overridePendingTransition(0, 0);
119cc94118f978146761cfb697091da5401cbec6a1bgoneil    }
120cc94118f978146761cfb697091da5401cbec6a1bgoneil}
121