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