1 /*
2  * Copyright (C) 2018 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.network.telephony;
18 
19 import android.app.Dialog;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.os.Bundle;
24 import android.telephony.SubscriptionInfo;
25 import android.telephony.SubscriptionManager;
26 
27 import androidx.appcompat.app.AlertDialog;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
31 import com.android.settings.network.SubscriptionUtil;
32 import com.android.settings.wifi.WifiPickerTrackerHelper;
33 
34 /**
35  * Dialog Fragment to show dialog for "mobile data"
36  *
37  * 1. When user want to disable data in single sim case, show dialog to confirm
38  * 2. When user want to enable data in multiple sim case, show dialog to confirm to disable other
39  * sim
40  */
41 public class MobileDataDialogFragment extends InstrumentedDialogFragment implements
42         DialogInterface.OnClickListener {
43 
44     public static final int TYPE_DISABLE_DIALOG = 0;
45     public static final int TYPE_MULTI_SIM_DIALOG = 1;
46 
47     private static final String ARG_DIALOG_TYPE = "dialog_type";
48     private static final String ARG_SUB_ID = "subId";
49 
50     private SubscriptionManager mSubscriptionManager;
51     private int mType;
52     private int mSubId;
53 
54     private WifiPickerTrackerHelper mWifiPickerTrackerHelper;
55 
newInstance(int type, int subId)56     public static MobileDataDialogFragment newInstance(int type, int subId) {
57         final MobileDataDialogFragment dialogFragment = new MobileDataDialogFragment();
58 
59         Bundle args = new Bundle();
60         args.putInt(ARG_DIALOG_TYPE, type);
61         args.putInt(ARG_SUB_ID, subId);
62         dialogFragment.setArguments(args);
63 
64         return dialogFragment;
65     }
66 
67     @Override
onCreate(Bundle savedInstanceState)68     public void onCreate(Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70         mSubscriptionManager = getContext().getSystemService(SubscriptionManager.class);
71         mWifiPickerTrackerHelper = new WifiPickerTrackerHelper(getSettingsLifecycle(), getContext(),
72                 null /* WifiPickerTrackerCallback */);
73     }
74 
75     @Override
onCreateDialog(Bundle savedInstanceState)76     public Dialog onCreateDialog(Bundle savedInstanceState) {
77         final Bundle bundle = getArguments();
78         final Context context = getContext();
79 
80         mType = bundle.getInt(ARG_DIALOG_TYPE);
81         mSubId = bundle.getInt(ARG_SUB_ID);
82 
83         switch (mType) {
84             case TYPE_DISABLE_DIALOG:
85                 return new AlertDialog.Builder(context)
86                         .setMessage(R.string.data_usage_disable_mobile)
87                         .setPositiveButton(android.R.string.ok, this)
88                         .setNegativeButton(android.R.string.cancel, null)
89                         .create();
90             case TYPE_MULTI_SIM_DIALOG:
91                 final SubscriptionInfo currentSubInfo =
92                         mSubscriptionManager.getActiveSubscriptionInfo(mSubId);
93                 final SubscriptionInfo nextSubInfo =
94                         mSubscriptionManager.getActiveSubscriptionInfo(
95                                 mSubscriptionManager.getDefaultDataSubscriptionId());
96 
97                 final String previousName = (nextSubInfo == null)
98                         ? getContext().getResources().getString(
99                         R.string.sim_selection_required_pref)
100                         : SubscriptionUtil.getUniqueSubscriptionDisplayName(
101                                 nextSubInfo, getContext()).toString();
102 
103                 final String newName = (currentSubInfo == null)
104                         ? getContext().getResources().getString(
105                         R.string.sim_selection_required_pref)
106                         : SubscriptionUtil.getUniqueSubscriptionDisplayName(
107                                 currentSubInfo, getContext()).toString();
108 
109                 return new AlertDialog.Builder(context)
110                         .setTitle(context.getString(R.string.sim_change_data_title, newName))
111                         .setMessage(context.getString(R.string.sim_change_data_message,
112                                 newName, previousName))
113                         .setPositiveButton(
114                                 context.getString(R.string.sim_change_data_ok, newName),
115                                 this)
116                         .setNegativeButton(R.string.cancel, null)
117                         .create();
118             default:
119                 throw new IllegalArgumentException("unknown type " + mType);
120         }
121     }
122 
123     @Override
getMetricsCategory()124     public int getMetricsCategory() {
125         return SettingsEnums.MOBILE_DATA_DIALOG;
126     }
127 
128     @Override
onClick(DialogInterface dialog, int which)129     public void onClick(DialogInterface dialog, int which) {
130         switch (mType) {
131             case TYPE_DISABLE_DIALOG:
132                 MobileNetworkUtils.setMobileDataEnabled(getContext(), mSubId, false /* enabled */,
133                         false /* disableOtherSubscriptions */);
134                 if (mWifiPickerTrackerHelper != null
135                         && !mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(mSubId)) {
136                     mWifiPickerTrackerHelper.setCarrierNetworkEnabled(false);
137                 }
138                 break;
139             case TYPE_MULTI_SIM_DIALOG:
140                 mSubscriptionManager.setDefaultDataSubId(mSubId);
141                 MobileNetworkUtils.setMobileDataEnabled(getContext(), mSubId, true /* enabled */,
142                         true /* disableOtherSubscriptions */);
143                 if (mWifiPickerTrackerHelper != null
144                         && !mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(mSubId)) {
145                     mWifiPickerTrackerHelper.setCarrierNetworkEnabled(true);
146                 }
147                 break;
148             default:
149                 throw new IllegalArgumentException("unknown type " + mType);
150         }
151     }
152 
153 }
154