• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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.inputmethod;
18 
19 import android.content.Context;
20 import android.hardware.input.InputSettings;
21 import android.net.Uri;
22 import android.provider.Settings;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.lifecycle.LifecycleObserver;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settingslib.PrimarySwitchPreference;
30 import com.android.settingslib.widget.MainSwitchPreference;
31 
32 public class KeyboardRepeatKeysController extends
33         InputSettingPreferenceController implements
34         LifecycleObserver {
35     private static final String KEY_REPEAT_KEY = "physical_keyboard_repeat_keys";
36     private static final String KEY_REPEAT_KEY_MAIN_PAGE = "repeat_key_main_switch";
37 
38     @Nullable
39     private PrimarySwitchPreference mPrimarySwitchPreference;
40     @Nullable
41     private MainSwitchPreference mMainSwitchPreference;
42 
KeyboardRepeatKeysController(@onNull Context context, @NonNull String key)43     public KeyboardRepeatKeysController(@NonNull Context context,
44             @NonNull String key) {
45         super(context, key);
46     }
47 
48     @Override
displayPreference(@onNull PreferenceScreen screen)49     public void displayPreference(@NonNull PreferenceScreen screen) {
50         super.displayPreference(screen);
51         if (KEY_REPEAT_KEY.equals(getPreferenceKey())) {
52             mPrimarySwitchPreference = screen.findPreference(getPreferenceKey());
53         } else if (KEY_REPEAT_KEY_MAIN_PAGE.equals(getPreferenceKey())) {
54             mMainSwitchPreference = screen.findPreference(getPreferenceKey());
55         }
56     }
57 
58     @Override
getAvailabilityStatus()59     public int getAvailabilityStatus() {
60         return InputSettings.isRepeatKeysFeatureFlagEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
61     }
62 
63     @Override
isChecked()64     public boolean isChecked() {
65         return InputSettings.isRepeatKeysEnabled(mContext);
66     }
67 
68     @Override
setChecked(boolean isChecked)69     public boolean setChecked(boolean isChecked) {
70         InputSettings.setRepeatKeysEnabled(mContext, isChecked);
71         return true;
72     }
73 
74     @Override
onInputSettingUpdated()75     protected void onInputSettingUpdated() {
76         if (mPrimarySwitchPreference != null) {
77             mPrimarySwitchPreference.setChecked(InputSettings.isRepeatKeysEnabled(mContext));
78         } else if (mMainSwitchPreference != null) {
79             mMainSwitchPreference.setChecked(InputSettings.isRepeatKeysEnabled(mContext));
80         }
81     }
82 
83     @Override
getSettingUri()84     protected Uri getSettingUri() {
85         return Settings.Secure.getUriFor(
86                 Settings.Secure.KEY_REPEAT_ENABLED);
87     }
88 }
89