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