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