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; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 29 | import com.android.internal.util.XmlUtils; |
| 30 | import org.xmlpull.v1.XmlPullParser; |
| 31 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 32 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 33 | import java.io.IOException; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Manages quick launch shortcuts by: |
| 37 | * <li> Keeping the local copy in sync with the database (this is an observer) |
| 38 | * <li> Returning a shortcut-matching intent to clients |
| 39 | */ |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 40 | class ShortcutManager { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 41 | private static final String TAG = "ShortcutManager"; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 42 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 43 | private static final String TAG_BOOKMARKS = "bookmarks"; |
| 44 | private static final String TAG_BOOKMARK = "bookmark"; |
| 45 | |
| 46 | private static final String ATTRIBUTE_PACKAGE = "package"; |
| 47 | private static final String ATTRIBUTE_CLASS = "class"; |
| 48 | private static final String ATTRIBUTE_SHORTCUT = "shortcut"; |
| 49 | private static final String ATTRIBUTE_CATEGORY = "category"; |
| 50 | |
| 51 | private final SparseArray<ShortcutInfo> mShortcuts = new SparseArray<>(); |
| 52 | |
| 53 | private final Context mContext; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 54 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 55 | public ShortcutManager(Context context) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 56 | mContext = context; |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 57 | loadShortcuts(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 58 | } |
| 59 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Gets the shortcut intent for a given keycode+modifier. Make sure you |
| 62 | * strip whatever modifier is used for invoking shortcuts (for example, |
| 63 | * if 'Sym+A' should invoke a shortcut on 'A', you should strip the |
| 64 | * 'Sym' bit from the modifiers before calling this method. |
| 65 | * <p> |
| 66 | * This will first try an exact match (with modifiers), and then try a |
| 67 | * match without modifiers (primary character on a key). |
| 68 | * |
Jeff Brown | c1fb48d | 2010-12-08 16:52:09 -0800 | [diff] [blame] | 69 | * @param kcm The key character map of the device on which the key was pressed. |
| 70 | * @param keyCode The key code. |
| 71 | * @param metaState The meta state, omitting any modifiers that were used |
| 72 | * to invoke the shortcut. |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 73 | * @return The intent that matches the shortcut, or null if not found. |
| 74 | */ |
Jeff Brown | c1fb48d | 2010-12-08 16:52:09 -0800 | [diff] [blame] | 75 | public Intent getIntent(KeyCharacterMap kcm, int keyCode, int metaState) { |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 76 | ShortcutInfo shortcut = null; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 77 | |
Jeff Brown | c1fb48d | 2010-12-08 16:52:09 -0800 | [diff] [blame] | 78 | // First try the exact keycode (with modifiers). |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 79 | int shortcutChar = kcm.get(keyCode, metaState); |
| 80 | if (shortcutChar != 0) { |
| 81 | shortcut = mShortcuts.get(shortcutChar); |
Jeff Brown | c1fb48d | 2010-12-08 16:52:09 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Next try the primary character on that key. |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 85 | if (shortcut == null) { |
| 86 | shortcutChar = Character.toLowerCase(kcm.getDisplayLabel(keyCode)); |
| 87 | if (shortcutChar != 0) { |
| 88 | shortcut = mShortcuts.get(shortcutChar); |
Jeff Brown | c1fb48d | 2010-12-08 16:52:09 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 92 | return (shortcut != null) ? shortcut.intent : null; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Svetoslav | 683914b | 2015-01-15 14:22:26 -0800 | [diff] [blame] | 95 | private void loadShortcuts() { |
| 96 | PackageManager packageManager = mContext.getPackageManager(); |
| 97 | try { |
| 98 | XmlResourceParser parser = mContext.getResources().getXml( |
| 99 | com.android.internal.R.xml.bookmarks); |
| 100 | XmlUtils.beginDocument(parser, TAG_BOOKMARKS); |
| 101 | |
| 102 | while (true) { |
| 103 | XmlUtils.nextElement(parser); |
| 104 | |
| 105 | if (parser.getEventType() == XmlPullParser.END_DOCUMENT) { |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | if (!TAG_BOOKMARK.equals(parser.getName())) { |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | String packageName = parser.getAttributeValue(null, ATTRIBUTE_PACKAGE); |
| 114 | String className = parser.getAttributeValue(null, ATTRIBUTE_CLASS); |
| 115 | String shortcutName = parser.getAttributeValue(null, ATTRIBUTE_SHORTCUT); |
| 116 | String categoryName = parser.getAttributeValue(null, ATTRIBUTE_CATEGORY); |
| 117 | |
| 118 | if (TextUtils.isEmpty(shortcutName)) { |
| 119 | Log.w(TAG, "Unable to get shortcut for: " + packageName + "/" + className); |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | final int shortcutChar = shortcutName.charAt(0); |
| 124 | |
| 125 | final Intent intent; |
| 126 | final String title; |
| 127 | if (packageName != null && className != null) { |
| 128 | ActivityInfo info = null; |
| 129 | ComponentName componentName = new ComponentName(packageName, className); |
| 130 | try { |
| 131 | info = packageManager.getActivityInfo(componentName, 0); |
| 132 | } catch (PackageManager.NameNotFoundException e) { |
| 133 | String[] packages = packageManager.canonicalToCurrentPackageNames( |
| 134 | new String[] { packageName }); |
| 135 | componentName = new ComponentName(packages[0], className); |
| 136 | try { |
| 137 | info = packageManager.getActivityInfo(componentName, 0); |
| 138 | } catch (PackageManager.NameNotFoundException e1) { |
| 139 | Log.w(TAG, "Unable to add bookmark: " + packageName |
| 140 | + "/" + className, e); |
| 141 | continue; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | intent = new Intent(Intent.ACTION_MAIN); |
| 146 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 147 | intent.setComponent(componentName); |
| 148 | title = info.loadLabel(packageManager).toString(); |
| 149 | } else if (categoryName != null) { |
| 150 | intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, categoryName); |
| 151 | title = ""; |
| 152 | } else { |
| 153 | Log.w(TAG, "Unable to add bookmark for shortcut " + shortcutName |
| 154 | + ": missing package/class or category attributes"); |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | ShortcutInfo shortcut = new ShortcutInfo(title, intent); |
| 159 | mShortcuts.put(shortcutChar, shortcut); |
| 160 | } |
| 161 | } catch (XmlPullParserException e) { |
| 162 | Log.w(TAG, "Got exception parsing bookmarks.", e); |
| 163 | } catch (IOException e) { |
| 164 | Log.w(TAG, "Got exception parsing bookmarks.", e); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private static final class ShortcutInfo { |
| 169 | public final String title; |
| 170 | public final Intent intent; |
| 171 | |
| 172 | public ShortcutInfo(String title, Intent intent) { |
| 173 | this.title = title; |
| 174 | this.intent = intent; |
| 175 | } |
| 176 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 177 | } |