diff --git a/res/layout/modifier_key_item.xml b/res/layout/modifier_key_item.xml index a189479c0a7..683f631f14a 100644 --- a/res/layout/modifier_key_item.xml +++ b/res/layout/modifier_key_item.xml @@ -19,8 +19,7 @@ android:layout_marginTop="8dip" android:layout_marginBottom="8dip" android:minHeight="?android:attr/listPreferredItemHeight" - android:paddingEnd="?android:attr/scrollbarSize" - android:layout_weight="1"> + android:paddingEnd="?android:attr/scrollbarSize"> + + + + + + + + diff --git a/res/layout/modifier_keys_custom_key.xml b/res/layout/modifier_keys_custom_key.xml new file mode 100644 index 00000000000..f390c004b49 --- /dev/null +++ b/res/layout/modifier_keys_custom_key.xml @@ -0,0 +1,107 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/xml/modifier_keys_settings.xml b/res/xml/modifier_keys_settings.xml index 63e7ee1722b..25525ae39b6 100644 --- a/res/xml/modifier_keys_settings.xml +++ b/res/xml/modifier_keys_settings.xml @@ -21,25 +21,22 @@ android:title="@string/modifier_keys_settings" android:key="modifier_keys_all" settings:controller="com.android.settings.inputmethod.ModifierKeysPreferenceController"> - + android:layout="@layout/modifier_keys_custom_key" /> - + android:layout="@layout/modifier_keys_custom_key" /> - + android:layout="@layout/modifier_keys_custom_key" /> - + android:layout="@layout/modifier_keys_custom_key" /> mRemappableKeyList = new ArrayList<>(Arrays.asList( @@ -83,6 +91,8 @@ public class ModifierKeysPickerDialogFragment extends DialogFragment { super.onCreateDialog(savedInstanceState); mActivity = getActivity(); + FeatureFactory featureFactory = FeatureFactory.getFactory(mActivity); + mFeatureProvider = featureFactory.getKeyboardSettingsFeatureProvider(); InputManager inputManager = mActivity.getSystemService(InputManager.class); mKeyDefaultName = getArguments().getString(DEFAULT_KEY); mKeyFocus = getArguments().getString(SELECTION_KEY); @@ -97,6 +107,10 @@ public class ModifierKeysPickerDialogFragment extends DialogFragment { for (int i = 0; i < modifierKeys.size(); i++) { mRemappableKeyMap.put(modifierKeys.get(i), mRemappableKeyList.get(i)); } + Drawable drawable = mFeatureProvider.getActionKeyIcon(mActivity); + if (drawable != null) { + mActionKeyDrawable = DrawableCompat.wrap(drawable); + } View dialoglayout = LayoutInflater.from(mActivity).inflate(R.layout.modifier_key_picker_dialog, null); @@ -226,10 +240,18 @@ public class ModifierKeysPickerDialogFragment extends DialogFragment { checkIcon.setImageAlpha(255); view.setBackground( mActivity.getDrawable(R.drawable.modifier_key_lisetview_background)); + if (mActionKeyDrawable != null && i == 2) { + setActionKeyIcon(view); + setActionKeyColor(getColorOfMaterialColorPrimary()); + } } else { textView.setTextColor(getColorOfTextColorPrimary()); checkIcon.setImageAlpha(0); view.setBackground(null); + if (mActionKeyDrawable != null && i == 2) { + setActionKeyIcon(view); + setActionKeyColor(getColorOfTextColorPrimary()); + } } return view; } @@ -243,6 +265,21 @@ public class ModifierKeysPickerDialogFragment extends DialogFragment { } } + private void setActionKeyIcon(View view) { + mLeftBracket = view.findViewById(R.id.modifier_key_left_bracket); + mRightBracket = view.findViewById(R.id.modifier_key_right_bracket); + mActionKeyIcon = view.findViewById(R.id.modifier_key_action_key_icon); + mLeftBracket.setText("("); + mRightBracket.setText(")"); + mActionKeyIcon.setImageDrawable(mActionKeyDrawable); + } + + private void setActionKeyColor(int color) { + mLeftBracket.setTextColor(color); + mRightBracket.setTextColor(color); + DrawableCompat.setTint(mActionKeyDrawable, color); + } + private int getColorOfTextColorPrimary() { return Utils.getColorAttrDefaultColor(mActivity, android.R.attr.textColorPrimary); } diff --git a/src/com/android/settings/inputmethod/ModifierKeysPreferenceController.java b/src/com/android/settings/inputmethod/ModifierKeysPreferenceController.java index 5d8149ae64a..77def483795 100644 --- a/src/com/android/settings/inputmethod/ModifierKeysPreferenceController.java +++ b/src/com/android/settings/inputmethod/ModifierKeysPreferenceController.java @@ -17,12 +17,16 @@ package com.android.settings.inputmethod; import android.content.Context; +import android.graphics.drawable.Drawable; import android.hardware.input.InputManager; import android.os.Bundle; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; +import android.util.Pair; import android.view.KeyEvent; +import android.widget.ImageView; +import android.widget.TextView; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; @@ -31,7 +35,9 @@ import androidx.preference.PreferenceScreen; import com.android.settings.R; import com.android.settings.core.BasePreferenceController; +import com.android.settings.overlay.FeatureFactory; import com.android.settingslib.Utils; +import com.android.settingslib.widget.LayoutPreference; import java.util.ArrayList; import java.util.Arrays; @@ -53,6 +59,7 @@ public class ModifierKeysPreferenceController extends BasePreferenceController { private FragmentManager mFragmentManager; private final InputManager mIm; private PreferenceScreen mScreen; + private Drawable mDrawable; private final List mRemappableKeys = new ArrayList<>( Arrays.asList( @@ -61,6 +68,14 @@ public class ModifierKeysPreferenceController extends BasePreferenceController { KeyEvent.KEYCODE_ALT_LEFT, KeyEvent.KEYCODE_ALT_RIGHT, KeyEvent.KEYCODE_CAPS_LOCK)); + private final List> mKeys = new ArrayList<>( + Arrays.asList( + Pair.create(KEY_PREFERENCE_CTRL, R.string.modifier_keys_ctrl), + Pair.create(KEY_PREFERENCE_META, R.string.modifier_keys_meta), + Pair.create(KEY_PREFERENCE_ALT, R.string.modifier_keys_alt), + Pair.create(KEY_PREFERENCE_CAPS_LOCK, R.string.modifier_keys_caps_lock) + )); + private String[] mKeyNames = new String[] { mContext.getString(R.string.modifier_keys_ctrl), mContext.getString(R.string.modifier_keys_ctrl), @@ -74,6 +89,9 @@ public class ModifierKeysPreferenceController extends BasePreferenceController { super(context, key); mIm = context.getSystemService(InputManager.class); Objects.requireNonNull(mIm, "InputManager service cannot be null"); + KeyboardSettingsFeatureProvider featureProvider = + FeatureFactory.getFactory(context).getKeyboardSettingsFeatureProvider(); + mDrawable = featureProvider.getActionKeyIcon(context); } public void setFragment(Fragment parent) { @@ -91,33 +109,59 @@ public class ModifierKeysPreferenceController extends BasePreferenceController { } private void refreshUi() { + initDefaultKeysName(); for (Map.Entry entry : mIm.getModifierKeyRemapping().entrySet()) { int fromKey = entry.getKey(); int toKey = entry.getValue(); int index = mRemappableKeys.indexOf(toKey); if (isCtrl(fromKey) && mRemappableKeys.contains(toKey)) { - Preference preference = mScreen.findPreference(KEY_PREFERENCE_CTRL); - preference.setSummary(changeSummaryColor(mKeyNames[index])); + setSummaryColor(KEY_PREFERENCE_CTRL, index); } if (isMeta(fromKey) && mRemappableKeys.contains(toKey)) { - Preference preference = mScreen.findPreference(KEY_PREFERENCE_META); - preference.setSummary(changeSummaryColor(mKeyNames[index])); + setSummaryColor(KEY_PREFERENCE_META, index); } if (isAlt(fromKey) && mRemappableKeys.contains(toKey)) { - Preference preference = mScreen.findPreference(KEY_PREFERENCE_ALT); - preference.setSummary(changeSummaryColor(mKeyNames[index])); + setSummaryColor(KEY_PREFERENCE_ALT, index); } if (isCapLock(fromKey) && mRemappableKeys.contains(toKey)) { - Preference preference = mScreen.findPreference(KEY_PREFERENCE_CAPS_LOCK); - preference.setSummary(changeSummaryColor(mKeyNames[index])); + setSummaryColor(KEY_PREFERENCE_CAPS_LOCK, index); } } } + private void initDefaultKeysName() { + for (Pair key : mKeys) { + LayoutPreference layoutPreference = mScreen.findPreference(key.first); + TextView title = layoutPreference.findViewById(R.id.title); + TextView summary = layoutPreference.findViewById(R.id.summary); + title.setText(key.second); + summary.setText(R.string.modifier_keys_default_summary); + + if (key.first.equals(KEY_PREFERENCE_META) && mDrawable != null) { + setActionKeyIcon(layoutPreference, mDrawable); + } + } + } + + private static void setActionKeyIcon(LayoutPreference preference, Drawable drawable) { + TextView leftBracket = preference.findViewById(R.id.modifier_key_left_bracket); + TextView rightBracket = preference.findViewById(R.id.modifier_key_right_bracket); + ImageView actionKeyIcon = preference.findViewById(R.id.modifier_key_action_key_icon); + leftBracket.setText("("); + rightBracket.setText(")"); + actionKeyIcon.setImageDrawable(drawable); + } + + private void setSummaryColor(String key, int targetIndex) { + LayoutPreference layoutPreference = mScreen.findPreference(key); + TextView summary = layoutPreference.findViewById(R.id.summary); + summary.setText(changeSummaryColor(mKeyNames[targetIndex])); + } + @Override public boolean handlePreferenceTreeClick(Preference preference) { if (preference.getKey().equals(KEY_RESTORE_PREFERENCE)) { @@ -137,12 +181,14 @@ public class ModifierKeysPreferenceController extends BasePreferenceController { ModifierKeysPickerDialogFragment fragment = new ModifierKeysPickerDialogFragment(); fragment.setTargetFragment(mParent, 0); Bundle bundle = new Bundle(); + TextView title = ((LayoutPreference) preference).findViewById(R.id.title); + TextView summary = ((LayoutPreference) preference).findViewById(R.id.summary); bundle.putString( ModifierKeysPickerDialogFragment.DEFAULT_KEY, - preference.getTitle().toString()); + title.getText().toString()); bundle.putString( ModifierKeysPickerDialogFragment.SELECTION_KEY, - preference.getSummary().toString()); + summary.getText().toString()); fragment.setArguments(bundle); fragment.show(mFragmentManager, KEY_TAG); } diff --git a/tests/unit/src/com/android/settings/inputmethod/KeyboardSettingsFeatureProviderImplTest.java b/tests/unit/src/com/android/settings/inputmethod/KeyboardSettingsFeatureProviderImplTest.java index 72facc3dfa4..6675d5a27c4 100644 --- a/tests/unit/src/com/android/settings/inputmethod/KeyboardSettingsFeatureProviderImplTest.java +++ b/tests/unit/src/com/android/settings/inputmethod/KeyboardSettingsFeatureProviderImplTest.java @@ -57,4 +57,9 @@ public class KeyboardSettingsFeatureProviderImplTest { assertThat(mFeatureProvider.addFirmwareUpdateCategory(mContext, screen)).isFalse(); } + + @Test + public void getActionKeyIcon_defaultValue_returnsNull() { + assertThat(mFeatureProvider.getActionKeyIcon(mContext)).isNull(); + } }