Only register debug hooks when requested

Always registering them was causing ANRs because of blocking binder calls,
registering on a background thread also isn't ideal beacuse wake locks might
be released and let the device sleep.
Let's only register them when explicitly required.

Change-Id: I52c45fcb38c277903ece662afdd5e6c35cf2aac4
Fixes: 119436283
Test: adb shell setprop debug.aod_brightness 1
(cherry picked from commit bf2fbd5b68b82838e36eae085c82fa9165038ecd)
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java b/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java
index 01a2345..1dd3101 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java
@@ -24,20 +24,21 @@
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
 import android.hardware.SensorManager;
-import android.os.Build;
 import android.os.Handler;
+import android.os.SystemProperties;
 import android.os.Trace;
 import android.os.UserHandle;
 import android.provider.Settings;
 
 import com.android.internal.annotations.VisibleForTesting;
-import com.android.systemui.Dependency;
 
 /**
  * Controls the screen brightness when dozing.
  */
 public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachine.Part,
         SensorEventListener {
+    private static final boolean DEBUG_AOD_BRIGHTNESS = SystemProperties
+            .getBoolean("debug.aod_brightness", false);
     protected static final String ACTION_AOD_BRIGHTNESS =
             "com.android.systemui.doze.AOD_BRIGHTNESS";
     protected static final String BRIGHTNESS_BUCKET = "brightness_bucket";
@@ -83,11 +84,9 @@
         mSensorToScrimOpacity = sensorToScrimOpacity;
 
         if (mDebuggable) {
-            Dependency.get(Dependency.BG_HANDLER).post(()-> {
-                IntentFilter filter = new IntentFilter();
-                filter.addAction(ACTION_AOD_BRIGHTNESS);
-                mContext.registerReceiverAsUser(this, UserHandle.ALL, filter, null, handler);
-            });
+            IntentFilter filter = new IntentFilter();
+            filter.addAction(ACTION_AOD_BRIGHTNESS);
+            mContext.registerReceiverAsUser(this, UserHandle.ALL, filter, null, handler);
         }
     }
 
@@ -97,7 +96,7 @@
         this(context, service, sensorManager, lightSensor, host, handler,
                 context.getResources().getInteger(
                         com.android.internal.R.integer.config_screenBrightnessDoze),
-                policy.screenBrightnessArray, policy.dimmingScrimArray, Build.IS_DEBUGGABLE);
+                policy.screenBrightnessArray, policy.dimmingScrimArray, DEBUG_AOD_BRIGHTNESS);
     }
 
     @Override
@@ -126,9 +125,7 @@
     private void onDestroy() {
         setLightSensorEnabled(false);
         if (mDebuggable) {
-            Dependency.get(Dependency.BG_HANDLER).post(()-> {
-                mContext.unregisterReceiver(this);
-            });
+            mContext.unregisterReceiver(this);
         }
     }