Skip to content

Commit

Permalink
Added keyboard shortcuts to Chrome.
Browse files Browse the repository at this point in the history
When someone presses Meta + / in chrome on Android Nougat or higher they
will see additional shortcuts on top of the system shortcuts. This is a new method into the KeyboardShortcuts.java that provides the groups of shortcuts possible.

We override the onProvideKeyboardShortcuts method in ChromeTabbedActivity so it can be used wherever there is a chrome tab.

BUG=613619

Review-Url: https://codereview.chromium.org/2518153002
Cr-Commit-Position: refs/heads/master@{#434559}
  • Loading branch information
jwanda authored and Commit bot committed Nov 26, 2016
1 parent bb0113e commit e7146f6
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import android.text.TextUtils;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.KeyboardShortcutGroup;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
Expand Down Expand Up @@ -1475,6 +1477,12 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
|| super.onKeyDown(keyCode, event);
}

@Override
public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, Menu menu,
int deviceId) {
data.addAll(KeyboardShortcuts.createShortcutGroup(this));
}

@VisibleForTesting
public View getTabsView() {
return getCompositorViewHolder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

package org.chromium.chrome.browser;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.KeyEvent;
import android.view.KeyboardShortcutGroup;
import android.view.KeyboardShortcutInfo;

import org.chromium.base.annotations.SuppressFBWarnings;
import org.chromium.chrome.R;
Expand All @@ -13,6 +18,9 @@
import org.chromium.chrome.browser.tabmodel.TabModelUtils;
import org.chromium.content.browser.ContentViewCore;

import java.util.ArrayList;
import java.util.List;

/**
* Implements app-level keyboard shortcuts for ChromeTabbedActivity and DocumentActivity.
*/
Expand Down Expand Up @@ -93,6 +101,85 @@ public static Boolean dispatchKeyEvent(KeyEvent event, ChromeActivity activity,
return null;
}

/**
* This method should be called when overriding from
* {@link android.app.Activity#onProvideKeyboardShortcuts(List, android.view.Menu, int)}
* in an activity. It will return a list of the possible shortcuts. If
* someone adds a shortcut they also need to add an explanation in the
* appropriate group in this method so the user can see it when this method
* is called.
*
* @param context We need an activity so we can call the strings from our
* resource.
* @return a list of shortcuts organized into groups.
*/
@TargetApi(Build.VERSION_CODES.N)
public static List<KeyboardShortcutGroup> createShortcutGroup(Context context) {

final int ctrlShift = KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON;

List<KeyboardShortcutGroup> shortcutGroups = new ArrayList<>();

KeyboardShortcutGroup tabShortcutGroup = new KeyboardShortcutGroup(
context.getString(R.string.keyboard_shortcut_tab_group_header));
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_open_new_tab,
KeyEvent.KEYCODE_N, KeyEvent.META_CTRL_ON);
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_reopen_new_tab,
KeyEvent.KEYCODE_T, ctrlShift);
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_new_incognito_tab,
KeyEvent.KEYCODE_N, ctrlShift);
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_next_tab,
KeyEvent.KEYCODE_TAB, KeyEvent.META_CTRL_ON);
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_prev_tab,
KeyEvent.KEYCODE_TAB, ctrlShift);
addShortcut(context, tabShortcutGroup, R.string.keyboard_shortcut_close_tab,
KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON);
shortcutGroups.add(tabShortcutGroup);

KeyboardShortcutGroup chromeFeatureShortcutGroup = new KeyboardShortcutGroup(
context.getString(R.string.keyboard_shortcut_chrome_feature_group_header));
addShortcut(context, chromeFeatureShortcutGroup, R.string.keyboard_shortcut_open_menu,
KeyEvent.KEYCODE_E, KeyEvent.META_ALT_ON);
addShortcut(context, chromeFeatureShortcutGroup,
R.string.keyboard_shortcut_bookmark_manager, KeyEvent.KEYCODE_B, ctrlShift);
addShortcut(context, chromeFeatureShortcutGroup, R.string.keyboard_shortcut_history_manager,
KeyEvent.KEYCODE_H, KeyEvent.META_CTRL_ON);
addShortcut(context, chromeFeatureShortcutGroup, R.string.keyboard_shortcut_find_bar,
KeyEvent.KEYCODE_F, KeyEvent.META_CTRL_ON);
addShortcut(context, chromeFeatureShortcutGroup, R.string.keyboard_shortcut_address_bar,
KeyEvent.KEYCODE_L, KeyEvent.META_CTRL_ON);
shortcutGroups.add(chromeFeatureShortcutGroup);

KeyboardShortcutGroup webpageShortcutGroup = new KeyboardShortcutGroup(
context.getString(R.string.keyboard_shortcut_webpage_group_header));
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_print_page,
KeyEvent.KEYCODE_P, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_reload_page,
KeyEvent.KEYCODE_R, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_reload_no_cache,
KeyEvent.KEYCODE_R, ctrlShift);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_bookmark_page,
KeyEvent.KEYCODE_D, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_zoom_in,
KeyEvent.KEYCODE_PLUS, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_zoom_out,
KeyEvent.KEYCODE_MINUS, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_reset_zoom,
KeyEvent.KEYCODE_0, KeyEvent.META_CTRL_ON);
addShortcut(context, webpageShortcutGroup, R.string.keyboard_shortcut_help_center,
KeyEvent.KEYCODE_SLASH, ctrlShift);
shortcutGroups.add(webpageShortcutGroup);

return shortcutGroups;
}

@TargetApi(Build.VERSION_CODES.N)
private static void addShortcut(Context context,
KeyboardShortcutGroup shortcutGroup, int resId, int keyCode, int keyModifier) {
shortcutGroup.addItem(new KeyboardShortcutInfo(context.getString(resId), keyCode,
keyModifier));
}

/**
* This should be called from the Activity's onKeyDown() to handle keyboard shortcuts.
*
Expand Down
68 changes: 68 additions & 0 deletions chrome/android/java/strings/android_chrome_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -2753,6 +2753,74 @@ You can control the Physical Web in Chrome Settings.
<message name="IDS_WEBAPK_UNKNOWN_SOURCES_SETTINGS_BUTTON" desc="Button label for opening Android Settings from dialog warning user that installation from Unknown sources is required for WebAPK experiment.">
Settings
</message>

<!-- Keyboard shortcuts in Android N-->
<message name="IDS_KEYBOARD_SHORTCUT_OPEN_NEW_TAB" desc="A text label that appears next to the keyboard shortcut to open a new tab in Chrome. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Open a new tab
</message>
<message name="IDS_KEYBOARD_SHORTCUT_REOPEN_NEW_TAB" desc="A text label that appears next to the keyboard shortcut for reopening the last tab in the chrome app. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Reopen the last closed tab
</message>
<message name="IDS_KEYBOARD_SHORTCUT_NEW_INCOGNITO_TAB" desc="A text label that appears next to a keyboard shortcut to open a new tab in incognito mode in Chrome. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Open a new tab in Incognito mode
</message>
<message name="IDS_KEYBOARD_SHORTCUT_OPEN_MENU" desc="A text label that appears next to a keyboard shortcut that opens the overflow menu in Chrome. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Open the menu
</message>
<message name="IDS_KEYBOARD_SHORTCUT_NEXT_TAB" desc="A text label that appears next to the keyboard shortcut that will move the user to the next tab in the Chrome app. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Jump to the next tab
</message>
<message name="IDS_KEYBOARD_SHORTCUT_PREV_TAB" desc="A text label that appears next to the keyboard shortcut that will move the user to the previous tab in the Chrome app. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Jump to the previous tab
</message>
<message name="IDS_KEYBOARD_SHORTCUT_CLOSE_TAB" desc="A text label that appears next to the keyboard shortcut that will close the current tab in Chrome. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Close current tab
</message>
<message name="IDS_KEYBOARD_SHORTCUT_FIND_BAR" desc="A text label that appears next to the keyboard shortcut that will open the find bar that searches what is on the screen. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Open the Find Bar
</message>
<message name="IDS_KEYBOARD_SHORTCUT_ADDRESS_BAR" desc="A text label that appears next to the keyboard that will let you focus on the address bar on the screen. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Jump to the address bar
</message>
<message name="IDS_KEYBOARD_SHORTCUT_BOOKMARK_MANAGER" desc="A text label that appears next to the keyboard shortcut that will open the bookmarks manager in Chrome. On a tablet this is a new tab, on the phone this is a new activity. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Open the bookmarks manager
</message>
<message name="IDS_KEYBOARD_SHORTCUT_BOOKMARK_PAGE" desc="A text label that appears next to the keyboard shortcut that will bookmark the page that is currently on screen. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Bookmark the current page
</message>
<message name="IDS_KEYBOARD_SHORTCUT_HISTORY_MANAGER" desc="A text label that appears next to the keyboard shorcut that will open up the history page. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Open the history page
</message>
<message name="IDS_KEYBOARD_SHORTCUT_PRINT_PAGE" desc="A text label that appears next to the keyboard shortcut that will open the print page. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Open options to print page
</message>
<message name="IDS_KEYBOARD_SHORTCUT_ZOOM_IN" desc="A text label that appears next to the keyboard shortcut that will increase everything on the page. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Make everything on the page bigger
</message>
<message name="IDS_KEYBOARD_SHORTCUT_ZOOM_OUT" desc="A text label that appears next to the keyboard shortcut that will decrease everything on the page. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Make everything on the page smaller
</message>
<message name="IDS_KEYBOARD_SHORTCUT_RESET_ZOOM" desc="A text label that appears next to the keyboard shortcut that will reset the zoom back to the original size. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Return everything on the page to default size
</message>
<message name="IDS_KEYBOARD_SHORTCUT_RELOAD_PAGE" desc="A text label that appears next to the keyboard shortcut that will reload the current page. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Reload the current page
</message>
<message name="IDS_KEYBOARD_SHORTCUT_RELOAD_NO_CACHE" desc="A text label that appears next to the keyboard shortcut that will reload the current page without a cache. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Reload the current page, ignoring cached content
</message>
<message name="IDS_KEYBOARD_SHORTCUT_HELP_CENTER" desc="A text label that appears next to the keyboard shortcut that will open the Google Chrome Help Center in a new tab. The shortcut description is shown in a system dialog along with all other supported shortcuts. [CHAR-LIMIT=50]">
Open the Chrome Help Center in a new tab
</message>
<message name="IDS_KEYBOARD_SHORTCUT_TAB_GROUP_HEADER" desc="A text label that appears above a list of shortcuts that are related to the tab window. This group is part of several groups of keyboard shortcuts all shown in a dialog.">
Tab and window shortcuts
</message>
<message name="IDS_KEYBOARD_SHORTCUT_CHROME_FEATURE_GROUP_HEADER" desc="A text label that appears above a list of shortcuts that are related to the Chrome app features. This group is part of several groups of keyboard shortcuts all shown in a dialog.">
Google Chrome feature shortcuts
</message>
<message name="IDS_KEYBOARD_SHORTCUT_WEBPAGE_GROUP_HEADER" desc="A text label that appears above a list of shortcuts that are related to manipulation of the current tab window. This group is part of several groups of keyboard shortcuts all shown in a dialog.">
Webpage shortcuts
</message>
</messages>
</release>
</grit>

0 comments on commit e7146f6

Please sign in to comment.