Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c80b0a8f authored by Wale Ogunwale's avatar Wale Ogunwale Committed by Android (Google) Code Review
Browse files

Merge "Add helper class FoldStateListener" into sc-dev

parents bd8f2158 28ebbbf0
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -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);
            }
        }
    }
}
+2 −27
Original line number Diff line number Diff line
@@ -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);
        }
    }
}