1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.input.motionprediction.common;
18 
19 import static androidx.annotation.RestrictTo.Scope.LIBRARY;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.RestrictTo;
23 
24 /**
25  */
26 @RestrictTo(LIBRARY)
27 public class Configuration {
28     public static final int STRATEGY_BALANCED = 0;
29     public static final int STRATEGY_SAFE = 1;
30     public static final int STRATEGY_AGGRESSIVE = 2;
31 
32     private static volatile Configuration sInstance = null;
33     private static final Object sLock = new Object();
34 
35     private final boolean mPredictLift;
36     private final boolean mPreferSystemPrediction;
37     private final int mPredictionOffset;
38     private final int mPredictionStrategy;
39 
40     /**
41      * Returns the configuration for prediction in this system.
42      *
43      * @return the prediction configuration
44      */
getInstance()45     public static @NonNull Configuration getInstance() {
46         if (sInstance == null) {
47             synchronized (sLock) {
48                 if (sInstance == null) {
49                     sInstance = new Configuration();
50                 }
51             }
52         }
53         return sInstance;
54     }
55 
Configuration()56     private Configuration() {
57         // This class is non-instantiable externally.
58         mPreferSystemPrediction = SystemProperty
59                 .getBoolean("debug.input.androidx_prefer_system_prediction");
60         mPredictionOffset = SystemProperty.getInt("debug.input.androidx_prediction_offset");
61         mPredictLift = SystemProperty.getBoolean("debug.input.androidx_predict_lift");
62         mPredictionStrategy = SystemProperty.getInt("debug.input.androidx_prediction_strategy");
63     }
64 
65     /**
66      * Returns whether or not the library should prefer the system prediction when available
67      *
68      * @return true if the system prediction should be used when available
69      */
preferSystemPrediction()70     public boolean preferSystemPrediction() {
71         return mPreferSystemPrediction;
72     }
73 
74     /**
75      * Returns the number of milliseconds to add to the computed prediction target
76      *
77      * @return number of additional milliseconds to predict
78      */
predictionOffset()79     public int predictionOffset() {
80         return mPredictionOffset;
81     }
82 
83     /**
84      * Returns whether or not the pressure should be used to adjust the distance of the prediction
85      *
86      * @return true if the pressure should be used to determine the prediction length
87      */
predictLift()88     public boolean predictLift() {
89         return mPredictLift;
90     }
91 
92     /**
93      * Returns the default prediction strategy
94      *
95      * @return the strategy to use as default; 0 is balanced
96      */
predictionStrategy()97     public int predictionStrategy() {
98         return mPredictionStrategy;
99     }
100 }
101