OSDN Git Service

Make WallpaperService watch the actual display state.
authorJeff Brown <jeffbrown@google.com>
Sat, 22 Nov 2014 03:01:13 +0000 (19:01 -0800)
committerJeff Brown <jeffbrown@google.com>
Sat, 22 Nov 2014 03:25:39 +0000 (03:25 +0000)
Bug: 18471411
Change-Id: Ie9d2e70e4e105dfbb2cb4d9726f632bcf2cc0a31

core/java/android/service/wallpaper/WallpaperService.java
core/java/com/android/internal/os/HandlerCaller.java

index 26e9a30..4621c43 100644 (file)
@@ -23,6 +23,7 @@ import android.util.DisplayMetrics;
 import android.util.TypedValue;
 import android.view.ViewRootImpl;
 import android.view.WindowInsets;
+
 import com.android.internal.R;
 import com.android.internal.os.HandlerCaller;
 import com.android.internal.view.BaseIWindow;
@@ -32,18 +33,17 @@ import android.annotation.SdkConstant;
 import android.annotation.SdkConstant.SdkConstantType;
 import android.app.Service;
 import android.app.WallpaperManager;
-import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
-import android.content.IntentFilter;
 import android.content.res.Configuration;
 import android.graphics.PixelFormat;
 import android.graphics.Rect;
+import android.hardware.display.DisplayManager;
+import android.hardware.display.DisplayManager.DisplayListener;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.os.Looper;
 import android.os.Message;
-import android.os.PowerManager;
 import android.os.RemoteException;
 import android.util.Log;
 import android.view.Display;
@@ -139,7 +139,6 @@ public abstract class WallpaperService extends Service {
         
         boolean mInitializing = true;
         boolean mVisible;
-        boolean mScreenOn = true;
         boolean mReportedVisible;
         boolean mDestroyed;
         
@@ -191,20 +190,10 @@ public abstract class WallpaperService extends Service {
         float mPendingYOffsetStep;
         boolean mPendingSync;
         MotionEvent mPendingMove;
-        
-        final BroadcastReceiver mReceiver = new BroadcastReceiver() {
-            @Override
-            public void onReceive(Context context, Intent intent) {
-                if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
-                    mScreenOn = true;
-                    reportVisibility();
-                } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
-                    mScreenOn = false;
-                    reportVisibility();
-                }
-            }
-        };
-        
+
+        DisplayManager mDisplayManager;
+        Display mDisplay;
+
         final BaseSurfaceHolder mSurfaceHolder = new BaseSurfaceHolder() {
             {
                 mRequestedFormat = PixelFormat.RGBX_8888;
@@ -536,8 +525,8 @@ public abstract class WallpaperService extends Service {
             out.print(prefix); out.print("mInitializing="); out.print(mInitializing);
                     out.print(" mDestroyed="); out.println(mDestroyed);
             out.print(prefix); out.print("mVisible="); out.print(mVisible);
-                    out.print(" mScreenOn="); out.print(mScreenOn);
                     out.print(" mReportedVisible="); out.println(mReportedVisible);
+            out.print(prefix); out.print("mDisplay="); out.println(mDisplay);
             out.print(prefix); out.print("mCreated="); out.print(mCreated);
                     out.print(" mSurfaceCreated="); out.print(mSurfaceCreated);
                     out.print(" mIsCreating="); out.print(mIsCreating);
@@ -875,13 +864,10 @@ public abstract class WallpaperService extends Service {
             
             mWindow.setSession(mSession);
 
-            mScreenOn = ((PowerManager)getSystemService(Context.POWER_SERVICE)).isScreenOn();
+            mDisplayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
+            mDisplayManager.registerDisplayListener(mDisplayListener, mCaller.getHandler());
+            mDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
 
-            IntentFilter filter = new IntentFilter();
-            filter.addAction(Intent.ACTION_SCREEN_ON);
-            filter.addAction(Intent.ACTION_SCREEN_OFF);
-            registerReceiver(mReceiver, filter);
-            
             if (DEBUG) Log.v(TAG, "onCreate(): " + this);
             onCreate(mSurfaceHolder);
             
@@ -920,7 +906,8 @@ public abstract class WallpaperService extends Service {
 
         void reportVisibility() {
             if (!mDestroyed) {
-                boolean visible = mVisible && mScreenOn;
+                boolean visible = mVisible
+                        & mDisplay != null && mDisplay.getState() != Display.STATE_OFF;
                 if (mReportedVisible != visible) {
                     mReportedVisible = visible;
                     if (DEBUG) Log.v(TAG, "onVisibilityChanged(" + visible
@@ -1023,7 +1010,11 @@ public abstract class WallpaperService extends Service {
             }
             
             mDestroyed = true;
-            
+
+            if (mDisplayManager != null) {
+                mDisplayManager.unregisterDisplayListener(mDisplayListener);
+            }
+
             if (mVisible) {
                 mVisible = false;
                 if (DEBUG) Log.v(TAG, "onVisibilityChanged(false): " + this);
@@ -1034,9 +1025,7 @@ public abstract class WallpaperService extends Service {
             
             if (DEBUG) Log.v(TAG, "onDestroy(): " + this);
             onDestroy();
-            
-            unregisterReceiver(mReceiver);
-            
+
             if (mCreated) {
                 try {
                     if (DEBUG) Log.v(TAG, "Removing window and destroying surface "
@@ -1061,8 +1050,25 @@ public abstract class WallpaperService extends Service {
                 }
             }
         }
+
+        private final DisplayListener mDisplayListener = new DisplayListener() {
+            @Override
+            public void onDisplayChanged(int displayId) {
+                if (mDisplay.getDisplayId() == displayId) {
+                    reportVisibility();
+                }
+            }
+
+            @Override
+            public void onDisplayRemoved(int displayId) {
+            }
+
+            @Override
+            public void onDisplayAdded(int displayId) {
+            }
+        };
     }
-    
+
     class IWallpaperEngineWrapper extends IWallpaperEngine.Stub
             implements HandlerCaller.Callback {
         private final HandlerCaller mCaller;
index 17685fd..99286cb 100644 (file)
@@ -49,6 +49,10 @@ public class HandlerCaller {
         mCallback = callback;
     }
 
+    public Handler getHandler() {
+        return mH;
+    }
+
     public void executeOrSendMessage(Message msg) {
         // If we are calling this from the main thread, then we can call
         // right through.  Otherwise, we need to send the message to the