Replace rotation listener with display listener

pull/4469/head
Simon Chan 7 months ago
parent e90abc8c8a
commit f212b246aa
No known key found for this signature in database
GPG Key ID: A8B69F750B9BCEDD

@ -36,8 +36,8 @@ public final class Device {
public static final int LOCK_VIDEO_ORIENTATION_UNLOCKED = -1;
public static final int LOCK_VIDEO_ORIENTATION_INITIAL = -2;
public interface RotationListener {
void onRotationChanged(int rotation);
public interface DisplayChangeListener {
void onDisplayChanged();
}
public interface FoldListener {
@ -54,7 +54,7 @@ public final class Device {
private Size deviceSize;
private ScreenInfo screenInfo;
private RotationListener rotationListener;
private DisplayChangeListener displayChangeListener;
private FoldListener foldListener;
private ClipboardListener clipboardListener;
private final AtomicBoolean isSettingClipboard = new AtomicBoolean();
@ -114,25 +114,11 @@ public final class Device {
deviceSize = displayInfo.getSize();
screenInfo = ScreenInfo.computeScreenInfo(displayInfo.getRotation(), deviceSize, crop, maxSize, lockVideoOrientation);
if (foldListener != null) {
foldListener.onFoldChanged(displayId, false);
if (displayChangeListener != null) {
displayChangeListener.onDisplayChanged();
}
}
}, displayListenerHandler);
ServiceManager.getWindowManager().registerRotationWatcher(new IRotationWatcher.Stub() {
@Override
public void onRotationChanged(int rotation) {
synchronized (Device.this) {
screenInfo = screenInfo.withDeviceRotation(rotation);
// notify
if (rotationListener != null) {
rotationListener.onRotationChanged(rotation);
}
}
}
}, displayId);
}, displayListenerHandler, DisplayManager.EVENT_FLAG_DISPLAY_CHANGED);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ServiceManager.getWindowManager().registerDisplayFoldListener(new IDisplayFoldListener.Stub() {
@ -288,8 +274,8 @@ public final class Device {
return ServiceManager.getPowerManager().isScreenOn();
}
public synchronized void setRotationListener(RotationListener rotationListener) {
this.rotationListener = rotationListener;
public synchronized void setDisplayChangeListener(DisplayChangeListener displayChangeListener) {
this.displayChangeListener = displayChangeListener;
}
public synchronized void setFoldListener(FoldListener foldlistener) {

@ -7,7 +7,7 @@ import android.os.Build;
import android.os.IBinder;
import android.view.Surface;
public class ScreenCapture extends SurfaceCapture implements Device.RotationListener, Device.FoldListener {
public class ScreenCapture extends SurfaceCapture implements Device.DisplayChangeListener, Device.FoldListener {
private final Device device;
private IBinder display;
@ -18,7 +18,7 @@ public class ScreenCapture extends SurfaceCapture implements Device.RotationList
@Override
public void init() {
device.setRotationListener(this);
device.setDisplayChangeListener(this);
device.setFoldListener(this);
}
@ -41,7 +41,7 @@ public class ScreenCapture extends SurfaceCapture implements Device.RotationList
@Override
public void release() {
device.setRotationListener(null);
device.setDisplayChangeListener(null);
device.setFoldListener(null);
if (display != null) {
SurfaceControl.destroyDisplay(display);
@ -65,7 +65,7 @@ public class ScreenCapture extends SurfaceCapture implements Device.RotationList
}
@Override
public void onRotationChanged(int rotation) {
public void onDisplayChanged() {
requestReset();
}

@ -14,6 +14,13 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class DisplayManager {
/**
* Event type for when a display is changed.
*
* @see #registerDisplayListener(DisplayListener, Handler, long)
*/
public static final long EVENT_FLAG_DISPLAY_CHANGED = 1L << 2;
private final Object manager; // instance of hidden class android.hardware.display.DisplayManagerGlobal
public DisplayManager(Object manager) {
@ -97,7 +104,7 @@ public final class DisplayManager {
}
}
public void registerDisplayListener(DisplayListener listener, Handler handler) {
public void registerDisplayListener(DisplayListener listener, Handler handler, long eventMask) {
try {
Class<?> displayListenerClass = Class.forName("android.hardware.display.DisplayManager$DisplayListener");
Object displayListenerProxy = Proxy.newProxyInstance(
@ -119,12 +126,20 @@ public final class DisplayManager {
}
return null;
});
manager
.getClass()
.getMethod("registerDisplayListener", displayListenerClass, Handler.class)
.invoke(manager, displayListenerProxy, handler);
try {
manager
.getClass()
.getMethod("registerDisplayListener", displayListenerClass, Handler.class, long.class)
.invoke(manager, displayListenerProxy, handler, eventMask);
} catch (NoSuchMethodException e) {
manager
.getClass()
.getMethod("registerDisplayListener", displayListenerClass, Handler.class)
.invoke(manager, displayListenerProxy, handler);
}
} catch (Exception e) {
throw new AssertionError(e);
// Rotation and screen size won't be updated, not a fatal error
Ln.w("Could not register display listener", e);
}
}

@ -95,22 +95,6 @@ public final class WindowManager {
}
}
public void registerRotationWatcher(IRotationWatcher rotationWatcher, int displayId) {
try {
Class<?> cls = manager.getClass();
try {
// display parameter added since this commit:
// https://android.googlesource.com/platform/frameworks/base/+/35fa3c26adcb5f6577849fd0df5228b1f67cf2c6%5E%21/#F1
cls.getMethod("watchRotation", IRotationWatcher.class, int.class).invoke(manager, rotationWatcher, displayId);
} catch (NoSuchMethodException e) {
// old version
cls.getMethod("watchRotation", IRotationWatcher.class).invoke(manager, rotationWatcher);
}
} catch (Exception e) {
Ln.e("Could not register rotation watcher", e);
}
}
@TargetApi(29)
public void registerDisplayFoldListener(IDisplayFoldListener foldListener) {
try {

Loading…
Cancel
Save