summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/accessibility/ToggleAutoclickPreferenceController.java
blob: b9af7ce3ee80d1d71f6e8b5f56679cbfd1f3f593 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (C) 2020 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 com.android.settings.accessibility;

import static android.content.Context.MODE_PRIVATE;

import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.provider.Settings;
import android.util.ArrayMap;

import androidx.lifecycle.LifecycleObserver;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.LayoutPreference;
import com.android.settingslib.widget.RadioButtonPreference;

import java.util.Map;

/**
 * Controller class that controls accessibility autoclick settings.
 */
public class ToggleAutoclickPreferenceController extends BasePreferenceController implements
        LifecycleObserver, RadioButtonPreference.OnClickListener, PreferenceControllerMixin {

    private static final String CONTROL_AUTOCLICK_DELAY_SECURE =
            Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY;
    private static final String KEY_AUTOCLICK_CUSTOM_SEEKBAR = "autoclick_custom_seekbar";
    static final String KEY_DELAY_MODE = "delay_mode";

    private static final int AUTOCLICK_OFF_MODE = 0;
    private static final int AUTOCLICK_CUSTOM_MODE = 2000;

    // Pair the preference key and autoclick mode value.
    private final Map<String, Integer> mAccessibilityAutoclickKeyToValueMap = new ArrayMap<>();

    private SharedPreferences mSharedPreferences;
    private final ContentResolver mContentResolver;
    private final Resources mResources;
    private OnChangeListener mOnChangeListener;
    private RadioButtonPreference mDelayModePref;

    /**
     * Seek bar preference for autoclick delay value. The seek bar has values between 0 and
     * number of possible discrete autoclick delay values. These will have to be converted to actual
     * delay values before saving them in settings.
     */
    private LayoutPreference mSeekBerPreference;
    private int mCurrentUiAutoClickMode;

    public ToggleAutoclickPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);

        mSharedPreferences = context.getSharedPreferences(context.getPackageName(), MODE_PRIVATE);
        mContentResolver = context.getContentResolver();
        mResources = context.getResources();

        setAutoclickModeToKeyMap();
    }

    public ToggleAutoclickPreferenceController(Context context, Lifecycle lifecycle,
            String preferenceKey) {
        super(context, preferenceKey);

        mSharedPreferences = context.getSharedPreferences(context.getPackageName(), MODE_PRIVATE);
        mContentResolver = context.getContentResolver();
        mResources = context.getResources();

        setAutoclickModeToKeyMap();

        if (lifecycle != null) {
            lifecycle.addObserver(this);
        }
    }

    public void setOnChangeListener(OnChangeListener listener) {
        mOnChangeListener = listener;
    }

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);

        mDelayModePref = (RadioButtonPreference)
                screen.findPreference(getPreferenceKey());
        mDelayModePref.setOnClickListener(this);
        mSeekBerPreference = (LayoutPreference) screen.findPreference(KEY_AUTOCLICK_CUSTOM_SEEKBAR);
        updateState((Preference) mDelayModePref);
    }

    @Override
    public void onRadioButtonClicked(RadioButtonPreference preference) {
        final int value = mAccessibilityAutoclickKeyToValueMap.get(mPreferenceKey);
        handleRadioButtonPreferenceChange(value);
        if (mOnChangeListener != null) {
            mOnChangeListener.onCheckedChanged(mDelayModePref);
        }
    }

    private void updatePreferenceCheckedState(int mode) {
        if (mCurrentUiAutoClickMode == mode) {
            mDelayModePref.setChecked(true);
        }
    }

    private void updatePreferenceVisibleState(int mode) {
        mSeekBerPreference.setVisible(mCurrentUiAutoClickMode == mode);
    }

    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);

        final boolean enabled = Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, 0) == 1;

        mCurrentUiAutoClickMode =
                enabled ? getSharedPreferenceForAutoClickMode() : AUTOCLICK_OFF_MODE;

        // Reset RadioButton.
        mDelayModePref.setChecked(false);
        final int mode = mAccessibilityAutoclickKeyToValueMap.get(mDelayModePref.getKey());
        updatePreferenceCheckedState(mode);
        updatePreferenceVisibleState(mode);
    }

    /** Listener interface handles checked event. */
    public interface OnChangeListener {
        /**
         * A hook that is called when preference checked.
         */
        void onCheckedChanged(Preference preference);
    }

    private void setAutoclickModeToKeyMap() {
        final String[] autoclickKeys = mResources.getStringArray(
                R.array.accessibility_autoclick_control_selector_keys);

        final int[] autoclickValues = mResources.getIntArray(
                R.array.accessibility_autoclick_selector_values);

        final int autoclickValueCount = autoclickValues.length;
        for (int i = 0; i < autoclickValueCount; i++) {
            mAccessibilityAutoclickKeyToValueMap.put(autoclickKeys[i], autoclickValues[i]);
        }
    }

    private void handleRadioButtonPreferenceChange(int preference) {
        putSecureInt(Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED,
                (preference != AUTOCLICK_OFF_MODE) ? /* enabled */ 1 : /* disabled */ 0);

        mSharedPreferences.edit().putInt(KEY_DELAY_MODE, preference).apply();

        if (preference != AUTOCLICK_CUSTOM_MODE) {
            putSecureInt(CONTROL_AUTOCLICK_DELAY_SECURE, preference);
        }
    }

    private void putSecureInt(String name, int value) {
        Settings.Secure.putInt(mContentResolver, name, value);
    }

    private int getSharedPreferenceForAutoClickMode() {
        return mSharedPreferences.getInt(KEY_DELAY_MODE, AUTOCLICK_CUSTOM_MODE);
    }
}