Make WallpaperService watch the actual display state.

Bug: 18471411
Change-Id: Ie9d2e70e4e105dfbb2cb4d9726f632bcf2cc0a31
This commit is contained in:
Jeff Brown
2014-11-21 19:01:13 -08:00
parent fbdfdef43d
commit 3d110b2391
2 changed files with 41 additions and 31 deletions

View File

@@ -23,6 +23,7 @@ import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.ViewRootImpl; import android.view.ViewRootImpl;
import android.view.WindowInsets; import android.view.WindowInsets;
import com.android.internal.R; import com.android.internal.R;
import com.android.internal.os.HandlerCaller; import com.android.internal.os.HandlerCaller;
import com.android.internal.view.BaseIWindow; import com.android.internal.view.BaseIWindow;
@@ -32,18 +33,17 @@ import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SdkConstant.SdkConstantType;
import android.app.Service; import android.app.Service;
import android.app.WallpaperManager; import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.graphics.Rect; import android.graphics.Rect;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager.DisplayListener;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.os.PowerManager;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.Log; import android.util.Log;
import android.view.Display; import android.view.Display;
@@ -139,7 +139,6 @@ public abstract class WallpaperService extends Service {
boolean mInitializing = true; boolean mInitializing = true;
boolean mVisible; boolean mVisible;
boolean mScreenOn = true;
boolean mReportedVisible; boolean mReportedVisible;
boolean mDestroyed; boolean mDestroyed;
@@ -192,18 +191,8 @@ public abstract class WallpaperService extends Service {
boolean mPendingSync; boolean mPendingSync;
MotionEvent mPendingMove; MotionEvent mPendingMove;
final BroadcastReceiver mReceiver = new BroadcastReceiver() { DisplayManager mDisplayManager;
@Override Display mDisplay;
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();
}
}
};
final BaseSurfaceHolder mSurfaceHolder = new BaseSurfaceHolder() { final BaseSurfaceHolder mSurfaceHolder = new BaseSurfaceHolder() {
{ {
@@ -536,8 +525,8 @@ public abstract class WallpaperService extends Service {
out.print(prefix); out.print("mInitializing="); out.print(mInitializing); out.print(prefix); out.print("mInitializing="); out.print(mInitializing);
out.print(" mDestroyed="); out.println(mDestroyed); out.print(" mDestroyed="); out.println(mDestroyed);
out.print(prefix); out.print("mVisible="); out.print(mVisible); out.print(prefix); out.print("mVisible="); out.print(mVisible);
out.print(" mScreenOn="); out.print(mScreenOn);
out.print(" mReportedVisible="); out.println(mReportedVisible); 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(prefix); out.print("mCreated="); out.print(mCreated);
out.print(" mSurfaceCreated="); out.print(mSurfaceCreated); out.print(" mSurfaceCreated="); out.print(mSurfaceCreated);
out.print(" mIsCreating="); out.print(mIsCreating); out.print(" mIsCreating="); out.print(mIsCreating);
@@ -875,12 +864,9 @@ public abstract class WallpaperService extends Service {
mWindow.setSession(mSession); mWindow.setSession(mSession);
mScreenOn = ((PowerManager)getSystemService(Context.POWER_SERVICE)).isScreenOn(); mDisplayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
mDisplayManager.registerDisplayListener(mDisplayListener, mCaller.getHandler());
IntentFilter filter = new IntentFilter(); mDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mReceiver, filter);
if (DEBUG) Log.v(TAG, "onCreate(): " + this); if (DEBUG) Log.v(TAG, "onCreate(): " + this);
onCreate(mSurfaceHolder); onCreate(mSurfaceHolder);
@@ -920,7 +906,8 @@ public abstract class WallpaperService extends Service {
void reportVisibility() { void reportVisibility() {
if (!mDestroyed) { if (!mDestroyed) {
boolean visible = mVisible && mScreenOn; boolean visible = mVisible
& mDisplay != null && mDisplay.getState() != Display.STATE_OFF;
if (mReportedVisible != visible) { if (mReportedVisible != visible) {
mReportedVisible = visible; mReportedVisible = visible;
if (DEBUG) Log.v(TAG, "onVisibilityChanged(" + visible if (DEBUG) Log.v(TAG, "onVisibilityChanged(" + visible
@@ -1024,6 +1011,10 @@ public abstract class WallpaperService extends Service {
mDestroyed = true; mDestroyed = true;
if (mDisplayManager != null) {
mDisplayManager.unregisterDisplayListener(mDisplayListener);
}
if (mVisible) { if (mVisible) {
mVisible = false; mVisible = false;
if (DEBUG) Log.v(TAG, "onVisibilityChanged(false): " + this); if (DEBUG) Log.v(TAG, "onVisibilityChanged(false): " + this);
@@ -1035,8 +1026,6 @@ public abstract class WallpaperService extends Service {
if (DEBUG) Log.v(TAG, "onDestroy(): " + this); if (DEBUG) Log.v(TAG, "onDestroy(): " + this);
onDestroy(); onDestroy();
unregisterReceiver(mReceiver);
if (mCreated) { if (mCreated) {
try { try {
if (DEBUG) Log.v(TAG, "Removing window and destroying surface " if (DEBUG) Log.v(TAG, "Removing window and destroying surface "
@@ -1061,6 +1050,23 @@ 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 class IWallpaperEngineWrapper extends IWallpaperEngine.Stub

View File

@@ -49,6 +49,10 @@ public class HandlerCaller {
mCallback = callback; mCallback = callback;
} }
public Handler getHandler() {
return mH;
}
public void executeOrSendMessage(Message msg) { public void executeOrSendMessage(Message msg) {
// If we are calling this from the main thread, then we can call // If we are calling this from the main thread, then we can call
// right through. Otherwise, we need to send the message to the // right through. Otherwise, we need to send the message to the