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