From eabdee3014b66573066be876c9143ab24d56f0bb Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Tue, 29 Mar 2022 17:30:09 -0700 Subject: [PATCH] Store VelocityTracker strategy in ViewConfiguration To avoid excessive binder calls from the UI thread while processing input events, let's store the VelocityTracker strategy in ViewConfiguration. This also solves the problem of some apps not being able to read device config - now, this config will be read by system_server and will be available to anyone. Bug: 226190749 Test: atest BinderTests#testSwitchToOverview Change-Id: I0dac85d1bb3cee56cc5da4a3cbd69eba8919cbbd --- .../android/hardware/input/IInputManager.aidl | 2 ++ .../android/hardware/input/InputManager.java | 15 +++++++++++ core/java/android/view/VelocityTracker.java | 27 +++---------------- core/java/android/view/ViewConfiguration.java | 13 +++++++++ .../server/input/InputManagerService.java | 12 +++++++++ 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/core/java/android/hardware/input/IInputManager.aidl b/core/java/android/hardware/input/IInputManager.aidl index e1ffd4a6761df..bb8af8fed9625 100644 --- a/core/java/android/hardware/input/IInputManager.aidl +++ b/core/java/android/hardware/input/IInputManager.aidl @@ -38,6 +38,8 @@ import android.view.VerifiedInputEvent; /** @hide */ interface IInputManager { + // Gets the current VelocityTracker strategy + String getVelocityTrackerStrategy(); // Gets input device information. InputDevice getInputDevice(int deviceId); int[] getInputDeviceIds(); diff --git a/core/java/android/hardware/input/InputManager.java b/core/java/android/hardware/input/InputManager.java index 3d204ea7962f1..a9b1df99628e1 100644 --- a/core/java/android/hardware/input/InputManager.java +++ b/core/java/android/hardware/input/InputManager.java @@ -272,8 +272,15 @@ public final class InputManager { */ public static final int SWITCH_STATE_ON = 1; + private static String sVelocityTrackerStrategy; + private InputManager(IInputManager im) { mIm = im; + try { + sVelocityTrackerStrategy = mIm.getVelocityTrackerStrategy(); + } catch (RemoteException ex) { + Log.w(TAG, "Could not get VelocityTracker strategy: " + ex); + } } /** @@ -325,6 +332,14 @@ public final class InputManager { } } + /** + * Get the current VelocityTracker strategy. Only works when the system has fully booted up. + * @hide + */ + public String getVelocityTrackerStrategy() { + return sVelocityTrackerStrategy; + } + /** * Gets information about the input device with the specified id. * @param id The device id. diff --git a/core/java/android/view/VelocityTracker.java b/core/java/android/view/VelocityTracker.java index 73322dddfff09..f838d7fed7cf5 100644 --- a/core/java/android/view/VelocityTracker.java +++ b/core/java/android/view/VelocityTracker.java @@ -16,17 +16,10 @@ package android.view; -import static android.Manifest.permission.READ_DEVICE_CONFIG; -import static android.content.pm.PackageManager.PERMISSION_GRANTED; -import static android.provider.DeviceConfig.NAMESPACE_INPUT_NATIVE_BOOT; - import android.annotation.IntDef; -import android.annotation.Nullable; +import android.app.ActivityThread; import android.compat.annotation.UnsupportedAppUsage; import android.os.Build; -import android.os.Process; -import android.permission.PermissionManager; -import android.provider.DeviceConfig; import android.util.ArrayMap; import android.util.Pools.SynchronizedPool; @@ -178,9 +171,6 @@ public final class VelocityTracker { }) public @interface VelocityTrackerStrategy {} - // Feature flag name for the strategy to be used in VelocityTracker - private static final String VELOCITYTRACKER_STRATEGY = "velocitytracker_strategy"; - private long mPtr; @VelocityTrackerStrategy private final int mStrategy; @@ -285,22 +275,13 @@ public final class VelocityTracker { return mStrategy; } - @Nullable - // Read the VelocityTracker strategy property from DeviceConfig - private static String readStrategyProperty() { - if (PermissionManager.checkPermission( - READ_DEVICE_CONFIG, Process.myPid(), Process.myUid()) == PERMISSION_GRANTED) { - return DeviceConfig.getProperty(NAMESPACE_INPUT_NATIVE_BOOT, VELOCITYTRACKER_STRATEGY); - } - // The process using VelocityTracker can't read the config. - return null; - } - private VelocityTracker(@VelocityTrackerStrategy int strategy) { // If user has not selected a specific strategy if (strategy == VELOCITY_TRACKER_STRATEGY_DEFAULT) { + final String strategyProperty = ViewConfiguration.get( + ActivityThread.currentActivityThread().getApplication()) + .getVelocityTrackerStrategy(); // Check if user specified strategy by overriding system property. - final String strategyProperty = readStrategyProperty(); if (strategyProperty == null || strategyProperty.isEmpty()) { mStrategy = strategy; } else { diff --git a/core/java/android/view/ViewConfiguration.java b/core/java/android/view/ViewConfiguration.java index ebc409e470e90..30ae9c826f40e 100644 --- a/core/java/android/view/ViewConfiguration.java +++ b/core/java/android/view/ViewConfiguration.java @@ -27,6 +27,7 @@ import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.Rect; +import android.hardware.input.InputManager; import android.os.Build; import android.os.Bundle; import android.os.RemoteException; @@ -348,6 +349,7 @@ public class ViewConfiguration { private final int mSmartSelectionInitializedTimeout; private final int mSmartSelectionInitializingTimeout; private final int mPreferKeepClearForFocusDelay; + private final String mVelocityTrackerStrategy; @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 123768915) private boolean sHasPermanentMenuKey; @@ -394,6 +396,7 @@ public class ViewConfiguration { mSmartSelectionInitializedTimeout = SMART_SELECTION_INITIALIZED_TIMEOUT_IN_MILLISECOND; mSmartSelectionInitializingTimeout = SMART_SELECTION_INITIALIZING_TIMEOUT_IN_MILLISECOND; mPreferKeepClearForFocusDelay = -1; + mVelocityTrackerStrategy = InputManager.getInstance().getVelocityTrackerStrategy(); } /** @@ -510,6 +513,16 @@ public class ViewConfiguration { com.android.internal.R.integer.config_smartSelectionInitializingTimeoutMillis); mPreferKeepClearForFocusDelay = res.getInteger( com.android.internal.R.integer.config_preferKeepClearForFocusDelayMillis); + + mVelocityTrackerStrategy = InputManager.getInstance().getVelocityTrackerStrategy(); + } + + /** + * Get the current VelocityTracker strategy + * @hide + */ + public String getVelocityTrackerStrategy() { + return mVelocityTrackerStrategy; } /** diff --git a/services/core/java/com/android/server/input/InputManagerService.java b/services/core/java/com/android/server/input/InputManagerService.java index 8a53260c7f97b..21a94da158d26 100644 --- a/services/core/java/com/android/server/input/InputManagerService.java +++ b/services/core/java/com/android/server/input/InputManagerService.java @@ -16,6 +16,7 @@ package com.android.server.input; +import static android.provider.DeviceConfig.NAMESPACE_INPUT_NATIVE_BOOT; import static android.view.KeyEvent.KEYCODE_UNKNOWN; import android.annotation.NonNull; @@ -159,6 +160,8 @@ public class InputManagerService extends IInputManager.Stub // Feature flag name for the deep press feature private static final String DEEP_PRESS_ENABLED = "deep_press_enabled"; + // Feature flag name for the strategy to be used in VelocityTracker + private static final String VELOCITYTRACKER_STRATEGY_PROPERTY = "velocitytracker_strategy"; private static final int MSG_DELIVER_INPUT_DEVICES_CHANGED = 1; private static final int MSG_SWITCH_KEYBOARD_LAYOUT = 2; @@ -447,6 +450,8 @@ public class InputManagerService extends IInputManager.Stub public static final int SW_CAMERA_LENS_COVER_BIT = 1 << SW_CAMERA_LENS_COVER; public static final int SW_MUTE_DEVICE_BIT = 1 << SW_MUTE_DEVICE; + private final String mVelocityTrackerStrategy; + /** Whether to use the dev/input/event or uevent subsystem for the audio jack. */ final boolean mUseDevInputEventForAudioJack; @@ -466,6 +471,8 @@ public class InputManagerService extends IInputManager.Stub mDoubleTouchGestureEnableFile = TextUtils.isEmpty(doubleTouchGestureEnablePath) ? null : new File(doubleTouchGestureEnablePath); + mVelocityTrackerStrategy = DeviceConfig.getProperty( + NAMESPACE_INPUT_NATIVE_BOOT, VELOCITYTRACKER_STRATEGY_PROPERTY); LocalServices.addService(InputManagerInternal.class, new LocalService()); } @@ -942,6 +949,11 @@ public class InputManagerService extends IInputManager.Stub return nativeVerifyInputEvent(mPtr, event); } + @Override // Binder call + public String getVelocityTrackerStrategy() { + return mVelocityTrackerStrategy; + } + /** * Gets information about the input device with the specified id. * @param deviceId The device id.