Merge "Avoid caching the ViewConfiguration"
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index c0abbf6..4b61970 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -2242,7 +2242,7 @@
      */
     private final class PowerKeyRule extends SingleKeyGestureDetector.SingleKeyRule {
         PowerKeyRule(int gestures) {
-            super(mContext, KEYCODE_POWER, gestures);
+            super(KEYCODE_POWER, gestures);
         }
 
         @Override
@@ -2293,7 +2293,7 @@
      */
     private final class BackKeyRule extends SingleKeyGestureDetector.SingleKeyRule {
         BackKeyRule(int gestures) {
-            super(mContext, KEYCODE_BACK, gestures);
+            super(KEYCODE_BACK, gestures);
         }
 
         @Override
@@ -2317,7 +2317,7 @@
      */
     private final class StemPrimaryKeyRule extends SingleKeyGestureDetector.SingleKeyRule {
         StemPrimaryKeyRule(int gestures) {
-            super(mContext, KeyEvent.KEYCODE_STEM_PRIMARY, gestures);
+            super(KeyEvent.KEYCODE_STEM_PRIMARY, gestures);
         }
 
         @Override
@@ -2342,7 +2342,7 @@
     }
 
     private void initSingleKeyGestureRules() {
-        mSingleKeyGestureDetector = new SingleKeyGestureDetector();
+        mSingleKeyGestureDetector = SingleKeyGestureDetector.get(mContext);
 
         int powerKeyGestures = 0;
         if (hasVeryLongPressOnPowerBehavior()) {
diff --git a/services/core/java/com/android/server/policy/SingleKeyGestureDetector.java b/services/core/java/com/android/server/policy/SingleKeyGestureDetector.java
index 0276b37..7efa897 100644
--- a/services/core/java/com/android/server/policy/SingleKeyGestureDetector.java
+++ b/services/core/java/com/android/server/policy/SingleKeyGestureDetector.java
@@ -55,12 +55,15 @@
     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 @@
     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 @@
          *  press timeout.
          */
         long getLongPressTimeoutMs() {
-            return mDefaultLongPressTimeout;
+            return sDefaultLongPressTimeout;
         }
         /**
          *  Callback when long press has been detected.
@@ -157,7 +154,7 @@
          *  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 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();
     }
 
diff --git a/services/tests/wmtests/src/com/android/server/policy/SingleKeyGestureTests.java b/services/tests/wmtests/src/com/android/server/policy/SingleKeyGestureTests.java
index 00bc546..063f625 100644
--- a/services/tests/wmtests/src/com/android/server/policy/SingleKeyGestureTests.java
+++ b/services/tests/wmtests/src/com/android/server/policy/SingleKeyGestureTests.java
@@ -37,7 +37,6 @@
 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 @@
     @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 @@
             }
         });
 
-        mDetector.addRule(new SingleKeyGestureDetector.SingleKeyRule(mContext, KEYCODE_BACK, 0) {
+        mDetector.addRule(new SingleKeyGestureDetector.SingleKeyRule(KEYCODE_BACK, 0) {
             @Override
             int getMaxMultiPressCount() {
                 return mMaxMultiPressCount;