1 /*
2  * Copyright (C) 2019 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 com.android.settings.accessibility.AccessibilityUtil.State.OFF;
20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
21 
22 import android.content.Context;
23 import android.provider.Settings;
24 import android.view.accessibility.AccessibilityManager;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.BasePreferenceController;
28 
29 /** Preference controller for autoclick (dwell timing). */
30 public class AutoclickPreferenceController extends BasePreferenceController {
31 
32     /**
33      * Resource ids from which autoclick preference summaries should be derived. The strings have
34      * placeholder for integer delay value.
35      */
36     private static final int[] AUTOCLICK_PREFERENCE_SUMMARIES = {
37             R.string.accessibilty_autoclick_preference_subtitle_short_delay,
38             R.string.accessibilty_autoclick_preference_subtitle_medium_delay,
39             R.string.accessibilty_autoclick_preference_subtitle_long_delay
40     };
41 
AutoclickPreferenceController(Context context, String preferenceKey)42     public AutoclickPreferenceController(Context context, String preferenceKey) {
43         super(context, preferenceKey);
44     }
45 
46     @Override
getAvailabilityStatus()47     public int getAvailabilityStatus() {
48         return AVAILABLE;
49     }
50 
51     @Override
getSummary()52     public CharSequence getSummary() {
53         final boolean enabled = Settings.Secure.getInt(mContext.getContentResolver(),
54                 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, OFF) == ON;
55         if (!enabled) {
56             return mContext.getResources().getText(R.string.autoclick_disabled);
57         }
58         final int delayMillis = Settings.Secure.getInt(mContext.getContentResolver(),
59                 Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY,
60                 AccessibilityManager.AUTOCLICK_DELAY_DEFAULT);
61         final int summaryIndex = getAutoclickPreferenceSummaryIndex(delayMillis);
62         return AutoclickUtils.getAutoclickDelaySummary(mContext,
63                 AUTOCLICK_PREFERENCE_SUMMARIES[summaryIndex], delayMillis);
64     }
65 
66     /** Finds index of the summary that should be used for the provided autoclick delay. */
getAutoclickPreferenceSummaryIndex(int delay)67     private int getAutoclickPreferenceSummaryIndex(int delay) {
68         if (delay <= AutoclickUtils.MIN_AUTOCLICK_DELAY_MS) {
69             return 0;
70         }
71         if (delay >= AutoclickUtils.MAX_AUTOCLICK_DELAY_MS) {
72             return AUTOCLICK_PREFERENCE_SUMMARIES.length - 1;
73         }
74         int delayRange =
75                 AutoclickUtils.MAX_AUTOCLICK_DELAY_MS - AutoclickUtils.MIN_AUTOCLICK_DELAY_MS;
76         int rangeSize = (delayRange) / (AUTOCLICK_PREFERENCE_SUMMARIES.length - 1);
77         return (delay - AutoclickUtils.MIN_AUTOCLICK_DELAY_MS) / rangeSize;
78     }
79 }
80