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.isDataAllowedInVoiceCall(); 94 } 95 96 @Override setChecked(boolean isChecked)97 public boolean setChecked(boolean isChecked) { 98 mManager.setDataAllowedDuringVoiceCall(isChecked); 99 return true; 100 } 101 102 @Override getAvailabilityStatus(int subId)103 public int getAvailabilityStatus(int subId) { 104 if (!SubscriptionManager.isValidSubscriptionId(subId) 105 || SubscriptionManager.getDefaultDataSubscriptionId() == subId) { 106 return CONDITIONALLY_UNAVAILABLE; 107 } 108 return AVAILABLE; 109 } 110 111 @Override updateState(Preference preference)112 public void updateState(Preference preference) { 113 super.updateState(preference); 114 if (preference == null) { 115 return; 116 } 117 preference.setVisible(isAvailable()); 118 } 119 120 @Override onAirplaneModeChanged(boolean airplaneModeEnabled)121 public void onAirplaneModeChanged(boolean airplaneModeEnabled) {} 122 123 @Override onSubscriptionsChanged()124 public void onSubscriptionsChanged() { 125 updateState(mPreference); 126 } 127 128 /** 129 * Trigger displaying preference when Mobilde data content changed. 130 */ 131 @VisibleForTesting refreshPreference()132 public void refreshPreference() { 133 if (mScreen != null) { 134 super.displayPreference(mScreen); 135 } 136 } 137 } 138