From d842533f224280484839b77d016c2e8da697a0ed Mon Sep 17 00:00:00 2001 From: Vladislav Kaznacheev Date: Mon, 26 Feb 2018 15:02:59 -0800 Subject: [PATCH] Display shortcut modifiers in menu Prepend relevant meta key names (such as Ctrl+) to the shortcut displayed in a menu. Do not prepend "Menu+" if the device does not have a hardware Menu key. Bug: 31045453 Test: run ApiDemos (MenuInflateFromXml), select "Shortcuts", observe Ctrl+ prepended to shortcuts. Change-Id: I1a38bd1baf069dd1adb24a26f89c6db6390b8b8d (cherry picked from commit 0cc2c6b190695ef78029c7b05a175fb3bb7e4098) --- .../internal/view/menu/MenuItemImpl.java | 63 +++++++++++-------- core/res/res/values/strings.xml | 12 ++++ core/res/res/values/symbols.xml | 6 ++ 3 files changed, 56 insertions(+), 25 deletions(-) diff --git a/core/java/com/android/internal/view/menu/MenuItemImpl.java b/core/java/com/android/internal/view/menu/MenuItemImpl.java index 9d012de33089e..0c5ea6327dff1 100644 --- a/core/java/com/android/internal/view/menu/MenuItemImpl.java +++ b/core/java/com/android/internal/view/menu/MenuItemImpl.java @@ -23,6 +23,7 @@ import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.content.res.ColorStateList; +import android.content.res.Resources; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.util.Log; @@ -33,6 +34,7 @@ import android.view.LayoutInflater; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; +import android.view.ViewConfiguration; import android.view.ViewDebug; import android.widget.LinearLayout; @@ -108,13 +110,6 @@ public final class MenuItemImpl implements MenuItem { private CharSequence mContentDescription; private CharSequence mTooltipText; - private static String sLanguage; - private static String sPrependShortcutLabel; - private static String sEnterShortcutLabel; - private static String sDeleteShortcutLabel; - private static String sSpaceShortcutLabel; - - /** * Instantiates this menu item. * @@ -130,20 +125,6 @@ public final class MenuItemImpl implements MenuItem { MenuItemImpl(MenuBuilder menu, int group, int id, int categoryOrder, int ordering, CharSequence title, int showAsAction) { - String lang = menu.getContext().getResources().getConfiguration().locale.toString(); - if (sPrependShortcutLabel == null || !lang.equals(sLanguage)) { - sLanguage = lang; - // This is instantiated from the UI thread, so no chance of sync issues - sPrependShortcutLabel = menu.getContext().getResources().getString( - com.android.internal.R.string.prepend_shortcut_label); - sEnterShortcutLabel = menu.getContext().getResources().getString( - com.android.internal.R.string.menu_enter_shortcut_label); - sDeleteShortcutLabel = menu.getContext().getResources().getString( - com.android.internal.R.string.menu_delete_shortcut_label); - sSpaceShortcutLabel = menu.getContext().getResources().getString( - com.android.internal.R.string.menu_space_shortcut_label); - } - mMenu = menu; mId = id; mGroup = group; @@ -353,19 +334,45 @@ public final class MenuItemImpl implements MenuItem { return ""; } - StringBuilder sb = new StringBuilder(sPrependShortcutLabel); + final Resources res = mMenu.getContext().getResources(); + + StringBuilder sb = new StringBuilder(); + if (ViewConfiguration.get(mMenu.getContext()).hasPermanentMenuKey()) { + // Only prepend "Menu+" if there is a hardware menu key. + sb.append(res.getString( + com.android.internal.R.string.prepend_shortcut_label)); + } + + final int modifiers = + mMenu.isQwertyMode() ? mShortcutAlphabeticModifiers : mShortcutNumericModifiers; + appendModifier(sb, modifiers, KeyEvent.META_META_ON, res.getString( + com.android.internal.R.string.menu_meta_shortcut_label)); + appendModifier(sb, modifiers, KeyEvent.META_CTRL_ON, res.getString( + com.android.internal.R.string.menu_ctrl_shortcut_label)); + appendModifier(sb, modifiers, KeyEvent.META_ALT_ON, res.getString( + com.android.internal.R.string.menu_alt_shortcut_label)); + appendModifier(sb, modifiers, KeyEvent.META_SHIFT_ON, res.getString( + com.android.internal.R.string.menu_shift_shortcut_label)); + appendModifier(sb, modifiers, KeyEvent.META_SYM_ON, res.getString( + com.android.internal.R.string.menu_sym_shortcut_label)); + appendModifier(sb, modifiers, KeyEvent.META_FUNCTION_ON, res.getString( + com.android.internal.R.string.menu_function_shortcut_label)); + switch (shortcut) { case '\n': - sb.append(sEnterShortcutLabel); + sb.append(res.getString( + com.android.internal.R.string.menu_enter_shortcut_label)); break; case '\b': - sb.append(sDeleteShortcutLabel); + sb.append(res.getString( + com.android.internal.R.string.menu_delete_shortcut_label)); break; case ' ': - sb.append(sSpaceShortcutLabel); + sb.append(res.getString( + com.android.internal.R.string.menu_space_shortcut_label)); break; default: @@ -376,6 +383,12 @@ public final class MenuItemImpl implements MenuItem { return sb.toString(); } + private static void appendModifier(StringBuilder sb, int mask, int modifier, String label) { + if ((mask & modifier) == modifier) { + sb.append(label); + } + } + /** * @return Whether this menu item should be showing shortcuts (depends on * whether the menu should show shortcuts and whether this item has diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index dc937e63c11e5..20cc44d55b6ff 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -2447,6 +2447,18 @@ More Menu+ + + Meta+ + + Ctrl+ + + Alt+ + + Shift+ + + Sym+ + + Function+ space diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 97116f54e7c96..30193a81d7785 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -528,9 +528,15 @@ + + + + + +