1/*
2 * Copyright (C) 2018 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.internal.telephony.uicc;
18
19import android.app.Activity;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.res.Resources;
24import android.os.Bundle;
25import android.provider.Settings;
26import android.text.TextUtils;
27import android.util.Log;
28
29import java.util.concurrent.TimeUnit;
30
31/**
32 * Trampoline activity used to start the full screen dialog that is shown when a SIM is inserted
33 * and requires a carrier app download
34 */
35public class InstallCarrierAppTrampolineActivity extends Activity {
36    private static final String LOG_TAG = "CarrierAppInstall";
37    private static final int INSTALL_CARRIER_APP_DIALOG_REQUEST = 1;
38
39    // TODO(b/73648962): Move DOWNLOAD_RESULT and CARRIER_NAME to a shared location
40    /**
41     * This must remain in sync with
42     * {@link com.android.simappdialog.InstallCarrierAppActivity#DOWNLOAD_RESULT}
43     */
44    private static final int DOWNLOAD_RESULT = 2;
45
46    /**
47     * This must remain in sync with
48     * {@link com.android.simappdialog.InstallCarrierAppActivity#BUNDLE_KEY_CARRIER_NAME}
49     */
50    private static final String CARRIER_NAME = "carrier_name";
51
52    /** Bundle key for the name of the package to be downloaded */
53    private static final String BUNDLE_KEY_PACKAGE_NAME = "package_name";
54
55    /** Returns intent used to start this activity */
56    public static Intent get(Context context, String packageName) {
57        Intent intent = new Intent(context, InstallCarrierAppTrampolineActivity.class);
58        intent.putExtra(BUNDLE_KEY_PACKAGE_NAME, packageName);
59        return intent;
60    }
61
62    private String mPackageName;
63
64    @Override
65    protected void onCreate(Bundle savedInstanceState) {
66        super.onCreate(savedInstanceState);
67        Intent intent = getIntent();
68        if (intent != null) {
69            mPackageName = intent.getStringExtra(BUNDLE_KEY_PACKAGE_NAME);
70        }
71
72        // If this is the first activity creation, show notification after delay regardless of
73        // result code, but only if the app is not installed.
74        if (savedInstanceState == null) {
75            long sleepTimeMillis = Settings.Global.getLong(getContentResolver(),
76                    Settings.Global.INSTALL_CARRIER_APP_NOTIFICATION_SLEEP_MILLIS,
77                    TimeUnit.HOURS.toMillis(24));
78            Log.d(LOG_TAG, "Sleeping carrier app install notification for : " + sleepTimeMillis
79                    + " millis");
80            InstallCarrierAppUtils.showNotificationIfNotInstalledDelayed(
81                    this,
82                    mPackageName,
83                    sleepTimeMillis);
84        }
85
86        // Display dialog activity if available
87        Intent showDialogIntent = new Intent();
88        ComponentName dialogComponentName = ComponentName.unflattenFromString(
89                Resources.getSystem().getString(
90                        com.android.internal.R.string.config_carrierAppInstallDialogComponent));
91        showDialogIntent.setComponent(dialogComponentName);
92        String appName = InstallCarrierAppUtils.getAppNameFromPackageName(this, mPackageName);
93        if (!TextUtils.isEmpty(appName)) {
94            showDialogIntent.putExtra(CARRIER_NAME, appName);
95        }
96
97        if (showDialogIntent.resolveActivity(getPackageManager()) == null) {
98            Log.d(LOG_TAG, "Could not resolve activity for installing the carrier app");
99            finishNoAnimation();
100        } else {
101            startActivityForResult(showDialogIntent, INSTALL_CARRIER_APP_DIALOG_REQUEST);
102        }
103    }
104
105    @Override
106    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
107        super.onActivityResult(requestCode, resultCode, data);
108        if (requestCode == INSTALL_CARRIER_APP_DIALOG_REQUEST) {
109            if (resultCode == DOWNLOAD_RESULT) {
110                startActivity(InstallCarrierAppUtils.getPlayStoreIntent(mPackageName));
111            }
112            finishNoAnimation();
113        }
114    }
115
116    private void finishNoAnimation() {
117        finish();
118        overridePendingTransition(0, 0);
119    }
120}
121