Connect my plumbing to dsandler's awesome lights out mode.

It took a little bit of refactoring to move the authoritative state
about whether the lights are on or not into the StatusBarManagerService,
so that if the system ui process crashes, the bar comes up in the
right mode.

Change-Id: I95cfaf8f78ca4443ded5262272ea755d44dc5d17
diff --git a/core/java/com/android/internal/statusbar/IStatusBarService.aidl b/core/java/com/android/internal/statusbar/IStatusBarService.aidl
index 07cc43e..0763f5e 100644
--- a/core/java/com/android/internal/statusbar/IStatusBarService.aidl
+++ b/core/java/com/android/internal/statusbar/IStatusBarService.aidl
@@ -43,4 +43,5 @@
             int uid, int initialPid, String message);
     void onClearAllNotifications();
     void onNotificationClear(String pkg, String tag, int id);
+    void setLightsOn(boolean on);
 }
diff --git a/packages/SystemUI/res/layout-xlarge/status_bar.xml b/packages/SystemUI/res/layout-xlarge/status_bar.xml
index f4040d91..e0b34cc 100644
--- a/packages/SystemUI/res/layout-xlarge/status_bar.xml
+++ b/packages/SystemUI/res/layout-xlarge/status_bar.xml
@@ -159,7 +159,6 @@
         android:layout_height="match_parent"
         android:visibility="invisible"
         android:clickable="true"
-        android:onClick="toggleLightsOut"
         />
 </FrameLayout>
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBarService.java b/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBarService.java
index b33af99..5ba1fab 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBarService.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBarService.java
@@ -150,15 +150,12 @@
         mBarContents = sb.findViewById(R.id.bar_contents);
         mCurtains = sb.findViewById(R.id.lights_out);
         View systemInfo = sb.findViewById(R.id.systemInfo);
-        View.OnLongClickListener toggle = new View.OnLongClickListener() {
-            public boolean onLongClick(View v) {
-                toggleLightsOut(v);
-                return true;
-            }
-        };
-        
-        systemInfo.setOnLongClickListener(toggle);
-        mCurtains.setOnLongClickListener(toggle);
+
+        systemInfo.setOnLongClickListener(new SetLightsOnListener(false));
+
+        SetLightsOnListener on = new SetLightsOnListener(true);
+        mCurtains.setOnClickListener(on);
+        mCurtains.setOnLongClickListener(on);
 
         // the more notifications icon
         mNotificationIconArea = (NotificationIconArea)sb.findViewById(R.id.notificationIcons);
@@ -492,11 +489,22 @@
     }
 
     public void setLightsOn(boolean on) {
-        //Slog.d(TAG, "setLightsOn on=" + on);
-        if (!on) {
+        if (on) {
+            mCurtains.setAnimation(AnimationUtils.loadAnimation((Context)this,
+                        R.anim.lights_out_out));
+            mCurtains.setVisibility(View.GONE);
+            mBarContents.setAnimation(AnimationUtils.loadAnimation((Context)this,
+                        R.anim.status_bar_in));
+            mBarContents.setVisibility(View.VISIBLE);
+        } else {
             animateCollapse();
+            mCurtains.setAnimation(AnimationUtils.loadAnimation((Context)this,
+                        R.anim.lights_out_in));
+            mCurtains.setVisibility(View.VISIBLE);
+            mBarContents.setAnimation(AnimationUtils.loadAnimation((Context)this,
+                        R.anim.status_bar_out));
+            mBarContents.setVisibility(View.GONE);
         }
-        // TODO: implement lights out mode
     }
 
     public void notificationIconsClicked(View v) {
@@ -738,26 +746,31 @@
         return true;
     }
 
-    protected void setLightsOut(boolean out) {
-        if (out) {
-            mCurtains.setAnimation(AnimationUtils.loadAnimation((Context)this,
-                        R.anim.lights_out_in));
-            mCurtains.setVisibility(View.VISIBLE);
-            mBarContents.setAnimation(AnimationUtils.loadAnimation((Context)this,
-                        R.anim.status_bar_out));
-            mBarContents.setVisibility(View.GONE);
-        } else {
-            mCurtains.setAnimation(AnimationUtils.loadAnimation((Context)this,
-                        R.anim.lights_out_out));
-            mCurtains.setVisibility(View.GONE);
-            mBarContents.setAnimation(AnimationUtils.loadAnimation((Context)this,
-                        R.anim.status_bar_in));
-            mBarContents.setVisibility(View.VISIBLE);
-        }
-    }
+    public class SetLightsOnListener implements View.OnLongClickListener,
+           View.OnClickListener {
+        private boolean mOn;
 
-    public void toggleLightsOut(View v) {
-        setLightsOut(mCurtains.getVisibility() != View.VISIBLE);
+        SetLightsOnListener(boolean on) {
+            mOn = on;
+        }
+
+        public void onClick(View v) {
+            try {
+                mBarService.setLightsOn(mOn);
+            } catch (RemoteException ex) {
+                // system process
+            }
+        }
+
+        public boolean onLongClick(View v) {
+            try {
+                mBarService.setLightsOn(mOn);
+            } catch (RemoteException ex) {
+                // system process
+            }
+            return true;
+        }
+
     }
 }
 
diff --git a/services/java/com/android/server/StatusBarManagerService.java b/services/java/com/android/server/StatusBarManagerService.java
index dc86eeb..b1baec5 100644
--- a/services/java/com/android/server/StatusBarManagerService.java
+++ b/services/java/com/android/server/StatusBarManagerService.java
@@ -246,27 +246,53 @@
         }
     }
 
+    /**
+     * This is used for the automatic version of lights-out mode.  Only call this from
+     * the window manager.
+     *
+     * @see setLightsOn(boolean)
+     */
     public void setActiveWindowIsFullscreen(boolean fullscreen) {
         // We could get away with a separate permission here, but STATUS_BAR is
         // signatureOrSystem which is probably good enough.  There is no public API
         // for this, so the question is a security issue, not an API compatibility issue.
         enforceStatusBar();
 
-        final boolean lightsOn = !fullscreen;
         synchronized (mLock) {
-            if (mLightsOn != lightsOn) {
-                mLightsOn = lightsOn;
-                mHandler.post(new Runnable() {
-                        public void run() {
-                            if (mBar != null) {
-                                try {
-                                    mBar.setLightsOn(lightsOn);
-                                } catch (RemoteException ex) {
-                                }
+            updateLightsOnLocked(!fullscreen);
+        }
+    }
+
+    /**
+     * This is used for the user-controlled version of lights-out mode.  Only call this from
+     * the status bar itself.
+     *
+     * We have two different functions here, because I think we're going to want to
+     * tweak the behavior when the user keeps turning lights-out mode off and the
+     * app keeps trying to turn it on.  For now they can just fight it out.  Having
+     * these two separte inputs will allow us to keep that change local to here.  --joeo
+     */
+    public void setLightsOn(boolean lightsOn) {
+        enforceStatusBarService();
+
+        synchronized (mLock) {
+            updateLightsOnLocked(lightsOn);
+        }
+    }
+
+    private void updateLightsOnLocked(final boolean lightsOn) {
+        if (mLightsOn != lightsOn) {
+            mLightsOn = lightsOn;
+            mHandler.post(new Runnable() {
+                    public void run() {
+                        if (mBar != null) {
+                            try {
+                                mBar.setLightsOn(lightsOn);
+                            } catch (RemoteException ex) {
                             }
                         }
-                    });
-            }
+                    }
+                });
         }
     }