• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.Dialog;
22 import android.app.settings.SettingsEnums;
23 import android.content.DialogInterface;
24 import android.os.Bundle;
25 import android.telephony.SubscriptionInfo;
26 import android.telephony.SubscriptionManager;
27 import android.text.TextUtils;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.widget.TextView;
32 
33 import androidx.annotation.NonNull;
34 import androidx.annotation.Nullable;
35 import androidx.annotation.VisibleForTesting;
36 import androidx.appcompat.app.AlertDialog;
37 
38 import com.android.settings.R;
39 import com.android.settings.network.SubscriptionUtil;
40 
41 import java.util.List;
42 
43 /**
44  * Presents a dialog asking the user if they want to switch the data to another sim
45  */
46 public class SelectSpecificDataSimDialogFragment extends SimDialogFragment implements
47         DialogInterface.OnClickListener {
48     private static final String TAG = "PreferredSimDialogFrag";
49 
50     private SubscriptionInfo mSubscriptionInfo;
51 
52     /**
53      * @return the dialog fragment.
54      */
newInstance()55     public static SelectSpecificDataSimDialogFragment newInstance() {
56         final SelectSpecificDataSimDialogFragment
57                 fragment = new SelectSpecificDataSimDialogFragment();
58         final Bundle args = initArguments(SimDialogActivity.DATA_PICK,
59                 R.string.select_specific_sim_for_data_title);
60         fragment.setArguments(args);
61         return fragment;
62     }
63 
64     @NonNull
65     @Override
onCreateDialog(@ullable Bundle savedInstanceState)66     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
67         final AlertDialog dialog = new AlertDialog.Builder(getContext())
68                 .setNegativeButton(R.string.sim_action_no_thanks, null)
69                 .create();
70         updateDialog(dialog);
71         return dialog;
72     }
73 
74     @Override
onDismiss(@onNull DialogInterface dialog)75     public void onDismiss(@NonNull DialogInterface dialog) {
76         Log.d(TAG, "Dialog onDismiss, dismiss status: " + mWasDismissed);
77         if (!mWasDismissed) {
78             // This dialog might be called onDismiss twice due to first time called by onDismiss()
79             // as a consequence of user action. We need this fragment alive so the activity
80             // doesn't end, which allows the following dialog to attach. Upon the second dialog
81             // dismiss, this fragment is removed from SimDialogActivity.onFragmentDismissed to
82             // end the activity.
83             mWasDismissed = true;
84             final SimDialogActivity activity = (SimDialogActivity) getActivity();
85             activity.showEnableAutoDataSwitchDialog();
86             // Not using super.onDismiss because it will result in an immediate end of the activity,
87             // before the second auto data switch dialog can attach.
88             if (getDialog() != null) getDialog().dismiss();
89         }
90     }
91 
92     @Override
onClick(DialogInterface dialog, int buttonClicked)93     public void onClick(DialogInterface dialog, int buttonClicked) {
94         final SimDialogActivity activity = (SimDialogActivity) getActivity();
95         if (buttonClicked == DialogInterface.BUTTON_POSITIVE) {
96             final SubscriptionInfo info = getTargetSubscriptionInfo();
97             if (info != null) {
98                 activity.onSubscriptionSelected(getDialogType(), info.getSubscriptionId());
99             }
100         }
101     }
102 
getNonDefaultDataSubscriptionInfo(SubscriptionInfo dds)103     private SubscriptionInfo getNonDefaultDataSubscriptionInfo(SubscriptionInfo dds) {
104         List<SubscriptionInfo> subInfos = getSubscriptionManager().getActiveSubscriptionInfoList();
105         if (subInfos == null || dds == null) {
106             return null;
107         }
108         return subInfos.stream().filter(subinfo -> subinfo.getSubscriptionId()
109                 != dds.getSubscriptionId()).findFirst().orElse(null);
110     }
111 
getDefaultDataSubInfo()112     private SubscriptionInfo getDefaultDataSubInfo() {
113         return getSubscriptionManager().getDefaultDataSubscriptionInfo();
114     }
115 
updateDialog(AlertDialog dialog)116     private void updateDialog(AlertDialog dialog) {
117         Log.d(TAG, "Dialog updated, dismiss status: " + mWasDismissed);
118         if (mWasDismissed) {
119             return;
120         }
121 
122         if (dialog == null) {
123             Log.d(TAG, "Dialog is null.");
124             dismiss();
125         }
126 
127         SubscriptionInfo currentDataSubInfo = getDefaultDataSubInfo();
128         SubscriptionInfo newSubInfo = getNonDefaultDataSubscriptionInfo(currentDataSubInfo);
129 
130         if (newSubInfo == null || currentDataSubInfo == null) {
131             Log.d(TAG, "one of target SubscriptionInfos is null");
132             dismiss();
133             return;
134         }
135 
136         if ((newSubInfo.isEmbedded() && newSubInfo.getProfileClass() == PROFILE_CLASS_PROVISIONING)
137                 || (currentDataSubInfo.isEmbedded()
138                 && currentDataSubInfo.getProfileClass() == PROFILE_CLASS_PROVISIONING)) {
139             Log.d(TAG, "do not set the provision eSIM");
140             dismiss();
141             return;
142         }
143 
144         Log.d(TAG, "newSubId: " + newSubInfo.getSubscriptionId()
145                 + "currentDataSubID: " + currentDataSubInfo.getSubscriptionId());
146         setTargetSubscriptionInfo(newSubInfo);
147 
148         CharSequence newDataCarrierName = SubscriptionUtil.getUniqueSubscriptionDisplayName(
149                 newSubInfo, getContext());
150         CharSequence currentDataCarrierName = SubscriptionUtil.getUniqueSubscriptionDisplayName(
151                 currentDataSubInfo, getContext());
152 
153         String positive = getContext().getString(
154                 R.string.select_specific_sim_for_data_button, newDataCarrierName);
155         String message = getContext().getString(R.string.select_specific_sim_for_data_msg,
156                 newDataCarrierName, currentDataCarrierName);
157 
158         View content = LayoutInflater.from(getContext()).inflate(
159                 R.layout.sim_confirm_dialog_multiple_enabled_profiles_supported, null);
160         TextView dialogMessage = content != null ? content.findViewById(R.id.msg) : null;
161         if (!TextUtils.isEmpty(message) && dialogMessage != null) {
162             dialogMessage.setText(message);
163             dialogMessage.setVisibility(View.VISIBLE);
164         }
165         dialog.setView(content);
166 
167         View titleView = LayoutInflater.from(getContext()).inflate(
168                 R.layout.sim_confirm_dialog_title_multiple_enabled_profiles_supported, null);
169         TextView titleTextView = titleView.findViewById(R.id.title);
170         titleTextView.setText(getContext().getString(getTitleResId(), newDataCarrierName));
171 
172         dialog.setCustomTitle(titleTextView);
173         dialog.setButton(AlertDialog.BUTTON_POSITIVE, positive, this);
174     }
175 
setTargetSubscriptionInfo(SubscriptionInfo subInfo)176     private void setTargetSubscriptionInfo(SubscriptionInfo subInfo) {
177         mSubscriptionInfo = subInfo;
178     }
179 
getTargetSubscriptionInfo()180     private SubscriptionInfo getTargetSubscriptionInfo() {
181         return mSubscriptionInfo;
182     }
183 
184     @Override
updateDialog()185     public void updateDialog() {
186         updateDialog((AlertDialog) getDialog());
187     }
188 
189     @VisibleForTesting
getSubscriptionManager()190     protected SubscriptionManager getSubscriptionManager() {
191         return getContext().getSystemService(SubscriptionManager.class);
192     }
193 
194     @Override
getMetricsCategory()195     public int getMetricsCategory() {
196         return SettingsEnums.DIALOG_SPECIFIC_DDS_SIM_PICKER;
197     }
198 }
199