Add support for shortcuts with the shift key.

Now shortcuts with the shift key can be defined in
frameworks/base/packages/SettingsProvider/res/xml/bookmarks.xml.

Sample:

<bookmark
    package="package"
    class="package.Class"
    shift="true"
    shortcut="z"  />

Bug 26540880

Change-Id: Ib6d6ea59124226d4e4eb8ec9972059f2d71bd77e
(cherry picked from commit e7319ecbbe865db8884914ceb10f526f4e160a64)
This commit is contained in:
Jiaquan He
2016-01-13 15:46:52 -08:00
parent cc1251b9b2
commit e7d716d5e3

View File

@@ -26,6 +26,7 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import com.android.internal.util.XmlUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@@ -47,8 +48,10 @@ class ShortcutManager {
private static final String ATTRIBUTE_CLASS = "class";
private static final String ATTRIBUTE_SHORTCUT = "shortcut";
private static final String ATTRIBUTE_CATEGORY = "category";
private static final String ATTRIBUTE_SHIFT = "shift";
private final SparseArray<ShortcutInfo> mShortcuts = new SparseArray<>();
private final SparseArray<ShortcutInfo> mShiftShortcuts = new SparseArray<>();
private final Context mContext;
@@ -75,17 +78,21 @@ class ShortcutManager {
public Intent getIntent(KeyCharacterMap kcm, int keyCode, int metaState) {
ShortcutInfo shortcut = null;
// If the Shift key is preesed, then search for the shift shortcuts.
boolean isShiftOn = (metaState & KeyEvent.META_SHIFT_ON) == KeyEvent.META_SHIFT_ON;
SparseArray<ShortcutInfo> shortcutMap = isShiftOn ? mShiftShortcuts : mShortcuts;
// First try the exact keycode (with modifiers).
int shortcutChar = kcm.get(keyCode, metaState);
if (shortcutChar != 0) {
shortcut = mShortcuts.get(shortcutChar);
shortcut = shortcutMap.get(shortcutChar);
}
// Next try the primary character on that key.
if (shortcut == null) {
shortcutChar = Character.toLowerCase(kcm.getDisplayLabel(keyCode));
if (shortcutChar != 0) {
shortcut = mShortcuts.get(shortcutChar);
shortcut = shortcutMap.get(shortcutChar);
}
}
@@ -114,6 +121,7 @@ class ShortcutManager {
String className = parser.getAttributeValue(null, ATTRIBUTE_CLASS);
String shortcutName = parser.getAttributeValue(null, ATTRIBUTE_SHORTCUT);
String categoryName = parser.getAttributeValue(null, ATTRIBUTE_CATEGORY);
String shiftName = parser.getAttributeValue(null, ATTRIBUTE_SHIFT);
if (TextUtils.isEmpty(shortcutName)) {
Log.w(TAG, "Unable to get shortcut for: " + packageName + "/" + className);
@@ -121,6 +129,7 @@ class ShortcutManager {
}
final int shortcutChar = shortcutName.charAt(0);
final boolean isShiftShortcut = (shiftName != null && shiftName.equals("true"));
final Intent intent;
final String title;
@@ -158,7 +167,11 @@ class ShortcutManager {
}
ShortcutInfo shortcut = new ShortcutInfo(title, intent);
mShortcuts.put(shortcutChar, shortcut);
if (isShiftShortcut) {
mShiftShortcuts.put(shortcutChar, shortcut);
} else {
mShortcuts.put(shortcutChar, shortcut);
}
}
} catch (XmlPullParserException e) {
Log.w(TAG, "Got exception parsing bookmarks.", e);