Disable system prediction by default
At the moment there are no heuristics applied to the system
prediction, which makes the errors noticeable on some implementations.
This change will prefer the internal implementation for the time being;
a new property has been added, `debug.input.prefer_system_prediction`,
which will reenable the system prediction if present.
Test: `adb shell setprop debug.input.prefer_system_prediction true`
Bug: 302300930
Change-Id: Ic36bcb894d6bce319882f456840bcd2f9d67822b
diff --git a/input/input-motionprediction/src/main/java/androidx/input/motionprediction/MotionEventPredictor.java b/input/input-motionprediction/src/main/java/androidx/input/motionprediction/MotionEventPredictor.java
index a08bba9..6dcaaa3 100644
--- a/input/input-motionprediction/src/main/java/androidx/input/motionprediction/MotionEventPredictor.java
+++ b/input/input-motionprediction/src/main/java/androidx/input/motionprediction/MotionEventPredictor.java
@@ -23,6 +23,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
+import androidx.input.motionprediction.common.SystemProperty;
import androidx.input.motionprediction.kalman.KalmanMotionEventPredictor;
import androidx.input.motionprediction.system.SystemMotionEventPredictor;
@@ -71,7 +72,8 @@
@NonNull
static MotionEventPredictor newInstance(@NonNull View view) {
Context context = view.getContext();
- if (Build.VERSION.SDK_INT >= 34) {
+ if (Build.VERSION.SDK_INT >= 34
+ && SystemProperty.getBoolean("debug.input.prefer_system_prediction")) {
return SystemMotionEventPredictor.newInstance(context);
} else {
return new KalmanMotionEventPredictor(context);
diff --git a/input/input-motionprediction/src/main/java/androidx/input/motionprediction/utils/PredictionEstimator.java b/input/input-motionprediction/src/main/java/androidx/input/motionprediction/common/PredictionEstimator.java
similarity index 98%
rename from input/input-motionprediction/src/main/java/androidx/input/motionprediction/utils/PredictionEstimator.java
rename to input/input-motionprediction/src/main/java/androidx/input/motionprediction/common/PredictionEstimator.java
index 3ed9a1b..3376643 100644
--- a/input/input-motionprediction/src/main/java/androidx/input/motionprediction/utils/PredictionEstimator.java
+++ b/input/input-motionprediction/src/main/java/androidx/input/motionprediction/common/PredictionEstimator.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package androidx.input.motionprediction.utils;
+package androidx.input.motionprediction.common;
import static androidx.annotation.RestrictTo.Scope.LIBRARY;
diff --git a/input/input-motionprediction/src/main/java/androidx/input/motionprediction/common/SystemProperty.java b/input/input-motionprediction/src/main/java/androidx/input/motionprediction/common/SystemProperty.java
new file mode 100644
index 0000000..2899359
--- /dev/null
+++ b/input/input-motionprediction/src/main/java/androidx/input/motionprediction/common/SystemProperty.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.input.motionprediction.common;
+
+import static androidx.annotation.RestrictTo.Scope.LIBRARY;
+
+import android.annotation.SuppressLint;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.RestrictTo;
+
+import java.lang.reflect.Method;
+
+/**
+ */
+@RestrictTo(LIBRARY)
+public class SystemProperty {
+ private static final boolean PROPERTY_DEFAULT = false;
+
+ private SystemProperty() {
+ // This class is non-instantiable.
+ }
+
+ /**
+ * Reads a system property and returns its boolean value.
+ *
+ * @param name the name of the system property
+ * @return true if the property is set and true, false otherwise
+ */
+ public static boolean getBoolean(@NonNull String name) {
+ try {
+ @SuppressLint("PrivateApi")
+ Class<?> systemProperties = Class.forName("android.os.SystemProperties");
+ Method getMethod = systemProperties.getMethod(
+ "getBoolean",
+ String.class,
+ boolean.class);
+ @SuppressLint("BanUncheckedReflection")
+ Boolean result = (Boolean) getMethod.invoke(systemProperties, name, PROPERTY_DEFAULT);
+ if (result != null) {
+ return result.booleanValue();
+ }
+ } catch (Exception e) {
+ }
+ return false;
+ }
+}
diff --git a/input/input-motionprediction/src/main/java/androidx/input/motionprediction/kalman/KalmanMotionEventPredictor.java b/input/input-motionprediction/src/main/java/androidx/input/motionprediction/kalman/KalmanMotionEventPredictor.java
index 1b7d73b..64adc26 100644
--- a/input/input-motionprediction/src/main/java/androidx/input/motionprediction/kalman/KalmanMotionEventPredictor.java
+++ b/input/input-motionprediction/src/main/java/androidx/input/motionprediction/kalman/KalmanMotionEventPredictor.java
@@ -25,7 +25,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.input.motionprediction.MotionEventPredictor;
-import androidx.input.motionprediction.utils.PredictionEstimator;
+import androidx.input.motionprediction.common.PredictionEstimator;
/**
*/
diff --git a/input/input-motionprediction/src/main/java/androidx/input/motionprediction/system/SystemMotionEventPredictor.java b/input/input-motionprediction/src/main/java/androidx/input/motionprediction/system/SystemMotionEventPredictor.java
index 93beded..a1a6b20 100644
--- a/input/input-motionprediction/src/main/java/androidx/input/motionprediction/system/SystemMotionEventPredictor.java
+++ b/input/input-motionprediction/src/main/java/androidx/input/motionprediction/system/SystemMotionEventPredictor.java
@@ -28,8 +28,8 @@
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.input.motionprediction.MotionEventPredictor;
+import androidx.input.motionprediction.common.PredictionEstimator;
import androidx.input.motionprediction.kalman.MultiPointerPredictor;
-import androidx.input.motionprediction.utils.PredictionEstimator;
import java.util.concurrent.TimeUnit;