Implement modifier keys settings UI.

Add four modifier keys and one reset button.

Bug: 244535460
Test: local test
Change-Id: I45822b0d8391022c17439dc25ab86fe022e9f43f
This commit is contained in:
danielwbhuang
2022-10-26 20:40:32 +08:00
parent e0fa3d3381
commit 85e0deb14d
18 changed files with 1029 additions and 1 deletions

View File

@@ -0,0 +1,198 @@
/*
* Copyright (C) 2022 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 android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.fragment.app.DialogFragment;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settingslib.Utils;
import java.util.ArrayList;
import java.util.List;
public class ModifierKeysPickerDialogFragment extends DialogFragment {
private Preference mPreference;
private String mKeyDefaultName;
private Context mContext;
public ModifierKeysPickerDialogFragment() {
}
public ModifierKeysPickerDialogFragment(Preference preference) {
mPreference = preference;
mKeyDefaultName = preference.getTitle().toString();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
mContext = getActivity();
String[] modifierKeys = new String[] {
mContext.getString(R.string.modifier_keys_caps_lock),
mContext.getString(R.string.modifier_keys_ctrl),
mContext.getString(R.string.modifier_keys_meta),
mContext.getString(R.string.modifier_keys_alt)};
View dialoglayout =
LayoutInflater.from(mContext).inflate(R.layout.modifier_key_picker_dialog, null);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
dialogBuilder.setView(dialoglayout);
TextView summary = dialoglayout.findViewById(R.id.modifier_key_picker_summary);
CharSequence summaryText = mContext.getString(
R.string.modifier_keys_picker_summary, mKeyDefaultName);
summary.setText(summaryText);
List<String> list = new ArrayList<>();
for (int i = 0; i < modifierKeys.length; i++) {
list.add(modifierKeys[i]);
}
ModifierKeyAdapter adapter = new ModifierKeyAdapter(list);
ListView listView = dialoglayout.findViewById(R.id.modifier_key_picker);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.setCurrentItem(i);
adapter.setClick(true);
adapter.notifyDataSetChanged();
}
});
AlertDialog modifierKeyDialog = dialogBuilder.create();
Button doneButton = dialoglayout.findViewById(R.id.modifier_key_done_button);
doneButton.setOnClickListener(v -> {
String selectedItem = list.get(adapter.getCurrentItem());
Spannable itemSummary;
if (selectedItem.equals(mKeyDefaultName)) {
itemSummary = new SpannableString(
mContext.getString(R.string.modifier_keys_default_summary));
itemSummary.setSpan(
new ForegroundColorSpan(getColorOfTextColorSecondary()),
0, itemSummary.length(), 0);
// TODO(b/252812993): remapModifierKey
} else {
itemSummary = new SpannableString(selectedItem);
itemSummary.setSpan(
new ForegroundColorSpan(getColorOfColorAccentPrimaryVariant()),
0, itemSummary.length(), 0);
}
mPreference.setSummary(itemSummary);
modifierKeyDialog.dismiss();
});
Button cancelButton = dialoglayout.findViewById(R.id.modifier_key_cancel_button);
cancelButton.setOnClickListener(v -> {
modifierKeyDialog.dismiss();
});
final Window window = modifierKeyDialog.getWindow();
window.setType(TYPE_SYSTEM_DIALOG);
return modifierKeyDialog;
}
class ModifierKeyAdapter extends BaseAdapter {
private int mCurrentItem = 0;
private boolean mIsClick = false;
private List<String> mList;
ModifierKeyAdapter(List<String> list) {
this.mList = list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int i) {
return mList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.modifier_key_item, null);
}
TextView textView = view.findViewById(R.id.modifier_key_text);
ImageView checkIcon = view.findViewById(R.id.modifier_key_check_icon);
textView.setText(mList.get(i));
if (mCurrentItem == i && mIsClick) {
textView.setTextColor(getColorOfColorAccentPrimaryVariant());
checkIcon.setImageAlpha(255);
} else {
textView.setTextColor(getColorOfTextColorPrimary());
checkIcon.setImageAlpha(0);
}
return view;
}
public void setCurrentItem(int currentItem) {
this.mCurrentItem = currentItem;
}
public int getCurrentItem() {
return this.mCurrentItem;
}
public void setClick(boolean click) {
this.mIsClick = click;
}
}
private int getColorOfTextColorPrimary() {
return Utils.getColorAttrDefaultColor(mContext, android.R.attr.textColorPrimary);
}
private int getColorOfTextColorSecondary() {
return Utils.getColorAttrDefaultColor(mContext, android.R.attr.textColorSecondary);
}
private int getColorOfColorAccentPrimaryVariant() {
return Utils.getColorAttrDefaultColor(
mContext, com.android.internal.R.attr.colorAccentPrimaryVariant);
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright (C) 2022 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 androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
public class ModifierKeysPreferenceController extends BasePreferenceController {
private static String KEY_TAG = "modifier_keys_dialog_tag";
private static String KEY_RESTORE_PREFERENCE = "modifier_keys_restore";
private Fragment mParent;
private FragmentManager mFragmentManager;
public ModifierKeysPreferenceController(Context context, String key) {
super(context, key);
}
public void setFragment(Fragment parent) {
mParent = parent;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
// TODO: getModifierKeyRemapping()
// setTitle
// setSummary
if (mParent == null) {
return;
}
// The dialog screen depends on the previous selected key's fragment.
// In the rotation scenario, we should remove the previous dialog screen first.
clearPreviousDialog();
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (preference.getKey().equals(KEY_RESTORE_PREFERENCE)) {
return false;
}
showModifierKeysDialog(preference);
return true;
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
private void showModifierKeysDialog(Preference preference) {
ModifierKeysPickerDialogFragment fragment =
new ModifierKeysPickerDialogFragment(preference);
fragment.setTargetFragment(mParent, 0);
fragment.show(mFragmentManager, KEY_TAG);
}
private void clearPreviousDialog() {
mFragmentManager = mParent.getFragmentManager();
DialogFragment preKeysDialogFragment =
(DialogFragment) mFragmentManager.findFragmentByTag(KEY_TAG);
if (preKeysDialogFragment != null) {
preKeysDialogFragment.dismiss();
}
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2022 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 android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import androidx.fragment.app.DialogFragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settingslib.Utils;
public class ModifierKeysResetDialogFragment extends DialogFragment {
private static final String MODIFIER_KEYS_CAPS_LOCK = "modifier_keys_caps_lock";
private static final String MODIFIER_KEYS_CTRL = "modifier_keys_ctrl";
private static final String MODIFIER_KEYS_META = "modifier_keys_meta";
private static final String MODIFIER_KEYS_ALT = "modifier_keys_alt";
private PreferenceScreen mScreen;
private String[] mKeys = {
MODIFIER_KEYS_CAPS_LOCK,
MODIFIER_KEYS_CTRL,
MODIFIER_KEYS_META,
MODIFIER_KEYS_ALT};
public ModifierKeysResetDialogFragment() {
}
public ModifierKeysResetDialogFragment(PreferenceScreen screen) {
mScreen = screen;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
Context mContext = getActivity();
View dialoglayout =
LayoutInflater.from(mContext).inflate(R.layout.modifier_key_reset_dialog, null);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
dialogBuilder.setView(dialoglayout);
AlertDialog modifierKeyResetDialog = dialogBuilder.create();
Button restoreButton = dialoglayout.findViewById(R.id.modifier_key_reset_restore_button);
restoreButton.setOnClickListener(v -> {
resetToDefault();
modifierKeyResetDialog.dismiss();
});
Button cancelButton = dialoglayout.findViewById(R.id.modifier_key_reset_cancel_button);
cancelButton.setOnClickListener(v -> {
modifierKeyResetDialog.dismiss();
});
final Window window = modifierKeyResetDialog.getWindow();
window.setType(TYPE_SYSTEM_DIALOG);
return modifierKeyResetDialog;
}
private void resetToDefault() {
Context mContext = getActivity();
for (int i = 0; i < mKeys.length; i++) {
Preference preference = mScreen.findPreference(mKeys[i]);
Spannable title = new SpannableString(
mContext.getString(R.string.modifier_keys_default_summary));
title.setSpan(
new ForegroundColorSpan(getColorOfTextColorSecondary()),
0, title.length(), 0);
preference.setSummary(title);
}
// TODO(b/252812993): clearAllModifierKeyRemappings()
}
private int getColorOfTextColorSecondary() {
return Utils.getColorAttrDefaultColor(getActivity(), android.R.attr.textColorSecondary);
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright (C) 2022 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.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.Utils;
public class ModifierKeysRestorePreferenceController extends BasePreferenceController {
private static String KEY_TAG = "modifier_keys_dialog_tag";
private Fragment mParent;
private FragmentManager mFragmentManager;
private PreferenceScreen mScreen;
public ModifierKeysRestorePreferenceController(Context context, String key) {
super(context, key);
}
public void setFragment(Fragment parent) {
mParent = parent;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
if (mParent == null) {
return;
}
mScreen = screen;
// The dialog screen depends on the previous selected key's fragment.
// In the rotation scenario, we should remove the previous dialog first.
clearPreviousDialog();
setResetKeyColor();
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (!preference.getKey().equals(getPreferenceKey())) {
return false;
}
showResetDialog();
return true;
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
private void showResetDialog() {
ModifierKeysResetDialogFragment fragment =
new ModifierKeysResetDialogFragment(mScreen);
fragment.setTargetFragment(mParent, 0);
fragment.show(mFragmentManager, KEY_TAG);
}
private void setResetKeyColor() {
Preference preference = mScreen.findPreference(getPreferenceKey());
Spannable title = new SpannableString(
mParent.getActivity().getString(R.string.modifier_keys_reset_title));
title.setSpan(
new ForegroundColorSpan(getColorOfColorAccentPrimaryVariant()),
0, title.length(), 0);
preference.setTitle(title);
}
private int getColorOfColorAccentPrimaryVariant() {
return Utils.getColorAttrDefaultColor(
mParent.getActivity(), com.android.internal.R.attr.colorAccentPrimaryVariant);
}
private void clearPreviousDialog() {
mFragmentManager = mParent.getFragmentManager();
DialogFragment preResetDialogFragment =
(DialogFragment) mFragmentManager.findFragmentByTag(KEY_TAG);
if (preResetDialogFragment != null) {
preResetDialogFragment.dismiss();
}
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2022 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.app.settings.SettingsEnums;
import android.content.Context;
import android.util.FeatureFlagUtils;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
@SearchIndexable
public class ModifierKeysSettings extends DashboardFragment {
private static final String TAG = "ModifierKeysSettings";
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(ModifierKeysPreferenceController.class).setFragment(this /*parent*/);
use(ModifierKeysRestorePreferenceController.class).setFragment(this /*parent*/);
}
@Override
public int getMetricsCategory() {
return SettingsEnums.SETTINGS_KEYBOARDS_MODIFIER_KEYS;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.modifier_keys_settings;
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.modifier_keys_settings) {
@Override
protected boolean isPageSearchEnabled(Context context) {
return FeatureFlagUtils
.isEnabled(
context, FeatureFlagUtils.SETTINGS_NEW_KEYBOARD_MODIFIER_KEY);
}
};
}

View File

@@ -67,6 +67,7 @@ public final class PhysicalKeyboardFragment extends SettingsPreferenceFragment
private static final String KEYBOARD_OPTIONS_CATEGORY = "keyboard_options_category";
private static final String SHOW_VIRTUAL_KEYBOARD_SWITCH = "show_virtual_keyboard_switch";
private static final String KEYBOARD_SHORTCUTS_HELPER = "keyboard_shortcuts_helper";
private static final String MODIFIER_KEYS_SETTINGS = "modifier_keys_settings";
@NonNull
private final ArrayList<HardKeyboardDeviceInfo> mLastHardKeyboards = new ArrayList<>();
@@ -94,9 +95,14 @@ public final class PhysicalKeyboardFragment extends SettingsPreferenceFragment
mShowVirtualKeyboardSwitch = Preconditions.checkNotNull(
(SwitchPreference) mKeyboardAssistanceCategory.findPreference(
SHOW_VIRTUAL_KEYBOARD_SWITCH));
mIsNewKeyboardSettings = FeatureFlagUtils.isEnabled(
getContext(), FeatureFlagUtils.SETTINGS_NEW_KEYBOARD_UI);
// TODO(b/247080921): Support shortcuts list & modifier keys
boolean isModifierKeySettingsEnabled = FeatureFlagUtils
.isEnabled(getContext(), FeatureFlagUtils.SETTINGS_NEW_KEYBOARD_MODIFIER_KEY);
if (!isModifierKeySettingsEnabled) {
mKeyboardAssistanceCategory.removePreference(findPreference(MODIFIER_KEYS_SETTINGS));
}
}
@Override