The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jorim Jaggi | b10e33f | 2015-02-04 21:57:40 +0100 | [diff] [blame] | 17 | package com.android.server.policy; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 18 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 19 | import android.content.ComponentName; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 22 | import android.content.pm.ActivityInfo; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.res.XmlResourceParser; |
| 25 | import android.text.TextUtils; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | import android.util.SparseArray; |
| 28 | import android.view.KeyCharacterMap; |
Jiaquan He | e7d716d | 2016-01-13 15:46:52 -0800 | [diff] [blame] | 29 | import android.view.KeyEvent; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 30 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 31 | import com.android.internal.util.XmlUtils; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 32 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 33 | import org.xmlpull.v1.XmlPullParser; |
| 34 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 35 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 36 | import java.io.IOException; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Manages quick launch shortcuts by: |
| 40 | * <li> Keeping the local copy in sync with the database (this is an observer) |
| 41 | * <li> Returning a shortcut-matching intent to clients |
| 42 | */ |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 43 | class ShortcutManager { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 44 | private static final String TAG = "ShortcutManager"; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 45 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 46 | private static final String TAG_BOOKMARKS = "bookmarks"; |
| 47 | private static final String TAG_BOOKMARK = "bookmark"; |
| 48 | |
| 49 | private static final String ATTRIBUTE_PACKAGE = "package"; |
| 50 | private static final String ATTRIBUTE_CLASS = "class"; |
| 51 | private static final String ATTRIBUTE_SHORTCUT = "shortcut"; |
| 52 | private static final String ATTRIBUTE_CATEGORY = "category"; |
Jiaquan He | e7d716d | 2016-01-13 15:46:52 -0800 | [diff] [blame] | 53 | private static final String ATTRIBUTE_SHIFT = "shift"; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 54 | |
| 55 | private final SparseArray<ShortcutInfo> mShortcuts = new SparseArray<>(); |
Jiaquan He | e7d716d | 2016-01-13 15:46:52 -0800 | [diff] [blame] | 56 | private final SparseArray<ShortcutInfo> mShiftShortcuts = new SparseArray<>(); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 57 | |
| 58 | private final Context mContext; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 59 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 60 | public ShortcutManager(Context context) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 61 | mContext = context; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 62 | loadShortcuts(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 63 | } |
| 64 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Gets the shortcut intent for a given keycode+modifier. Make sure you |
| 67 | * strip whatever modifier is used for invoking shortcuts (for example, |
| 68 | * if 'Sym+A' should invoke a shortcut on 'A', you should strip the |
| 69 | * 'Sym' bit from the modifiers before calling this method. |
| 70 | * <p> |
| 71 | * This will first try an exact match (with modifiers), and then try a |
| 72 | * match without modifiers (primary character on a key). |
| 73 | * |
Jeff Brown | c1fb48d | 2010-12-08 16:52:09 -0800 | [diff] [blame] | 74 | * @param kcm The key character map of the device on which the key was pressed. |
| 75 | * @param keyCode The key code. |
| 76 | * @param metaState The meta state, omitting any modifiers that were used |
| 77 | * to invoke the shortcut. |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 78 | * @return The intent that matches the shortcut, or null if not found. |
| 79 | */ |
Jeff Brown | c1fb48d | 2010-12-08 16:52:09 -0800 | [diff] [blame] | 80 | public Intent getIntent(KeyCharacterMap kcm, int keyCode, int metaState) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 81 | ShortcutInfo shortcut = null; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 82 | |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 83 | // If the Shift key is pressed, then search for the shift shortcuts. |
Jiaquan He | e7d716d | 2016-01-13 15:46:52 -0800 | [diff] [blame] | 84 | boolean isShiftOn = (metaState & KeyEvent.META_SHIFT_ON) == KeyEvent.META_SHIFT_ON; |
| 85 | SparseArray<ShortcutInfo> shortcutMap = isShiftOn ? mShiftShortcuts : mShortcuts; |
| 86 | |
Jeff Brown | c1fb48d | 2010-12-08 16:52:09 -0800 | [diff] [blame] | 87 | // First try the exact keycode (with modifiers). |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 88 | int shortcutChar = kcm.get(keyCode, metaState); |
| 89 | if (shortcutChar != 0) { |
Jiaquan He | e7d716d | 2016-01-13 15:46:52 -0800 | [diff] [blame] | 90 | shortcut = shortcutMap.get(shortcutChar); |
Jeff Brown | c1fb48d | 2010-12-08 16:52:09 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Next try the primary character on that key. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 94 | if (shortcut == null) { |
| 95 | shortcutChar = Character.toLowerCase(kcm.getDisplayLabel(keyCode)); |
| 96 | if (shortcutChar != 0) { |
Jiaquan He | e7d716d | 2016-01-13 15:46:52 -0800 | [diff] [blame] | 97 | shortcut = shortcutMap.get(shortcutChar); |
Jeff Brown | c1fb48d | 2010-12-08 16:52:09 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 101 | return (shortcut != null) ? shortcut.intent : null; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 104 | private void loadShortcuts() { |
| 105 | PackageManager packageManager = mContext.getPackageManager(); |
| 106 | try { |
| 107 | XmlResourceParser parser = mContext.getResources().getXml( |
| 108 | com.android.internal.R.xml.bookmarks); |
| 109 | XmlUtils.beginDocument(parser, TAG_BOOKMARKS); |
| 110 | |
| 111 | while (true) { |
| 112 | XmlUtils.nextElement(parser); |
| 113 | |
| 114 | if (parser.getEventType() == XmlPullParser.END_DOCUMENT) { |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | if (!TAG_BOOKMARK.equals(parser.getName())) { |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | String packageName = parser.getAttributeValue(null, ATTRIBUTE_PACKAGE); |
| 123 | String className = parser.getAttributeValue(null, ATTRIBUTE_CLASS); |
| 124 | String shortcutName = parser.getAttributeValue(null, ATTRIBUTE_SHORTCUT); |
| 125 | String categoryName = parser.getAttributeValue(null, ATTRIBUTE_CATEGORY); |
Jiaquan He | e7d716d | 2016-01-13 15:46:52 -0800 | [diff] [blame] | 126 | String shiftName = parser.getAttributeValue(null, ATTRIBUTE_SHIFT); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 127 | |
| 128 | if (TextUtils.isEmpty(shortcutName)) { |
| 129 | Log.w(TAG, "Unable to get shortcut for: " + packageName + "/" + className); |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | final int shortcutChar = shortcutName.charAt(0); |
Jiaquan He | e7d716d | 2016-01-13 15:46:52 -0800 | [diff] [blame] | 134 | final boolean isShiftShortcut = (shiftName != null && shiftName.equals("true")); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 135 | |
| 136 | final Intent intent; |
| 137 | final String title; |
| 138 | if (packageName != null && className != null) { |
| 139 | ActivityInfo info = null; |
| 140 | ComponentName componentName = new ComponentName(packageName, className); |
| 141 | try { |
Jeff Sharkey | 2a9e3f8 | 2015-12-18 10:57:58 -0700 | [diff] [blame] | 142 | info = packageManager.getActivityInfo(componentName, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 143 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 144 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 145 | | PackageManager.MATCH_UNINSTALLED_PACKAGES); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 146 | } catch (PackageManager.NameNotFoundException e) { |
| 147 | String[] packages = packageManager.canonicalToCurrentPackageNames( |
| 148 | new String[] { packageName }); |
| 149 | componentName = new ComponentName(packages[0], className); |
| 150 | try { |
Jeff Sharkey | 2a9e3f8 | 2015-12-18 10:57:58 -0700 | [diff] [blame] | 151 | info = packageManager.getActivityInfo(componentName, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 152 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 153 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 154 | | PackageManager.MATCH_UNINSTALLED_PACKAGES); |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 155 | } catch (PackageManager.NameNotFoundException e1) { |
| 156 | Log.w(TAG, "Unable to add bookmark: " + packageName |
| 157 | + "/" + className, e); |
| 158 | continue; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | intent = new Intent(Intent.ACTION_MAIN); |
| 163 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 164 | intent.setComponent(componentName); |
| 165 | title = info.loadLabel(packageManager).toString(); |
| 166 | } else if (categoryName != null) { |
| 167 | intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, categoryName); |
| 168 | title = ""; |
| 169 | } else { |
| 170 | Log.w(TAG, "Unable to add bookmark for shortcut " + shortcutName |
| 171 | + ": missing package/class or category attributes"); |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | ShortcutInfo shortcut = new ShortcutInfo(title, intent); |
Jiaquan He | e7d716d | 2016-01-13 15:46:52 -0800 | [diff] [blame] | 176 | if (isShiftShortcut) { |
| 177 | mShiftShortcuts.put(shortcutChar, shortcut); |
| 178 | } else { |
| 179 | mShortcuts.put(shortcutChar, shortcut); |
| 180 | } |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 181 | } |
| 182 | } catch (XmlPullParserException e) { |
| 183 | Log.w(TAG, "Got exception parsing bookmarks.", e); |
| 184 | } catch (IOException e) { |
| 185 | Log.w(TAG, "Got exception parsing bookmarks.", e); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | private static final class ShortcutInfo { |
| 190 | public final String title; |
| 191 | public final Intent intent; |
| 192 | |
| 193 | public ShortcutInfo(String title, Intent intent) { |
| 194 | this.title = title; |
| 195 | this.intent = intent; |
| 196 | } |
| 197 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 198 | } |