1 /*
2  * Copyright (C) 2024 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.settings.network.telephony;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.telephony.SubscriptionManager;
24 import android.util.Log;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.fragment.app.FragmentActivity;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.SubSettingLauncher;
32 import com.android.settings.overlay.FeatureFactory;
33 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
34 
35 /** This dialog activity advise the user to have connectivity if the eSIM uses a RAC. */
36 public class EuiccRacConnectivityDialogActivity extends FragmentActivity
37         implements WarningDialogFragment.OnConfirmListener {
38 
39     private static final String TAG = "EuiccRacConnectivityDialogActivity";
40     private static final String ARG_SUB_ID = "sub_id";
41     private static final String ARG_RESET_MOBILE_NETWORK_ID = "reset_mobile_netword_id";
42 
43     private int mSubId;
44     @Nullable
45     private Intent mResetMobileNetworkIntent;
46     private MetricsFeatureProvider mMetricsFeatureProvider;
47 
48     /**
49      * Returns an intent of EuiccRacConnectivityDialogActivity for Settings: erase eSIM.
50      *
51      * @param context The context used to start the EuiccRacConnectivityDialogActivity.
52      * @param subId The subscription ID of the subscription needs to be deleted. If the subscription
53      *     belongs to a group of subscriptions, all subscriptions from the group will be deleted.
54      */
55     @NonNull
getIntent(@onNull Context context, int subId)56     public static Intent getIntent(@NonNull Context context, int subId) {
57         Intent intent = new Intent(context, EuiccRacConnectivityDialogActivity.class);
58         intent.putExtra(ARG_SUB_ID, subId);
59         return intent;
60     }
61 
62     /**
63      * Returns an intent of EuiccRacConnectivityDialogActivity for Reset: Mobile network settings.
64      *
65      * @param context The context used to start the EuiccRacConnectivityDialogActivity.
66      * @param resetMobileNetworkIntent The intent that will continue the reset of mobile network
67      *     settings.
68      */
69     @NonNull
getIntent(@onNull Context context, @NonNull Intent resetMobileNetworkIntent)70     public static Intent getIntent(@NonNull Context context,
71             @NonNull Intent resetMobileNetworkIntent) {
72         Intent intent = new Intent(context, EuiccRacConnectivityDialogActivity.class);
73         intent.putExtra(ARG_RESET_MOBILE_NETWORK_ID, resetMobileNetworkIntent);
74         return intent;
75     }
76 
77     @Override
onCreate(Bundle savedInstanceState)78     protected void onCreate(Bundle savedInstanceState) {
79         super.onCreate(savedInstanceState);
80 
81         Intent intent = getIntent();
82         mSubId = intent.getIntExtra(ARG_SUB_ID, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
83         mResetMobileNetworkIntent =
84                 intent.getParcelableExtra(ARG_RESET_MOBILE_NETWORK_ID, Intent.class);
85         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
86 
87         if (savedInstanceState == null) {
88             showConnectivityWarningDialog();
89         }
90     }
91 
92     @Override
onConfirm(int tag, boolean confirmed)93     public void onConfirm(int tag, boolean confirmed) {
94         if (tag == SettingsEnums.ACTION_SETTINGS_ESIM_RAC_CONNECTIVITY_WARNING
95                 || tag == SettingsEnums.ACTION_RESET_MOBILE_NETWORK_RAC_CONNECTIVITY_WARNING) {
96             mMetricsFeatureProvider.action(this, tag, confirmed ? 1 : 0);
97         }
98 
99         if (!confirmed) {
100             finish();
101             return;
102         }
103 
104         finish();
105         switch (tag) {
106             case SettingsEnums.ACTION_SETTINGS_ESIM_RAC_CONNECTIVITY_WARNING:
107                 Log.i(TAG, "Show dialogue activity that handles deleting eSIM profile");
108                 startActivity(DeleteEuiccSubscriptionDialogActivity.getIntent(this, mSubId));
109                 break;
110             case SettingsEnums.ACTION_RESET_MOBILE_NETWORK_RAC_CONNECTIVITY_WARNING:
111                 if (mResetMobileNetworkIntent != null) {
112                     Log.i(TAG, "Show fragment activity that handles mobile network settings reset");
113                     new SubSettingLauncher(this).launchWithIntent(mResetMobileNetworkIntent);
114                 }
115                 break;
116             default:
117                 Log.e(TAG, "Unrecognized confirmation dialog tag: " + tag);
118                 break;
119         }
120     }
121 
122     /* Displays warning to have connectivity because subscription is RAC dialog. */
showConnectivityWarningDialog()123     private void showConnectivityWarningDialog() {
124         WarningDialogFragment.show(
125                 this,
126                 WarningDialogFragment.OnConfirmListener.class,
127                 getMetricsTag(),
128                 getString(R.string.wifi_warning_dialog_title),
129                 getString(R.string.wifi_warning_dialog_text),
130                 getString(R.string.wifi_warning_continue_button),
131                 getString(R.string.wifi_warning_return_button));
132     }
133 
134     /* Get the metrics tag depending on the intent. */
getMetricsTag()135     private int getMetricsTag() {
136         if (mResetMobileNetworkIntent != null) {
137             return SettingsEnums.ACTION_RESET_MOBILE_NETWORK_RAC_CONNECTIVITY_WARNING;
138         } else {
139             return SettingsEnums.ACTION_SETTINGS_ESIM_RAC_CONNECTIVITY_WARNING;
140         }
141     }
142 }
143