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

Commit 68da5001 authored by Arthur Hung's avatar Arthur Hung
Browse files

Avoid caching the ViewConfiguration

Get the default timeout value directly without calling
`ViewConfiguration.get` that may generate some unexpected cache.

Bug: 211040066
Test: atest SingleKeyGestureTests
Change-Id: Ia0bcdf6f279b35dde60147999749052a18edab53
parent b937cc5e
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -2219,7 +2219,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
     */
    private final class PowerKeyRule extends SingleKeyGestureDetector.SingleKeyRule {
        PowerKeyRule(int gestures) {
            super(mContext, KEYCODE_POWER, gestures);
            super(KEYCODE_POWER, gestures);
        }

        @Override
@@ -2270,7 +2270,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
     */
    private final class BackKeyRule extends SingleKeyGestureDetector.SingleKeyRule {
        BackKeyRule(int gestures) {
            super(mContext, KEYCODE_BACK, gestures);
            super(KEYCODE_BACK, gestures);
        }

        @Override
@@ -2294,7 +2294,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
     */
    private final class StemPrimaryKeyRule extends SingleKeyGestureDetector.SingleKeyRule {
        StemPrimaryKeyRule(int gestures) {
            super(mContext, KeyEvent.KEYCODE_STEM_PRIMARY, gestures);
            super(KeyEvent.KEYCODE_STEM_PRIMARY, gestures);
        }

        @Override
@@ -2319,7 +2319,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    }

    private void initSingleKeyGestureRules() {
        mSingleKeyGestureDetector = new SingleKeyGestureDetector();
        mSingleKeyGestureDetector = SingleKeyGestureDetector.get(mContext);

        int powerKeyGestures = 0;
        if (hasVeryLongPressOnPowerBehavior()) {
+17 −11
Original line number Diff line number Diff line
@@ -55,12 +55,15 @@ public final class SingleKeyGestureDetector {
    private volatile boolean mHandledByLongPress = false;
    private final Handler mHandler;
    private long mLastDownTime = 0;
    private static final long MULTI_PRESS_TIMEOUT = ViewConfiguration.getMultiPressTimeout();

    /** Supported gesture flags */
    public static final int KEY_LONGPRESS = 1 << 1;
    public static final int KEY_VERYLONGPRESS = 1 << 2;

    static final long MULTI_PRESS_TIMEOUT = ViewConfiguration.getMultiPressTimeout();
    static long sDefaultLongPressTimeout;
    static long sDefaultVeryLongPressTimeout;

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(prefix = { "KEY_" }, value = {
@@ -86,16 +89,10 @@ public final class SingleKeyGestureDetector {
    abstract static class SingleKeyRule {
        private final int mKeyCode;
        private final int mSupportedGestures;
        private final long mDefaultLongPressTimeout;
        private final long mDefaultVeryLongPressTimeout;

        SingleKeyRule(Context context, int keyCode, @KeyGestureFlag int supportedGestures) {
        SingleKeyRule(int keyCode, @KeyGestureFlag int supportedGestures) {
            mKeyCode = keyCode;
            mSupportedGestures = supportedGestures;
            mDefaultLongPressTimeout =
                ViewConfiguration.get(context).getDeviceGlobalActionKeyTimeout();
            mDefaultVeryLongPressTimeout = context.getResources().getInteger(
                com.android.internal.R.integer.config_veryLongPressTimeout);
        }

        /**
@@ -145,7 +142,7 @@ public final class SingleKeyGestureDetector {
         *  press timeout.
         */
        long getLongPressTimeoutMs() {
            return mDefaultLongPressTimeout;
            return sDefaultLongPressTimeout;
        }
        /**
         *  Callback when long press has been detected.
@@ -157,7 +154,7 @@ public final class SingleKeyGestureDetector {
         *  If long press is supported, this should always be longer than the long press timeout.
         */
        long getVeryLongPressTimeoutMs() {
            return mDefaultVeryLongPressTimeout;
            return sDefaultVeryLongPressTimeout;
        }
        /**
         *  Callback when very long press has been detected.
@@ -173,7 +170,16 @@ public final class SingleKeyGestureDetector {
        }
    }

    public SingleKeyGestureDetector() {
    static SingleKeyGestureDetector get(Context context) {
        SingleKeyGestureDetector detector = new SingleKeyGestureDetector();
        sDefaultLongPressTimeout = context.getResources().getInteger(
                com.android.internal.R.integer.config_globalActionsKeyTimeout);
        sDefaultVeryLongPressTimeout = context.getResources().getInteger(
                com.android.internal.R.integer.config_veryLongPressTimeout);
        return detector;
    }

    private SingleKeyGestureDetector() {
        mHandler = new KeyHandler();
    }

+6 −8
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@ import android.os.HandlerThread;
import android.os.Process;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.ViewConfiguration;

import org.junit.Before;
import org.junit.Test;
@@ -75,18 +74,17 @@ public class SingleKeyGestureTests {
    @Before
    public void setUp() {
        mInstrumentation.runOnMainSync(() -> {
            mDetector = new SingleKeyGestureDetector();
            mDetector = SingleKeyGestureDetector.get(mContext);
            initSingleKeyGestureRules();
        });

        mWaitTimeout = ViewConfiguration.getMultiPressTimeout() + 50;
        mLongPressTime = ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout() + 50;
        mVeryLongPressTime = mContext.getResources().getInteger(
                com.android.internal.R.integer.config_veryLongPressTimeout) + 50;
        mWaitTimeout = SingleKeyGestureDetector.MULTI_PRESS_TIMEOUT + 50;
        mLongPressTime = SingleKeyGestureDetector.sDefaultLongPressTimeout + 50;
        mVeryLongPressTime = SingleKeyGestureDetector.sDefaultVeryLongPressTimeout + 50;
    }

    private void initSingleKeyGestureRules() {
        mDetector.addRule(new SingleKeyGestureDetector.SingleKeyRule(mContext, KEYCODE_POWER,
        mDetector.addRule(new SingleKeyGestureDetector.SingleKeyRule(KEYCODE_POWER,
                KEY_LONGPRESS | KEY_VERYLONGPRESS) {
            @Override
            int getMaxMultiPressCount() {
@@ -124,7 +122,7 @@ public class SingleKeyGestureTests {
            }
        });

        mDetector.addRule(new SingleKeyGestureDetector.SingleKeyRule(mContext, KEYCODE_BACK, 0) {
        mDetector.addRule(new SingleKeyGestureDetector.SingleKeyRule(KEYCODE_BACK, 0) {
            @Override
            int getMaxMultiPressCount() {
                return mMaxMultiPressCount;