Use activity for brightness dialog

Also fixes brightness controller failure to unregister callbacks.

BUG: 15512088
Change-Id: Ia665e006d93391d5a66d4ace614660c4e6d2d5b5
diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java
index 4153a02..4b5ec48 100644
--- a/core/java/android/content/Intent.java
+++ b/core/java/android/content/Intent.java
@@ -2682,8 +2682,7 @@
             "android.intent.action.QUICK_CLOCK";
 
     /**
-     * Broadcast Action: This is broadcast when a user action should request the
-     * brightness setting dialog.
+     * Activity Action: Shows the brightness setting dialog.
      * @hide
      */
     public static final String ACTION_SHOW_BRIGHTNESS_DIALOG =
diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml
index b7210e1..641d77f 100644
--- a/packages/SystemUI/AndroidManifest.xml
+++ b/packages/SystemUI/AndroidManifest.xml
@@ -302,6 +302,19 @@
             </intent-filter>
         </activity>
 
+        <activity
+            android:name=".settings.BrightnessDialog"
+            android:label="@string/quick_settings_brightness_dialog_title"
+            android:theme="@android:style/Theme.DeviceDefault.Light.Dialog"
+            android:finishOnCloseSystemDialogs="true"
+            android:launchMode="singleInstance"
+            android:excludeFromRecents="true"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
 
         <!-- I dream of notifications -->
         <service
diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
index 630ba13..b3f90d7 100644
--- a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
+++ b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
@@ -46,8 +46,7 @@
             com.android.systemui.statusbar.SystemBars.class,
             com.android.systemui.usb.StorageNotification.class,
             com.android.systemui.power.PowerUI.class,
-            com.android.systemui.media.RingtonePlayer.class,
-            com.android.systemui.settings.SettingsUI.class,
+            com.android.systemui.media.RingtonePlayer.class
     };
 
     /**
diff --git a/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java b/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
index 6d5bb9d..108c8df 100644
--- a/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
+++ b/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
@@ -163,15 +163,17 @@
         if (mListening) {
             return;
         }
+
         mBrightnessObserver.startObserving();
         mUserTracker.startTracking();
 
-        // Update the slider and mode before attaching the listener so we don't receive the
-        // onChanged notifications for the initial values.
+        // Update the slider and mode before attaching the listener so we don't
+        // receive the onChanged notifications for the initial values.
         updateMode();
         updateSlider();
 
         mControl.setOnChangedListener(this);
+        mListening = true;
     }
 
     /** Unregister all call backs, both to and from the controller */
@@ -179,10 +181,11 @@
         if (!mListening) {
             return;
         }
+
         mBrightnessObserver.stopObserving();
-        mChangeCallbacks.clear();
         mUserTracker.stopTracking();
         mControl.setOnChangedListener(null);
+        mListening = false;
     }
 
     public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) {
diff --git a/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java b/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
index 65e1cc6..ad98168 100644
--- a/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
+++ b/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
@@ -16,6 +16,7 @@
 
 package com.android.systemui.settings;
 
+import android.app.Activity;
 import android.app.Dialog;
 import android.content.Context;
 import android.content.res.Resources;
@@ -30,76 +31,66 @@
 import com.android.systemui.R;
 
 /** A dialog that provides controls for adjusting the screen brightness. */
-public class BrightnessDialog extends Dialog implements
+public class BrightnessDialog extends Activity implements
         BrightnessController.BrightnessStateChangeCallback {
-
-    private static final String TAG = "BrightnessDialog";
-    private static final boolean DEBUG = false;
-
-    protected Handler mHandler = new Handler();
+    private final Handler mHandler = new Handler();
 
     private BrightnessController mBrightnessController;
-    private final int mBrightnessDialogLongTimeout;
-    private final int mBrightnessDialogShortTimeout;
+    private int mBrightnessDialogLongTimeout;
+    private int mBrightnessDialogShortTimeout;
 
     private final Runnable mDismissDialogRunnable = new Runnable() {
         public void run() {
-            if (BrightnessDialog.this.isShowing()) {
-                BrightnessDialog.this.dismiss();
-            }
+            finish();
         };
     };
 
-
-    public BrightnessDialog(Context ctx) {
-        super(ctx);
-        Resources r = ctx.getResources();
-        mBrightnessDialogLongTimeout =
-                r.getInteger(R.integer.quick_settings_brightness_dialog_long_timeout);
-        mBrightnessDialogShortTimeout =
-                r.getInteger(R.integer.quick_settings_brightness_dialog_short_timeout);
-    }
-
-
-    /**
-     * Create the brightness dialog and any resources that are used for the
-     * entire lifetime of the dialog.
-     */
     @Override
-    public void onCreate(Bundle savedInstanceState) {
+    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        Window window = getWindow();
-        window.setGravity(Gravity.TOP);
-        WindowManager.LayoutParams lp = window.getAttributes();
+
+        final Resources r = getResources();
+        mBrightnessDialogLongTimeout = r.getInteger(
+                R.integer.quick_settings_brightness_dialog_long_timeout);
+        mBrightnessDialogShortTimeout = r.getInteger(
+                R.integer.quick_settings_brightness_dialog_short_timeout);
+
+        final Window window = getWindow();
+        final WindowManager.LayoutParams lp = window.getAttributes();
+
         // Offset from the top
-        lp.y = getContext().getResources().getDimensionPixelOffset(R.dimen.volume_panel_top);
+        lp.y = getResources().getDimensionPixelOffset(R.dimen.volume_panel_top);
         lp.type = WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY;
-        lp.privateFlags |=
-                WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
+        lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
+
         window.setAttributes(lp);
+        window.setGravity(Gravity.TOP);
         window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
         window.requestFeature(Window.FEATURE_NO_TITLE);
 
         setContentView(R.layout.quick_settings_brightness_dialog);
-        setCanceledOnTouchOutside(true);
     }
 
-
     @Override
     protected void onStart() {
         super.onStart();
-        mBrightnessController = new BrightnessController(getContext(),
-                (ImageView) findViewById(R.id.brightness_icon),
-                (ToggleSlider) findViewById(R.id.brightness_slider));
+
+        final ImageView icon = (ImageView) findViewById(R.id.brightness_icon);
+        final ToggleSlider slider = (ToggleSlider) findViewById(R.id.brightness_slider);
+        mBrightnessController = new BrightnessController(this, icon, slider);
         mBrightnessController.registerCallbacks();
-        dismissBrightnessDialog(mBrightnessDialogLongTimeout);
         mBrightnessController.addStateChangedCallback(this);
+
+        dismissBrightnessDialog(mBrightnessDialogLongTimeout);
     }
 
     @Override
     protected void onStop() {
         super.onStop();
+
+        mBrightnessController.removeStateChangedCallback(this);
         mBrightnessController.unregisterCallbacks();
+
         removeAllBrightnessDialogCallbacks();
     }
 
@@ -109,6 +100,7 @@
 
     private void dismissBrightnessDialog(int timeout) {
         removeAllBrightnessDialogCallbacks();
+
         mHandler.postDelayed(mDismissDialogRunnable, timeout);
     }
 
@@ -118,11 +110,12 @@
 
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
-        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
-                keyCode == KeyEvent.KEYCODE_VOLUME_UP ||
-                keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
-            dismiss();
+        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
+                || keyCode == KeyEvent.KEYCODE_VOLUME_UP
+                || keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
+            finish();
         }
+
         return super.onKeyDown(keyCode, event);
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/settings/SettingsUI.java b/packages/SystemUI/src/com/android/systemui/settings/SettingsUI.java
deleted file mode 100644
index 8bc72c9..0000000
--- a/packages/SystemUI/src/com/android/systemui/settings/SettingsUI.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.systemui.settings;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.os.Handler;
-import android.os.UserHandle;
-import android.util.Log;
-
-import com.android.systemui.SystemUI;
-
-import java.io.FileDescriptor;
-import java.io.PrintWriter;
-
-public class SettingsUI extends SystemUI {
-    private static final String TAG = "SettingsUI";
-    private static final boolean DEBUG = false;
-
-    private final Handler mHandler = new Handler();
-    private BrightnessDialog mBrightnessDialog;
-
-    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            String action = intent.getAction();
-            if (action.equals(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG)) {
-                if (DEBUG) Log.d(TAG, "showing brightness dialog");
-
-                if (mBrightnessDialog == null) {
-                    mBrightnessDialog = new BrightnessDialog(mContext);
-                    mBrightnessDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
-                        @Override
-                        public void onDismiss(DialogInterface dialog) {
-                            mBrightnessDialog = null;
-                        }
-                    });
-                }
-
-                if (!mBrightnessDialog.isShowing()) {
-                    mBrightnessDialog.show();
-                }
-
-            } else {
-                Log.w(TAG, "unknown intent: " + intent);
-            }
-        }
-    };
-
-    public void start() {
-        IntentFilter filter = new IntentFilter();
-        filter.addAction(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG);
-        mContext.registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, null, mHandler);
-    }
-
-    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
-        pw.print("mBrightnessDialog=");
-        pw.println(mBrightnessDialog == null ? "null" : mBrightnessDialog.toString());
-    }
-}
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index ef15a80..c892a47 100644
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -2327,8 +2327,8 @@
                 Settings.System.putIntForUser(mContext.getContentResolver(),
                         Settings.System.SCREEN_BRIGHTNESS, brightness,
                         UserHandle.USER_CURRENT_OR_SELF);
-                Intent intent = new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG);
-                mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF);
+                mContext.startActivityAsUser(new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG),
+                        UserHandle.CURRENT_OR_SELF);
             }
             return -1;
         } else if (keyCode == KeyEvent.KEYCODE_META_LEFT) {