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

Commit 0a17bd30 authored by Riddle Hsu's avatar Riddle Hsu Committed by Automerger Merge Worker
Browse files

Merge "Toggle orientation sensor outside of WM lock" into tm-dev am: 89e92e9b am: 111bca5c

parents 960fd2e5 111bca5c
Loading
Loading
Loading
Loading
+23 −40
Original line number Original line Diff line number Diff line
@@ -56,7 +56,6 @@ import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.UserHandle;
import android.provider.Settings;
import android.provider.Settings;
import android.util.Slog;
import android.util.Slog;
import android.util.SparseArray;
import android.util.TimeUtils;
import android.util.TimeUtils;
import android.util.proto.ProtoOutputStream;
import android.util.proto.ProtoOutputStream;
import android.view.IDisplayWindowRotationCallback;
import android.view.IDisplayWindowRotationCallback;
@@ -1024,13 +1023,7 @@ public class DisplayRotation {
                disable = false;
                disable = false;
                // Enable listener if not already enabled.
                // Enable listener if not already enabled.
                if (!mOrientationListener.mEnabled) {
                if (!mOrientationListener.mEnabled) {
                    // Don't clear the current sensor orientation if the keyguard is going away in
                    mOrientationListener.enable();
                    // dismiss mode. This allows window manager to use the last sensor reading to
                    // determine the orientation vs. falling back to the last known orientation if
                    // the sensor reading was cleared which can cause it to relaunch the app that
                    // will show in the wrong orientation first before correcting leading to app
                    // launch delays.
                    mOrientationListener.enable(true /* clearCurrentRotation */);
                }
                }
            }
            }
        }
        }
@@ -1570,35 +1563,13 @@ public class DisplayRotation {
        proto.end(token);
        proto.end(token);
    }
    }


    private class OrientationListener extends WindowOrientationListener {
    private class OrientationListener extends WindowOrientationListener implements Runnable {
        final SparseArray<Runnable> mRunnableCache = new SparseArray<>(5);
        transient boolean mEnabled;
        boolean mEnabled;


        OrientationListener(Context context, Handler handler) {
        OrientationListener(Context context, Handler handler) {
            super(context, handler);
            super(context, handler);
        }
        }


        private class UpdateRunnable implements Runnable {
            final int mRotation;

            UpdateRunnable(int rotation) {
                mRotation = rotation;
            }

            @Override
            public void run() {
                // Send interaction power boost to improve redraw performance.
                mService.mPowerManagerInternal.setPowerBoost(Boost.INTERACTION, 0);
                if (isRotationChoicePossible(mCurrentAppOrientation)) {
                    final boolean isValid = isValidRotationChoice(mRotation);
                    sendProposedRotationChangeToStatusBarInternal(mRotation, isValid);
                } else {
                    mService.updateRotation(false /* alwaysSendConfiguration */,
                            false /* forceRelayout */);
                }
            }
        }

        @Override
        @Override
        public boolean isKeyguardLocked() {
        public boolean isKeyguardLocked() {
            return mService.isKeyguardLocked();
            return mService.isKeyguardLocked();
@@ -1615,27 +1586,39 @@ public class DisplayRotation {
        @Override
        @Override
        public void onProposedRotationChanged(int rotation) {
        public void onProposedRotationChanged(int rotation) {
            ProtoLog.v(WM_DEBUG_ORIENTATION, "onProposedRotationChanged, rotation=%d", rotation);
            ProtoLog.v(WM_DEBUG_ORIENTATION, "onProposedRotationChanged, rotation=%d", rotation);
            Runnable r = mRunnableCache.get(rotation, null);
            // Send interaction power boost to improve redraw performance.
            if (r == null) {
            mService.mPowerManagerInternal.setPowerBoost(Boost.INTERACTION, 0);
                r = new UpdateRunnable(rotation);
            if (isRotationChoicePossible(mCurrentAppOrientation)) {
                mRunnableCache.put(rotation, r);
                final boolean isValid = isValidRotationChoice(rotation);
                sendProposedRotationChangeToStatusBarInternal(rotation, isValid);
            } else {
                mService.updateRotation(false /* alwaysSendConfiguration */,
                        false /* forceRelayout */);
            }
            }
            getHandler().post(r);
        }
        }


        @Override
        @Override
        public void enable(boolean clearCurrentRotation) {
        public void enable() {
            super.enable(clearCurrentRotation);
            mEnabled = true;
            mEnabled = true;
            getHandler().post(this);
            ProtoLog.v(WM_DEBUG_ORIENTATION, "Enabling listeners");
            ProtoLog.v(WM_DEBUG_ORIENTATION, "Enabling listeners");
        }
        }


        @Override
        @Override
        public void disable() {
        public void disable() {
            super.disable();
            mEnabled = false;
            mEnabled = false;
            getHandler().post(this);
            ProtoLog.v(WM_DEBUG_ORIENTATION, "Disabling listeners");
            ProtoLog.v(WM_DEBUG_ORIENTATION, "Disabling listeners");
        }
        }

        @Override
        public void run() {
            if (mEnabled) {
                super.enable();
            } else {
                super.disable();
            }
        }
    }
    }


    private class SettingsObserver extends ContentObserver {
    private class SettingsObserver extends ContentObserver {
+7 −2
Original line number Original line Diff line number Diff line
@@ -320,6 +320,7 @@ public class DisplayRotationTests {
    private void verifyOrientationListenerRegistration(int numOfInvocation) {
    private void verifyOrientationListenerRegistration(int numOfInvocation) {
        final ArgumentCaptor<SensorEventListener> listenerCaptor = ArgumentCaptor.forClass(
        final ArgumentCaptor<SensorEventListener> listenerCaptor = ArgumentCaptor.forClass(
                SensorEventListener.class);
                SensorEventListener.class);
        waitForUiHandler();
        verify(mMockSensorManager, times(numOfInvocation)).registerListener(
        verify(mMockSensorManager, times(numOfInvocation)).registerListener(
                listenerCaptor.capture(),
                listenerCaptor.capture(),
                same(mFakeSensor),
                same(mFakeSensor),
@@ -480,10 +481,14 @@ public class DisplayRotationTests {
                SCREEN_ORIENTATION_UNSPECIFIED, Surface.ROTATION_0));
                SCREEN_ORIENTATION_UNSPECIFIED, Surface.ROTATION_0));
    }
    }


    private boolean waitForUiHandler() throws Exception {
    private boolean waitForUiHandler() {
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch = new CountDownLatch(1);
        UiThread.getHandler().post(latch::countDown);
        UiThread.getHandler().post(latch::countDown);
        try {
            return latch.await(UI_HANDLER_WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
            return latch.await(UI_HANDLER_WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ignored) {
        }
        throw new AssertionError("Failed to wait for ui handler");
    }
    }


    @Test
    @Test