1 /*
2  * Copyright (C) 2022 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 com.android.settings.accessibility;
18 
19 import static java.lang.annotation.RetentionPolicy.SOURCE;
20 
21 import android.annotation.IntDef;
22 import android.annotation.PluralsRes;
23 import android.annotation.StringRes;
24 import android.content.Context;
25 import android.content.res.Resources;
26 
27 import com.android.settings.R;
28 import com.android.settingslib.utils.StringUtil;
29 
30 import java.lang.annotation.Retention;
31 import java.util.HashMap;
32 import java.util.Map;
33 
34 /** Provides utility methods related auto click. */
35 public final class AutoclickUtils {
36 
37     /** Used for autoclick mode in the preferences editor. */
38     static final String KEY_DELAY_MODE = "delay_mode";
39 
40     /** Used for autoclick custom delay in the preferences editor. */
41     static final String KEY_CUSTOM_DELAY_VALUE = "custom_delay_value";
42 
43     /** Min allowed autoclick delay value. */
44     static final int MIN_AUTOCLICK_DELAY_MS = 200;
45 
46     /** Max allowed autoclick delay value. */
47     static final int MAX_AUTOCLICK_DELAY_MS = 1000;
48 
49     /**
50      * Allowed autoclick delay values are discrete. This is the difference between two allowed
51      * values.
52      */
53     static final int AUTOCLICK_DELAY_STEP = 100;
54 
55     @Retention(SOURCE)
56     @IntDef({
57         Quantity.ONE,
58         Quantity.FEW
59     })
60     private @interface Quantity {
61         int ONE = 1;
62         int FEW = 3;
63     }
64 
65     /**
66      * Gets string that should be used for provided autoclick delay.
67      *
68      * @param context context from which string should be retrieved.
69      * @param id The desired resource identifier, as generated by the aapt
70      *           tool. This integer encodes the package, type, and resource
71      *           entry. The value 0 is an invalid identifier.
72      * @param delayMillis Delay for whose value summary should be retrieved.
73      */
getAutoclickDelaySummary(Context context, @StringRes int id, int delayMillis)74     public static CharSequence getAutoclickDelaySummary(Context context,
75                                                         @StringRes int id, int delayMillis) {
76         final int quantity = (delayMillis == 1000) ? Quantity.ONE : Quantity.FEW;
77         final float delaySecond =  (float) delayMillis / 1000;
78         // Only show integer when delay time is 1.
79         final String decimalFormat = (delaySecond == 1) ? "%.0f" : "%.1f";
80 
81         Map<String, Object> arguments = new HashMap<>();
82         arguments.put("count", quantity);
83         arguments.put("time", String.format(decimalFormat, delaySecond));
84         return StringUtil.getIcuPluralsString(context, arguments, id);
85     }
86 
AutoclickUtils()87     private AutoclickUtils(){}
88 }
89