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(@ullable AlertDialog dialog)116 private void updateDialog(@Nullable 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 return; 126 } 127 128 SubscriptionInfo currentDataSubInfo = getDefaultDataSubInfo(); 129 SubscriptionInfo newSubInfo = getNonDefaultDataSubscriptionInfo(currentDataSubInfo); 130 131 if (newSubInfo == null || currentDataSubInfo == null) { 132 Log.d(TAG, "one of target SubscriptionInfos is null"); 133 dismiss(); 134 return; 135 } 136 137 if ((newSubInfo.isEmbedded() 138 && (newSubInfo.getProfileClass() == PROFILE_CLASS_PROVISIONING 139 || newSubInfo.isOnlyNonTerrestrialNetwork())) 140 || (currentDataSubInfo.isEmbedded() 141 && (currentDataSubInfo.getProfileClass() == PROFILE_CLASS_PROVISIONING 142 || currentDataSubInfo.isOnlyNonTerrestrialNetwork()))) { 143 Log.d(TAG, "do not set the provisioning or satellite eSIM"); 144 dismiss(); 145 return; 146 } 147 148 Log.d(TAG, "newSubId: " + newSubInfo.getSubscriptionId() 149 + "currentDataSubID: " + currentDataSubInfo.getSubscriptionId()); 150 setTargetSubscriptionInfo(newSubInfo); 151 152 CharSequence newDataCarrierName = SubscriptionUtil.getUniqueSubscriptionDisplayName( 153 newSubInfo, getContext()); 154 CharSequence currentDataCarrierName = SubscriptionUtil.getUniqueSubscriptionDisplayName( 155 currentDataSubInfo, getContext()); 156 157 String positive = getContext().getString( 158 R.string.select_specific_sim_for_data_button, newDataCarrierName); 159 String message = getContext().getString(R.string.select_specific_sim_for_data_msg, 160 newDataCarrierName, currentDataCarrierName); 161 162 View content = LayoutInflater.from(getContext()).inflate( 163 R.layout.sim_confirm_dialog_multiple_enabled_profiles_supported, null); 164 TextView dialogMessage = content != null ? content.findViewById(R.id.msg) : null; 165 if (!TextUtils.isEmpty(message) && dialogMessage != null) { 166 dialogMessage.setText(message); 167 dialogMessage.setVisibility(View.VISIBLE); 168 } 169 dialog.setView(content); 170 171 View titleView = LayoutInflater.from(getContext()).inflate( 172 R.layout.sim_confirm_dialog_title_multiple_enabled_profiles_supported, null); 173 TextView titleTextView = titleView.findViewById(R.id.title); 174 titleTextView.setText(getContext().getString(getTitleResId(), newDataCarrierName)); 175 176 dialog.setCustomTitle(titleTextView); 177 dialog.setButton(AlertDialog.BUTTON_POSITIVE, positive, this); 178 } 179 setTargetSubscriptionInfo(SubscriptionInfo subInfo)180 private void setTargetSubscriptionInfo(SubscriptionInfo subInfo) { 181 mSubscriptionInfo = subInfo; 182 } 183 getTargetSubscriptionInfo()184 private SubscriptionInfo getTargetSubscriptionInfo() { 185 return mSubscriptionInfo; 186 } 187 188 @Override updateDialog()189 public void updateDialog() { 190 updateDialog((AlertDialog) getDialog()); 191 } 192 193 @VisibleForTesting getSubscriptionManager()194 protected SubscriptionManager getSubscriptionManager() { 195 return getContext().getSystemService(SubscriptionManager.class).createForAllUserProfiles(); 196 } 197 198 @Override getMetricsCategory()199 public int getMetricsCategory() { 200 return SettingsEnums.DIALOG_SPECIFIC_DDS_SIM_PICKER; 201 } 202 } 203