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