Run WifiWatchdogServiceThread only on wifi enable

Bug: 2508997
Change-Id: Ib79ee25fcc8e39e9a1d6c5b9ef9681bc00b6d006
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() {