[Physical Keyboard] Add main page for Repeat keys

Page markup: https://screenshot.googleplex.com/5xCd2wVVyXajdCB.png

Test: atest SettingsRoboTests
Bug: 345399212
Flag: com.android.input.flags.keyboard_repeat_keys
Change-Id: Ie60f4b6ea4de973644103c0aa0bcdfe9a1e758eb
This commit is contained in:
shaoweishen
2024-10-09 14:31:29 +00:00
committed by Shaowei Shen
parent 145bbb1638
commit cbe75928d9
10 changed files with 546 additions and 12 deletions

View File

@@ -25,14 +25,20 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleObserver;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreferenceCompat;
import com.android.settingslib.PrimarySwitchPreference;
import com.android.settingslib.widget.MainSwitchPreference;
public class KeyboardRepeatKeysController extends
InputSettingPreferenceController implements
LifecycleObserver {
private static final String KEY_REPEAT_KEY = "physical_keyboard_repeat_keys";
private static final String KEY_REPEAT_KEY_MAIN_PAGE = "repeat_key_main_switch";
@Nullable
private SwitchPreferenceCompat mSwitchPreferenceCompat;
private PrimarySwitchPreference mPrimarySwitchPreference;
@Nullable
private MainSwitchPreference mMainSwitchPreference;
public KeyboardRepeatKeysController(@NonNull Context context,
@NonNull String key) {
@@ -42,7 +48,11 @@ public class KeyboardRepeatKeysController extends
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
mSwitchPreferenceCompat = screen.findPreference(getPreferenceKey());
if (KEY_REPEAT_KEY.equals(getPreferenceKey())) {
mPrimarySwitchPreference = screen.findPreference(getPreferenceKey());
} else if (KEY_REPEAT_KEY_MAIN_PAGE.equals(getPreferenceKey())) {
mMainSwitchPreference = screen.findPreference(getPreferenceKey());
}
}
@Override
@@ -63,8 +73,10 @@ public class KeyboardRepeatKeysController extends
@Override
protected void onInputSettingUpdated() {
if (mSwitchPreferenceCompat != null) {
mSwitchPreferenceCompat.setChecked(InputSettings.isRepeatKeysEnabled(mContext));
if (mPrimarySwitchPreference != null) {
mPrimarySwitchPreference.setChecked(InputSettings.isRepeatKeysEnabled(mContext));
} else if (mMainSwitchPreference != null) {
mMainSwitchPreference.setChecked(InputSettings.isRepeatKeysEnabled(mContext));
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2024 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.inputmethod;
import android.content.Context;
import android.hardware.input.InputSettings;
import androidx.annotation.NonNull;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.core.SliderPreferenceController;
import com.google.common.collect.ImmutableList;
public class KeyboardRepeatKeysDelayPreferenceController extends SliderPreferenceController {
@VisibleForTesting
static final ImmutableList<Integer> REPEAT_KEY_DELAY_VALUE_LIST = ImmutableList.of(2000, 1000,
500, 300, 200, 100, 50, 30, 20);
public KeyboardRepeatKeysDelayPreferenceController(@NonNull Context context,
@NonNull String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getSliderPosition() {
return REPEAT_KEY_DELAY_VALUE_LIST.indexOf(InputSettings.getRepeatKeysDelay(mContext));
}
@Override
public boolean setSliderPosition(int position) {
InputSettings.setRepeatKeysDelay(mContext, REPEAT_KEY_DELAY_VALUE_LIST.get(position));
return true;
}
@Override
public int getMax() {
return REPEAT_KEY_DELAY_VALUE_LIST.size() - 1;
}
@Override
public int getMin() {
return 0;
}
@Override
public int getAvailabilityStatus() {
return InputSettings.isRepeatKeysFeatureFlagEnabled()
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
}

View File

@@ -0,0 +1,170 @@
/*
* Copyright 2024 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.inputmethod;
import static com.android.settings.inputmethod.PhysicalKeyboardFragment.getHardKeyboards;
import android.app.settings.SettingsEnums;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.hardware.input.InputManager;
import android.hardware.input.InputSettings;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.UserHandle;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.android.internal.util.Preconditions;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.keyboard.Flags;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.LabeledSeekBarPreference;
import com.android.settingslib.utils.ThreadUtils;
import java.util.List;
public class KeyboardRepeatKeysMainFragment extends DashboardFragment
implements InputManager.InputDeviceListener {
private static final String TAG = "RepeatKeysMainFragment";
private static final String TIME_OUT_KEY = "repeat_keys_timeout_preference";
private static final String DELAY_KEY = "repeat_keys_delay_preference";
private final Uri mRepeatKeyUri = Settings.Secure.getUriFor(
Settings.Secure.KEY_REPEAT_ENABLED);
private final ContentObserver mContentObserver = new ContentObserver(new Handler(true)) {
@Override
public void onChange(boolean selfChange, Uri uri) {
if (mRepeatKeyUri.equals(uri)) {
updatePreferencesState();
}
}
};
private InputManager mInputManager;
private ContentResolver mContentResolver;
@Nullable
private LabeledSeekBarPreference mRepeatTimeoutPreference;
@Nullable
private LabeledSeekBarPreference mRepeatDelayPreference;
@Override
public int getMetricsCategory() {
return SettingsEnums.PHYSICAL_KEYBOARD_A11Y;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
mInputManager = Preconditions.checkNotNull(getActivity()
.getSystemService(InputManager.class));
mContentResolver = context.getContentResolver();
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
super.onCreatePreferences(bundle, s);
mRepeatTimeoutPreference = findPreference(TIME_OUT_KEY);
mRepeatDelayPreference = findPreference(DELAY_KEY);
updatePreferencesState();
}
@Override
public void onResume() {
super.onResume();
finishEarlyIfNeeded();
mInputManager.registerInputDeviceListener(this, null);
registerSettingsObserver();
}
@Override
public void onPause() {
super.onPause();
mInputManager.unregisterInputDeviceListener(this);
unregisterSettingsObserver();
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.repeat_key_main_page;
}
private void updatePreferencesState() {
boolean isRepeatKeyEnabled = InputSettings.isRepeatKeysEnabled(getContext());
if (mRepeatTimeoutPreference != null && mRepeatDelayPreference != null) {
mRepeatTimeoutPreference.setEnabled(isRepeatKeyEnabled);
mRepeatDelayPreference.setEnabled(isRepeatKeyEnabled);
}
}
private void registerSettingsObserver() {
unregisterSettingsObserver();
mContentResolver.registerContentObserver(
mRepeatKeyUri,
false,
mContentObserver,
UserHandle.myUserId());
}
private void unregisterSettingsObserver() {
mContentResolver.unregisterContentObserver(mContentObserver);
}
@Override
public void onInputDeviceAdded(int deviceId) {
finishEarlyIfNeeded();
}
@Override
public void onInputDeviceRemoved(int deviceId) {
finishEarlyIfNeeded();
}
@Override
public void onInputDeviceChanged(int deviceId) {
finishEarlyIfNeeded();
}
private void finishEarlyIfNeeded() {
final Context context = getContext();
ThreadUtils.postOnBackgroundThread(() -> {
final List<PhysicalKeyboardFragment.HardKeyboardDeviceInfo> newHardKeyboards =
getHardKeyboards(context);
if (newHardKeyboards.isEmpty()) {
getActivity().finish();
}
});
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.repeat_key_main_page) {
@Override
protected boolean isPageSearchEnabled(Context context) {
return Flags.keyboardAndTouchpadA11yNewPageEnabled()
&& !getHardKeyboards(context).isEmpty();
}
};
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2024 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.inputmethod;
import android.content.Context;
import android.hardware.input.InputSettings;
import androidx.annotation.NonNull;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.core.SliderPreferenceController;
import com.google.common.collect.ImmutableList;
public class KeyboardRepeatKeysTimeOutPreferenceController extends SliderPreferenceController {
@VisibleForTesting
static final ImmutableList<Integer> REPEAT_KEY_TIMEOUT_VALUE_LIST = ImmutableList.of(2000, 1500,
1000, 400, 300, 200, 150);
public KeyboardRepeatKeysTimeOutPreferenceController(
@NonNull Context context,
@NonNull String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getSliderPosition() {
return REPEAT_KEY_TIMEOUT_VALUE_LIST.indexOf(InputSettings.getRepeatKeysTimeout(mContext));
}
@Override
public boolean setSliderPosition(int position) {
InputSettings.setRepeatKeysTimeout(mContext, REPEAT_KEY_TIMEOUT_VALUE_LIST.get(position));
return true;
}
@Override
public int getMax() {
return REPEAT_KEY_TIMEOUT_VALUE_LIST.size() - 1;
}
@Override
public int getMin() {
return 0;
}
@Override
public int getAvailabilityStatus() {
return InputSettings.isRepeatKeysFeatureFlagEnabled()
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
}