1 /*
2  * Copyright (C) 2020 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 package com.android.settings.development;
17 
18 import android.content.ContentResolver;
19 import android.content.Context;
20 import android.database.ContentObserver;
21 import android.debug.IAdbManager;
22 import android.net.Uri;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.os.RemoteException;
26 import android.os.ServiceManager;
27 import android.provider.Settings;
28 import android.util.Log;
29 
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settings.core.PreferenceControllerMixin;
34 import com.android.settings.widget.MasterSwitchPreference;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import com.android.settingslib.core.lifecycle.LifecycleObserver;
37 import com.android.settingslib.core.lifecycle.events.OnPause;
38 import com.android.settingslib.core.lifecycle.events.OnResume;
39 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
40 
41 /**
42  * This controls the master switch controller in the developer options page for
43  * "Wireless debugging".
44  */
45 public class WirelessDebuggingPreferenceController extends DeveloperOptionsPreferenceController
46         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin,
47         LifecycleObserver, OnResume, OnPause {
48     private static final String TAG = "WirelessDebugPrefCtrl";
49     private final IAdbManager mAdbManager;
50     private final ContentResolver mContentResolver;
51     private final ContentObserver mSettingsObserver;
52     private final Handler mHandler = new Handler(Looper.getMainLooper());
53 
54     public static final String KEY_TOGGLE_ADB_WIRELESS = "toggle_adb_wireless";
55 
WirelessDebuggingPreferenceController(Context context, Lifecycle lifecycle)56     public WirelessDebuggingPreferenceController(Context context, Lifecycle lifecycle) {
57         super(context);
58 
59         if (lifecycle != null) {
60             lifecycle.addObserver(this);
61         }
62         mAdbManager = IAdbManager.Stub.asInterface(ServiceManager.getService(Context.ADB_SERVICE));
63         mSettingsObserver = new ContentObserver(mHandler) {
64             @Override
65             public void onChange(boolean selfChange, Uri uri) {
66                 updateState(mPreference);
67             }
68         };
69         mContentResolver = context.getContentResolver();
70     }
71 
72     @Override
displayPreference(PreferenceScreen screen)73     public void displayPreference(PreferenceScreen screen) {
74         super.displayPreference(screen);
75     }
76 
77     @Override
isAvailable()78     public boolean isAvailable() {
79         try {
80             return mAdbManager.isAdbWifiSupported();
81         } catch (RemoteException e) {
82             Log.e(TAG, "Unable to check if adb wifi is supported.", e);
83         }
84 
85         return false;
86     }
87 
88     @Override
getPreferenceKey()89     public String getPreferenceKey() {
90         return KEY_TOGGLE_ADB_WIRELESS;
91     }
92 
93     /**
94      * Called when developer options is enabled and the preference is available
95      */
96     @Override
onDeveloperOptionsSwitchEnabled()97     protected void onDeveloperOptionsSwitchEnabled() {
98         super.onDeveloperOptionsSwitchEnabled();
99         mPreference.setEnabled(true);
100     }
101 
102     /**
103      * Called when developer options is disabled and the preference is available
104      */
105     @Override
onDeveloperOptionsSwitchDisabled()106     protected void onDeveloperOptionsSwitchDisabled() {
107         super.onDeveloperOptionsSwitchDisabled();
108         mPreference.setEnabled(false);
109         Settings.Global.putInt(mContext.getContentResolver(),
110                 Settings.Global.ADB_WIFI_ENABLED,
111                 AdbPreferenceController.ADB_SETTING_OFF);
112     }
113 
114     @Override
onResume()115     public void onResume() {
116         mContentResolver.registerContentObserver(
117                 Settings.Global.getUriFor(Settings.Global.ADB_WIFI_ENABLED), false,
118                 mSettingsObserver);
119     }
120 
121     @Override
onPause()122     public void onPause() {
123         mContentResolver.unregisterContentObserver(mSettingsObserver);
124     }
125 
126     @Override
updateState(Preference preference)127     public void updateState(Preference preference) {
128         boolean enabled = Settings.Global.getInt(mContentResolver,
129                 Settings.Global.ADB_WIFI_ENABLED, AdbPreferenceController.ADB_SETTING_OFF)
130                     != AdbPreferenceController.ADB_SETTING_OFF;
131         ((MasterSwitchPreference) preference).setChecked(enabled);
132     }
133 
134     @Override
onPreferenceChange(Preference preference, Object newValue)135     public boolean onPreferenceChange(Preference preference, Object newValue) {
136         final boolean enabled = (Boolean) newValue;
137         Settings.Global.putInt(mContext.getContentResolver(),
138                 Settings.Global.ADB_WIFI_ENABLED,
139                 enabled ? AdbPreferenceController.ADB_SETTING_ON
140                 : AdbPreferenceController.ADB_SETTING_OFF);
141         return true;
142     }
143 }
144