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;
18 
19 import static com.android.settings.network.TetherEnabler.TETHERING_BLUETOOTH_ON;
20 import static com.android.settings.network.TetherEnabler.TETHERING_ETHERNET_ON;
21 import static com.android.settings.network.TetherEnabler.TETHERING_USB_ON;
22 import static com.android.settings.network.TetherEnabler.TETHERING_WIFI_ON;
23 
24 import android.content.Context;
25 import android.net.ConnectivityManager;
26 
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.R;
31 import com.android.settings.Utils;
32 
33 /**
34  * This controller helps to manage the switch state and visibility of wifi tether disable switch
35  * preference. When the preference checked, wifi tether will be disabled.
36  *
37  * @see BluetoothTetherPreferenceController
38  * @see UsbTetherPreferenceController
39  */
40 public final class WifiTetherDisablePreferenceController extends TetherBasePreferenceController {
41 
42     private static final String TAG = "WifiTetherDisablePreferenceController";
43 
44     private PreferenceScreen mScreen;
45 
WifiTetherDisablePreferenceController(Context context, String prefKey)46     public WifiTetherDisablePreferenceController(Context context, String prefKey) {
47         super(context, prefKey);
48     }
49 
50     @Override
isChecked()51     public boolean isChecked() {
52         return !super.isChecked();
53     }
54 
55     @Override
setChecked(boolean isChecked)56     public boolean setChecked(boolean isChecked) {
57         return super.setChecked(!isChecked);
58     }
59 
getTetheringStateOfOtherInterfaces()60     private int getTetheringStateOfOtherInterfaces() {
61         return mTetheringState & (~TETHERING_WIFI_ON);
62     }
63 
64     @Override
shouldEnable()65     public boolean shouldEnable() {
66         return true;
67     }
68 
69     @Override
shouldShow()70     public boolean shouldShow() {
71         final String[] wifiRegexs = mCm.getTetherableWifiRegexs();
72         return wifiRegexs != null && wifiRegexs.length != 0 && !Utils.isMonkeyRunning()
73                 && getTetheringStateOfOtherInterfaces() != TetherEnabler.TETHERING_OFF;
74     }
75 
76     @Override
getTetherType()77     public int getTetherType() {
78         return ConnectivityManager.TETHERING_WIFI;
79     }
80 
81     @Override
getSummary()82     public CharSequence getSummary() {
83         switch (getTetheringStateOfOtherInterfaces()) {
84             case TETHERING_USB_ON:
85                 return mContext.getString(R.string.disable_wifi_hotspot_when_usb_on);
86             case TETHERING_BLUETOOTH_ON:
87                 return mContext.getString(R.string.disable_wifi_hotspot_when_bluetooth_on);
88             case TETHERING_ETHERNET_ON:
89                 return mContext.getString(R.string.disable_wifi_hotspot_when_ethernet_on);
90             case TETHERING_USB_ON | TETHERING_BLUETOOTH_ON:
91                 return mContext.getString(R.string.disable_wifi_hotspot_when_usb_and_bluetooth_on);
92             case TETHERING_USB_ON | TETHERING_ETHERNET_ON:
93                 return mContext.getString(R.string.disable_wifi_hotspot_when_usb_and_ethernet_on);
94             case TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON:
95                 return mContext.getString(
96                         R.string.disable_wifi_hotspot_when_bluetooth_and_ethernet_on);
97             case TETHERING_USB_ON | TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON:
98                 return mContext.getString(
99                         R.string.disable_wifi_hotspot_when_usb_and_bluetooth_and_ethernet_on);
100             default:
101                 return mContext.getString(R.string.summary_placeholder);
102         }
103     }
104 
105     @Override
displayPreference(PreferenceScreen screen)106     public void displayPreference(PreferenceScreen screen) {
107         super.displayPreference(screen);
108         mScreen = screen;
109         if (mPreference != null) {
110             mPreference.setOnPreferenceChangeListener(this);
111         }
112     }
113 
114     @Override
updateState(Preference preference)115     public void updateState(Preference preference) {
116         super.updateState(preference);
117         preference.setVisible(isAvailable());
118         refreshSummary(preference);
119     }
120 }
121