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.telephony.SubscriptionManager; 24 import android.telephony.TelephonyManager; 25 26 import androidx.lifecycle.Lifecycle; 27 import androidx.lifecycle.LifecycleObserver; 28 import androidx.lifecycle.OnLifecycleEvent; 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceScreen; 31 import androidx.preference.SwitchPreference; 32 33 import com.android.settings.network.SubscriptionsChangeListener; 34 35 public class DataDuringCallsPreferenceController extends TelephonyTogglePreferenceController 36 implements LifecycleObserver, 37 SubscriptionsChangeListener.SubscriptionsChangeListenerClient { 38 39 private SwitchPreference mPreference; 40 private SubscriptionsChangeListener mChangeListener; 41 private TelephonyManager mManager; 42 DataDuringCallsPreferenceController(Context context, String preferenceKey)43 public DataDuringCallsPreferenceController(Context context, 44 String preferenceKey) { 45 super(context, preferenceKey); 46 mChangeListener = new SubscriptionsChangeListener(mContext, this); 47 } 48 init(Lifecycle lifecycle, int subId)49 public void init(Lifecycle lifecycle, int subId) { 50 this.mSubId = subId; 51 mManager = mContext.getSystemService(TelephonyManager.class).createForSubscriptionId(subId); 52 lifecycle.addObserver(this); 53 } 54 55 @OnLifecycleEvent(ON_RESUME) onResume()56 public void onResume() { 57 mChangeListener.start(); 58 } 59 60 @OnLifecycleEvent(ON_PAUSE) onPause()61 public void onPause() { 62 mChangeListener.stop(); 63 } 64 65 @Override displayPreference(PreferenceScreen screen)66 public void displayPreference(PreferenceScreen screen) { 67 super.displayPreference(screen); 68 mPreference = screen.findPreference(getPreferenceKey()); 69 } 70 71 @Override isChecked()72 public boolean isChecked() { 73 return mManager.isDataAllowedInVoiceCall(); 74 } 75 76 @Override setChecked(boolean isChecked)77 public boolean setChecked(boolean isChecked) { 78 mManager.setDataAllowedDuringVoiceCall(isChecked); 79 return true; 80 } 81 82 @Override getAvailabilityStatus(int subId)83 public int getAvailabilityStatus(int subId) { 84 if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID || 85 SubscriptionManager.getDefaultDataSubscriptionId() == mSubId) { 86 return CONDITIONALLY_UNAVAILABLE; 87 } 88 return AVAILABLE; 89 } 90 91 @Override updateState(Preference preference)92 public void updateState(Preference preference) { 93 super.updateState(preference); 94 preference.setVisible(isAvailable()); 95 } 96 97 @Override onAirplaneModeChanged(boolean airplaneModeEnabled)98 public void onAirplaneModeChanged(boolean airplaneModeEnabled) {} 99 100 @Override onSubscriptionsChanged()101 public void onSubscriptionsChanged() { 102 updateState(mPreference); 103 } 104 } 105