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         mChangeListener = new SubscriptionsChangeListener(mContext, this);
53         mMobileDataContentObserver = new MobileDataContentObserver(
54                 new Handler(Looper.getMainLooper()));
55         mMobileDataContentObserver.setOnMobileDataChangedListener(()->refreshPreference());
56     }
57 
init(Lifecycle lifecycle, int subId)58     public void init(Lifecycle lifecycle, int subId) {
59         this.mSubId = subId;
60         mManager = mContext.getSystemService(TelephonyManager.class).createForSubscriptionId(subId);
61         lifecycle.addObserver(this);
62     }
63 
64     @OnLifecycleEvent(ON_RESUME)
onResume()65     public void onResume() {
66         mChangeListener.start();
67         mMobileDataContentObserver.register(mContext, mSubId);
68     }
69 
70     @OnLifecycleEvent(ON_PAUSE)
onPause()71     public void onPause() {
72         mChangeListener.stop();
73         mMobileDataContentObserver.unRegister(mContext);
74     }
75 
76     @Override
displayPreference(PreferenceScreen screen)77     public void displayPreference(PreferenceScreen screen) {
78         super.displayPreference(screen);
79         mPreference = screen.findPreference(getPreferenceKey());
80         mScreen = screen;
81     }
82 
83     @Override
isChecked()84     public boolean isChecked() {
85         return mManager.isDataAllowedInVoiceCall();
86     }
87 
88     @Override
setChecked(boolean isChecked)89     public boolean setChecked(boolean isChecked) {
90         mManager.setDataAllowedDuringVoiceCall(isChecked);
91         return true;
92     }
93 
94     @Override
getAvailabilityStatus(int subId)95     public int getAvailabilityStatus(int subId) {
96         if (!SubscriptionManager.isValidSubscriptionId(subId)
97                 || SubscriptionManager.getDefaultDataSubscriptionId() == subId) {
98             return CONDITIONALLY_UNAVAILABLE;
99         }
100         return AVAILABLE;
101     }
102 
103     @Override
updateState(Preference preference)104     public void updateState(Preference preference) {
105         super.updateState(preference);
106         preference.setVisible(isAvailable());
107     }
108 
109     @Override
onAirplaneModeChanged(boolean airplaneModeEnabled)110     public void onAirplaneModeChanged(boolean airplaneModeEnabled) {}
111 
112     @Override
onSubscriptionsChanged()113     public void onSubscriptionsChanged() {
114         updateState(mPreference);
115     }
116 
117     /**
118      * Trigger displaying preference when Mobilde data content changed.
119      */
120     @VisibleForTesting
refreshPreference()121     public void refreshPreference() {
122         if (mScreen != null) {
123             super.displayPreference(mScreen);
124         }
125     }
126 }
127