[Physical Keybard] add Mouse key main page

Add page for Mouse key, which contain a main switch toggle and list of
explain images.
demo video: b/346949547#comment38
images:
https://screenshot.googleplex.com/3mc6KnyMdvfAia9.png
https://screenshot.googleplex.com/489mTfSYg9KMUpW.png

Bug: 346949547
Test: atest SettingsRoboTests
Flag: com.android.settings.keyboard.keyboard_and_touchpad_a11y_new_page_enabled
Change-Id: Ia40d5f071cc674ce0118d7ec8a4f0d5e914ce8b9
This commit is contained in:
shaoweishen
2024-10-08 02:36:36 +00:00
parent 1d6b6cef69
commit 0393a8165b
20 changed files with 2856 additions and 8 deletions

View File

@@ -22,15 +22,23 @@ import android.net.Uri;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleObserver;
import androidx.preference.PreferenceScreen;
import androidx.preference.TwoStatePreference;
import com.android.settingslib.PrimarySwitchPreference;
import com.android.settingslib.widget.MainSwitchPreference;
public class KeyboardAccessibilityMouseKeysController extends
InputSettingPreferenceController implements
LifecycleObserver {
private static final String KEY_MOUSE_KEY = "accessibility_mouse_keys";
private static final String KEY_MOUSE_KEY_MAIN_PAGE = "mouse_keys_main_switch";
private TwoStatePreference mTwoStatePreference;
@Nullable
private PrimarySwitchPreference mPrimaryPreference;
@Nullable
private MainSwitchPreference mMainSwitchPreference;
public KeyboardAccessibilityMouseKeysController(@NonNull Context context, @NonNull String key) {
super(context, key);
@@ -39,7 +47,11 @@ public class KeyboardAccessibilityMouseKeysController extends
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
mTwoStatePreference = screen.findPreference(getPreferenceKey());
if (KEY_MOUSE_KEY.equals(getPreferenceKey())) {
mPrimaryPreference = screen.findPreference(getPreferenceKey());
} else if (KEY_MOUSE_KEY_MAIN_PAGE.equals(getPreferenceKey())) {
mMainSwitchPreference = screen.findPreference(getPreferenceKey());
}
}
@Override
@@ -63,8 +75,11 @@ public class KeyboardAccessibilityMouseKeysController extends
@Override
protected void onInputSettingUpdated() {
if (mTwoStatePreference != null) {
mTwoStatePreference.setChecked(
if (mPrimaryPreference != null) {
mPrimaryPreference.setChecked(
InputSettings.isAccessibilityMouseKeysEnabled(mContext));
} else if (mMainSwitchPreference != null) {
mMainSwitchPreference.setChecked(
InputSettings.isAccessibilityMouseKeysEnabled(mContext));
}
}

View File

@@ -0,0 +1,138 @@
/*
* 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.view.InputDevice;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MouseKeysImageListAdapter extends
RecyclerView.Adapter<MouseKeysImageListAdapter.MouseKeyImageViewHolder> {
private static final ImmutableList<Integer> DRAWABLE_LIST = ImmutableList.of(
R.drawable.mouse_keys_directional, R.drawable.mouse_keys_click,
R.drawable.mouse_keys_press_hold, R.drawable.mouse_keys_release,
R.drawable.mouse_keys_toggle_scroll, R.drawable.mouse_keys_release2);
private static final ImmutableList<Integer> DIRECTIONAL_CHAR_KEYCODE_LIST = ImmutableList.of(
KeyEvent.KEYCODE_7, KeyEvent.KEYCODE_8, KeyEvent.KEYCODE_9, KeyEvent.KEYCODE_U,
KeyEvent.KEYCODE_O, KeyEvent.KEYCODE_J, KeyEvent.KEYCODE_K, KeyEvent.KEYCODE_L
);
private static final int LEFT_CLICK_CHAR_KEYCODE =
KeyEvent.KEYCODE_I;
private static final int PRESS_HOLD_CHAR_KEYCODE =
KeyEvent.KEYCODE_M;
private static final int RELEASE_CHAR_KEYCODE =
KeyEvent.KEYCODE_COMMA;
private static final ImmutableList<Integer> TOGGLE_SCROLL_CHAR_KEYCODE_LIST = ImmutableList.of(
KeyEvent.KEYCODE_PERIOD, KeyEvent.KEYCODE_8, KeyEvent.KEYCODE_K, KeyEvent.KEYCODE_O,
KeyEvent.KEYCODE_U
);
private static final int RIGHT_CLICK_CHAR_KEYCODE =
KeyEvent.KEYCODE_SLASH;
private final List<String> mComposedSummaryList = new ArrayList<>();
public MouseKeysImageListAdapter(@NonNull Context context,
@Nullable InputDevice currentInputDevice) {
composeSummaryForImages(context, currentInputDevice);
}
@NonNull
@Override
public MouseKeyImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.mouse_keys_image_item, parent, false);
return new MouseKeyImageViewHolder(view, parent.getContext());
}
@Override
public void onBindViewHolder(@NonNull MouseKeyImageViewHolder holder, int position) {
((MouseKeyImageViewHolder) holder).bindView(DRAWABLE_LIST.get(position),
mComposedSummaryList.get(position));
}
@Override
public int getItemCount() {
return DRAWABLE_LIST.size();
}
private void composeSummaryForImages(Context context,
@Nullable InputDevice currentInputDevice) {
if (currentInputDevice == null) {
return;
}
mComposedSummaryList.clear();
List<String> directionalLabelList = DIRECTIONAL_CHAR_KEYCODE_LIST.stream().map(
(key) -> getDisplayLabel(currentInputDevice, key)).toList();
mComposedSummaryList.add(context.getString(R.string.mouse_keys_directional_summary,
String.join(",", directionalLabelList)));
String leftClickLabel = getDisplayLabel(currentInputDevice, LEFT_CLICK_CHAR_KEYCODE);
mComposedSummaryList.add(
context.getString(R.string.mouse_keys_click_summary, leftClickLabel));
String pressHoldLabel = getDisplayLabel(currentInputDevice, PRESS_HOLD_CHAR_KEYCODE);
mComposedSummaryList.add(
context.getString(R.string.mouse_keys_press_hold_summary, pressHoldLabel));
String releaseLabel = getDisplayLabel(currentInputDevice, RELEASE_CHAR_KEYCODE);
mComposedSummaryList.add(
context.getString(R.string.mouse_keys_release_summary, releaseLabel));
List<String> toggleScrollLabelList = TOGGLE_SCROLL_CHAR_KEYCODE_LIST.stream().map(
(key) -> getDisplayLabel(currentInputDevice, key)).toList();
mComposedSummaryList.add(context.getString(R.string.mouse_keys_toggle_scroll_summary,
toggleScrollLabelList.getFirst(),
String.join(",", toggleScrollLabelList.subList(1, toggleScrollLabelList.size()))
));
String rightClickLabel = getDisplayLabel(currentInputDevice, RIGHT_CLICK_CHAR_KEYCODE);
mComposedSummaryList.add(
context.getString(R.string.mouse_keys_release2_summary, rightClickLabel));
}
private String getDisplayLabel(InputDevice currentInputDevice, int keycode) {
return String.valueOf(currentInputDevice.getKeyCharacterMap().getDisplayLabel(
currentInputDevice.getKeyCodeForKeyLocation(keycode))).toLowerCase(Locale.ROOT);
}
public static class MouseKeyImageViewHolder extends RecyclerView.ViewHolder {
private final TextView mTextView;
private final Context mContext;
public MouseKeyImageViewHolder(View itemView, Context context) {
super(itemView);
mTextView = (TextView) itemView;
mContext = context;
}
void bindView(int drawableRes, String summary) {
mTextView.setText(summary);
mTextView.setCompoundDrawablesWithIntrinsicBounds(null,
mContext.getDrawable(drawableRes), null, null);
}
}
}

View File

@@ -0,0 +1,163 @@
/*
* 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.Context;
import android.hardware.input.InputManager;
import android.os.Bundle;
import android.view.InputDevice;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceScreen;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
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.settingslib.search.SearchIndexable;
import com.android.settingslib.utils.ThreadUtils;
import com.android.settingslib.widget.LayoutPreference;
import java.util.List;
@SearchIndexable
public class MouseKeysMainPageFragment extends DashboardFragment
implements InputManager.InputDeviceListener {
private static final String TAG = "MouseKeysMainPageFragment";
private static final String KEY_MOUSE_KEY_LIST = "mouse_keys_list";
private InputManager mInputManager;
private LayoutPreference mMouseKeyImagesPreference;
@Nullable
private InputDevice mCurrentInputDevice;
@Override
public void onCreate(@NonNull Bundle bundle) {
super.onCreate(bundle);
mCurrentInputDevice = getInputDevice();
final PreferenceScreen screen = getPreferenceScreen();
mMouseKeyImagesPreference = screen.findPreference(KEY_MOUSE_KEY_LIST);
mInputManager = Preconditions.checkNotNull(getActivity()
.getSystemService(InputManager.class));
String title = mCurrentInputDevice == null ? getActivity().getString(R.string.mouse_keys)
: getActivity().getString(R.string.mouse_key_main_page_title,
mCurrentInputDevice.getName());
getActivity().setTitle(title);
configureImagesPreference();
}
@Override
public void onResume() {
super.onResume();
finishEarlyIfNeeded();
mInputManager.registerInputDeviceListener(this, null);
}
@Override
public void onPause() {
super.onPause();
mInputManager.unregisterInputDeviceListener(this);
}
@Override
public int getMetricsCategory() {
return SettingsEnums.PHYSICAL_KEYBOARD_A11Y;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.mouse_keys_main_page;
}
@Override
protected String getLogTag() {
return TAG;
}
@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();
}
});
}
private void configureImagesPreference() {
final RecyclerView recyclerView = mMouseKeyImagesPreference.findViewById(
R.id.mouse_keys_image_recycler_list);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
recyclerView.setAdapter(new MouseKeysImageListAdapter(getActivity(), mCurrentInputDevice));
}
/**
* Priority of picking input device:
* 1. internal keyboard(built-in keyboard)
* 2. first keyboard in the list
*/
@Nullable
private InputDevice getInputDevice() {
InputDevice inputDevice = null;
for (int deviceId : InputDevice.getDeviceIds()) {
final InputDevice device = InputDevice.getDevice(deviceId);
if (device == null || device.isVirtual() || !device.isFullKeyboard()) {
continue;
}
if (inputDevice == null) {
inputDevice = device;
} else if (!device.isExternal()) {
inputDevice = device;
break;
}
}
return inputDevice;
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.mouse_keys_main_page) {
@Override
protected boolean isPageSearchEnabled(Context context) {
return Flags.keyboardAndTouchpadA11yNewPageEnabled()
&& !getHardKeyboards(context).isEmpty();
}
};
}