Add helper class FoldStateListener

Reduces the need for each code that needs to know about fold/unfold from
duplicating the exact same code.

Bug: 184795211
Test: They pass!
Change-Id: Ic05a2067c2ec64db7dab0227a7204283c1144a5f
This commit is contained in:
Wale Ogunwale
2021-05-03 09:07:07 -07:00
parent 3c865b129a
commit 28ebbbf0ab
2 changed files with 35 additions and 27 deletions

View File

@@ -24,7 +24,10 @@ import android.annotation.SystemService;
import android.annotation.TestApi;
import android.content.Context;
import com.android.internal.util.ArrayUtils;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
/**
* Manages the state of the system for devices with user-configurable hardware like a foldable
@@ -170,4 +173,34 @@ public final class DeviceStateManager {
*/
void onStateChanged(int state);
}
/**
* Listens to changes in device state and reports the state as folded if the device state
* matches the value in the {@link com.android.internal.R.integer.config_foldedDeviceState}
* resource.
* @hide
*/
public static class FoldStateListener implements DeviceStateCallback {
private final int[] mFoldedDeviceStates;
private final Consumer<Boolean> mDelegate;
@Nullable
private Boolean lastResult;
public FoldStateListener(Context context, Consumer<Boolean> listener) {
mFoldedDeviceStates = context.getResources().getIntArray(
com.android.internal.R.array.config_foldedDeviceStates);
mDelegate = listener;
}
@Override
public final void onStateChanged(int state) {
final boolean folded = ArrayUtils.contains(mFoldedDeviceStates, state);
if (lastResult == null || !lastResult.equals(folded)) {
lastResult = folded;
mDelegate.accept(folded);
}
}
}
}

View File

@@ -21,6 +21,7 @@ import android.content.Context;
import android.graphics.Rect;
import android.hardware.ICameraService;
import android.hardware.devicestate.DeviceStateManager;
import android.hardware.devicestate.DeviceStateManager.FoldStateListener;
import android.hardware.display.DisplayManagerInternal;
import android.os.Handler;
import android.os.HandlerExecutor;
@@ -75,7 +76,7 @@ class DisplayFoldController {
DeviceStateManager deviceStateManager = context.getSystemService(DeviceStateManager.class);
deviceStateManager.registerCallback(new HandlerExecutor(handler),
new DeviceStateListener(context));
new FoldStateListener(context, folded -> setDeviceFolded(folded)));
}
void finishedGoingToSleep() {
@@ -202,30 +203,4 @@ class DisplayFoldController {
return new DisplayFoldController(context, windowManagerService, displayService,
cameraServiceProxy, displayId, foldedArea, DisplayThread.getHandler());
}
/**
* Listens to changes in device state and reports the state as folded if the device state
* matches the value in the {@link com.android.internal.R.integer.config_foldedDeviceState}
* resource.
*/
private class DeviceStateListener implements DeviceStateManager.DeviceStateCallback {
private final int[] mFoldedDeviceStates;
DeviceStateListener(Context context) {
mFoldedDeviceStates = context.getResources().getIntArray(
com.android.internal.R.array.config_foldedDeviceStates);
}
@Override
public void onStateChanged(int deviceState) {
boolean folded = false;
for (int i = 0; i < mFoldedDeviceStates.length; i++) {
if (deviceState == mFoldedDeviceStates[i]) {
folded = true;
break;
}
}
setDeviceFolded(folded);
}
}
}