1 /*
2  * Copyright (C) 2019 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.sim;
18 
19 import static android.telephony.SubscriptionManager.PROFILE_CLASS_PROVISIONING;
20 
21 import android.app.Activity;
22 import android.app.Dialog;
23 import android.app.settings.SettingsEnums;
24 import android.content.DialogInterface;
25 import android.os.Bundle;
26 import android.telephony.SubscriptionInfo;
27 import android.telephony.SubscriptionManager;
28 import android.util.Log;
29 
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 import androidx.annotation.VisibleForTesting;
33 import androidx.appcompat.app.AlertDialog;
34 
35 import com.android.internal.telephony.flags.Flags;
36 import com.android.settings.R;
37 import com.android.settings.network.SubscriptionUtil;
38 
39 /**
40  * Presents a dialog asking the user if they want to update all services to use a given "preferred"
41  * SIM. Typically this would be used in a case where a device goes from having multiple SIMs down to
42  * only one.
43  */
44 public class PreferredSimDialogFragment extends SimDialogFragment implements
45         DialogInterface.OnClickListener {
46     private static final String TAG = "PreferredSimDialogFrag";
47 
newInstance()48     public static PreferredSimDialogFragment newInstance() {
49         final PreferredSimDialogFragment fragment = new PreferredSimDialogFragment();
50         final Bundle args = initArguments(SimDialogActivity.PREFERRED_PICK,
51                 R.string.sim_preferred_title);
52         fragment.setArguments(args);
53         return fragment;
54     }
55 
56     @NonNull
57     @Override
onCreateDialog(@ullable Bundle savedInstanceState)58     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
59         final AlertDialog dialog = new AlertDialog.Builder(getContext())
60                 .setTitle(getTitleResId())
61                 .setPositiveButton(R.string.yes, this)
62                 .setNegativeButton(R.string.no, null)
63                 .create();
64         updateDialog(dialog);
65         return dialog;
66     }
67 
68     @Override
onClick(DialogInterface dialog, int buttonClicked)69     public void onClick(DialogInterface dialog, int buttonClicked) {
70         if (buttonClicked != DialogInterface.BUTTON_POSITIVE) {
71             return;
72         }
73         final SimDialogActivity activity = (SimDialogActivity) getActivity();
74         final SubscriptionInfo info = getPreferredSubscription();
75         if (info != null) {
76             activity.onSubscriptionSelected(getDialogType(), info.getSubscriptionId());
77         }
78     }
79 
getPreferredSubscription()80     public SubscriptionInfo getPreferredSubscription() {
81         final Activity activity = getActivity();
82         final int slotId = activity.getIntent().getIntExtra(SimDialogActivity.PREFERRED_SIM,
83                 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
84         return getSubscriptionManager().getActiveSubscriptionInfoForSimSlotIndex(slotId);
85     }
86 
updateDialog(AlertDialog dialog)87     private void updateDialog(AlertDialog dialog) {
88         Log.d(TAG, "Dialog updated, dismiss status: " + mWasDismissed);
89 
90         if (mWasDismissed) {
91             return;
92         }
93 
94         if (dialog == null) {
95             Log.d(TAG, "Dialog is null.");
96             dismiss();
97             return;
98         }
99 
100         final SubscriptionInfo info = getPreferredSubscription();
101         if (info == null || (info.isEmbedded()
102             && (info.getProfileClass() == PROFILE_CLASS_PROVISIONING
103                 || (Flags.oemEnabledSatelliteFlag() && info.isOnlyNonTerrestrialNetwork())))) {
104             dismiss();
105             return;
106         }
107         final String message =
108                 getContext().getString(
109                         R.string.sim_preferred_message,
110                         SubscriptionUtil.getUniqueSubscriptionDisplayName(info, getContext()));
111         dialog.setMessage(message);
112     }
113 
114     @Override
updateDialog()115     public void updateDialog() {
116         updateDialog((AlertDialog) getDialog());
117     }
118 
119     @VisibleForTesting
getSubscriptionManager()120     protected SubscriptionManager getSubscriptionManager() {
121         return getContext().getSystemService(SubscriptionManager.class);
122     }
123 
124     @Override
getMetricsCategory()125     public int getMetricsCategory() {
126         return SettingsEnums.DIALOG_PREFERRED_SIM_PICKER;
127     }
128 }
129