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 
17 package com.android.settings.development;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.database.ContentObserver;
22 import android.net.Uri;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.provider.Settings;
26 import android.util.Log;
27 
28 import com.android.settings.widget.SwitchWidgetController;
29 import com.android.settingslib.core.lifecycle.Lifecycle;
30 import com.android.settingslib.core.lifecycle.LifecycleObserver;
31 import com.android.settingslib.core.lifecycle.events.OnPause;
32 import com.android.settingslib.core.lifecycle.events.OnResume;
33 
34 /**
35  * Class to control the switch bar in the wireless debugging fragment.
36  */
37 public class WirelessDebuggingEnabler implements SwitchWidgetController.OnSwitchChangeListener,
38         LifecycleObserver, OnResume, OnPause {
39     private static final String TAG = "WirelessDebuggingEnabler";
40 
41     private final SwitchWidgetController mSwitchWidget;
42     private Context mContext;
43     private boolean mListeningToOnSwitchChange = false;
44     private OnEnabledListener mListener;
45     private final ContentResolver mContentResolver;
46     private final ContentObserver mSettingsObserver;
47     private final Handler mHandler = new Handler(Looper.getMainLooper());
48 
WirelessDebuggingEnabler(Context context, SwitchWidgetController switchWidget, OnEnabledListener listener, Lifecycle lifecycle)49     public WirelessDebuggingEnabler(Context context, SwitchWidgetController switchWidget,
50             OnEnabledListener listener, Lifecycle lifecycle) {
51         mContext = context;
52         mSwitchWidget = switchWidget;
53         mSwitchWidget.setListener(this);
54         mSwitchWidget.setupView();
55 
56         mListener = listener;
57         if (lifecycle != null) {
58             lifecycle.addObserver(this);
59         }
60 
61         mContentResolver = context.getContentResolver();
62         mSettingsObserver = new ContentObserver(mHandler) {
63             @Override
64             public void onChange(boolean selfChange, Uri uri) {
65                 Log.i(TAG, "ADB_WIFI_ENABLED=" + isAdbWifiEnabled());
66                 onWirelessDebuggingEnabled(isAdbWifiEnabled());
67             }
68         };
69     }
70 
isAdbWifiEnabled()71     private boolean isAdbWifiEnabled() {
72         return Settings.Global.getInt(mContentResolver, Settings.Global.ADB_WIFI_ENABLED,
73                 AdbPreferenceController.ADB_SETTING_OFF)
74                 != AdbPreferenceController.ADB_SETTING_OFF;
75     }
76 
77     /**
78      * Tears down the switch controller for the wireless debugging switch.
79      */
teardownSwitchController()80     public void teardownSwitchController() {
81         if (mListeningToOnSwitchChange) {
82             mSwitchWidget.stopListening();
83             mListeningToOnSwitchChange = false;
84         }
85         mSwitchWidget.teardownView();
86     }
87 
88     @Override
onResume()89     public void onResume() {
90         if (!mListeningToOnSwitchChange) {
91             mSwitchWidget.startListening();
92             mListeningToOnSwitchChange = true;
93         }
94         onWirelessDebuggingEnabled(isAdbWifiEnabled());
95         mContentResolver.registerContentObserver(
96                 Settings.Global.getUriFor(Settings.Global.ADB_WIFI_ENABLED), false,
97                 mSettingsObserver);
98     }
99 
100     @Override
onPause()101     public void onPause() {
102         if (mListeningToOnSwitchChange) {
103             mSwitchWidget.stopListening();
104             mListeningToOnSwitchChange = false;
105         }
106         mContentResolver.unregisterContentObserver(mSettingsObserver);
107     }
108 
onWirelessDebuggingEnabled(boolean enabled)109     private void onWirelessDebuggingEnabled(boolean enabled) {
110         mSwitchWidget.setChecked(enabled);
111         if (mListener != null) {
112             mListener.onEnabled(enabled);
113         }
114     }
115 
writeAdbWifiSetting(boolean enabled)116     protected void writeAdbWifiSetting(boolean enabled) {
117         Settings.Global.putInt(mContext.getContentResolver(),
118                 Settings.Global.ADB_WIFI_ENABLED, enabled ? AdbPreferenceController.ADB_SETTING_ON
119                 : AdbPreferenceController.ADB_SETTING_OFF);
120     }
121 
122     @Override
onSwitchToggled(boolean isChecked)123     public boolean onSwitchToggled(boolean isChecked) {
124         writeAdbWifiSetting(isChecked);
125         return true;
126     }
127 
128     /**
129      * Interface for subscribers to implement in order to listen for
130      * wireless debugging state changes.
131      */
132     public interface OnEnabledListener {
133         /**
134          * Called when wireless debugging state changes.
135          *
136          * @param enabled the state of wireless debugging
137          */
onEnabled(boolean enabled)138         void onEnabled(boolean enabled);
139     }
140 }
141