diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java index fc20d969238e4..81b8d40b0a37d 100644 --- a/services/java/com/android/server/ConnectivityService.java +++ b/services/java/com/android/server/ConnectivityService.java @@ -81,8 +81,6 @@ public class ConnectivityService extends IConnectivityManager.Stub { */ private List mNetRequestersPids[]; - private WifiWatchdogService mWifiWatchdogService; - // priority order of the nettrackers // (excluding dynamically set mNetworkPreference) // TODO - move mNetworkTypePreference into this @@ -298,11 +296,10 @@ public class ConnectivityService extends IConnectivityManager.Stub { WifiStateTracker wst = new WifiStateTracker(context, mHandler); WifiService wifiService = new WifiService(context, wst); ServiceManager.addService(Context.WIFI_SERVICE, wifiService); + wifiService.startWifi(); mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst; wst.startMonitoring(); - // Constructing this starts it too - mWifiWatchdogService = new WifiWatchdogService(context, wst); break; case ConnectivityManager.TYPE_MOBILE: mNetTrackers[netType] = new MobileDataStateTracker(context, mHandler, diff --git a/services/java/com/android/server/WifiService.java b/services/java/com/android/server/WifiService.java index 248f5798ee167..97a43294f967f 100644 --- a/services/java/com/android/server/WifiService.java +++ b/services/java/com/android/server/WifiService.java @@ -124,6 +124,7 @@ public class WifiService extends IWifiManager.Stub { private INetworkManagementService nwService; ConnectivityManager mCm; + private WifiWatchdogService mWifiWatchdogService = null; private String[] mWifiRegexs; /** @@ -217,8 +218,6 @@ public class WifiService extends IWifiManager.Stub { mWifiStateTracker.setWifiState(WIFI_STATE_DISABLED); mWifiApState = WIFI_AP_STATE_DISABLED; - boolean wifiEnabled = getPersistedWifiEnabled(); - boolean wifiAPEnabled = wifiEnabled ? false : getPersistedWifiApEnabled(); mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); Intent idleIntent = new Intent(ACTION_DEVICE_IDLE, null); @@ -240,9 +239,6 @@ public class WifiService extends IWifiManager.Stub { } ); - Slog.i(TAG, "WifiService starting up with Wi-Fi " + - (wifiEnabled ? "enabled" : "disabled")); - mContext.registerReceiver( new BroadcastReceiver() { @Override @@ -267,7 +263,17 @@ public class WifiService extends IWifiManager.Stub { } },new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)); + } + /** + * Check if Wi-Fi needs to be enabled and start + * if needed + */ + public void startWifi() { + boolean wifiEnabled = getPersistedWifiEnabled(); + boolean wifiAPEnabled = wifiEnabled ? false : getPersistedWifiApEnabled(); + Slog.i(TAG, "WifiService starting up with Wi-Fi " + + (wifiEnabled ? "enabled" : "disabled")); setWifiEnabledBlocking(wifiEnabled, false, Process.myUid()); setWifiApEnabledBlocking(wifiAPEnabled, true, Process.myUid(), null); } @@ -446,6 +452,7 @@ public class WifiService extends IWifiManager.Stub { registerForBroadcasts(); mWifiStateTracker.startEventLoop(); + } else { mContext.unregisterReceiver(mReceiver); @@ -1803,6 +1810,9 @@ public class WifiService extends IWifiManager.Stub { switch (msg.what) { case MESSAGE_ENABLE_WIFI: + if (mWifiWatchdogService == null) { + mWifiWatchdogService = new WifiWatchdogService(mContext, mWifiStateTracker); + } setWifiEnabledBlocking(true, msg.arg1 == 1, msg.arg2); sWakeLock.release(); break; @@ -1821,6 +1831,10 @@ public class WifiService extends IWifiManager.Stub { // a non-zero msg.arg1 value means the "enabled" setting // should be persisted setWifiEnabledBlocking(false, msg.arg1 == 1, msg.arg2); + if (mWifiWatchdogService != null) { + mWifiWatchdogService.quit(); + mWifiWatchdogService = null; + } sWakeLock.release(); break; diff --git a/services/java/com/android/server/WifiWatchdogService.java b/services/java/com/android/server/WifiWatchdogService.java index e50b317a99928..e2c523ddfd4e8 100644 --- a/services/java/com/android/server/WifiWatchdogService.java +++ b/services/java/com/android/server/WifiWatchdogService.java @@ -89,6 +89,8 @@ public class WifiWatchdogService { */ private WifiWatchdogHandler mHandler; + private ContentObserver mContentObserver; + /** * The current watchdog state. Only written from the main thread! */ @@ -132,7 +134,7 @@ public class WifiWatchdogService { ContentResolver contentResolver = mContext.getContentResolver(); contentResolver.registerContentObserver( Settings.Secure.getUriFor(Settings.Secure.WIFI_WATCHDOG_ON), false, - new ContentObserver(mHandler) { + mContentObserver = new ContentObserver(mHandler) { @Override public void onChange(boolean selfChange) { if (isWatchdogEnabled()) { @@ -271,6 +273,16 @@ public class WifiWatchdogService { waitForHandlerCreation(); } + /** + * Unregister broadcasts and quit the watchdog thread + */ + public void quit() { + unregisterForWifiBroadcasts(); + mContext.getContentResolver().unregisterContentObserver(mContentObserver); + mHandler.removeAllActions(); + mHandler.getLooper().quit(); + } + /** * Waits for the main watchdog thread to create the handler. */