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