Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0cc2c6b1 authored by Vladislav Kaznacheev's avatar Vladislav Kaznacheev
Browse files

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
parent 2102bf63
Loading
Loading
Loading
Loading
+38 −25
Original line number Diff line number Diff line
@@ -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
+12 −0
Original line number Diff line number Diff line
@@ -2429,6 +2429,18 @@
    <string name="more_item_label">More</string>
    <!-- Prepended to the shortcut for a menu item to indicate that the user should hold the MENU button together with the shortcut to invoke the item. For example, if the shortcut to open a new tab in browser is MENU and B together, then this would be prepended to the letter "B" -->
    <string name="prepend_shortcut_label">Menu+</string>
    <!-- Prepended to the shortcut for a menu item to indicate that the user should hold the META key together with the shortcut to invoke the item. -->
    <string name="menu_meta_shortcut_label">Meta+</string>
    <!-- Prepended to the shortcut for a menu item to indicate that the user should hold the CTRL key together with the shortcut to invoke the item. -->
    <string name="menu_ctrl_shortcut_label">Ctrl+</string>
    <!-- Prepended to the shortcut for a menu item to indicate that the user should hold the ALT key together with the shortcut to invoke the item. -->
    <string name="menu_alt_shortcut_label">Alt+</string>
    <!-- Prepended to the shortcut for a menu item to indicate that the user should hold the SHIFT key together with the shortcut to invoke the item. -->
    <string name="menu_shift_shortcut_label">Shift+</string>
    <!-- Prepended to the shortcut for a menu item to indicate that the user should hold the SYM key together with the shortcut to invoke the item. -->
    <string name="menu_sym_shortcut_label">Sym+</string>
    <!-- Prepended to the shortcut for a menu item to indicate that the user should hold the FUNCTION key together with the shortcut to invoke the item. -->
    <string name="menu_function_shortcut_label">Function+</string>
    <!-- Displayed in place of the regular shortcut letter when a menu item has Menu+space for the shortcut. -->
    <string name="menu_space_shortcut_label">space</string>
    <!-- Displayed in place of the regular shortcut letter when a menu item has Menu+enter for the shortcut. -->
+6 −0
Original line number Diff line number Diff line
@@ -522,9 +522,15 @@
  <java-symbol type="string" name="delete" />
  <java-symbol type="string" name="deleteText" />
  <java-symbol type="string" name="grant_permissions_header_text" />
  <java-symbol type="string" name="menu_alt_shortcut_label" />
  <java-symbol type="string" name="menu_ctrl_shortcut_label" />
  <java-symbol type="string" name="menu_delete_shortcut_label" />
  <java-symbol type="string" name="menu_enter_shortcut_label" />
  <java-symbol type="string" name="menu_function_shortcut_label" />
  <java-symbol type="string" name="menu_meta_shortcut_label" />
  <java-symbol type="string" name="menu_space_shortcut_label" />
  <java-symbol type="string" name="menu_shift_shortcut_label" />
  <java-symbol type="string" name="menu_sym_shortcut_label" />
  <java-symbol type="string" name="notification_title" />
  <java-symbol type="string" name="permission_request_notification_with_subtitle" />
  <java-symbol type="string" name="prepend_shortcut_label" />