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.network.telephony; 18 19 import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE; 20 import static androidx.lifecycle.Lifecycle.Event.ON_RESUME; 21 22 import android.content.Context; 23 import android.os.Handler; 24 import android.os.Looper; 25 import android.telephony.SubscriptionManager; 26 import android.telephony.TelephonyManager; 27 28 import androidx.lifecycle.Lifecycle; 29 import androidx.lifecycle.LifecycleObserver; 30 import androidx.lifecycle.OnLifecycleEvent; 31 import androidx.preference.Preference; 32 import androidx.preference.PreferenceScreen; 33 import androidx.preference.SwitchPreference; 34 35 import com.android.internal.annotations.VisibleForTesting; 36 import com.android.settings.network.MobileDataContentObserver; 37 import com.android.settings.network.SubscriptionsChangeListener; 38 39 public class DataDuringCallsPreferenceController extends TelephonyTogglePreferenceController 40 implements LifecycleObserver, 41 SubscriptionsChangeListener.SubscriptionsChangeListenerClient { 42 43 private SwitchPreference mPreference; 44 private SubscriptionsChangeListener mChangeListener; 45 private TelephonyManager mManager; 46 private MobileDataContentObserver mMobileDataContentObserver; 47 private PreferenceScreen mScreen; 48 DataDuringCallsPreferenceController(Context context, String preferenceKey)49 public DataDuringCallsPreferenceController(Context context, 50 String preferenceKey) { 51 super(context, preferenceKey); 52 } 53 init(Lifecycle lifecycle, int subId)54 public void init(Lifecycle lifecycle, int subId) { 55 this.mSubId = subId; 56 mManager = mContext.getSystemService(TelephonyManager.class).createForSubscriptionId(subId); 57 lifecycle.addObserver(this); 58 } 59 60 @OnLifecycleEvent(ON_RESUME) onResume()61 public void onResume() { 62 if (mChangeListener == null) { 63 mChangeListener = new SubscriptionsChangeListener(mContext, this); 64 } 65 mChangeListener.start(); 66 if (mMobileDataContentObserver == null) { 67 mMobileDataContentObserver = new MobileDataContentObserver( 68 new Handler(Looper.getMainLooper())); 69 mMobileDataContentObserver.setOnMobileDataChangedListener(() -> refreshPreference()); 70 } 71 mMobileDataContentObserver.register(mContext, mSubId); 72 } 73 74 @OnLifecycleEvent(ON_PAUSE) onPause()75 public void onPause() { 76 if (mChangeListener != null) { 77 mChangeListener.stop(); 78 } 79 if (mMobileDataContentObserver != null) { 80 mMobileDataContentObserver.unRegister(mContext); 81 } 82 } 83 84 @Override displayPreference(PreferenceScreen screen)85 public void displayPreference(PreferenceScreen screen) { 86 super.displayPreference(screen); 87 mPreference = screen.findPreference(getPreferenceKey()); 88 mScreen = screen; 89 } 90 91 @Override isChecked()92 public boolean isChecked() { 93 return mManager.isMobileDataPolicyEnabled( 94 TelephonyManager.MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL); 95 } 96 97 @Override setChecked(boolean isChecked)98 public boolean setChecked(boolean isChecked) { 99 mManager.setMobileDataPolicyEnabled( 100 TelephonyManager.MOBILE_DATA_POLICY_DATA_ON_NON_DEFAULT_DURING_VOICE_CALL, 101 isChecked); 102 return true; 103 } 104 105 @Override getAvailabilityStatus(int subId)106 public int getAvailabilityStatus(int subId) { 107 if (!SubscriptionManager.isValidSubscriptionId(subId) 108 || SubscriptionManager.getDefaultDataSubscriptionId() == subId) { 109 return CONDITIONALLY_UNAVAILABLE; 110 } 111 return AVAILABLE; 112 } 113 114 @Override updateState(Preference preference)115 public void updateState(Preference preference) { 116 super.updateState(preference); 117 if (preference == null) { 118 return; 119 } 120 preference.setVisible(isAvailable()); 121 } 122 123 @Override onAirplaneModeChanged(boolean airplaneModeEnabled)124 public void onAirplaneModeChanged(boolean airplaneModeEnabled) {} 125 126 @Override onSubscriptionsChanged()127 public void onSubscriptionsChanged() { 128 updateState(mPreference); 129 } 130 131 /** 132 * Trigger displaying preference when Mobilde data content changed. 133 */ 134 @VisibleForTesting refreshPreference()135 public void refreshPreference() { 136 if (mScreen != null) { 137 super.displayPreference(mScreen); 138 } 139 } 140 } 141