Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.server.pm; |
| 17 | |
| 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 20 | import android.annotation.UserIdInt; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Intent; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 23 | import android.content.pm.PackageInfo; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 24 | import android.content.pm.ShortcutInfo; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 26 | import android.os.PersistableBundle; |
| 27 | import android.text.format.Formatter; |
| 28 | import android.util.ArrayMap; |
| 29 | import android.util.ArraySet; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 31 | import android.util.Slog; |
| 32 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 33 | import com.android.internal.annotations.VisibleForTesting; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 34 | import com.android.internal.util.Preconditions; |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 35 | import com.android.internal.util.XmlUtils; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 36 | import com.android.server.pm.ShortcutService.ShortcutOperation; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 37 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 38 | import org.xmlpull.v1.XmlPullParser; |
| 39 | import org.xmlpull.v1.XmlPullParserException; |
| 40 | import org.xmlpull.v1.XmlSerializer; |
| 41 | |
| 42 | import java.io.File; |
| 43 | import java.io.IOException; |
| 44 | import java.io.PrintWriter; |
| 45 | import java.util.ArrayList; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 46 | import java.util.Collections; |
| 47 | import java.util.Comparator; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 48 | import java.util.List; |
Makoto Onuki | be73a80 | 2016-04-15 14:46:35 -0700 | [diff] [blame] | 49 | import java.util.Set; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 50 | import java.util.function.Predicate; |
| 51 | |
| 52 | /** |
| 53 | * Package information used by {@link ShortcutService}. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 54 | * User information used by {@link ShortcutService}. |
| 55 | * |
| 56 | * All methods should be guarded by {@code #mShortcutUser.mService.mLock}. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 57 | */ |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 58 | class ShortcutPackage extends ShortcutPackageItem { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 59 | private static final String TAG = ShortcutService.TAG; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 60 | private static final String TAG_VERIFY = ShortcutService.TAG + ".verify"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 61 | |
| 62 | static final String TAG_ROOT = "package"; |
| 63 | private static final String TAG_INTENT_EXTRAS = "intent-extras"; |
| 64 | private static final String TAG_EXTRAS = "extras"; |
| 65 | private static final String TAG_SHORTCUT = "shortcut"; |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 66 | private static final String TAG_CATEGORIES = "categories"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 67 | |
| 68 | private static final String ATTR_NAME = "name"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 69 | private static final String ATTR_CALL_COUNT = "call-count"; |
| 70 | private static final String ATTR_LAST_RESET = "last-reset"; |
| 71 | private static final String ATTR_ID = "id"; |
| 72 | private static final String ATTR_ACTIVITY = "activity"; |
| 73 | private static final String ATTR_TITLE = "title"; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 74 | private static final String ATTR_TITLE_RES_ID = "titleid"; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 75 | private static final String ATTR_TITLE_RES_NAME = "titlename"; |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 76 | private static final String ATTR_TEXT = "text"; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 77 | private static final String ATTR_TEXT_RES_ID = "textid"; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 78 | private static final String ATTR_TEXT_RES_NAME = "textname"; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 79 | private static final String ATTR_DISABLED_MESSAGE = "dmessage"; |
| 80 | private static final String ATTR_DISABLED_MESSAGE_RES_ID = "dmessageid"; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 81 | private static final String ATTR_DISABLED_MESSAGE_RES_NAME = "dmessagename"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 82 | private static final String ATTR_INTENT = "intent"; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 83 | private static final String ATTR_RANK = "rank"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 84 | private static final String ATTR_TIMESTAMP = "timestamp"; |
| 85 | private static final String ATTR_FLAGS = "flags"; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 86 | private static final String ATTR_ICON_RES_ID = "icon-res"; |
| 87 | private static final String ATTR_ICON_RES_NAME = "icon-resname"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 88 | private static final String ATTR_BITMAP_PATH = "bitmap-path"; |
| 89 | |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 90 | private static final String NAME_CATEGORIES = "categories"; |
| 91 | |
| 92 | private static final String TAG_STRING_ARRAY_XMLUTILS = "string-array"; |
| 93 | private static final String ATTR_NAME_XMLUTILS = "name"; |
| 94 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 95 | /** |
| 96 | * All the shortcuts from the package, keyed on IDs. |
| 97 | */ |
| 98 | final private ArrayMap<String, ShortcutInfo> mShortcuts = new ArrayMap<>(); |
| 99 | |
| 100 | /** |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 101 | * # of times the package has called rate-limited APIs. |
| 102 | */ |
| 103 | private int mApiCallCount; |
| 104 | |
| 105 | /** |
| 106 | * When {@link #mApiCallCount} was reset last time. |
| 107 | */ |
| 108 | private long mLastResetTime; |
| 109 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 110 | private final int mPackageUid; |
| 111 | |
| 112 | private long mLastKnownForegroundElapsedTime; |
| 113 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 114 | private ShortcutPackage(ShortcutUser shortcutUser, |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 115 | int packageUserId, String packageName, ShortcutPackageInfo spi) { |
| 116 | super(shortcutUser, packageUserId, packageName, |
| 117 | spi != null ? spi : ShortcutPackageInfo.newEmpty()); |
| 118 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 119 | mPackageUid = shortcutUser.mService.injectGetPackageUid(packageName, packageUserId); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 122 | public ShortcutPackage(ShortcutUser shortcutUser, int packageUserId, String packageName) { |
| 123 | this(shortcutUser, packageUserId, packageName, null); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public int getOwnerUserId() { |
| 128 | // For packages, always owner user == package user. |
| 129 | return getPackageUserId(); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 132 | public int getPackageUid() { |
| 133 | return mPackageUid; |
| 134 | } |
| 135 | |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 136 | /** |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 137 | * Called when a shortcut is about to be published. At this point we know the publisher |
| 138 | * package |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 139 | * exists (as opposed to Launcher trying to fetch shortcuts from a non-existent package), so |
| 140 | * we do some initialization for the package. |
| 141 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 142 | private void ensurePackageVersionInfo() { |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 143 | // Make sure we have the version code for the app. We need the version code in |
| 144 | // handlePackageUpdated(). |
| 145 | if (getPackageInfo().getVersionCode() < 0) { |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 146 | final ShortcutService s = mShortcutUser.mService; |
| 147 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 148 | final PackageInfo pi = s.getPackageInfo(getPackageName(), getOwnerUserId()); |
| 149 | if (pi != null) { |
| 150 | if (ShortcutService.DEBUG) { |
| 151 | Slog.d(TAG, String.format("Package %s version = %d", getPackageName(), |
| 152 | pi.versionCode)); |
| 153 | } |
| 154 | getPackageInfo().updateVersionInfo(pi); |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 155 | s.scheduleSaveUser(getOwnerUserId()); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 160 | @Nullable |
| 161 | public Resources getPackageResources() { |
| 162 | return mShortcutUser.mService.injectGetResourcesForApplicationAsUser( |
| 163 | getPackageName(), getPackageUserId()); |
| 164 | } |
| 165 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 166 | @Override |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 167 | protected void onRestoreBlocked() { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 168 | // Can't restore due to version/signature mismatch. Remove all shortcuts. |
| 169 | mShortcuts.clear(); |
| 170 | } |
| 171 | |
| 172 | @Override |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 173 | protected void onRestored() { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 174 | // Because some launchers may not have been restored (e.g. allowBackup=false), |
| 175 | // we need to re-calculate the pinned shortcuts. |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 176 | refreshPinnedFlags(); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Note this does *not* provide a correct view to the calling launcher. |
| 181 | */ |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 182 | @Nullable |
| 183 | public ShortcutInfo findShortcutById(String id) { |
| 184 | return mShortcuts.get(id); |
| 185 | } |
| 186 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 187 | private void ensureNotImmutable(@Nullable ShortcutInfo shortcut) { |
| 188 | if (shortcut != null && shortcut.isImmutable()) { |
| 189 | throw new IllegalArgumentException( |
| 190 | "Manifest shortcut ID=" + shortcut.getId() |
| 191 | + " may not be manipulated via APIs"); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | private void ensureNotImmutable(@NonNull String id) { |
| 196 | ensureNotImmutable(mShortcuts.get(id)); |
| 197 | } |
| 198 | |
| 199 | public void ensureImmutableShortcutsNotIncludedWithIds(@NonNull List<String> shortcutIds) { |
| 200 | for (int i = shortcutIds.size() - 1; i >= 0; i--) { |
| 201 | ensureNotImmutable(shortcutIds.get(i)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | public void ensureImmutableShortcutsNotIncluded(@NonNull List<ShortcutInfo> shortcuts) { |
| 206 | for (int i = shortcuts.size() - 1; i >= 0; i--) { |
| 207 | ensureNotImmutable(shortcuts.get(i).getId()); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private ShortcutInfo deleteShortcutInner(@NonNull String id) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 212 | final ShortcutInfo shortcut = mShortcuts.remove(id); |
| 213 | if (shortcut != null) { |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 214 | mShortcutUser.mService.removeIcon(getPackageUserId(), shortcut); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 215 | shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED |
| 216 | | ShortcutInfo.FLAG_MANIFEST); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 217 | } |
| 218 | return shortcut; |
| 219 | } |
| 220 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 221 | private void addShortcutInner(@NonNull ShortcutInfo newShortcut) { |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 222 | final ShortcutService s = mShortcutUser.mService; |
| 223 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 224 | deleteShortcutInner(newShortcut.getId()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 225 | |
| 226 | // Extract Icon and update the icon res ID and the bitmap path. |
| 227 | s.saveIconAndFixUpShortcut(getPackageUserId(), newShortcut); |
| 228 | s.fixUpShortcutResourceNamesAndValues(newShortcut); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 229 | mShortcuts.put(newShortcut.getId(), newShortcut); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Add a shortcut, or update one with the same ID, with taking over existing flags. |
| 234 | * |
| 235 | * It checks the max number of dynamic shortcuts. |
| 236 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 237 | public void addOrUpdateDynamicShortcut(@NonNull ShortcutInfo newShortcut) { |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 238 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 239 | Preconditions.checkArgument(newShortcut.isEnabled(), |
| 240 | "add/setDynamicShortcuts() cannot publish disabled shortcuts"); |
| 241 | |
| 242 | ensurePackageVersionInfo(); |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 243 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 244 | newShortcut.addFlags(ShortcutInfo.FLAG_DYNAMIC); |
| 245 | |
| 246 | final ShortcutInfo oldShortcut = mShortcuts.get(newShortcut.getId()); |
| 247 | |
| 248 | final boolean wasPinned; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 249 | |
| 250 | if (oldShortcut == null) { |
| 251 | wasPinned = false; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 252 | } else { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 253 | // It's an update case. |
| 254 | // Make sure the target is updatable. (i.e. should be mutable.) |
| 255 | oldShortcut.ensureUpdatableWith(newShortcut); |
| 256 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 257 | wasPinned = oldShortcut.isPinned(); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 260 | // If it was originally pinned, the new one should be pinned too. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 261 | if (wasPinned) { |
| 262 | newShortcut.addFlags(ShortcutInfo.FLAG_PINNED); |
| 263 | } |
| 264 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 265 | addShortcutInner(newShortcut); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Remove all shortcuts that aren't pinned nor dynamic. |
| 270 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 271 | private void removeOrphans() { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 272 | ArrayList<String> removeList = null; // Lazily initialize. |
| 273 | |
| 274 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 275 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 276 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 277 | if (si.isAlive()) continue; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 278 | |
| 279 | if (removeList == null) { |
| 280 | removeList = new ArrayList<>(); |
| 281 | } |
| 282 | removeList.add(si.getId()); |
| 283 | } |
| 284 | if (removeList != null) { |
| 285 | for (int i = removeList.size() - 1; i >= 0; i--) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 286 | deleteShortcutInner(removeList.get(i)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Remove all dynamic shortcuts. |
| 293 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 294 | public void deleteAllDynamicShortcuts() { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 295 | final long now = mShortcutUser.mService.injectCurrentTimeMillis(); |
| 296 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 297 | boolean changed = false; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 298 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 299 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 300 | if (si.isDynamic()) { |
| 301 | changed = true; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 302 | |
| 303 | si.setTimestamp(now); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 304 | si.clearFlags(ShortcutInfo.FLAG_DYNAMIC); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 305 | si.setRank(0); // It may still be pinned, so clear the rank. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 306 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 307 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 308 | if (changed) { |
| 309 | removeOrphans(); |
| 310 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 314 | * Remove a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut |
| 315 | * is pinned, it'll remain as a pinned shortcut, and is still enabled. |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 316 | * |
| 317 | * @return true if it's actually removed because it wasn't pinned, or false if it's still |
| 318 | * pinned. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 319 | */ |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 320 | public boolean deleteDynamicWithId(@NonNull String shortcutId) { |
| 321 | final ShortcutInfo removed = deleteOrDisableWithId( |
| 322 | shortcutId, /* disable =*/ false, /* overrideImmutable=*/ false); |
| 323 | return removed == null; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut |
| 328 | * is pinned, it'll remain as a pinned shortcut, but will be disabled. |
| 329 | * |
| 330 | * @return true if it's actually removed because it wasn't pinned, or false if it's still |
| 331 | * pinned. |
| 332 | */ |
| 333 | private boolean disableDynamicWithId(@NonNull String shortcutId) { |
| 334 | final ShortcutInfo disabled = deleteOrDisableWithId( |
| 335 | shortcutId, /* disable =*/ true, /* overrideImmutable=*/ false); |
| 336 | return disabled == null; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 339 | /** |
| 340 | * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut |
| 341 | * is pinned, it'll remain as a pinned shortcut but will be disabled. |
| 342 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 343 | public void disableWithId(@NonNull String shortcutId, String disabledMessage, |
| 344 | int disabledMessageResId, boolean overrideImmutable) { |
| 345 | final ShortcutInfo disabled = deleteOrDisableWithId(shortcutId, /* disable =*/ true, |
| 346 | overrideImmutable); |
| 347 | |
| 348 | if (disabled != null) { |
| 349 | if (disabledMessage != null) { |
| 350 | disabled.setDisabledMessage(disabledMessage); |
| 351 | } else if (disabledMessageResId != 0) { |
| 352 | disabled.setDisabledMessageResId(disabledMessageResId); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 353 | |
| 354 | mShortcutUser.mService.fixUpShortcutResourceNamesAndValues(disabled); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | @Nullable |
| 360 | private ShortcutInfo deleteOrDisableWithId(@NonNull String shortcutId, boolean disable, |
| 361 | boolean overrideImmutable) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 362 | final ShortcutInfo oldShortcut = mShortcuts.get(shortcutId); |
| 363 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 364 | if (oldShortcut == null || !oldShortcut.isEnabled()) { |
| 365 | return null; // Doesn't exist or already disabled. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 366 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 367 | if (!overrideImmutable) { |
| 368 | ensureNotImmutable(oldShortcut); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 369 | } |
| 370 | if (oldShortcut.isPinned()) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 371 | |
| 372 | oldShortcut.setRank(0); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 373 | oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_MANIFEST); |
| 374 | if (disable) { |
| 375 | oldShortcut.addFlags(ShortcutInfo.FLAG_DISABLED); |
| 376 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 377 | oldShortcut.setTimestamp(mShortcutUser.mService.injectCurrentTimeMillis()); |
| 378 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 379 | return oldShortcut; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 380 | } else { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 381 | deleteShortcutInner(shortcutId); |
| 382 | return null; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | public void enableWithId(@NonNull String shortcutId) { |
| 387 | final ShortcutInfo shortcut = mShortcuts.get(shortcutId); |
| 388 | if (shortcut != null) { |
| 389 | ensureNotImmutable(shortcut); |
| 390 | shortcut.clearFlags(ShortcutInfo.FLAG_DISABLED); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Called after a launcher updates the pinned set. For each shortcut in this package, |
| 396 | * set FLAG_PINNED if any launcher has pinned it. Otherwise, clear it. |
| 397 | * |
| 398 | * <p>Then remove all shortcuts that are not dynamic and no longer pinned either. |
| 399 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 400 | public void refreshPinnedFlags() { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 401 | // First, un-pin all shortcuts |
| 402 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 403 | mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_PINNED); |
| 404 | } |
| 405 | |
| 406 | // Then, for the pinned set for each launcher, set the pin flag one by one. |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 407 | mShortcutUser.mService.getUserShortcutsLocked(getPackageUserId()) |
| 408 | .forAllLaunchers(launcherShortcuts -> { |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 409 | final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds( |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 410 | getPackageName(), getPackageUserId()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 411 | |
| 412 | if (pinned == null || pinned.size() == 0) { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 413 | return; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 414 | } |
| 415 | for (int i = pinned.size() - 1; i >= 0; i--) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 416 | final String id = pinned.valueAt(i); |
| 417 | final ShortcutInfo si = mShortcuts.get(id); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 418 | if (si == null) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 419 | // This happens if a launcher pinned shortcuts from this package, then backup& |
| 420 | // restored, but this package doesn't allow backing up. |
| 421 | // In that case the launcher ends up having a dangling pinned shortcuts. |
| 422 | // That's fine, when the launcher is restored, we'll fix it. |
| 423 | continue; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 424 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 425 | si.addFlags(ShortcutInfo.FLAG_PINNED); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 426 | } |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 427 | }); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 428 | |
| 429 | // Lastly, remove the ones that are no longer pinned nor dynamic. |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 430 | removeOrphans(); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Number of calls that the caller has made, since the last reset. |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 435 | * |
| 436 | * <p>This takes care of the resetting the counter for foreground apps as well as after |
| 437 | * locale changes. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 438 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 439 | public int getApiCallCount() { |
| 440 | mShortcutUser.resetThrottlingIfNeeded(); |
| 441 | |
| 442 | final ShortcutService s = mShortcutUser.mService; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 443 | |
| 444 | // Reset the counter if: |
| 445 | // - the package is in foreground now. |
| 446 | // - the package is *not* in foreground now, but was in foreground at some point |
| 447 | // since the previous time it had been. |
| 448 | if (s.isUidForegroundLocked(mPackageUid) |
| 449 | || mLastKnownForegroundElapsedTime |
| 450 | < s.getUidLastForegroundElapsedTimeLocked(mPackageUid)) { |
| 451 | mLastKnownForegroundElapsedTime = s.injectElapsedRealtime(); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 452 | resetRateLimiting(); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // Note resetThrottlingIfNeeded() and resetRateLimiting() will set 0 to mApiCallCount, |
| 456 | // but we just can't return 0 at this point, because we may have to update |
| 457 | // mLastResetTime. |
| 458 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 459 | final long last = s.getLastResetTimeLocked(); |
| 460 | |
| 461 | final long now = s.injectCurrentTimeMillis(); |
| 462 | if (ShortcutService.isClockValid(now) && mLastResetTime > now) { |
| 463 | Slog.w(TAG, "Clock rewound"); |
| 464 | // Clock rewound. |
| 465 | mLastResetTime = now; |
| 466 | mApiCallCount = 0; |
| 467 | return mApiCallCount; |
| 468 | } |
| 469 | |
| 470 | // If not reset yet, then reset. |
| 471 | if (mLastResetTime < last) { |
| 472 | if (ShortcutService.DEBUG) { |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 473 | Slog.d(TAG, String.format("%s: last reset=%d, now=%d, last=%d: resetting", |
| 474 | getPackageName(), mLastResetTime, now, last)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 475 | } |
| 476 | mApiCallCount = 0; |
| 477 | mLastResetTime = last; |
| 478 | } |
| 479 | return mApiCallCount; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * If the caller app hasn't been throttled yet, increment {@link #mApiCallCount} |
| 484 | * and return true. Otherwise just return false. |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 485 | * |
| 486 | * <p>This takes care of the resetting the counter for foreground apps as well as after |
| 487 | * locale changes, which is done internally by {@link #getApiCallCount}. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 488 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 489 | public boolean tryApiCall() { |
| 490 | final ShortcutService s = mShortcutUser.mService; |
| 491 | |
| 492 | if (getApiCallCount() >= s.mMaxUpdatesPerInterval) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 493 | return false; |
| 494 | } |
| 495 | mApiCallCount++; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 496 | s.scheduleSaveUser(getOwnerUserId()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 497 | return true; |
| 498 | } |
| 499 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 500 | public void resetRateLimiting() { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 501 | if (ShortcutService.DEBUG) { |
| 502 | Slog.d(TAG, "resetRateLimiting: " + getPackageName()); |
| 503 | } |
| 504 | if (mApiCallCount > 0) { |
| 505 | mApiCallCount = 0; |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 506 | mShortcutUser.mService.scheduleSaveUser(getOwnerUserId()); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | public void resetRateLimitingForCommandLineNoSaving() { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 511 | mApiCallCount = 0; |
| 512 | mLastResetTime = 0; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Find all shortcuts that match {@code query}. |
| 517 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 518 | public void findAll(@NonNull List<ShortcutInfo> result, |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 519 | @Nullable Predicate<ShortcutInfo> query, int cloneFlag) { |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 520 | findAll(result, query, cloneFlag, null, 0); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Find all shortcuts that match {@code query}. |
| 525 | * |
| 526 | * This will also provide a "view" for each launcher -- a non-dynamic shortcut that's not pinned |
| 527 | * by the calling launcher will not be included in the result, and also "isPinned" will be |
| 528 | * adjusted for the caller too. |
| 529 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 530 | public void findAll(@NonNull List<ShortcutInfo> result, |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 531 | @Nullable Predicate<ShortcutInfo> query, int cloneFlag, |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 532 | @Nullable String callingLauncher, int launcherUserId) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 533 | if (getPackageInfo().isShadow()) { |
| 534 | // Restored and the app not installed yet, so don't return any. |
| 535 | return; |
| 536 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 537 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 538 | final ShortcutService s = mShortcutUser.mService; |
| 539 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 540 | // Set of pinned shortcuts by the calling launcher. |
| 541 | final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 542 | : s.getLauncherShortcutsLocked(callingLauncher, getPackageUserId(), launcherUserId) |
| 543 | .getPinnedShortcutIds(getPackageName(), getPackageUserId()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 544 | |
| 545 | for (int i = 0; i < mShortcuts.size(); i++) { |
| 546 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 547 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 548 | // Need to adjust PINNED flag depending on the caller. |
| 549 | // Basically if the caller is a launcher (callingLauncher != null) and the launcher |
| 550 | // isn't pinning it, then we need to clear PINNED for this caller. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 551 | final boolean isPinnedByCaller = (callingLauncher == null) |
| 552 | || ((pinnedByCallerSet != null) && pinnedByCallerSet.contains(si.getId())); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 553 | |
| 554 | if (si.isFloating()) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 555 | if (!isPinnedByCaller) { |
| 556 | continue; |
| 557 | } |
| 558 | } |
| 559 | final ShortcutInfo clone = si.clone(cloneFlag); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 560 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 561 | // Fix up isPinned for the caller. Note we need to do it before the "test" callback, |
| 562 | // since it may check isPinned. |
| 563 | if (!isPinnedByCaller) { |
| 564 | clone.clearFlags(ShortcutInfo.FLAG_PINNED); |
| 565 | } |
| 566 | if (query == null || query.test(clone)) { |
| 567 | result.add(clone); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | public void resetThrottling() { |
| 573 | mApiCallCount = 0; |
| 574 | } |
| 575 | |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 576 | /** |
Makoto Onuki | 6c1dbd5 | 2016-05-02 15:19:32 -0700 | [diff] [blame] | 577 | * Return the filenames (excluding path names) of icon bitmap files from this package. |
| 578 | */ |
| 579 | public ArraySet<String> getUsedBitmapFiles() { |
| 580 | final ArraySet<String> usedFiles = new ArraySet<>(mShortcuts.size()); |
| 581 | |
| 582 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 583 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 584 | if (si.getBitmapPath() != null) { |
| 585 | usedFiles.add(getFileName(si.getBitmapPath())); |
| 586 | } |
| 587 | } |
| 588 | return usedFiles; |
| 589 | } |
| 590 | |
| 591 | private static String getFileName(@NonNull String path) { |
| 592 | final int sep = path.lastIndexOf(File.separatorChar); |
| 593 | if (sep == -1) { |
| 594 | return path; |
| 595 | } else { |
| 596 | return path.substring(sep + 1); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | /** |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 601 | * Called when the package is updated or added. |
| 602 | * |
| 603 | * Add case: |
| 604 | * - Publish manifest shortcuts. |
| 605 | * |
| 606 | * Update case: |
| 607 | * - Re-publish manifest shortcuts. |
| 608 | * - If there are shortcuts with resources (icons or strings), update their timestamps. |
| 609 | * |
| 610 | * @return TRUE if any shortcuts have been changed. |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 611 | */ |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 612 | public boolean handlePackageAddedOrUpdated(boolean isNewApp, boolean forceRescan) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 613 | final PackageInfo pi = mShortcutUser.mService.getPackageInfo( |
| 614 | getPackageName(), getPackageUserId()); |
| 615 | if (pi == null) { |
| 616 | return false; // Shouldn't happen. |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 619 | if (!isNewApp && !forceRescan) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 620 | // Make sure the version code or last update time has changed. |
| 621 | // Otherwise, nothing to do. |
| 622 | if (getPackageInfo().getVersionCode() >= pi.versionCode |
| 623 | && getPackageInfo().getLastUpdateTime() >= pi.lastUpdateTime) { |
| 624 | return false; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | // Now prepare to publish manifest shortcuts. |
| 629 | List<ShortcutInfo> newManifestShortcutList = null; |
| 630 | try { |
| 631 | newManifestShortcutList = ShortcutParser.parseShortcuts(mShortcutUser.mService, |
| 632 | getPackageName(), getPackageUserId()); |
| 633 | } catch (IOException|XmlPullParserException e) { |
| 634 | Slog.e(TAG, "Failed to load shortcuts from AndroidManifest.xml.", e); |
| 635 | } |
| 636 | final int manifestShortcutSize = newManifestShortcutList == null ? 0 |
| 637 | : newManifestShortcutList.size(); |
| 638 | if (ShortcutService.DEBUG) { |
| 639 | Slog.d(TAG, String.format("Package %s has %d manifest shortcut(s)", |
| 640 | getPackageName(), manifestShortcutSize)); |
| 641 | } |
| 642 | if (isNewApp && (manifestShortcutSize == 0)) { |
| 643 | // If it's a new app, and it doesn't have manifest shortcuts, then nothing to do. |
| 644 | |
| 645 | // If it's an update, then it may already have manifest shortcuts, which need to be |
| 646 | // disabled. |
| 647 | return false; |
| 648 | } |
| 649 | if (ShortcutService.DEBUG) { |
| 650 | Slog.d(TAG, String.format("Package %s %s, version %d -> %d", getPackageName(), |
| 651 | (isNewApp ? "added" : "updated"), |
| 652 | getPackageInfo().getVersionCode(), pi.versionCode)); |
| 653 | } |
| 654 | |
| 655 | getPackageInfo().updateVersionInfo(pi); |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 656 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 657 | final ShortcutService s = mShortcutUser.mService; |
| 658 | |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 659 | boolean changed = false; |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 660 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 661 | // For existing shortcuts, update timestamps if they have any resources. |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 662 | // Also check if shortcuts' activities are still main activities. Otherwise, disable them. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 663 | if (!isNewApp) { |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 664 | Resources publisherRes = null; |
| 665 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 666 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 667 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 668 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 669 | if (si.isDynamic()) { |
| 670 | if (!s.injectIsMainActivity(si.getActivity(), getPackageUserId())) { |
| 671 | Slog.w(TAG, String.format( |
| 672 | "%s is no longer main activity. Disabling shorcut %s.", |
| 673 | getPackageName(), si.getId())); |
| 674 | if (disableDynamicWithId(si.getId())) { |
| 675 | continue; // Actually removed. |
| 676 | } |
| 677 | // Still pinned, so fall-through and possibly update the resources. |
| 678 | } |
| 679 | changed = true; |
| 680 | } |
| 681 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 682 | if (si.hasAnyResources()) { |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 683 | if (!si.isOriginallyFromManifest()) { |
| 684 | if (publisherRes == null) { |
| 685 | publisherRes = getPackageResources(); |
| 686 | if (publisherRes == null) { |
| 687 | break; // Resources couldn't be loaded. |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | // If this shortcut is not from a manifest, then update all resource IDs |
| 692 | // from resource names. (We don't allow resource strings for |
| 693 | // non-manifest at the moment, but icons can still be resources.) |
| 694 | si.lookupAndFillInResourceIds(publisherRes); |
| 695 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 696 | changed = true; |
| 697 | si.setTimestamp(s.injectCurrentTimeMillis()); |
| 698 | } |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 699 | } |
| 700 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 701 | |
| 702 | // (Re-)publish manifest shortcut. |
| 703 | changed |= publishManifestShortcuts(newManifestShortcutList); |
| 704 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 705 | if (newManifestShortcutList != null) { |
| 706 | changed |= pushOutExcessShortcuts(); |
| 707 | } |
| 708 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 709 | s.verifyStates(); |
| 710 | |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 711 | if (changed) { |
| 712 | // This will send a notification to the launcher, and also save . |
| 713 | s.packageShortcutsChanged(getPackageName(), getPackageUserId()); |
| 714 | } else { |
| 715 | // Still save the version code. |
| 716 | s.scheduleSaveUser(getPackageUserId()); |
| 717 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 718 | return changed; |
| 719 | } |
| 720 | |
| 721 | private boolean publishManifestShortcuts(List<ShortcutInfo> newManifestShortcutList) { |
| 722 | if (ShortcutService.DEBUG) { |
| 723 | Slog.d(TAG, String.format( |
| 724 | "Package %s: publishing manifest shortcuts", getPackageName())); |
| 725 | } |
| 726 | boolean changed = false; |
| 727 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 728 | // Keep the previous IDs. |
| 729 | ArraySet<String> toDisableList = null; |
| 730 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 731 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 732 | |
| 733 | if (si.isManifestShortcut()) { |
| 734 | if (toDisableList == null) { |
| 735 | toDisableList = new ArraySet<>(); |
| 736 | } |
| 737 | toDisableList.add(si.getId()); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | // Publish new ones. |
| 742 | if (newManifestShortcutList != null) { |
| 743 | final int newListSize = newManifestShortcutList.size(); |
| 744 | |
| 745 | for (int i = 0; i < newListSize; i++) { |
| 746 | changed = true; |
| 747 | |
| 748 | final ShortcutInfo newShortcut = newManifestShortcutList.get(i); |
| 749 | final boolean newDisabled = !newShortcut.isEnabled(); |
| 750 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 751 | final String id = newShortcut.getId(); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 752 | final ShortcutInfo oldShortcut = mShortcuts.get(id); |
| 753 | |
| 754 | boolean wasPinned = false; |
| 755 | |
| 756 | if (oldShortcut != null) { |
| 757 | if (!oldShortcut.isOriginallyFromManifest()) { |
| 758 | Slog.e(TAG, "Shortcut with ID=" + newShortcut.getId() |
| 759 | + " exists but is not from AndroidManifest.xml, not updating."); |
| 760 | continue; |
| 761 | } |
| 762 | // Take over the pinned flag. |
| 763 | if (oldShortcut.isPinned()) { |
| 764 | wasPinned = true; |
| 765 | newShortcut.addFlags(ShortcutInfo.FLAG_PINNED); |
| 766 | } |
| 767 | } |
| 768 | if (newDisabled && !wasPinned) { |
| 769 | // If the shortcut is disabled, and it was *not* pinned, then this |
| 770 | // just doesn't have to be published. |
| 771 | // Just keep it in toDisableList, so the previous one would be removed. |
| 772 | continue; |
| 773 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 774 | |
| 775 | // Note even if enabled=false, we still need to update all fields, so do it |
| 776 | // regardless. |
| 777 | addShortcutInner(newShortcut); // This will clean up the old one too. |
| 778 | |
| 779 | if (!newDisabled && toDisableList != null) { |
| 780 | // Still alive, don't remove. |
| 781 | toDisableList.remove(id); |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | // Disable the previous manifest shortcuts that are no longer in the manifest. |
| 787 | if (toDisableList != null) { |
| 788 | if (ShortcutService.DEBUG) { |
| 789 | Slog.d(TAG, String.format( |
| 790 | "Package %s: disabling %d stale shortcuts", getPackageName(), |
| 791 | toDisableList.size())); |
| 792 | } |
| 793 | for (int i = toDisableList.size() - 1; i >= 0; i--) { |
| 794 | changed = true; |
| 795 | |
| 796 | final String id = toDisableList.valueAt(i); |
| 797 | |
| 798 | disableWithId(id, /* disable message =*/ null, /* disable message resid */ 0, |
| 799 | /* overrideImmutable=*/ true); |
| 800 | } |
| 801 | removeOrphans(); |
| 802 | } |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 803 | adjustRanks(); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 804 | return changed; |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 807 | /** |
| 808 | * For each target activity, make sure # of dynamic + manifest shortcuts <= max. |
| 809 | * If too many, we'll remove the dynamic with the lowest ranks. |
| 810 | */ |
| 811 | private boolean pushOutExcessShortcuts() { |
| 812 | final ShortcutService service = mShortcutUser.mService; |
| 813 | final int maxShortcuts = service.getMaxActivityShortcuts(); |
| 814 | |
| 815 | boolean changed = false; |
| 816 | |
| 817 | final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all = |
| 818 | sortShortcutsToActivities(); |
| 819 | for (int outer = all.size() - 1; outer >= 0; outer--) { |
| 820 | final ArrayList<ShortcutInfo> list = all.valueAt(outer); |
| 821 | if (list.size() <= maxShortcuts) { |
| 822 | continue; |
| 823 | } |
| 824 | // Sort by isManifestShortcut() and getRank(). |
| 825 | Collections.sort(list, mShortcutTypeAndRankComparator); |
| 826 | |
| 827 | // Keep [0 .. max), and remove (as dynamic) [max .. size) |
| 828 | for (int inner = list.size() - 1; inner >= maxShortcuts; inner--) { |
| 829 | final ShortcutInfo shortcut = list.get(inner); |
| 830 | |
| 831 | if (shortcut.isManifestShortcut()) { |
| 832 | // This shouldn't happen -- excess shortcuts should all be non-manifest. |
| 833 | // But just in case. |
| 834 | service.wtf("Found manifest shortcuts in excess list."); |
| 835 | continue; |
| 836 | } |
| 837 | deleteDynamicWithId(shortcut.getId()); |
| 838 | } |
| 839 | } |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 840 | |
| 841 | return changed; |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * To sort by isManifestShortcut() and getRank(). i.e. manifest shortcuts come before |
| 846 | * non-manifest shortcuts, then sort by rank. |
| 847 | * |
| 848 | * This is used to decide which dynamic shortcuts to remove when an upgraded version has more |
| 849 | * manifest shortcuts than before and as a result we need to remove some of the dynamic |
| 850 | * shortcuts. We sort manifest + dynamic shortcuts by this order, and remove the ones with |
| 851 | * the last ones. |
| 852 | * |
| 853 | * (Note the number of manifest shortcuts is always <= the max number, because if there are |
| 854 | * more, ShortcutParser would ignore the rest.) |
| 855 | */ |
| 856 | final Comparator<ShortcutInfo> mShortcutTypeAndRankComparator = (ShortcutInfo a, |
| 857 | ShortcutInfo b) -> { |
| 858 | if (a.isManifestShortcut() && !b.isManifestShortcut()) { |
| 859 | return -1; |
| 860 | } |
| 861 | if (!a.isManifestShortcut() && b.isManifestShortcut()) { |
| 862 | return 1; |
| 863 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 864 | return Integer.compare(a.getRank(), b.getRank()); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 865 | }; |
| 866 | |
| 867 | /** |
| 868 | * Build a list of shortcuts for each target activity and return as a map. The result won't |
| 869 | * contain "floating" shortcuts because they don't belong on any activities. |
| 870 | */ |
| 871 | private ArrayMap<ComponentName, ArrayList<ShortcutInfo>> sortShortcutsToActivities() { |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 872 | final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> activitiesToShortcuts |
| 873 | = new ArrayMap<>(); |
| 874 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 875 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 876 | if (si.isFloating()) { |
| 877 | continue; // Ignore floating shortcuts, which are not tied to any activities. |
| 878 | } |
| 879 | |
| 880 | final ComponentName activity = si.getActivity(); |
| 881 | |
| 882 | ArrayList<ShortcutInfo> list = activitiesToShortcuts.get(activity); |
| 883 | if (list == null) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 884 | list = new ArrayList<>(); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 885 | activitiesToShortcuts.put(activity, list); |
| 886 | } |
| 887 | list.add(si); |
| 888 | } |
| 889 | return activitiesToShortcuts; |
| 890 | } |
| 891 | |
| 892 | /** Used by {@link #enforceShortcutCountsBeforeOperation} */ |
| 893 | private void incrementCountForActivity(ArrayMap<ComponentName, Integer> counts, |
| 894 | ComponentName cn, int increment) { |
| 895 | Integer oldValue = counts.get(cn); |
| 896 | if (oldValue == null) { |
| 897 | oldValue = 0; |
| 898 | } |
| 899 | |
| 900 | counts.put(cn, oldValue + increment); |
| 901 | } |
| 902 | |
| 903 | /** |
| 904 | * Called by |
| 905 | * {@link android.content.pm.ShortcutManager#setDynamicShortcuts}, |
| 906 | * {@link android.content.pm.ShortcutManager#addDynamicShortcuts}, and |
| 907 | * {@link android.content.pm.ShortcutManager#updateShortcuts} before actually performing |
| 908 | * the operation to make sure the operation wouldn't result in the target activities having |
| 909 | * more than the allowed number of dynamic/manifest shortcuts. |
| 910 | * |
| 911 | * @param newList shortcut list passed to set, add or updateShortcuts(). |
| 912 | * @param operation add, set or update. |
| 913 | * @throws IllegalArgumentException if the operation would result in going over the max |
| 914 | * shortcut count for any activity. |
| 915 | */ |
| 916 | public void enforceShortcutCountsBeforeOperation(List<ShortcutInfo> newList, |
| 917 | @ShortcutOperation int operation) { |
| 918 | final ShortcutService service = mShortcutUser.mService; |
| 919 | |
| 920 | // Current # of dynamic / manifest shortcuts for each activity. |
| 921 | // (If it's for update, then don't count dynamic shortcuts, since they'll be replaced |
| 922 | // anyway.) |
| 923 | final ArrayMap<ComponentName, Integer> counts = new ArrayMap<>(4); |
| 924 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 925 | final ShortcutInfo shortcut = mShortcuts.valueAt(i); |
| 926 | |
| 927 | if (shortcut.isManifestShortcut()) { |
| 928 | incrementCountForActivity(counts, shortcut.getActivity(), 1); |
| 929 | } else if (shortcut.isDynamic() && (operation != ShortcutService.OPERATION_SET)) { |
| 930 | incrementCountForActivity(counts, shortcut.getActivity(), 1); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | for (int i = newList.size() - 1; i >= 0; i--) { |
| 935 | final ShortcutInfo newShortcut = newList.get(i); |
| 936 | final ComponentName newActivity = newShortcut.getActivity(); |
| 937 | if (newActivity == null) { |
| 938 | if (operation != ShortcutService.OPERATION_UPDATE) { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 939 | service.wtf("Activity must not be null at this point"); |
| 940 | continue; // Just ignore this invalid case. |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 941 | } |
| 942 | continue; // Activity can be null for update. |
| 943 | } |
| 944 | |
| 945 | final ShortcutInfo original = mShortcuts.get(newShortcut.getId()); |
| 946 | if (original == null) { |
| 947 | if (operation == ShortcutService.OPERATION_UPDATE) { |
| 948 | continue; // When updating, ignore if there's no target. |
| 949 | } |
| 950 | // Add() or set(), and there's no existing shortcut with the same ID. We're |
| 951 | // simply publishing (as opposed to updating) this shortcut, so just +1. |
| 952 | incrementCountForActivity(counts, newActivity, 1); |
| 953 | continue; |
| 954 | } |
| 955 | if (original.isFloating() && (operation == ShortcutService.OPERATION_UPDATE)) { |
| 956 | // Updating floating shortcuts doesn't affect the count, so ignore. |
| 957 | continue; |
| 958 | } |
| 959 | |
| 960 | // If it's add() or update(), then need to decrement for the previous activity. |
| 961 | // Skip it for set() since it's already been taken care of by not counting the original |
| 962 | // dynamic shortcuts in the first loop. |
| 963 | if (operation != ShortcutService.OPERATION_SET) { |
| 964 | final ComponentName oldActivity = original.getActivity(); |
| 965 | if (!original.isFloating()) { |
| 966 | incrementCountForActivity(counts, oldActivity, -1); |
| 967 | } |
| 968 | } |
| 969 | incrementCountForActivity(counts, newActivity, 1); |
| 970 | } |
| 971 | |
| 972 | // Then make sure none of the activities have more than the max number of shortcuts. |
| 973 | for (int i = counts.size() - 1; i >= 0; i--) { |
| 974 | service.enforceMaxActivityShortcuts(counts.valueAt(i)); |
| 975 | } |
| 976 | } |
| 977 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 978 | /** |
| 979 | * For all the text fields, refresh the string values if they're from resources. |
| 980 | */ |
| 981 | public void resolveResourceStrings() { |
| 982 | final ShortcutService s = mShortcutUser.mService; |
| 983 | boolean changed = false; |
| 984 | |
| 985 | Resources publisherRes = null; |
| 986 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 987 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 988 | |
| 989 | if (si.hasStringResources()) { |
| 990 | changed = true; |
| 991 | |
| 992 | if (publisherRes == null) { |
| 993 | publisherRes = getPackageResources(); |
| 994 | if (publisherRes == null) { |
| 995 | break; // Resources couldn't be loaded. |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | si.resolveResourceStrings(publisherRes); |
| 1000 | si.setTimestamp(s.injectCurrentTimeMillis()); |
| 1001 | } |
| 1002 | } |
| 1003 | if (changed) { |
| 1004 | s.scheduleSaveUser(getPackageUserId()); |
| 1005 | } |
| 1006 | } |
| 1007 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1008 | /** Clears the implicit ranks for all shortcuts. */ |
| 1009 | public void clearAllImplicitRanks() { |
| 1010 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 1011 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 1012 | si.clearImplicitRankAndRankChangedFlag(); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | /** |
| 1017 | * Used to sort shortcuts for rank auto-adjusting. |
| 1018 | */ |
| 1019 | final Comparator<ShortcutInfo> mShortcutRankComparator = (ShortcutInfo a, ShortcutInfo b) -> { |
| 1020 | // First, sort by rank. |
| 1021 | int ret = Integer.compare(a.getRank(), b.getRank()); |
| 1022 | if (ret != 0) { |
| 1023 | return ret; |
| 1024 | } |
| 1025 | // When ranks are tie, then prioritize the ones that have just been assigned new ranks. |
| 1026 | // e.g. when there are 3 shortcuts, "s1" "s2" and "s3" with rank 0, 1, 2 respectively, |
| 1027 | // adding a shortcut "s4" with rank 1 will "insert" it between "s1" and "s2", because |
| 1028 | // "s2" and "s4" have the same rank 1 but s4 has isRankChanged() set. |
| 1029 | // Similarly, updating s3's rank to 1 will insert it between s1 and s2. |
| 1030 | if (a.isRankChanged() != b.isRankChanged()) { |
| 1031 | return a.isRankChanged() ? -1 : 1; |
| 1032 | } |
| 1033 | // If they're still tie, sort by implicit rank -- i.e. preserve the order in which |
| 1034 | // they're passed to the API. |
| 1035 | ret = Integer.compare(a.getImplicitRank(), b.getImplicitRank()); |
| 1036 | if (ret != 0) { |
| 1037 | return ret; |
| 1038 | } |
| 1039 | // If they're stil tie, just sort by their IDs. |
| 1040 | // This may happen with updateShortcuts() -- see |
| 1041 | // the testUpdateShortcuts_noManifestShortcuts() test. |
| 1042 | return a.getId().compareTo(b.getId()); |
| 1043 | }; |
| 1044 | |
| 1045 | /** |
| 1046 | * Re-calculate the ranks for all shortcuts. |
| 1047 | */ |
| 1048 | public void adjustRanks() { |
| 1049 | final ShortcutService s = mShortcutUser.mService; |
| 1050 | final long now = s.injectCurrentTimeMillis(); |
| 1051 | |
| 1052 | // First, clear ranks for floating shortcuts. |
| 1053 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 1054 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 1055 | if (si.isFloating()) { |
| 1056 | if (si.getRank() != 0) { |
| 1057 | si.setTimestamp(now); |
| 1058 | si.setRank(0); |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | // Then adjust ranks. Ranks are unique for each activity, so we first need to sort |
| 1064 | // shortcuts to each activity. |
| 1065 | // Then sort the shortcuts within each activity with mShortcutRankComparator, and |
| 1066 | // assign ranks from 0. |
| 1067 | final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all = |
| 1068 | sortShortcutsToActivities(); |
| 1069 | for (int outer = all.size() - 1; outer >= 0; outer--) { // For each activity. |
| 1070 | final ArrayList<ShortcutInfo> list = all.valueAt(outer); |
| 1071 | |
| 1072 | // Sort by ranks and other signals. |
| 1073 | Collections.sort(list, mShortcutRankComparator); |
| 1074 | |
| 1075 | int rank = 0; |
| 1076 | |
| 1077 | final int size = list.size(); |
| 1078 | for (int i = 0; i < size; i++) { |
| 1079 | final ShortcutInfo si = list.get(i); |
| 1080 | if (si.isManifestShortcut()) { |
| 1081 | // Don't adjust ranks for manifest shortcuts. |
| 1082 | continue; |
| 1083 | } |
| 1084 | // At this point, it must be dynamic. |
| 1085 | if (!si.isDynamic()) { |
| 1086 | s.wtf("Non-dynamic shortcut found."); |
| 1087 | continue; |
| 1088 | } |
| 1089 | final int thisRank = rank++; |
| 1090 | if (si.getRank() != thisRank) { |
| 1091 | si.setTimestamp(now); |
| 1092 | si.setRank(thisRank); |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1098 | public void dump(@NonNull PrintWriter pw, @NonNull String prefix) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1099 | pw.println(); |
| 1100 | |
| 1101 | pw.print(prefix); |
| 1102 | pw.print("Package: "); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1103 | pw.print(getPackageName()); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1104 | pw.print(" UID: "); |
| 1105 | pw.print(mPackageUid); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1106 | pw.println(); |
| 1107 | |
| 1108 | pw.print(prefix); |
| 1109 | pw.print(" "); |
| 1110 | pw.print("Calls: "); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1111 | pw.print(getApiCallCount()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1112 | pw.println(); |
| 1113 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1114 | // getApiCallCount() may have updated mLastKnownForegroundElapsedTime. |
| 1115 | pw.print(prefix); |
| 1116 | pw.print(" "); |
| 1117 | pw.print("Last known FG: "); |
| 1118 | pw.print(mLastKnownForegroundElapsedTime); |
| 1119 | pw.println(); |
| 1120 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1121 | // This should be after getApiCallCount(), which may update it. |
| 1122 | pw.print(prefix); |
| 1123 | pw.print(" "); |
| 1124 | pw.print("Last reset: ["); |
| 1125 | pw.print(mLastResetTime); |
| 1126 | pw.print("] "); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1127 | pw.print(ShortcutService.formatTime(mLastResetTime)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1128 | pw.println(); |
| 1129 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1130 | getPackageInfo().dump(pw, prefix + " "); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1131 | pw.println(); |
| 1132 | |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 1133 | pw.print(prefix); |
| 1134 | pw.println(" Shortcuts:"); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1135 | long totalBitmapSize = 0; |
| 1136 | final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts; |
| 1137 | final int size = shortcuts.size(); |
| 1138 | for (int i = 0; i < size; i++) { |
| 1139 | final ShortcutInfo si = shortcuts.valueAt(i); |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 1140 | pw.print(prefix); |
| 1141 | pw.print(" "); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1142 | pw.println(si.toInsecureString()); |
| 1143 | if (si.getBitmapPath() != null) { |
| 1144 | final long len = new File(si.getBitmapPath()).length(); |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 1145 | pw.print(prefix); |
| 1146 | pw.print(" "); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1147 | pw.print("bitmap size="); |
| 1148 | pw.println(len); |
| 1149 | |
| 1150 | totalBitmapSize += len; |
| 1151 | } |
| 1152 | } |
| 1153 | pw.print(prefix); |
| 1154 | pw.print(" "); |
| 1155 | pw.print("Total bitmap size: "); |
| 1156 | pw.print(totalBitmapSize); |
| 1157 | pw.print(" ("); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1158 | pw.print(Formatter.formatFileSize(mShortcutUser.mService.mContext, totalBitmapSize)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1159 | pw.println(")"); |
| 1160 | } |
| 1161 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1162 | @Override |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1163 | public void saveToXml(@NonNull XmlSerializer out, boolean forBackup) |
| 1164 | throws IOException, XmlPullParserException { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1165 | final int size = mShortcuts.size(); |
| 1166 | |
| 1167 | if (size == 0 && mApiCallCount == 0) { |
| 1168 | return; // nothing to write. |
| 1169 | } |
| 1170 | |
| 1171 | out.startTag(null, TAG_ROOT); |
| 1172 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1173 | ShortcutService.writeAttr(out, ATTR_NAME, getPackageName()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1174 | ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount); |
| 1175 | ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1176 | getPackageInfo().saveToXml(out); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1177 | |
| 1178 | for (int j = 0; j < size; j++) { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1179 | saveShortcut(out, mShortcuts.valueAt(j), forBackup); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | out.endTag(null, TAG_ROOT); |
| 1183 | } |
| 1184 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1185 | private static void saveShortcut(XmlSerializer out, ShortcutInfo si, boolean forBackup) |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1186 | throws IOException, XmlPullParserException { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1187 | if (forBackup) { |
Makoto Onuki | f3ba2e0 | 2016-07-12 09:18:50 -0700 | [diff] [blame] | 1188 | if (!(si.isPinned() && si.isEnabled())) { |
| 1189 | return; // We only backup pinned shortcuts that are enabled. |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1190 | } |
| 1191 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1192 | out.startTag(null, TAG_SHORTCUT); |
| 1193 | ShortcutService.writeAttr(out, ATTR_ID, si.getId()); |
| 1194 | // writeAttr(out, "package", si.getPackageName()); // not needed |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1195 | ShortcutService.writeAttr(out, ATTR_ACTIVITY, si.getActivity()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1196 | // writeAttr(out, "icon", si.getIcon()); // We don't save it. |
| 1197 | ShortcutService.writeAttr(out, ATTR_TITLE, si.getTitle()); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1198 | ShortcutService.writeAttr(out, ATTR_TITLE_RES_ID, si.getTitleResId()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1199 | ShortcutService.writeAttr(out, ATTR_TITLE_RES_NAME, si.getTitleResName()); |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 1200 | ShortcutService.writeAttr(out, ATTR_TEXT, si.getText()); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1201 | ShortcutService.writeAttr(out, ATTR_TEXT_RES_ID, si.getTextResId()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1202 | ShortcutService.writeAttr(out, ATTR_TEXT_RES_NAME, si.getTextResName()); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1203 | ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE, si.getDisabledMessage()); |
Makoto Onuki | eddbfec | 2016-05-31 17:04:34 -0700 | [diff] [blame] | 1204 | ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_ID, |
| 1205 | si.getDisabledMessageResourceId()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1206 | ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_NAME, |
| 1207 | si.getDisabledMessageResName()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1208 | ShortcutService.writeAttr(out, ATTR_INTENT, si.getIntentNoExtras()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1209 | ShortcutService.writeAttr(out, ATTR_TIMESTAMP, |
| 1210 | si.getLastChangedTimestamp()); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1211 | if (forBackup) { |
| 1212 | // Don't write icon information. Also drop the dynamic flag. |
| 1213 | ShortcutService.writeAttr(out, ATTR_FLAGS, |
| 1214 | si.getFlags() & |
| 1215 | ~(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES |
| 1216 | | ShortcutInfo.FLAG_DYNAMIC)); |
| 1217 | } else { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1218 | // When writing for backup, ranks shouldn't be saved, since shortcuts won't be restored |
| 1219 | // as dynamic. |
| 1220 | ShortcutService.writeAttr(out, ATTR_RANK, si.getRank()); |
| 1221 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1222 | ShortcutService.writeAttr(out, ATTR_FLAGS, si.getFlags()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1223 | ShortcutService.writeAttr(out, ATTR_ICON_RES_ID, si.getIconResourceId()); |
| 1224 | ShortcutService.writeAttr(out, ATTR_ICON_RES_NAME, si.getIconResName()); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1225 | ShortcutService.writeAttr(out, ATTR_BITMAP_PATH, si.getBitmapPath()); |
| 1226 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1227 | |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1228 | { |
Makoto Onuki | be73a80 | 2016-04-15 14:46:35 -0700 | [diff] [blame] | 1229 | final Set<String> cat = si.getCategories(); |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1230 | if (cat != null && cat.size() > 0) { |
| 1231 | out.startTag(null, TAG_CATEGORIES); |
| 1232 | XmlUtils.writeStringArrayXml(cat.toArray(new String[cat.size()]), |
| 1233 | NAME_CATEGORIES, out); |
| 1234 | out.endTag(null, TAG_CATEGORIES); |
| 1235 | } |
| 1236 | } |
| 1237 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1238 | ShortcutService.writeTagExtra(out, TAG_INTENT_EXTRAS, |
| 1239 | si.getIntentPersistableExtras()); |
| 1240 | ShortcutService.writeTagExtra(out, TAG_EXTRAS, si.getExtras()); |
| 1241 | |
| 1242 | out.endTag(null, TAG_SHORTCUT); |
| 1243 | } |
| 1244 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1245 | public static ShortcutPackage loadFromXml(ShortcutService s, ShortcutUser shortcutUser, |
| 1246 | XmlPullParser parser, boolean fromBackup) |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1247 | throws IOException, XmlPullParserException { |
| 1248 | |
| 1249 | final String packageName = ShortcutService.parseStringAttribute(parser, |
| 1250 | ATTR_NAME); |
| 1251 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1252 | final ShortcutPackage ret = new ShortcutPackage(shortcutUser, |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1253 | shortcutUser.getUserId(), packageName); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1254 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1255 | ret.mApiCallCount = |
| 1256 | ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT); |
| 1257 | ret.mLastResetTime = |
| 1258 | ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET); |
| 1259 | |
| 1260 | final int outerDepth = parser.getDepth(); |
| 1261 | int type; |
| 1262 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1263 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1264 | if (type != XmlPullParser.START_TAG) { |
| 1265 | continue; |
| 1266 | } |
| 1267 | final int depth = parser.getDepth(); |
| 1268 | final String tag = parser.getName(); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1269 | if (depth == outerDepth + 1) { |
| 1270 | switch (tag) { |
| 1271 | case ShortcutPackageInfo.TAG_ROOT: |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 1272 | ret.getPackageInfo().loadFromXml(parser, fromBackup); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1273 | continue; |
| 1274 | case TAG_SHORTCUT: |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1275 | final ShortcutInfo si = parseShortcut(parser, packageName, |
| 1276 | shortcutUser.getUserId()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1277 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1278 | // Don't use addShortcut(), we don't need to save the icon. |
| 1279 | ret.mShortcuts.put(si.getId(), si); |
| 1280 | continue; |
| 1281 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1282 | } |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1283 | ShortcutService.warnForInvalidTag(depth, tag); |
| 1284 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1285 | return ret; |
| 1286 | } |
| 1287 | |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1288 | private static ShortcutInfo parseShortcut(XmlPullParser parser, String packageName, |
| 1289 | @UserIdInt int userId) throws IOException, XmlPullParserException { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1290 | String id; |
| 1291 | ComponentName activityComponent; |
| 1292 | // Icon icon; |
| 1293 | String title; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1294 | int titleResId; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1295 | String titleResName; |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 1296 | String text; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1297 | int textResId; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1298 | String textResName; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1299 | String disabledMessage; |
| 1300 | int disabledMessageResId; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1301 | String disabledMessageResName; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1302 | Intent intent; |
| 1303 | PersistableBundle intentPersistableExtras = null; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1304 | int rank; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1305 | PersistableBundle extras = null; |
| 1306 | long lastChangedTimestamp; |
| 1307 | int flags; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1308 | int iconResId; |
| 1309 | String iconResName; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1310 | String bitmapPath; |
Makoto Onuki | be73a80 | 2016-04-15 14:46:35 -0700 | [diff] [blame] | 1311 | ArraySet<String> categories = null; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1312 | |
| 1313 | id = ShortcutService.parseStringAttribute(parser, ATTR_ID); |
| 1314 | activityComponent = ShortcutService.parseComponentNameAttribute(parser, |
| 1315 | ATTR_ACTIVITY); |
| 1316 | title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1317 | titleResId = ShortcutService.parseIntAttribute(parser, ATTR_TITLE_RES_ID); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1318 | titleResName = ShortcutService.parseStringAttribute(parser, ATTR_TITLE_RES_NAME); |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 1319 | text = ShortcutService.parseStringAttribute(parser, ATTR_TEXT); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1320 | textResId = ShortcutService.parseIntAttribute(parser, ATTR_TEXT_RES_ID); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1321 | textResName = ShortcutService.parseStringAttribute(parser, ATTR_TEXT_RES_NAME); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1322 | disabledMessage = ShortcutService.parseStringAttribute(parser, ATTR_DISABLED_MESSAGE); |
| 1323 | disabledMessageResId = ShortcutService.parseIntAttribute(parser, |
| 1324 | ATTR_DISABLED_MESSAGE_RES_ID); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1325 | disabledMessageResName = ShortcutService.parseStringAttribute(parser, |
| 1326 | ATTR_DISABLED_MESSAGE_RES_NAME); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1327 | intent = ShortcutService.parseIntentAttribute(parser, ATTR_INTENT); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1328 | rank = (int) ShortcutService.parseLongAttribute(parser, ATTR_RANK); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1329 | lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1330 | flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1331 | iconResId = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES_ID); |
| 1332 | iconResName = ShortcutService.parseStringAttribute(parser, ATTR_ICON_RES_NAME); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1333 | bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH); |
| 1334 | |
| 1335 | final int outerDepth = parser.getDepth(); |
| 1336 | int type; |
| 1337 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1338 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1339 | if (type != XmlPullParser.START_TAG) { |
| 1340 | continue; |
| 1341 | } |
| 1342 | final int depth = parser.getDepth(); |
| 1343 | final String tag = parser.getName(); |
| 1344 | if (ShortcutService.DEBUG_LOAD) { |
| 1345 | Slog.d(TAG, String.format(" depth=%d type=%d name=%s", |
| 1346 | depth, type, tag)); |
| 1347 | } |
| 1348 | switch (tag) { |
| 1349 | case TAG_INTENT_EXTRAS: |
| 1350 | intentPersistableExtras = PersistableBundle.restoreFromXml(parser); |
| 1351 | continue; |
| 1352 | case TAG_EXTRAS: |
| 1353 | extras = PersistableBundle.restoreFromXml(parser); |
| 1354 | continue; |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1355 | case TAG_CATEGORIES: |
| 1356 | // This just contains string-array. |
| 1357 | continue; |
| 1358 | case TAG_STRING_ARRAY_XMLUTILS: |
| 1359 | if (NAME_CATEGORIES.equals(ShortcutService.parseStringAttribute(parser, |
| 1360 | ATTR_NAME_XMLUTILS))) { |
Makoto Onuki | be73a80 | 2016-04-15 14:46:35 -0700 | [diff] [blame] | 1361 | final String[] ar = XmlUtils.readThisStringArrayXml( |
| 1362 | parser, TAG_STRING_ARRAY_XMLUTILS, null); |
| 1363 | categories = new ArraySet<>(ar.length); |
| 1364 | for (int i = 0; i < ar.length; i++) { |
| 1365 | categories.add(ar[i]); |
| 1366 | } |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1367 | } |
| 1368 | continue; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1369 | } |
| 1370 | throw ShortcutService.throwForInvalidTag(depth, tag); |
| 1371 | } |
Makoto Onuki | be73a80 | 2016-04-15 14:46:35 -0700 | [diff] [blame] | 1372 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1373 | return new ShortcutInfo( |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1374 | userId, id, packageName, activityComponent, /* icon =*/ null, |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1375 | title, titleResId, titleResName, text, textResId, textResName, |
| 1376 | disabledMessage, disabledMessageResId, disabledMessageResName, |
Makoto Onuki | be73a80 | 2016-04-15 14:46:35 -0700 | [diff] [blame] | 1377 | categories, intent, |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1378 | intentPersistableExtras, rank, extras, lastChangedTimestamp, flags, |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1379 | iconResId, iconResName, bitmapPath); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1380 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 1381 | |
| 1382 | @VisibleForTesting |
| 1383 | List<ShortcutInfo> getAllShortcutsForTest() { |
| 1384 | return new ArrayList<>(mShortcuts.values()); |
| 1385 | } |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1386 | |
| 1387 | @Override |
| 1388 | public void verifyStates() { |
| 1389 | super.verifyStates(); |
| 1390 | |
| 1391 | boolean failed = false; |
| 1392 | |
| 1393 | final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all = |
| 1394 | sortShortcutsToActivities(); |
| 1395 | |
| 1396 | // Make sure each activity won't have more than max shortcuts. |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1397 | for (int outer = all.size() - 1; outer >= 0; outer--) { |
| 1398 | final ArrayList<ShortcutInfo> list = all.valueAt(outer); |
| 1399 | if (list.size() > mShortcutUser.mService.getMaxActivityShortcuts()) { |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1400 | failed = true; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1401 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": activity " + all.keyAt(outer) |
| 1402 | + " has " + all.valueAt(outer).size() + " shortcuts."); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1403 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1404 | |
| 1405 | // Sort by rank. |
| 1406 | Collections.sort(list, (a, b) -> Integer.compare(a.getRank(), b.getRank())); |
| 1407 | |
| 1408 | // Split into two arrays for each kind. |
| 1409 | final ArrayList<ShortcutInfo> dynamicList = new ArrayList<>(list); |
| 1410 | dynamicList.removeIf((si) -> !si.isDynamic()); |
| 1411 | |
| 1412 | final ArrayList<ShortcutInfo> manifestList = new ArrayList<>(list); |
| 1413 | dynamicList.removeIf((si) -> !si.isManifestShortcut()); |
| 1414 | |
| 1415 | verifyRanksSequential(dynamicList); |
| 1416 | verifyRanksSequential(manifestList); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1419 | // Verify each shortcut's status. |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1420 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 1421 | final ShortcutInfo si = mShortcuts.valueAt(i); |
Makoto Onuki | ff14f73 | 2016-06-30 17:07:25 -0700 | [diff] [blame] | 1422 | if (!(si.isDeclaredInManifest() || si.isDynamic() || si.isPinned())) { |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1423 | failed = true; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1424 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1425 | + " is not manifest, dynamic or pinned."); |
| 1426 | } |
Makoto Onuki | ff14f73 | 2016-06-30 17:07:25 -0700 | [diff] [blame] | 1427 | if (si.isDeclaredInManifest() && si.isDynamic()) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1428 | failed = true; |
| 1429 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1430 | + " is both dynamic and manifest at the same time."); |
| 1431 | } |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1432 | if (si.getActivity() == null) { |
| 1433 | failed = true; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1434 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1435 | + " has null activity."); |
| 1436 | } |
| 1437 | if ((si.isDynamic() || si.isManifestShortcut()) && !si.isEnabled()) { |
| 1438 | failed = true; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1439 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1440 | + " is not floating, but is disabled."); |
| 1441 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1442 | if (si.isFloating() && si.getRank() != 0) { |
| 1443 | failed = true; |
| 1444 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1445 | + " is floating, but has rank=" + si.getRank()); |
| 1446 | } |
Makoto Onuki | dd09781 | 2016-06-29 13:10:09 -0700 | [diff] [blame] | 1447 | if (si.getIcon() != null) { |
| 1448 | failed = true; |
| 1449 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1450 | + " still has an icon"); |
| 1451 | } |
| 1452 | if (si.hasIconFile() && si.hasIconResource()) { |
| 1453 | failed = true; |
| 1454 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1455 | + " has both resource and bitmap icons"); |
| 1456 | } |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | if (failed) { |
| 1460 | throw new IllegalStateException("See logcat for errors"); |
| 1461 | } |
| 1462 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1463 | |
| 1464 | private boolean verifyRanksSequential(List<ShortcutInfo> list) { |
| 1465 | boolean failed = false; |
| 1466 | |
| 1467 | for (int i = 0; i < list.size(); i++) { |
| 1468 | final ShortcutInfo si = list.get(i); |
| 1469 | if (si.getRank() != i) { |
| 1470 | failed = true; |
| 1471 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1472 | + " rank=" + si.getRank() + " but expected to be "+ i); |
| 1473 | } |
| 1474 | } |
| 1475 | return failed; |
| 1476 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1477 | } |