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.car.developeroptions.network.telephony; 18 19 import android.content.Context; 20 import android.database.ContentObserver; 21 import android.net.Uri; 22 import android.os.Handler; 23 import android.os.Looper; 24 import android.os.PersistableBundle; 25 import android.provider.Settings; 26 import android.telephony.CarrierConfigManager; 27 import android.telephony.SubscriptionManager; 28 import android.telephony.TelephonyManager; 29 import android.text.TextUtils; 30 31 import androidx.annotation.VisibleForTesting; 32 import androidx.fragment.app.FragmentManager; 33 import androidx.preference.Preference; 34 import androidx.preference.PreferenceScreen; 35 36 import com.android.car.developeroptions.core.TogglePreferenceController; 37 import com.android.settingslib.RestrictedSwitchPreference; 38 import com.android.settingslib.core.lifecycle.LifecycleObserver; 39 import com.android.settingslib.core.lifecycle.events.OnStart; 40 import com.android.settingslib.core.lifecycle.events.OnStop; 41 42 /** 43 * Preference controller for "Roaming" 44 */ 45 public class RoamingPreferenceController extends TelephonyTogglePreferenceController implements 46 LifecycleObserver, OnStart, OnStop { 47 48 private static final String DIALOG_TAG = "MobileDataDialog"; 49 50 private RestrictedSwitchPreference mSwitchPreference; 51 private TelephonyManager mTelephonyManager; 52 private CarrierConfigManager mCarrierConfigManager; 53 private DataContentObserver mDataContentObserver; 54 @VisibleForTesting 55 boolean mNeedDialog; 56 @VisibleForTesting 57 FragmentManager mFragmentManager; 58 RoamingPreferenceController(Context context, String key)59 public RoamingPreferenceController(Context context, String key) { 60 super(context, key); 61 mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class); 62 mDataContentObserver = new DataContentObserver(new Handler(Looper.getMainLooper())); 63 } 64 65 @Override onStart()66 public void onStart() { 67 mDataContentObserver.register(mContext, mSubId); 68 } 69 70 @Override onStop()71 public void onStop() { 72 mDataContentObserver.unRegister(mContext); 73 } 74 75 @Override displayPreference(PreferenceScreen screen)76 public void displayPreference(PreferenceScreen screen) { 77 super.displayPreference(screen); 78 mSwitchPreference = screen.findPreference(getPreferenceKey()); 79 } 80 81 @Override getAvailabilityStatus(int subId)82 public int getAvailabilityStatus(int subId) { 83 return subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID 84 ? AVAILABLE 85 : AVAILABLE_UNSEARCHABLE; 86 } 87 88 @Override handlePreferenceTreeClick(Preference preference)89 public boolean handlePreferenceTreeClick(Preference preference) { 90 if (TextUtils.equals(preference.getKey(), getPreferenceKey())) { 91 if (mNeedDialog) { 92 showDialog(); 93 } 94 return true; 95 } 96 97 return false; 98 } 99 100 @Override setChecked(boolean isChecked)101 public boolean setChecked(boolean isChecked) { 102 mNeedDialog = isDialogNeeded(); 103 104 if (!mNeedDialog) { 105 // Update data directly if we don't need dialog 106 mTelephonyManager.setDataRoamingEnabled(isChecked); 107 return true; 108 } 109 110 return false; 111 } 112 113 @Override updateState(Preference preference)114 public void updateState(Preference preference) { 115 super.updateState(preference); 116 final RestrictedSwitchPreference switchPreference = (RestrictedSwitchPreference) preference; 117 switchPreference.setEnabled(mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID); 118 switchPreference.setChecked(isChecked()); 119 } 120 121 @VisibleForTesting isDialogNeeded()122 boolean isDialogNeeded() { 123 final boolean isRoamingEnabled = mTelephonyManager.isDataRoamingEnabled(); 124 final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId( 125 mSubId); 126 127 // Need dialog if we need to turn on roaming and the roaming charge indication is allowed 128 if (!isRoamingEnabled && (carrierConfig == null || !carrierConfig.getBoolean( 129 CarrierConfigManager.KEY_DISABLE_CHARGE_INDICATION_BOOL))) { 130 return true; 131 } 132 return false; 133 } 134 135 @Override isChecked()136 public boolean isChecked() { 137 return mTelephonyManager.isDataRoamingEnabled(); 138 } 139 init(FragmentManager fragmentManager, int subId)140 public void init(FragmentManager fragmentManager, int subId) { 141 mFragmentManager = fragmentManager; 142 mSubId = subId; 143 mTelephonyManager = TelephonyManager.from(mContext).createForSubscriptionId(mSubId); 144 } 145 showDialog()146 private void showDialog() { 147 final RoamingDialogFragment dialogFragment = RoamingDialogFragment.newInstance(mSubId); 148 149 dialogFragment.show(mFragmentManager, DIALOG_TAG); 150 } 151 152 /** 153 * Listener that listens data roaming change 154 */ 155 public class DataContentObserver extends ContentObserver { 156 DataContentObserver(Handler handler)157 public DataContentObserver(Handler handler) { 158 super(handler); 159 } 160 161 @Override onChange(boolean selfChange)162 public void onChange(boolean selfChange) { 163 super.onChange(selfChange); 164 updateState(mSwitchPreference); 165 } 166 register(Context context, int subId)167 public void register(Context context, int subId) { 168 Uri uri = Settings.Global.getUriFor(Settings.Global.DATA_ROAMING); 169 if (TelephonyManager.getDefault().getSimCount() != 1) { 170 uri = Settings.Global.getUriFor(Settings.Global.DATA_ROAMING + subId); 171 } 172 context.getContentResolver().registerContentObserver(uri, false, this); 173 174 } 175 unRegister(Context context)176 public void unRegister(Context context) { 177 context.getContentResolver().unregisterContentObserver(this); 178 } 179 } 180 } 181