Run WifiWatchdogServiceThread only on wifi enable

Bug: 2508997
Change-Id: Ib79ee25fcc8e39e9a1d6c5b9ef9681bc00b6d006
diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java
index fc20d96..81b8d40 100644
--- a/services/java/com/android/server/ConnectivityService.java
+++ b/services/java/com/android/server/ConnectivityService.java
@@ -81,8 +81,6 @@
      */
     private List mNetRequestersPids[];
 
-    private WifiWatchdogService mWifiWatchdogService;
-
     // priority order of the nettrackers
     // (excluding dynamically set mNetworkPreference)
     // TODO - move mNetworkTypePreference into this
@@ -298,11 +296,10 @@
                 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 248f579..97a4329 100644
--- a/services/java/com/android/server/WifiService.java
+++ b/services/java/com/android/server/WifiService.java
@@ -124,6 +124,7 @@
 
     private INetworkManagementService nwService;
     ConnectivityManager mCm;
+    private WifiWatchdogService mWifiWatchdogService = null;
     private String[] mWifiRegexs;
 
     /**
@@ -217,8 +218,6 @@
 
         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 @@
                 }
         );
 
-        Slog.i(TAG, "WifiService starting up with Wi-Fi " +
-                (wifiEnabled ? "enabled" : "disabled"));
-
         mContext.registerReceiver(
                 new BroadcastReceiver() {
                     @Override
@@ -267,7 +263,17 @@
 
                 }
             },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 @@
 
             registerForBroadcasts();
             mWifiStateTracker.startEventLoop();
+
         } else {
 
             mContext.unregisterReceiver(mReceiver);
@@ -1803,6 +1810,9 @@
             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 @@
                     // 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 e50b317..e2c523d 100644
--- a/services/java/com/android/server/WifiWatchdogService.java
+++ b/services/java/com/android/server/WifiWatchdogService.java
@@ -89,6 +89,8 @@
      */
     private WifiWatchdogHandler mHandler;
 
+    private ContentObserver mContentObserver;
+
     /**
      * The current watchdog state. Only written from the main thread!
      */
@@ -132,7 +134,7 @@
         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()) {
@@ -272,6 +274,16 @@
     }
 
     /**
+     * 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.
      */
     private void waitForHandlerCreation() {