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; |
Mehdi Alizadeh | ebb4b60 | 2019-02-05 15:52:18 -0800 | [diff] [blame] | 21 | import android.app.Person; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.Intent; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 24 | import android.content.IntentFilter; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageInfo; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 26 | import android.content.pm.ShortcutInfo; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 27 | import android.content.pm.ShortcutManager; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 28 | import android.content.res.Resources; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 29 | import android.os.PersistableBundle; |
| 30 | import android.text.format.Formatter; |
| 31 | import android.util.ArrayMap; |
| 32 | import android.util.ArraySet; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 33 | import android.util.Log; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 34 | import android.util.Slog; |
| 35 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 36 | import com.android.internal.annotations.VisibleForTesting; |
Mehdi Alizadeh | ebb4b60 | 2019-02-05 15:52:18 -0800 | [diff] [blame] | 37 | import com.android.internal.util.ArrayUtils; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 38 | import com.android.internal.util.Preconditions; |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 39 | import com.android.internal.util.XmlUtils; |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 40 | import com.android.server.pm.ShortcutService.DumpFilter; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 41 | import com.android.server.pm.ShortcutService.ShortcutOperation; |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 42 | import com.android.server.pm.ShortcutService.Stats; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 43 | |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 44 | import org.json.JSONException; |
| 45 | import org.json.JSONObject; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 46 | import org.xmlpull.v1.XmlPullParser; |
| 47 | import org.xmlpull.v1.XmlPullParserException; |
| 48 | import org.xmlpull.v1.XmlSerializer; |
| 49 | |
| 50 | import java.io.File; |
| 51 | import java.io.IOException; |
| 52 | import java.io.PrintWriter; |
| 53 | import java.util.ArrayList; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 54 | import java.util.Collections; |
| 55 | import java.util.Comparator; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 56 | import java.util.List; |
Makoto Onuki | be73a80 | 2016-04-15 14:46:35 -0700 | [diff] [blame] | 57 | import java.util.Set; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 58 | import java.util.function.Predicate; |
| 59 | |
| 60 | /** |
| 61 | * Package information used by {@link ShortcutService}. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 62 | * User information used by {@link ShortcutService}. |
| 63 | * |
| 64 | * All methods should be guarded by {@code #mShortcutUser.mService.mLock}. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 65 | */ |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 66 | class ShortcutPackage extends ShortcutPackageItem { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 67 | private static final String TAG = ShortcutService.TAG; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 68 | private static final String TAG_VERIFY = ShortcutService.TAG + ".verify"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 69 | |
| 70 | static final String TAG_ROOT = "package"; |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 71 | private static final String TAG_INTENT_EXTRAS_LEGACY = "intent-extras"; |
| 72 | private static final String TAG_INTENT = "intent"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 73 | private static final String TAG_EXTRAS = "extras"; |
| 74 | private static final String TAG_SHORTCUT = "shortcut"; |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 75 | private static final String TAG_CATEGORIES = "categories"; |
Mehdi Alizadeh | ebb4b60 | 2019-02-05 15:52:18 -0800 | [diff] [blame] | 76 | private static final String TAG_PERSON = "person"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 77 | |
| 78 | private static final String ATTR_NAME = "name"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 79 | private static final String ATTR_CALL_COUNT = "call-count"; |
| 80 | private static final String ATTR_LAST_RESET = "last-reset"; |
| 81 | private static final String ATTR_ID = "id"; |
| 82 | private static final String ATTR_ACTIVITY = "activity"; |
| 83 | private static final String ATTR_TITLE = "title"; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 84 | private static final String ATTR_TITLE_RES_ID = "titleid"; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 85 | private static final String ATTR_TITLE_RES_NAME = "titlename"; |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 86 | private static final String ATTR_TEXT = "text"; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 87 | private static final String ATTR_TEXT_RES_ID = "textid"; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 88 | private static final String ATTR_TEXT_RES_NAME = "textname"; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 89 | private static final String ATTR_DISABLED_MESSAGE = "dmessage"; |
| 90 | private static final String ATTR_DISABLED_MESSAGE_RES_ID = "dmessageid"; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 91 | private static final String ATTR_DISABLED_MESSAGE_RES_NAME = "dmessagename"; |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 92 | private static final String ATTR_DISABLED_REASON = "disabled-reason"; |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 93 | private static final String ATTR_INTENT_LEGACY = "intent"; |
| 94 | private static final String ATTR_INTENT_NO_EXTRA = "intent-base"; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 95 | private static final String ATTR_RANK = "rank"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 96 | private static final String ATTR_TIMESTAMP = "timestamp"; |
| 97 | private static final String ATTR_FLAGS = "flags"; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 98 | private static final String ATTR_ICON_RES_ID = "icon-res"; |
| 99 | private static final String ATTR_ICON_RES_NAME = "icon-resname"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 100 | private static final String ATTR_BITMAP_PATH = "bitmap-path"; |
| 101 | |
Mehdi Alizadeh | ebb4b60 | 2019-02-05 15:52:18 -0800 | [diff] [blame] | 102 | private static final String ATTR_PERSON_NAME = "name"; |
| 103 | private static final String ATTR_PERSON_URI = "uri"; |
| 104 | private static final String ATTR_PERSON_KEY = "key"; |
| 105 | private static final String ATTR_PERSON_IS_BOT = "is-bot"; |
| 106 | private static final String ATTR_PERSON_IS_IMPORTANT = "is-important"; |
| 107 | |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 108 | private static final String NAME_CATEGORIES = "categories"; |
| 109 | |
| 110 | private static final String TAG_STRING_ARRAY_XMLUTILS = "string-array"; |
| 111 | private static final String ATTR_NAME_XMLUTILS = "name"; |
| 112 | |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 113 | private static final String KEY_DYNAMIC = "dynamic"; |
| 114 | private static final String KEY_MANIFEST = "manifest"; |
| 115 | private static final String KEY_PINNED = "pinned"; |
| 116 | private static final String KEY_BITMAPS = "bitmaps"; |
| 117 | private static final String KEY_BITMAP_BYTES = "bitmapBytes"; |
| 118 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 119 | /** |
| 120 | * All the shortcuts from the package, keyed on IDs. |
| 121 | */ |
| 122 | final private ArrayMap<String, ShortcutInfo> mShortcuts = new ArrayMap<>(); |
| 123 | |
| 124 | /** |
Mehdi Alizadeh | 3277462 | 2018-11-05 17:32:01 -0800 | [diff] [blame] | 125 | * All the share targets from the package |
| 126 | */ |
| 127 | private final ArrayList<ShareTargetInfo> mShareTargets = new ArrayList<>(0); |
| 128 | |
| 129 | /** |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 130 | * # of times the package has called rate-limited APIs. |
| 131 | */ |
| 132 | private int mApiCallCount; |
| 133 | |
| 134 | /** |
| 135 | * When {@link #mApiCallCount} was reset last time. |
| 136 | */ |
| 137 | private long mLastResetTime; |
| 138 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 139 | private final int mPackageUid; |
| 140 | |
| 141 | private long mLastKnownForegroundElapsedTime; |
| 142 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 143 | private ShortcutPackage(ShortcutUser shortcutUser, |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 144 | int packageUserId, String packageName, ShortcutPackageInfo spi) { |
| 145 | super(shortcutUser, packageUserId, packageName, |
| 146 | spi != null ? spi : ShortcutPackageInfo.newEmpty()); |
| 147 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 148 | mPackageUid = shortcutUser.mService.injectGetPackageUid(packageName, packageUserId); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 151 | public ShortcutPackage(ShortcutUser shortcutUser, int packageUserId, String packageName) { |
| 152 | this(shortcutUser, packageUserId, packageName, null); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public int getOwnerUserId() { |
| 157 | // For packages, always owner user == package user. |
| 158 | return getPackageUserId(); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 161 | public int getPackageUid() { |
| 162 | return mPackageUid; |
| 163 | } |
| 164 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 165 | @Nullable |
| 166 | public Resources getPackageResources() { |
| 167 | return mShortcutUser.mService.injectGetResourcesForApplicationAsUser( |
| 168 | getPackageName(), getPackageUserId()); |
| 169 | } |
| 170 | |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 171 | public int getShortcutCount() { |
| 172 | return mShortcuts.size(); |
| 173 | } |
| 174 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 175 | @Override |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 176 | protected boolean canRestoreAnyVersion() { |
| 177 | return false; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 181 | protected void onRestored(int restoreBlockReason) { |
| 182 | // Shortcuts have been restored. |
| 183 | // - Unshadow all shortcuts. |
| 184 | // - Set disabled reason. |
| 185 | // - Disable if needed. |
| 186 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 187 | ShortcutInfo si = mShortcuts.valueAt(i); |
| 188 | si.clearFlags(ShortcutInfo.FLAG_SHADOW); |
| 189 | |
| 190 | si.setDisabledReason(restoreBlockReason); |
| 191 | if (restoreBlockReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED) { |
| 192 | si.addFlags(ShortcutInfo.FLAG_DISABLED); |
| 193 | } |
| 194 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 195 | // Because some launchers may not have been restored (e.g. allowBackup=false), |
| 196 | // we need to re-calculate the pinned shortcuts. |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 197 | refreshPinnedFlags(); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Note this does *not* provide a correct view to the calling launcher. |
| 202 | */ |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 203 | @Nullable |
| 204 | public ShortcutInfo findShortcutById(String id) { |
| 205 | return mShortcuts.get(id); |
| 206 | } |
| 207 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 208 | public boolean isShortcutExistsAndInvisibleToPublisher(String id) { |
| 209 | ShortcutInfo si = findShortcutById(id); |
| 210 | return si != null && !si.isVisibleToPublisher(); |
| 211 | } |
| 212 | |
| 213 | public boolean isShortcutExistsAndVisibleToPublisher(String id) { |
| 214 | ShortcutInfo si = findShortcutById(id); |
| 215 | return si != null && si.isVisibleToPublisher(); |
| 216 | } |
| 217 | |
| 218 | private void ensureNotImmutable(@Nullable ShortcutInfo shortcut, boolean ignoreInvisible) { |
| 219 | if (shortcut != null && shortcut.isImmutable() |
| 220 | && (!ignoreInvisible || shortcut.isVisibleToPublisher())) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 221 | throw new IllegalArgumentException( |
| 222 | "Manifest shortcut ID=" + shortcut.getId() |
| 223 | + " may not be manipulated via APIs"); |
| 224 | } |
| 225 | } |
| 226 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 227 | public void ensureNotImmutable(@NonNull String id, boolean ignoreInvisible) { |
| 228 | ensureNotImmutable(mShortcuts.get(id), ignoreInvisible); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 231 | public void ensureImmutableShortcutsNotIncludedWithIds(@NonNull List<String> shortcutIds, |
| 232 | boolean ignoreInvisible) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 233 | for (int i = shortcutIds.size() - 1; i >= 0; i--) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 234 | ensureNotImmutable(shortcutIds.get(i), ignoreInvisible); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 238 | public void ensureImmutableShortcutsNotIncluded(@NonNull List<ShortcutInfo> shortcuts, |
| 239 | boolean ignoreInvisible) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 240 | for (int i = shortcuts.size() - 1; i >= 0; i--) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 241 | ensureNotImmutable(shortcuts.get(i).getId(), ignoreInvisible); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 245 | /** |
| 246 | * Delete a shortcut by ID. This will *always* remove it even if it's immutable or invisible. |
| 247 | */ |
| 248 | private ShortcutInfo forceDeleteShortcutInner(@NonNull String id) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 249 | final ShortcutInfo shortcut = mShortcuts.remove(id); |
| 250 | if (shortcut != null) { |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 251 | mShortcutUser.mService.removeIconLocked(shortcut); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 252 | shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 253 | | ShortcutInfo.FLAG_MANIFEST); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 254 | } |
| 255 | return shortcut; |
| 256 | } |
| 257 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 258 | /** |
| 259 | * Force replace a shortcut. If there's already a shortcut with the same ID, it'll be removed, |
| 260 | * even if it's invisible. |
| 261 | */ |
| 262 | private void forceReplaceShortcutInner(@NonNull ShortcutInfo newShortcut) { |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 263 | final ShortcutService s = mShortcutUser.mService; |
| 264 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 265 | forceDeleteShortcutInner(newShortcut.getId()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 266 | |
| 267 | // Extract Icon and update the icon res ID and the bitmap path. |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 268 | s.saveIconAndFixUpShortcutLocked(newShortcut); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 269 | s.fixUpShortcutResourceNamesAndValues(newShortcut); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 270 | mShortcuts.put(newShortcut.getId(), newShortcut); |
| 271 | } |
| 272 | |
| 273 | /** |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 274 | * Add a shortcut. If there's already a one with the same ID, it'll be removed, even if it's |
| 275 | * invisible. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 276 | * |
| 277 | * It checks the max number of dynamic shortcuts. |
| 278 | */ |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 279 | public void addOrReplaceDynamicShortcut(@NonNull ShortcutInfo newShortcut) { |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 280 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 281 | Preconditions.checkArgument(newShortcut.isEnabled(), |
| 282 | "add/setDynamicShortcuts() cannot publish disabled shortcuts"); |
| 283 | |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 284 | newShortcut.addFlags(ShortcutInfo.FLAG_DYNAMIC); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 285 | |
| 286 | final ShortcutInfo oldShortcut = mShortcuts.get(newShortcut.getId()); |
| 287 | |
| 288 | final boolean wasPinned; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 289 | |
| 290 | if (oldShortcut == null) { |
| 291 | wasPinned = false; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 292 | } else { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 293 | // It's an update case. |
| 294 | // Make sure the target is updatable. (i.e. should be mutable.) |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 295 | oldShortcut.ensureUpdatableWith(newShortcut, /*isUpdating=*/ false); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 296 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 297 | wasPinned = oldShortcut.isPinned(); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 300 | // If it was originally pinned, the new one should be pinned too. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 301 | if (wasPinned) { |
| 302 | newShortcut.addFlags(ShortcutInfo.FLAG_PINNED); |
| 303 | } |
| 304 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 305 | forceReplaceShortcutInner(newShortcut); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Remove all shortcuts that aren't pinned nor dynamic. |
| 310 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 311 | private void removeOrphans() { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 312 | ArrayList<String> removeList = null; // Lazily initialize. |
| 313 | |
| 314 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 315 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 316 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 317 | if (si.isAlive()) continue; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 318 | |
| 319 | if (removeList == null) { |
| 320 | removeList = new ArrayList<>(); |
| 321 | } |
| 322 | removeList.add(si.getId()); |
| 323 | } |
| 324 | if (removeList != null) { |
| 325 | for (int i = removeList.size() - 1; i >= 0; i--) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 326 | forceDeleteShortcutInner(removeList.get(i)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Remove all dynamic shortcuts. |
| 333 | */ |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 334 | public void deleteAllDynamicShortcuts(boolean ignoreInvisible) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 335 | final long now = mShortcutUser.mService.injectCurrentTimeMillis(); |
| 336 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 337 | boolean changed = false; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 338 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 339 | final ShortcutInfo si = mShortcuts.valueAt(i); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 340 | if (si.isDynamic() && (!ignoreInvisible || si.isVisibleToPublisher())) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 341 | changed = true; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 342 | |
| 343 | si.setTimestamp(now); |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 344 | si.clearFlags(ShortcutInfo.FLAG_DYNAMIC); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 345 | si.setRank(0); // It may still be pinned, so clear the rank. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 346 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 347 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 348 | if (changed) { |
| 349 | removeOrphans(); |
| 350 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /** |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 354 | * Remove a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut |
| 355 | * 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] | 356 | * |
| 357 | * @return true if it's actually removed because it wasn't pinned, or false if it's still |
| 358 | * pinned. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 359 | */ |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 360 | public boolean deleteDynamicWithId(@NonNull String shortcutId, boolean ignoreInvisible) { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 361 | final ShortcutInfo removed = deleteOrDisableWithId( |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 362 | shortcutId, /* disable =*/ false, /* overrideImmutable=*/ false, ignoreInvisible, |
| 363 | ShortcutInfo.DISABLED_REASON_NOT_DISABLED); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 364 | return removed == null; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut |
| 369 | * is pinned, it'll remain as a pinned shortcut, but will be disabled. |
| 370 | * |
| 371 | * @return true if it's actually removed because it wasn't pinned, or false if it's still |
| 372 | * pinned. |
| 373 | */ |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 374 | private boolean disableDynamicWithId(@NonNull String shortcutId, boolean ignoreInvisible, |
| 375 | int disabledReason) { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 376 | final ShortcutInfo disabled = deleteOrDisableWithId( |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 377 | shortcutId, /* disable =*/ true, /* overrideImmutable=*/ false, ignoreInvisible, |
| 378 | disabledReason); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 379 | return disabled == null; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Disable a dynamic shortcut by ID. It'll be removed from the dynamic set, but if the shortcut |
| 384 | * is pinned, it'll remain as a pinned shortcut but will be disabled. |
| 385 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 386 | public void disableWithId(@NonNull String shortcutId, String disabledMessage, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 387 | int disabledMessageResId, boolean overrideImmutable, boolean ignoreInvisible, |
| 388 | int disabledReason) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 389 | final ShortcutInfo disabled = deleteOrDisableWithId(shortcutId, /* disable =*/ true, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 390 | overrideImmutable, ignoreInvisible, disabledReason); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 391 | |
| 392 | if (disabled != null) { |
| 393 | if (disabledMessage != null) { |
| 394 | disabled.setDisabledMessage(disabledMessage); |
| 395 | } else if (disabledMessageResId != 0) { |
| 396 | disabled.setDisabledMessageResId(disabledMessageResId); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 397 | |
| 398 | mShortcutUser.mService.fixUpShortcutResourceNamesAndValues(disabled); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | @Nullable |
| 404 | private ShortcutInfo deleteOrDisableWithId(@NonNull String shortcutId, boolean disable, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 405 | boolean overrideImmutable, boolean ignoreInvisible, int disabledReason) { |
| 406 | Preconditions.checkState( |
| 407 | (disable == (disabledReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED)), |
| 408 | "disable and disabledReason disagree: " + disable + " vs " + disabledReason); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 409 | final ShortcutInfo oldShortcut = mShortcuts.get(shortcutId); |
| 410 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 411 | if (oldShortcut == null || !oldShortcut.isEnabled() |
| 412 | && (ignoreInvisible && !oldShortcut.isVisibleToPublisher())) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 413 | return null; // Doesn't exist or already disabled. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 414 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 415 | if (!overrideImmutable) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 416 | ensureNotImmutable(oldShortcut, /*ignoreInvisible=*/ true); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 417 | } |
| 418 | if (oldShortcut.isPinned()) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 419 | |
| 420 | oldShortcut.setRank(0); |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 421 | oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_MANIFEST); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 422 | if (disable) { |
| 423 | oldShortcut.addFlags(ShortcutInfo.FLAG_DISABLED); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 424 | // Do not overwrite the disabled reason if one is alreay set. |
| 425 | if (oldShortcut.getDisabledReason() == ShortcutInfo.DISABLED_REASON_NOT_DISABLED) { |
| 426 | oldShortcut.setDisabledReason(disabledReason); |
| 427 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 428 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 429 | oldShortcut.setTimestamp(mShortcutUser.mService.injectCurrentTimeMillis()); |
| 430 | |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 431 | // See ShortcutRequestPinProcessor.directPinShortcut(). |
| 432 | if (mShortcutUser.mService.isDummyMainActivity(oldShortcut.getActivity())) { |
| 433 | oldShortcut.setActivity(null); |
| 434 | } |
| 435 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 436 | return oldShortcut; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 437 | } else { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 438 | forceDeleteShortcutInner(shortcutId); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 439 | return null; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | public void enableWithId(@NonNull String shortcutId) { |
| 444 | final ShortcutInfo shortcut = mShortcuts.get(shortcutId); |
| 445 | if (shortcut != null) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 446 | ensureNotImmutable(shortcut, /*ignoreInvisible=*/ true); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 447 | shortcut.clearFlags(ShortcutInfo.FLAG_DISABLED); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 448 | shortcut.setDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 452 | public void updateInvisibleShortcutForPinRequestWith(@NonNull ShortcutInfo shortcut) { |
| 453 | final ShortcutInfo source = mShortcuts.get(shortcut.getId()); |
| 454 | Preconditions.checkNotNull(source); |
| 455 | |
| 456 | mShortcutUser.mService.validateShortcutForPinRequest(shortcut); |
| 457 | |
| 458 | shortcut.addFlags(ShortcutInfo.FLAG_PINNED); |
| 459 | |
| 460 | forceReplaceShortcutInner(shortcut); |
| 461 | |
| 462 | adjustRanks(); |
| 463 | } |
| 464 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 465 | /** |
| 466 | * Called after a launcher updates the pinned set. For each shortcut in this package, |
| 467 | * set FLAG_PINNED if any launcher has pinned it. Otherwise, clear it. |
| 468 | * |
| 469 | * <p>Then remove all shortcuts that are not dynamic and no longer pinned either. |
| 470 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 471 | public void refreshPinnedFlags() { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 472 | // First, un-pin all shortcuts |
| 473 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 474 | mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_PINNED); |
| 475 | } |
| 476 | |
| 477 | // Then, for the pinned set for each launcher, set the pin flag one by one. |
Makoto Onuki | 47ed171 | 2017-12-20 14:40:20 +0900 | [diff] [blame] | 478 | mShortcutUser.forAllLaunchers(launcherShortcuts -> { |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 479 | final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds( |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 480 | getPackageName(), getPackageUserId()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 481 | |
| 482 | if (pinned == null || pinned.size() == 0) { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 483 | return; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 484 | } |
| 485 | for (int i = pinned.size() - 1; i >= 0; i--) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 486 | final String id = pinned.valueAt(i); |
| 487 | final ShortcutInfo si = mShortcuts.get(id); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 488 | if (si == null) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 489 | // This happens if a launcher pinned shortcuts from this package, then backup& |
| 490 | // restored, but this package doesn't allow backing up. |
| 491 | // In that case the launcher ends up having a dangling pinned shortcuts. |
| 492 | // That's fine, when the launcher is restored, we'll fix it. |
| 493 | continue; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 494 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 495 | si.addFlags(ShortcutInfo.FLAG_PINNED); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 496 | } |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 497 | }); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 498 | |
| 499 | // Lastly, remove the ones that are no longer pinned nor dynamic. |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 500 | removeOrphans(); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Number of calls that the caller has made, since the last reset. |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 505 | * |
| 506 | * <p>This takes care of the resetting the counter for foreground apps as well as after |
| 507 | * locale changes. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 508 | */ |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 509 | public int getApiCallCount(boolean unlimited) { |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 510 | final ShortcutService s = mShortcutUser.mService; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 511 | |
| 512 | // Reset the counter if: |
| 513 | // - the package is in foreground now. |
| 514 | // - the package is *not* in foreground now, but was in foreground at some point |
| 515 | // since the previous time it had been. |
| 516 | if (s.isUidForegroundLocked(mPackageUid) |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 517 | || (mLastKnownForegroundElapsedTime |
| 518 | < s.getUidLastForegroundElapsedTimeLocked(mPackageUid)) |
| 519 | || unlimited) { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 520 | mLastKnownForegroundElapsedTime = s.injectElapsedRealtime(); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 521 | resetRateLimiting(); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | // Note resetThrottlingIfNeeded() and resetRateLimiting() will set 0 to mApiCallCount, |
| 525 | // but we just can't return 0 at this point, because we may have to update |
| 526 | // mLastResetTime. |
| 527 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 528 | final long last = s.getLastResetTimeLocked(); |
| 529 | |
| 530 | final long now = s.injectCurrentTimeMillis(); |
| 531 | if (ShortcutService.isClockValid(now) && mLastResetTime > now) { |
| 532 | Slog.w(TAG, "Clock rewound"); |
| 533 | // Clock rewound. |
| 534 | mLastResetTime = now; |
| 535 | mApiCallCount = 0; |
| 536 | return mApiCallCount; |
| 537 | } |
| 538 | |
| 539 | // If not reset yet, then reset. |
| 540 | if (mLastResetTime < last) { |
| 541 | if (ShortcutService.DEBUG) { |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 542 | Slog.d(TAG, String.format("%s: last reset=%d, now=%d, last=%d: resetting", |
| 543 | getPackageName(), mLastResetTime, now, last)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 544 | } |
| 545 | mApiCallCount = 0; |
| 546 | mLastResetTime = last; |
| 547 | } |
| 548 | return mApiCallCount; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * If the caller app hasn't been throttled yet, increment {@link #mApiCallCount} |
| 553 | * and return true. Otherwise just return false. |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 554 | * |
| 555 | * <p>This takes care of the resetting the counter for foreground apps as well as after |
| 556 | * locale changes, which is done internally by {@link #getApiCallCount}. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 557 | */ |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 558 | public boolean tryApiCall(boolean unlimited) { |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 559 | final ShortcutService s = mShortcutUser.mService; |
| 560 | |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 561 | if (getApiCallCount(unlimited) >= s.mMaxUpdatesPerInterval) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 562 | return false; |
| 563 | } |
| 564 | mApiCallCount++; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 565 | s.scheduleSaveUser(getOwnerUserId()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 566 | return true; |
| 567 | } |
| 568 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 569 | public void resetRateLimiting() { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 570 | if (ShortcutService.DEBUG) { |
| 571 | Slog.d(TAG, "resetRateLimiting: " + getPackageName()); |
| 572 | } |
| 573 | if (mApiCallCount > 0) { |
| 574 | mApiCallCount = 0; |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 575 | mShortcutUser.mService.scheduleSaveUser(getOwnerUserId()); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | public void resetRateLimitingForCommandLineNoSaving() { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 580 | mApiCallCount = 0; |
| 581 | mLastResetTime = 0; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Find all shortcuts that match {@code query}. |
| 586 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 587 | public void findAll(@NonNull List<ShortcutInfo> result, |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 588 | @Nullable Predicate<ShortcutInfo> query, int cloneFlag) { |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 589 | findAll(result, query, cloneFlag, null, 0, /*getPinnedByAnyLauncher=*/ false); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Find all shortcuts that match {@code query}. |
| 594 | * |
| 595 | * This will also provide a "view" for each launcher -- a non-dynamic shortcut that's not pinned |
| 596 | * by the calling launcher will not be included in the result, and also "isPinned" will be |
| 597 | * adjusted for the caller too. |
| 598 | */ |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 599 | public void findAll(@NonNull List<ShortcutInfo> result, |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 600 | @Nullable Predicate<ShortcutInfo> query, int cloneFlag, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 601 | @Nullable String callingLauncher, int launcherUserId, boolean getPinnedByAnyLauncher) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 602 | if (getPackageInfo().isShadow()) { |
| 603 | // Restored and the app not installed yet, so don't return any. |
| 604 | return; |
| 605 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 606 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 607 | final ShortcutService s = mShortcutUser.mService; |
| 608 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 609 | // Set of pinned shortcuts by the calling launcher. |
| 610 | final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 611 | : s.getLauncherShortcutsLocked(callingLauncher, getPackageUserId(), launcherUserId) |
| 612 | .getPinnedShortcutIds(getPackageName(), getPackageUserId()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 613 | |
| 614 | for (int i = 0; i < mShortcuts.size(); i++) { |
| 615 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 616 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 617 | // Need to adjust PINNED flag depending on the caller. |
| 618 | // Basically if the caller is a launcher (callingLauncher != null) and the launcher |
| 619 | // 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] | 620 | final boolean isPinnedByCaller = (callingLauncher == null) |
| 621 | || ((pinnedByCallerSet != null) && pinnedByCallerSet.contains(si.getId())); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 622 | |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 623 | if (!getPinnedByAnyLauncher) { |
| 624 | if (si.isFloating()) { |
| 625 | if (!isPinnedByCaller) { |
| 626 | continue; |
| 627 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | final ShortcutInfo clone = si.clone(cloneFlag); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 631 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 632 | // Fix up isPinned for the caller. Note we need to do it before the "test" callback, |
| 633 | // since it may check isPinned. |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 634 | // However, if getPinnedByAnyLauncher is set, we do it after the test. |
| 635 | if (!getPinnedByAnyLauncher) { |
| 636 | if (!isPinnedByCaller) { |
| 637 | clone.clearFlags(ShortcutInfo.FLAG_PINNED); |
| 638 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 639 | } |
| 640 | if (query == null || query.test(clone)) { |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 641 | if (!isPinnedByCaller) { |
| 642 | clone.clearFlags(ShortcutInfo.FLAG_PINNED); |
| 643 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 644 | result.add(clone); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | public void resetThrottling() { |
| 650 | mApiCallCount = 0; |
| 651 | } |
| 652 | |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 653 | /** |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 654 | * Returns a list of ShortcutInfos that match the given intent filter and the category of |
| 655 | * available ShareTarget definitions in this package. |
| 656 | */ |
| 657 | public List<ShortcutManager.ShareShortcutInfo> getMatchingShareTargets( |
| 658 | @NonNull IntentFilter filter) { |
| 659 | final List<ShareTargetInfo> matchedTargets = new ArrayList<>(); |
| 660 | for (int i = 0; i < mShareTargets.size(); i++) { |
| 661 | final ShareTargetInfo target = mShareTargets.get(i); |
| 662 | for (ShareTargetInfo.TargetData data : target.mTargetData) { |
| 663 | if (filter.hasDataType(data.mMimeType)) { |
| 664 | // Matched at least with one data type |
| 665 | matchedTargets.add(target); |
| 666 | break; |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | if (matchedTargets.isEmpty()) { |
| 672 | return new ArrayList<>(); |
| 673 | } |
| 674 | |
| 675 | // Get the list of all dynamic shortcuts in this package |
| 676 | final ArrayList<ShortcutInfo> shortcuts = new ArrayList<>(); |
| 677 | findAll(shortcuts, ShortcutInfo::isDynamicVisible, ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER); |
| 678 | |
| 679 | final List<ShortcutManager.ShareShortcutInfo> result = new ArrayList<>(); |
| 680 | for (int i = 0; i < shortcuts.size(); i++) { |
| 681 | final ShortcutInfo si = shortcuts.get(i); |
| 682 | for (int j = 0; j < matchedTargets.size(); j++) { |
| 683 | // Shortcut must have all of share target categories |
| 684 | boolean hasAllCategories = true; |
| 685 | final ShareTargetInfo target = matchedTargets.get(j); |
| 686 | for (int q = 0; q < target.mCategories.length; q++) { |
| 687 | if (!si.getCategories().contains(target.mCategories[q])) { |
| 688 | hasAllCategories = false; |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | if (hasAllCategories) { |
| 693 | result.add(new ShortcutManager.ShareShortcutInfo(si, new ComponentName( |
| 694 | getPackageName(), target.mTargetClass))); |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | return result; |
| 700 | } |
| 701 | |
Mehdi Alizadeh | 85fd3d5 | 2019-01-23 12:49:53 -0800 | [diff] [blame] | 702 | public boolean hasShareTargets() { |
| 703 | return !mShareTargets.isEmpty(); |
| 704 | } |
| 705 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 706 | /** |
Makoto Onuki | 6c1dbd5 | 2016-05-02 15:19:32 -0700 | [diff] [blame] | 707 | * Return the filenames (excluding path names) of icon bitmap files from this package. |
| 708 | */ |
| 709 | public ArraySet<String> getUsedBitmapFiles() { |
| 710 | final ArraySet<String> usedFiles = new ArraySet<>(mShortcuts.size()); |
| 711 | |
| 712 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 713 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 714 | if (si.getBitmapPath() != null) { |
| 715 | usedFiles.add(getFileName(si.getBitmapPath())); |
| 716 | } |
| 717 | } |
| 718 | return usedFiles; |
| 719 | } |
| 720 | |
| 721 | private static String getFileName(@NonNull String path) { |
| 722 | final int sep = path.lastIndexOf(File.separatorChar); |
| 723 | if (sep == -1) { |
| 724 | return path; |
| 725 | } else { |
| 726 | return path.substring(sep + 1); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /** |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 731 | * @return false if any of the target activities are no longer enabled. |
| 732 | */ |
| 733 | private boolean areAllActivitiesStillEnabled() { |
| 734 | if (mShortcuts.size() == 0) { |
| 735 | return true; |
| 736 | } |
| 737 | final ShortcutService s = mShortcutUser.mService; |
| 738 | |
| 739 | // Normally the number of target activities is 1 or so, so no need to use a complex |
| 740 | // structure like a set. |
| 741 | final ArrayList<ComponentName> checked = new ArrayList<>(4); |
| 742 | |
| 743 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 744 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 745 | final ComponentName activity = si.getActivity(); |
| 746 | |
| 747 | if (checked.contains(activity)) { |
| 748 | continue; // Already checked. |
| 749 | } |
| 750 | checked.add(activity); |
| 751 | |
Makoto Onuki | 40dc211 | 2017-10-18 12:52:45 -0700 | [diff] [blame] | 752 | if ((activity != null) |
| 753 | && !s.injectIsActivityEnabledAndExported(activity, getOwnerUserId())) { |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 754 | return false; |
| 755 | } |
| 756 | } |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Called when the package may be added or updated, or its activities may be disabled, and |
| 762 | * if so, rescan the package and do the necessary stuff. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 763 | * |
| 764 | * Add case: |
| 765 | * - Publish manifest shortcuts. |
| 766 | * |
| 767 | * Update case: |
| 768 | * - Re-publish manifest shortcuts. |
| 769 | * - If there are shortcuts with resources (icons or strings), update their timestamps. |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 770 | * - Disable shortcuts whose target activities are disabled. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 771 | * |
| 772 | * @return TRUE if any shortcuts have been changed. |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 773 | */ |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 774 | public boolean rescanPackageIfNeeded(boolean isNewApp, boolean forceRescan) { |
| 775 | final ShortcutService s = mShortcutUser.mService; |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 776 | final long start = s.getStatStartTime(); |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 777 | |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 778 | final PackageInfo pi; |
| 779 | try { |
| 780 | pi = mShortcutUser.mService.getPackageInfo( |
| 781 | getPackageName(), getPackageUserId()); |
| 782 | if (pi == null) { |
| 783 | return false; // Shouldn't happen. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 784 | } |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 785 | |
Makoto Onuki | 248a0ef | 2016-11-03 15:59:01 -0700 | [diff] [blame] | 786 | if (!isNewApp && !forceRescan) { |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 787 | // Return if the package hasn't changed, ie: |
| 788 | // - version code hasn't change |
| 789 | // - lastUpdateTime hasn't change |
| 790 | // - all target activities are still enabled. |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 791 | |
| 792 | // Note, system apps timestamps do *not* change after OTAs. (But they do |
| 793 | // after an adb sync or a local flash.) |
| 794 | // This means if a system app's version code doesn't change on an OTA, |
| 795 | // we don't notice it's updated. But that's fine since their version code *should* |
| 796 | // really change on OTAs. |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 797 | if ((getPackageInfo().getVersionCode() == pi.getLongVersionCode()) |
Makoto Onuki | 64183d5 | 2016-08-08 14:11:34 -0700 | [diff] [blame] | 798 | && (getPackageInfo().getLastUpdateTime() == pi.lastUpdateTime) |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 799 | && areAllActivitiesStillEnabled()) { |
| 800 | return false; |
| 801 | } |
| 802 | } |
| 803 | } finally { |
| 804 | s.logDurationStat(Stats.PACKAGE_UPDATE_CHECK, start); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | // Now prepare to publish manifest shortcuts. |
| 808 | List<ShortcutInfo> newManifestShortcutList = null; |
| 809 | try { |
| 810 | newManifestShortcutList = ShortcutParser.parseShortcuts(mShortcutUser.mService, |
Mehdi Alizadeh | 3277462 | 2018-11-05 17:32:01 -0800 | [diff] [blame] | 811 | getPackageName(), getPackageUserId(), mShareTargets); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 812 | } catch (IOException|XmlPullParserException e) { |
| 813 | Slog.e(TAG, "Failed to load shortcuts from AndroidManifest.xml.", e); |
| 814 | } |
| 815 | final int manifestShortcutSize = newManifestShortcutList == null ? 0 |
| 816 | : newManifestShortcutList.size(); |
| 817 | if (ShortcutService.DEBUG) { |
Mehdi Alizadeh | 3277462 | 2018-11-05 17:32:01 -0800 | [diff] [blame] | 818 | Slog.d(TAG, |
| 819 | String.format("Package %s has %d manifest shortcut(s), and %d share target(s)", |
| 820 | getPackageName(), manifestShortcutSize, mShareTargets.size())); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 821 | } |
| 822 | if (isNewApp && (manifestShortcutSize == 0)) { |
| 823 | // If it's a new app, and it doesn't have manifest shortcuts, then nothing to do. |
| 824 | |
| 825 | // If it's an update, then it may already have manifest shortcuts, which need to be |
| 826 | // disabled. |
| 827 | return false; |
| 828 | } |
| 829 | if (ShortcutService.DEBUG) { |
| 830 | Slog.d(TAG, String.format("Package %s %s, version %d -> %d", getPackageName(), |
| 831 | (isNewApp ? "added" : "updated"), |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 832 | getPackageInfo().getVersionCode(), pi.getLongVersionCode())); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 835 | getPackageInfo().updateFromPackageInfo(pi); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 836 | final long newVersionCode = getPackageInfo().getVersionCode(); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 837 | |
| 838 | // See if there are any shortcuts that were prevented restoring because the app was of a |
| 839 | // lower version, and re-enable them. |
| 840 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 841 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 842 | if (si.getDisabledReason() != ShortcutInfo.DISABLED_REASON_VERSION_LOWER) { |
| 843 | continue; |
| 844 | } |
| 845 | if (getPackageInfo().getBackupSourceVersionCode() > newVersionCode) { |
| 846 | if (ShortcutService.DEBUG) { |
| 847 | Slog.d(TAG, String.format("Shortcut %s require version %s, still not restored.", |
| 848 | si.getId(), getPackageInfo().getBackupSourceVersionCode())); |
| 849 | } |
| 850 | continue; |
| 851 | } |
| 852 | Slog.i(TAG, String.format("Restoring shortcut: %s", si.getId())); |
| 853 | si.clearFlags(ShortcutInfo.FLAG_DISABLED); |
| 854 | si.setDisabledReason(ShortcutInfo.DISABLED_REASON_NOT_DISABLED); |
| 855 | } |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 856 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 857 | // For existing shortcuts, update timestamps if they have any resources. |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 858 | // Also check if shortcuts' activities are still main activities. Otherwise, disable them. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 859 | if (!isNewApp) { |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 860 | Resources publisherRes = null; |
| 861 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 862 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 863 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 864 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 865 | // Disable dynamic shortcuts whose target activity is gone. |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 866 | if (si.isDynamic()) { |
Makoto Onuki | 3414553 | 2017-03-14 17:58:36 -0700 | [diff] [blame] | 867 | if (si.getActivity() == null) { |
| 868 | // Note if it's dynamic, it must have a target activity, but b/36228253. |
| 869 | s.wtf("null activity detected."); |
| 870 | // TODO Maybe remove it? |
| 871 | } else if (!s.injectIsMainActivity(si.getActivity(), getPackageUserId())) { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 872 | Slog.w(TAG, String.format( |
| 873 | "%s is no longer main activity. Disabling shorcut %s.", |
| 874 | getPackageName(), si.getId())); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 875 | if (disableDynamicWithId(si.getId(), /*ignoreInvisible*/ false, |
| 876 | ShortcutInfo.DISABLED_REASON_APP_CHANGED)) { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 877 | continue; // Actually removed. |
| 878 | } |
| 879 | // Still pinned, so fall-through and possibly update the resources. |
| 880 | } |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 883 | if (si.hasAnyResources()) { |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 884 | if (!si.isOriginallyFromManifest()) { |
| 885 | if (publisherRes == null) { |
| 886 | publisherRes = getPackageResources(); |
| 887 | if (publisherRes == null) { |
| 888 | break; // Resources couldn't be loaded. |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | // If this shortcut is not from a manifest, then update all resource IDs |
| 893 | // from resource names. (We don't allow resource strings for |
| 894 | // non-manifest at the moment, but icons can still be resources.) |
| 895 | si.lookupAndFillInResourceIds(publisherRes); |
| 896 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 897 | si.setTimestamp(s.injectCurrentTimeMillis()); |
| 898 | } |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 901 | |
| 902 | // (Re-)publish manifest shortcut. |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 903 | publishManifestShortcuts(newManifestShortcutList); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 904 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 905 | if (newManifestShortcutList != null) { |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 906 | pushOutExcessShortcuts(); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 909 | s.verifyStates(); |
| 910 | |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 911 | // This will send a notification to the launcher, and also save . |
| 912 | s.packageShortcutsChanged(getPackageName(), getPackageUserId()); |
| 913 | return true; // true means changed. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | private boolean publishManifestShortcuts(List<ShortcutInfo> newManifestShortcutList) { |
| 917 | if (ShortcutService.DEBUG) { |
| 918 | Slog.d(TAG, String.format( |
| 919 | "Package %s: publishing manifest shortcuts", getPackageName())); |
| 920 | } |
| 921 | boolean changed = false; |
| 922 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 923 | // Keep the previous IDs. |
| 924 | ArraySet<String> toDisableList = null; |
| 925 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 926 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 927 | |
| 928 | if (si.isManifestShortcut()) { |
| 929 | if (toDisableList == null) { |
| 930 | toDisableList = new ArraySet<>(); |
| 931 | } |
| 932 | toDisableList.add(si.getId()); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | // Publish new ones. |
| 937 | if (newManifestShortcutList != null) { |
| 938 | final int newListSize = newManifestShortcutList.size(); |
| 939 | |
| 940 | for (int i = 0; i < newListSize; i++) { |
| 941 | changed = true; |
| 942 | |
| 943 | final ShortcutInfo newShortcut = newManifestShortcutList.get(i); |
| 944 | final boolean newDisabled = !newShortcut.isEnabled(); |
| 945 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 946 | final String id = newShortcut.getId(); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 947 | final ShortcutInfo oldShortcut = mShortcuts.get(id); |
| 948 | |
| 949 | boolean wasPinned = false; |
| 950 | |
| 951 | if (oldShortcut != null) { |
| 952 | if (!oldShortcut.isOriginallyFromManifest()) { |
| 953 | Slog.e(TAG, "Shortcut with ID=" + newShortcut.getId() |
| 954 | + " exists but is not from AndroidManifest.xml, not updating."); |
| 955 | continue; |
| 956 | } |
| 957 | // Take over the pinned flag. |
| 958 | if (oldShortcut.isPinned()) { |
| 959 | wasPinned = true; |
| 960 | newShortcut.addFlags(ShortcutInfo.FLAG_PINNED); |
| 961 | } |
| 962 | } |
| 963 | if (newDisabled && !wasPinned) { |
| 964 | // If the shortcut is disabled, and it was *not* pinned, then this |
| 965 | // just doesn't have to be published. |
| 966 | // Just keep it in toDisableList, so the previous one would be removed. |
| 967 | continue; |
| 968 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 969 | |
| 970 | // Note even if enabled=false, we still need to update all fields, so do it |
| 971 | // regardless. |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 972 | forceReplaceShortcutInner(newShortcut); // This will clean up the old one too. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 973 | |
| 974 | if (!newDisabled && toDisableList != null) { |
| 975 | // Still alive, don't remove. |
| 976 | toDisableList.remove(id); |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | // Disable the previous manifest shortcuts that are no longer in the manifest. |
| 982 | if (toDisableList != null) { |
| 983 | if (ShortcutService.DEBUG) { |
| 984 | Slog.d(TAG, String.format( |
| 985 | "Package %s: disabling %d stale shortcuts", getPackageName(), |
| 986 | toDisableList.size())); |
| 987 | } |
| 988 | for (int i = toDisableList.size() - 1; i >= 0; i--) { |
| 989 | changed = true; |
| 990 | |
| 991 | final String id = toDisableList.valueAt(i); |
| 992 | |
| 993 | disableWithId(id, /* disable message =*/ null, /* disable message resid */ 0, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 994 | /* overrideImmutable=*/ true, /*ignoreInvisible=*/ false, |
| 995 | ShortcutInfo.DISABLED_REASON_APP_CHANGED); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 996 | } |
| 997 | removeOrphans(); |
| 998 | } |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 999 | adjustRanks(); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1000 | return changed; |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1003 | /** |
| 1004 | * For each target activity, make sure # of dynamic + manifest shortcuts <= max. |
| 1005 | * If too many, we'll remove the dynamic with the lowest ranks. |
| 1006 | */ |
| 1007 | private boolean pushOutExcessShortcuts() { |
| 1008 | final ShortcutService service = mShortcutUser.mService; |
| 1009 | final int maxShortcuts = service.getMaxActivityShortcuts(); |
| 1010 | |
| 1011 | boolean changed = false; |
| 1012 | |
| 1013 | final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all = |
| 1014 | sortShortcutsToActivities(); |
| 1015 | for (int outer = all.size() - 1; outer >= 0; outer--) { |
| 1016 | final ArrayList<ShortcutInfo> list = all.valueAt(outer); |
| 1017 | if (list.size() <= maxShortcuts) { |
| 1018 | continue; |
| 1019 | } |
| 1020 | // Sort by isManifestShortcut() and getRank(). |
| 1021 | Collections.sort(list, mShortcutTypeAndRankComparator); |
| 1022 | |
| 1023 | // Keep [0 .. max), and remove (as dynamic) [max .. size) |
| 1024 | for (int inner = list.size() - 1; inner >= maxShortcuts; inner--) { |
| 1025 | final ShortcutInfo shortcut = list.get(inner); |
| 1026 | |
| 1027 | if (shortcut.isManifestShortcut()) { |
| 1028 | // This shouldn't happen -- excess shortcuts should all be non-manifest. |
| 1029 | // But just in case. |
| 1030 | service.wtf("Found manifest shortcuts in excess list."); |
| 1031 | continue; |
| 1032 | } |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1033 | deleteDynamicWithId(shortcut.getId(), /*ignoreInvisible=*/ true); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1034 | } |
| 1035 | } |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1036 | |
| 1037 | return changed; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * To sort by isManifestShortcut() and getRank(). i.e. manifest shortcuts come before |
| 1042 | * non-manifest shortcuts, then sort by rank. |
| 1043 | * |
| 1044 | * This is used to decide which dynamic shortcuts to remove when an upgraded version has more |
| 1045 | * manifest shortcuts than before and as a result we need to remove some of the dynamic |
| 1046 | * shortcuts. We sort manifest + dynamic shortcuts by this order, and remove the ones with |
| 1047 | * the last ones. |
| 1048 | * |
| 1049 | * (Note the number of manifest shortcuts is always <= the max number, because if there are |
| 1050 | * more, ShortcutParser would ignore the rest.) |
| 1051 | */ |
| 1052 | final Comparator<ShortcutInfo> mShortcutTypeAndRankComparator = (ShortcutInfo a, |
| 1053 | ShortcutInfo b) -> { |
| 1054 | if (a.isManifestShortcut() && !b.isManifestShortcut()) { |
| 1055 | return -1; |
| 1056 | } |
| 1057 | if (!a.isManifestShortcut() && b.isManifestShortcut()) { |
| 1058 | return 1; |
| 1059 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1060 | return Integer.compare(a.getRank(), b.getRank()); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1061 | }; |
| 1062 | |
| 1063 | /** |
| 1064 | * Build a list of shortcuts for each target activity and return as a map. The result won't |
| 1065 | * contain "floating" shortcuts because they don't belong on any activities. |
| 1066 | */ |
| 1067 | private ArrayMap<ComponentName, ArrayList<ShortcutInfo>> sortShortcutsToActivities() { |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1068 | final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> activitiesToShortcuts |
| 1069 | = new ArrayMap<>(); |
| 1070 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 1071 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 1072 | if (si.isFloating()) { |
| 1073 | continue; // Ignore floating shortcuts, which are not tied to any activities. |
| 1074 | } |
| 1075 | |
| 1076 | final ComponentName activity = si.getActivity(); |
Makoto Onuki | 3414553 | 2017-03-14 17:58:36 -0700 | [diff] [blame] | 1077 | if (activity == null) { |
| 1078 | mShortcutUser.mService.wtf("null activity detected."); |
| 1079 | continue; |
| 1080 | } |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1081 | |
| 1082 | ArrayList<ShortcutInfo> list = activitiesToShortcuts.get(activity); |
| 1083 | if (list == null) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1084 | list = new ArrayList<>(); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1085 | activitiesToShortcuts.put(activity, list); |
| 1086 | } |
| 1087 | list.add(si); |
| 1088 | } |
| 1089 | return activitiesToShortcuts; |
| 1090 | } |
| 1091 | |
| 1092 | /** Used by {@link #enforceShortcutCountsBeforeOperation} */ |
| 1093 | private void incrementCountForActivity(ArrayMap<ComponentName, Integer> counts, |
| 1094 | ComponentName cn, int increment) { |
| 1095 | Integer oldValue = counts.get(cn); |
| 1096 | if (oldValue == null) { |
| 1097 | oldValue = 0; |
| 1098 | } |
| 1099 | |
| 1100 | counts.put(cn, oldValue + increment); |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * Called by |
| 1105 | * {@link android.content.pm.ShortcutManager#setDynamicShortcuts}, |
| 1106 | * {@link android.content.pm.ShortcutManager#addDynamicShortcuts}, and |
| 1107 | * {@link android.content.pm.ShortcutManager#updateShortcuts} before actually performing |
| 1108 | * the operation to make sure the operation wouldn't result in the target activities having |
| 1109 | * more than the allowed number of dynamic/manifest shortcuts. |
| 1110 | * |
| 1111 | * @param newList shortcut list passed to set, add or updateShortcuts(). |
| 1112 | * @param operation add, set or update. |
| 1113 | * @throws IllegalArgumentException if the operation would result in going over the max |
| 1114 | * shortcut count for any activity. |
| 1115 | */ |
| 1116 | public void enforceShortcutCountsBeforeOperation(List<ShortcutInfo> newList, |
| 1117 | @ShortcutOperation int operation) { |
| 1118 | final ShortcutService service = mShortcutUser.mService; |
| 1119 | |
| 1120 | // Current # of dynamic / manifest shortcuts for each activity. |
| 1121 | // (If it's for update, then don't count dynamic shortcuts, since they'll be replaced |
| 1122 | // anyway.) |
| 1123 | final ArrayMap<ComponentName, Integer> counts = new ArrayMap<>(4); |
| 1124 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 1125 | final ShortcutInfo shortcut = mShortcuts.valueAt(i); |
| 1126 | |
| 1127 | if (shortcut.isManifestShortcut()) { |
| 1128 | incrementCountForActivity(counts, shortcut.getActivity(), 1); |
| 1129 | } else if (shortcut.isDynamic() && (operation != ShortcutService.OPERATION_SET)) { |
| 1130 | incrementCountForActivity(counts, shortcut.getActivity(), 1); |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | for (int i = newList.size() - 1; i >= 0; i--) { |
| 1135 | final ShortcutInfo newShortcut = newList.get(i); |
| 1136 | final ComponentName newActivity = newShortcut.getActivity(); |
| 1137 | if (newActivity == null) { |
| 1138 | if (operation != ShortcutService.OPERATION_UPDATE) { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1139 | service.wtf("Activity must not be null at this point"); |
| 1140 | continue; // Just ignore this invalid case. |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1141 | } |
| 1142 | continue; // Activity can be null for update. |
| 1143 | } |
| 1144 | |
| 1145 | final ShortcutInfo original = mShortcuts.get(newShortcut.getId()); |
| 1146 | if (original == null) { |
| 1147 | if (operation == ShortcutService.OPERATION_UPDATE) { |
| 1148 | continue; // When updating, ignore if there's no target. |
| 1149 | } |
| 1150 | // Add() or set(), and there's no existing shortcut with the same ID. We're |
| 1151 | // simply publishing (as opposed to updating) this shortcut, so just +1. |
| 1152 | incrementCountForActivity(counts, newActivity, 1); |
| 1153 | continue; |
| 1154 | } |
| 1155 | if (original.isFloating() && (operation == ShortcutService.OPERATION_UPDATE)) { |
| 1156 | // Updating floating shortcuts doesn't affect the count, so ignore. |
| 1157 | continue; |
| 1158 | } |
| 1159 | |
| 1160 | // If it's add() or update(), then need to decrement for the previous activity. |
| 1161 | // Skip it for set() since it's already been taken care of by not counting the original |
| 1162 | // dynamic shortcuts in the first loop. |
| 1163 | if (operation != ShortcutService.OPERATION_SET) { |
| 1164 | final ComponentName oldActivity = original.getActivity(); |
| 1165 | if (!original.isFloating()) { |
| 1166 | incrementCountForActivity(counts, oldActivity, -1); |
| 1167 | } |
| 1168 | } |
| 1169 | incrementCountForActivity(counts, newActivity, 1); |
| 1170 | } |
| 1171 | |
| 1172 | // Then make sure none of the activities have more than the max number of shortcuts. |
| 1173 | for (int i = counts.size() - 1; i >= 0; i--) { |
| 1174 | service.enforceMaxActivityShortcuts(counts.valueAt(i)); |
| 1175 | } |
| 1176 | } |
| 1177 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1178 | /** |
| 1179 | * For all the text fields, refresh the string values if they're from resources. |
| 1180 | */ |
| 1181 | public void resolveResourceStrings() { |
| 1182 | final ShortcutService s = mShortcutUser.mService; |
| 1183 | boolean changed = false; |
| 1184 | |
| 1185 | Resources publisherRes = null; |
| 1186 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 1187 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 1188 | |
| 1189 | if (si.hasStringResources()) { |
| 1190 | changed = true; |
| 1191 | |
| 1192 | if (publisherRes == null) { |
| 1193 | publisherRes = getPackageResources(); |
| 1194 | if (publisherRes == null) { |
| 1195 | break; // Resources couldn't be loaded. |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | si.resolveResourceStrings(publisherRes); |
| 1200 | si.setTimestamp(s.injectCurrentTimeMillis()); |
| 1201 | } |
| 1202 | } |
| 1203 | if (changed) { |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 1204 | s.packageShortcutsChanged(getPackageName(), getPackageUserId()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1208 | /** Clears the implicit ranks for all shortcuts. */ |
| 1209 | public void clearAllImplicitRanks() { |
| 1210 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 1211 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 1212 | si.clearImplicitRankAndRankChangedFlag(); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * Used to sort shortcuts for rank auto-adjusting. |
| 1218 | */ |
| 1219 | final Comparator<ShortcutInfo> mShortcutRankComparator = (ShortcutInfo a, ShortcutInfo b) -> { |
| 1220 | // First, sort by rank. |
| 1221 | int ret = Integer.compare(a.getRank(), b.getRank()); |
| 1222 | if (ret != 0) { |
| 1223 | return ret; |
| 1224 | } |
| 1225 | // When ranks are tie, then prioritize the ones that have just been assigned new ranks. |
| 1226 | // e.g. when there are 3 shortcuts, "s1" "s2" and "s3" with rank 0, 1, 2 respectively, |
| 1227 | // adding a shortcut "s4" with rank 1 will "insert" it between "s1" and "s2", because |
| 1228 | // "s2" and "s4" have the same rank 1 but s4 has isRankChanged() set. |
| 1229 | // Similarly, updating s3's rank to 1 will insert it between s1 and s2. |
| 1230 | if (a.isRankChanged() != b.isRankChanged()) { |
| 1231 | return a.isRankChanged() ? -1 : 1; |
| 1232 | } |
| 1233 | // If they're still tie, sort by implicit rank -- i.e. preserve the order in which |
| 1234 | // they're passed to the API. |
| 1235 | ret = Integer.compare(a.getImplicitRank(), b.getImplicitRank()); |
| 1236 | if (ret != 0) { |
| 1237 | return ret; |
| 1238 | } |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1239 | // If they're still tie, just sort by their IDs. |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1240 | // This may happen with updateShortcuts() -- see |
| 1241 | // the testUpdateShortcuts_noManifestShortcuts() test. |
| 1242 | return a.getId().compareTo(b.getId()); |
| 1243 | }; |
| 1244 | |
| 1245 | /** |
| 1246 | * Re-calculate the ranks for all shortcuts. |
| 1247 | */ |
| 1248 | public void adjustRanks() { |
| 1249 | final ShortcutService s = mShortcutUser.mService; |
| 1250 | final long now = s.injectCurrentTimeMillis(); |
| 1251 | |
| 1252 | // First, clear ranks for floating shortcuts. |
| 1253 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 1254 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 1255 | if (si.isFloating()) { |
| 1256 | if (si.getRank() != 0) { |
| 1257 | si.setTimestamp(now); |
| 1258 | si.setRank(0); |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | // Then adjust ranks. Ranks are unique for each activity, so we first need to sort |
| 1264 | // shortcuts to each activity. |
| 1265 | // Then sort the shortcuts within each activity with mShortcutRankComparator, and |
| 1266 | // assign ranks from 0. |
| 1267 | final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all = |
| 1268 | sortShortcutsToActivities(); |
| 1269 | for (int outer = all.size() - 1; outer >= 0; outer--) { // For each activity. |
| 1270 | final ArrayList<ShortcutInfo> list = all.valueAt(outer); |
| 1271 | |
| 1272 | // Sort by ranks and other signals. |
| 1273 | Collections.sort(list, mShortcutRankComparator); |
| 1274 | |
| 1275 | int rank = 0; |
| 1276 | |
| 1277 | final int size = list.size(); |
| 1278 | for (int i = 0; i < size; i++) { |
| 1279 | final ShortcutInfo si = list.get(i); |
| 1280 | if (si.isManifestShortcut()) { |
| 1281 | // Don't adjust ranks for manifest shortcuts. |
| 1282 | continue; |
| 1283 | } |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1284 | // At this point, it must be dynamic. |
| 1285 | if (!si.isDynamic()) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1286 | s.wtf("Non-dynamic shortcut found."); |
| 1287 | continue; |
| 1288 | } |
| 1289 | final int thisRank = rank++; |
| 1290 | if (si.getRank() != thisRank) { |
| 1291 | si.setTimestamp(now); |
| 1292 | si.setRank(thisRank); |
| 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
Makoto Onuki | fc4cf2d | 2016-08-24 11:10:26 -0700 | [diff] [blame] | 1298 | /** @return true if there's any shortcuts that are not manifest shortcuts. */ |
| 1299 | public boolean hasNonManifestShortcuts() { |
| 1300 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 1301 | final ShortcutInfo si = mShortcuts.valueAt(i); |
| 1302 | if (!si.isDeclaredInManifest()) { |
| 1303 | return true; |
| 1304 | } |
| 1305 | } |
| 1306 | return false; |
| 1307 | } |
| 1308 | |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 1309 | public void dump(@NonNull PrintWriter pw, @NonNull String prefix, DumpFilter filter) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1310 | pw.println(); |
| 1311 | |
| 1312 | pw.print(prefix); |
| 1313 | pw.print("Package: "); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1314 | pw.print(getPackageName()); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1315 | pw.print(" UID: "); |
| 1316 | pw.print(mPackageUid); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1317 | pw.println(); |
| 1318 | |
| 1319 | pw.print(prefix); |
| 1320 | pw.print(" "); |
| 1321 | pw.print("Calls: "); |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 1322 | pw.print(getApiCallCount(/*unlimited=*/ false)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1323 | pw.println(); |
| 1324 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1325 | // getApiCallCount() may have updated mLastKnownForegroundElapsedTime. |
| 1326 | pw.print(prefix); |
| 1327 | pw.print(" "); |
| 1328 | pw.print("Last known FG: "); |
| 1329 | pw.print(mLastKnownForegroundElapsedTime); |
| 1330 | pw.println(); |
| 1331 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1332 | // This should be after getApiCallCount(), which may update it. |
| 1333 | pw.print(prefix); |
| 1334 | pw.print(" "); |
| 1335 | pw.print("Last reset: ["); |
| 1336 | pw.print(mLastResetTime); |
| 1337 | pw.print("] "); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1338 | pw.print(ShortcutService.formatTime(mLastResetTime)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1339 | pw.println(); |
| 1340 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1341 | getPackageInfo().dump(pw, prefix + " "); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1342 | pw.println(); |
| 1343 | |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 1344 | pw.print(prefix); |
| 1345 | pw.println(" Shortcuts:"); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1346 | long totalBitmapSize = 0; |
| 1347 | final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts; |
| 1348 | final int size = shortcuts.size(); |
| 1349 | for (int i = 0; i < size; i++) { |
| 1350 | final ShortcutInfo si = shortcuts.valueAt(i); |
Makoto Onuki | 6208c67 | 2017-10-02 16:20:35 -0700 | [diff] [blame] | 1351 | pw.println(si.toDumpString(prefix + " ")); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1352 | if (si.getBitmapPath() != null) { |
| 1353 | final long len = new File(si.getBitmapPath()).length(); |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 1354 | pw.print(prefix); |
| 1355 | pw.print(" "); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1356 | pw.print("bitmap size="); |
| 1357 | pw.println(len); |
| 1358 | |
| 1359 | totalBitmapSize += len; |
| 1360 | } |
| 1361 | } |
| 1362 | pw.print(prefix); |
| 1363 | pw.print(" "); |
| 1364 | pw.print("Total bitmap size: "); |
| 1365 | pw.print(totalBitmapSize); |
| 1366 | pw.print(" ("); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1367 | pw.print(Formatter.formatFileSize(mShortcutUser.mService.mContext, totalBitmapSize)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1368 | pw.println(")"); |
| 1369 | } |
| 1370 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1371 | @Override |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 1372 | public JSONObject dumpCheckin(boolean clear) throws JSONException { |
| 1373 | final JSONObject result = super.dumpCheckin(clear); |
| 1374 | |
| 1375 | int numDynamic = 0; |
| 1376 | int numPinned = 0; |
| 1377 | int numManifest = 0; |
| 1378 | int numBitmaps = 0; |
| 1379 | long totalBitmapSize = 0; |
| 1380 | |
| 1381 | final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts; |
| 1382 | final int size = shortcuts.size(); |
| 1383 | for (int i = 0; i < size; i++) { |
| 1384 | final ShortcutInfo si = shortcuts.valueAt(i); |
| 1385 | |
| 1386 | if (si.isDynamic()) numDynamic++; |
| 1387 | if (si.isDeclaredInManifest()) numManifest++; |
| 1388 | if (si.isPinned()) numPinned++; |
| 1389 | |
| 1390 | if (si.getBitmapPath() != null) { |
| 1391 | numBitmaps++; |
| 1392 | totalBitmapSize += new File(si.getBitmapPath()).length(); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | result.put(KEY_DYNAMIC, numDynamic); |
| 1397 | result.put(KEY_MANIFEST, numManifest); |
| 1398 | result.put(KEY_PINNED, numPinned); |
| 1399 | result.put(KEY_BITMAPS, numBitmaps); |
| 1400 | result.put(KEY_BITMAP_BYTES, totalBitmapSize); |
| 1401 | |
| 1402 | // TODO Log update frequency too. |
| 1403 | |
| 1404 | return result; |
| 1405 | } |
| 1406 | |
| 1407 | @Override |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1408 | public void saveToXml(@NonNull XmlSerializer out, boolean forBackup) |
| 1409 | throws IOException, XmlPullParserException { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1410 | final int size = mShortcuts.size(); |
| 1411 | |
| 1412 | if (size == 0 && mApiCallCount == 0) { |
| 1413 | return; // nothing to write. |
| 1414 | } |
| 1415 | |
| 1416 | out.startTag(null, TAG_ROOT); |
| 1417 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1418 | ShortcutService.writeAttr(out, ATTR_NAME, getPackageName()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1419 | ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount); |
| 1420 | ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime); |
Makoto Onuki | e3fffa9 | 2018-02-28 16:25:40 -0800 | [diff] [blame] | 1421 | getPackageInfo().saveToXml(mShortcutUser.mService, out, forBackup); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1422 | |
| 1423 | for (int j = 0; j < size; j++) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1424 | saveShortcut(out, mShortcuts.valueAt(j), forBackup, |
| 1425 | getPackageInfo().isBackupAllowed()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | out.endTag(null, TAG_ROOT); |
| 1429 | } |
| 1430 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1431 | private void saveShortcut(XmlSerializer out, ShortcutInfo si, boolean forBackup, |
| 1432 | boolean appSupportsBackup) |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1433 | throws IOException, XmlPullParserException { |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1434 | |
| 1435 | final ShortcutService s = mShortcutUser.mService; |
| 1436 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1437 | if (forBackup) { |
Makoto Onuki | f3ba2e0 | 2016-07-12 09:18:50 -0700 | [diff] [blame] | 1438 | if (!(si.isPinned() && si.isEnabled())) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1439 | // We only backup pinned shortcuts that are enabled. |
| 1440 | // Note, this means, shortcuts that are restored but are blocked restore, e.g. due |
| 1441 | // to a lower version code, will not be ported to a new device. |
| 1442 | return; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1443 | } |
| 1444 | } |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1445 | final boolean shouldBackupDetails = |
| 1446 | !forBackup // It's not backup |
| 1447 | || appSupportsBackup; // Or, it's a backup and app supports backup. |
| 1448 | |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1449 | // Note: at this point no shortcuts should have bitmaps pending save, but if they do, |
| 1450 | // just remove the bitmap. |
| 1451 | if (si.isIconPendingSave()) { |
| 1452 | s.removeIconLocked(si); |
| 1453 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1454 | out.startTag(null, TAG_SHORTCUT); |
| 1455 | ShortcutService.writeAttr(out, ATTR_ID, si.getId()); |
| 1456 | // writeAttr(out, "package", si.getPackageName()); // not needed |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1457 | ShortcutService.writeAttr(out, ATTR_ACTIVITY, si.getActivity()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1458 | // writeAttr(out, "icon", si.getIcon()); // We don't save it. |
| 1459 | ShortcutService.writeAttr(out, ATTR_TITLE, si.getTitle()); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1460 | ShortcutService.writeAttr(out, ATTR_TITLE_RES_ID, si.getTitleResId()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1461 | ShortcutService.writeAttr(out, ATTR_TITLE_RES_NAME, si.getTitleResName()); |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 1462 | ShortcutService.writeAttr(out, ATTR_TEXT, si.getText()); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1463 | ShortcutService.writeAttr(out, ATTR_TEXT_RES_ID, si.getTextResId()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1464 | ShortcutService.writeAttr(out, ATTR_TEXT_RES_NAME, si.getTextResName()); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1465 | if (shouldBackupDetails) { |
| 1466 | ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE, si.getDisabledMessage()); |
| 1467 | ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_ID, |
| 1468 | si.getDisabledMessageResourceId()); |
| 1469 | ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_NAME, |
| 1470 | si.getDisabledMessageResName()); |
| 1471 | } |
| 1472 | ShortcutService.writeAttr(out, ATTR_DISABLED_REASON, si.getDisabledReason()); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1473 | ShortcutService.writeAttr(out, ATTR_TIMESTAMP, |
| 1474 | si.getLastChangedTimestamp()); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1475 | if (forBackup) { |
| 1476 | // Don't write icon information. Also drop the dynamic flag. |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1477 | |
| 1478 | int flags = si.getFlags() & |
| 1479 | ~(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1480 | | ShortcutInfo.FLAG_ICON_FILE_PENDING_SAVE |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1481 | | ShortcutInfo.FLAG_DYNAMIC); |
| 1482 | ShortcutService.writeAttr(out, ATTR_FLAGS, flags); |
| 1483 | |
| 1484 | // Set the publisher version code at every backup. |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 1485 | final long packageVersionCode = getPackageInfo().getVersionCode(); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1486 | if (packageVersionCode == 0) { |
| 1487 | s.wtf("Package version code should be available at this point."); |
| 1488 | // However, 0 is a valid version code, so we just go ahead with it... |
| 1489 | } |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1490 | } else { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1491 | // When writing for backup, ranks shouldn't be saved, since shortcuts won't be restored |
| 1492 | // as dynamic. |
| 1493 | ShortcutService.writeAttr(out, ATTR_RANK, si.getRank()); |
| 1494 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1495 | ShortcutService.writeAttr(out, ATTR_FLAGS, si.getFlags()); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1496 | ShortcutService.writeAttr(out, ATTR_ICON_RES_ID, si.getIconResourceId()); |
| 1497 | ShortcutService.writeAttr(out, ATTR_ICON_RES_NAME, si.getIconResName()); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1498 | ShortcutService.writeAttr(out, ATTR_BITMAP_PATH, si.getBitmapPath()); |
| 1499 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1500 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1501 | if (shouldBackupDetails) { |
| 1502 | { |
| 1503 | final Set<String> cat = si.getCategories(); |
| 1504 | if (cat != null && cat.size() > 0) { |
| 1505 | out.startTag(null, TAG_CATEGORIES); |
| 1506 | XmlUtils.writeStringArrayXml(cat.toArray(new String[cat.size()]), |
| 1507 | NAME_CATEGORIES, out); |
| 1508 | out.endTag(null, TAG_CATEGORIES); |
| 1509 | } |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1510 | } |
Mehdi Alizadeh | ebb4b60 | 2019-02-05 15:52:18 -0800 | [diff] [blame] | 1511 | if (!forBackup) { // Don't backup the persons field. |
| 1512 | final Person[] persons = si.getPersons(); |
| 1513 | if (!ArrayUtils.isEmpty(persons)) { |
| 1514 | for (int i = 0; i < persons.length; i++) { |
| 1515 | final Person p = persons[i]; |
| 1516 | |
| 1517 | out.startTag(null, TAG_PERSON); |
| 1518 | ShortcutService.writeAttr(out, ATTR_PERSON_NAME, p.getName()); |
| 1519 | ShortcutService.writeAttr(out, ATTR_PERSON_URI, p.getUri()); |
| 1520 | ShortcutService.writeAttr(out, ATTR_PERSON_KEY, p.getKey()); |
| 1521 | ShortcutService.writeAttr(out, ATTR_PERSON_IS_BOT, p.isBot()); |
| 1522 | ShortcutService.writeAttr(out, ATTR_PERSON_IS_IMPORTANT, p.isImportant()); |
| 1523 | out.endTag(null, TAG_PERSON); |
| 1524 | } |
| 1525 | } |
| 1526 | } |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1527 | final Intent[] intentsNoExtras = si.getIntentsNoExtras(); |
| 1528 | final PersistableBundle[] intentsExtras = si.getIntentPersistableExtrases(); |
| 1529 | final int numIntents = intentsNoExtras.length; |
| 1530 | for (int i = 0; i < numIntents; i++) { |
| 1531 | out.startTag(null, TAG_INTENT); |
| 1532 | ShortcutService.writeAttr(out, ATTR_INTENT_NO_EXTRA, intentsNoExtras[i]); |
| 1533 | ShortcutService.writeTagExtra(out, TAG_EXTRAS, intentsExtras[i]); |
| 1534 | out.endTag(null, TAG_INTENT); |
| 1535 | } |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1536 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1537 | ShortcutService.writeTagExtra(out, TAG_EXTRAS, si.getExtras()); |
| 1538 | } |
Hakan Seyalioglu | 58fc95d | 2016-12-13 15:23:22 -0800 | [diff] [blame] | 1539 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1540 | out.endTag(null, TAG_SHORTCUT); |
| 1541 | } |
| 1542 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1543 | public static ShortcutPackage loadFromXml(ShortcutService s, ShortcutUser shortcutUser, |
| 1544 | XmlPullParser parser, boolean fromBackup) |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1545 | throws IOException, XmlPullParserException { |
| 1546 | |
| 1547 | final String packageName = ShortcutService.parseStringAttribute(parser, |
| 1548 | ATTR_NAME); |
| 1549 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1550 | final ShortcutPackage ret = new ShortcutPackage(shortcutUser, |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1551 | shortcutUser.getUserId(), packageName); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1552 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1553 | ret.mApiCallCount = |
| 1554 | ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT); |
| 1555 | ret.mLastResetTime = |
| 1556 | ShortcutService.parseLongAttribute(parser, ATTR_LAST_RESET); |
| 1557 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1558 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1559 | final int outerDepth = parser.getDepth(); |
| 1560 | int type; |
| 1561 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1562 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1563 | if (type != XmlPullParser.START_TAG) { |
| 1564 | continue; |
| 1565 | } |
| 1566 | final int depth = parser.getDepth(); |
| 1567 | final String tag = parser.getName(); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1568 | if (depth == outerDepth + 1) { |
| 1569 | switch (tag) { |
| 1570 | case ShortcutPackageInfo.TAG_ROOT: |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 1571 | ret.getPackageInfo().loadFromXml(parser, fromBackup); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1572 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1573 | continue; |
| 1574 | case TAG_SHORTCUT: |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1575 | final ShortcutInfo si = parseShortcut(parser, packageName, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1576 | shortcutUser.getUserId(), fromBackup); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1577 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1578 | // Don't use addShortcut(), we don't need to save the icon. |
| 1579 | ret.mShortcuts.put(si.getId(), si); |
| 1580 | continue; |
| 1581 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1582 | } |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1583 | ShortcutService.warnForInvalidTag(depth, tag); |
| 1584 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1585 | return ret; |
| 1586 | } |
| 1587 | |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1588 | private static ShortcutInfo parseShortcut(XmlPullParser parser, String packageName, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1589 | @UserIdInt int userId, boolean fromBackup) |
| 1590 | throws IOException, XmlPullParserException { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1591 | String id; |
| 1592 | ComponentName activityComponent; |
| 1593 | // Icon icon; |
| 1594 | String title; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1595 | int titleResId; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1596 | String titleResName; |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 1597 | String text; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1598 | int textResId; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1599 | String textResName; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1600 | String disabledMessage; |
| 1601 | int disabledMessageResId; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1602 | String disabledMessageResName; |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1603 | int disabledReason; |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1604 | Intent intentLegacy; |
| 1605 | PersistableBundle intentPersistableExtrasLegacy = null; |
| 1606 | ArrayList<Intent> intents = new ArrayList<>(); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1607 | int rank; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1608 | PersistableBundle extras = null; |
| 1609 | long lastChangedTimestamp; |
| 1610 | int flags; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1611 | int iconResId; |
| 1612 | String iconResName; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1613 | String bitmapPath; |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1614 | int backupVersionCode; |
Makoto Onuki | be73a80 | 2016-04-15 14:46:35 -0700 | [diff] [blame] | 1615 | ArraySet<String> categories = null; |
Mehdi Alizadeh | ebb4b60 | 2019-02-05 15:52:18 -0800 | [diff] [blame] | 1616 | ArrayList<Person> persons = new ArrayList<>(); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1617 | |
| 1618 | id = ShortcutService.parseStringAttribute(parser, ATTR_ID); |
| 1619 | activityComponent = ShortcutService.parseComponentNameAttribute(parser, |
| 1620 | ATTR_ACTIVITY); |
| 1621 | title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1622 | titleResId = ShortcutService.parseIntAttribute(parser, ATTR_TITLE_RES_ID); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1623 | titleResName = ShortcutService.parseStringAttribute(parser, ATTR_TITLE_RES_NAME); |
Makoto Onuki | e3ae7ec | 2016-03-29 15:45:25 -0700 | [diff] [blame] | 1624 | text = ShortcutService.parseStringAttribute(parser, ATTR_TEXT); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1625 | textResId = ShortcutService.parseIntAttribute(parser, ATTR_TEXT_RES_ID); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1626 | textResName = ShortcutService.parseStringAttribute(parser, ATTR_TEXT_RES_NAME); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1627 | disabledMessage = ShortcutService.parseStringAttribute(parser, ATTR_DISABLED_MESSAGE); |
| 1628 | disabledMessageResId = ShortcutService.parseIntAttribute(parser, |
| 1629 | ATTR_DISABLED_MESSAGE_RES_ID); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1630 | disabledMessageResName = ShortcutService.parseStringAttribute(parser, |
| 1631 | ATTR_DISABLED_MESSAGE_RES_NAME); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1632 | disabledReason = ShortcutService.parseIntAttribute(parser, ATTR_DISABLED_REASON); |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1633 | intentLegacy = ShortcutService.parseIntentAttributeNoDefault(parser, ATTR_INTENT_LEGACY); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1634 | rank = (int) ShortcutService.parseLongAttribute(parser, ATTR_RANK); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1635 | lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1636 | flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1637 | iconResId = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES_ID); |
| 1638 | iconResName = ShortcutService.parseStringAttribute(parser, ATTR_ICON_RES_NAME); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1639 | bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH); |
| 1640 | |
| 1641 | final int outerDepth = parser.getDepth(); |
| 1642 | int type; |
| 1643 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1644 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1645 | if (type != XmlPullParser.START_TAG) { |
| 1646 | continue; |
| 1647 | } |
| 1648 | final int depth = parser.getDepth(); |
| 1649 | final String tag = parser.getName(); |
| 1650 | if (ShortcutService.DEBUG_LOAD) { |
| 1651 | Slog.d(TAG, String.format(" depth=%d type=%d name=%s", |
| 1652 | depth, type, tag)); |
| 1653 | } |
| 1654 | switch (tag) { |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1655 | case TAG_INTENT_EXTRAS_LEGACY: |
| 1656 | intentPersistableExtrasLegacy = PersistableBundle.restoreFromXml(parser); |
| 1657 | continue; |
| 1658 | case TAG_INTENT: |
| 1659 | intents.add(parseIntent(parser)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1660 | continue; |
| 1661 | case TAG_EXTRAS: |
| 1662 | extras = PersistableBundle.restoreFromXml(parser); |
| 1663 | continue; |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1664 | case TAG_CATEGORIES: |
| 1665 | // This just contains string-array. |
| 1666 | continue; |
Mehdi Alizadeh | ebb4b60 | 2019-02-05 15:52:18 -0800 | [diff] [blame] | 1667 | case TAG_PERSON: |
| 1668 | persons.add(parsePerson(parser)); |
| 1669 | continue; |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1670 | case TAG_STRING_ARRAY_XMLUTILS: |
| 1671 | if (NAME_CATEGORIES.equals(ShortcutService.parseStringAttribute(parser, |
| 1672 | ATTR_NAME_XMLUTILS))) { |
Makoto Onuki | be73a80 | 2016-04-15 14:46:35 -0700 | [diff] [blame] | 1673 | final String[] ar = XmlUtils.readThisStringArrayXml( |
| 1674 | parser, TAG_STRING_ARRAY_XMLUTILS, null); |
| 1675 | categories = new ArraySet<>(ar.length); |
| 1676 | for (int i = 0; i < ar.length; i++) { |
| 1677 | categories.add(ar[i]); |
| 1678 | } |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1679 | } |
| 1680 | continue; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1681 | } |
| 1682 | throw ShortcutService.throwForInvalidTag(depth, tag); |
| 1683 | } |
Makoto Onuki | be73a80 | 2016-04-15 14:46:35 -0700 | [diff] [blame] | 1684 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1685 | if (intentLegacy != null) { |
| 1686 | // For the legacy file format which supported only one intent per shortcut. |
| 1687 | ShortcutInfo.setIntentExtras(intentLegacy, intentPersistableExtrasLegacy); |
| 1688 | intents.clear(); |
| 1689 | intents.add(intentLegacy); |
| 1690 | } |
Makoto Onuki | 9fd9019 | 2017-01-06 18:31:03 +0000 | [diff] [blame] | 1691 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1692 | |
| 1693 | if ((disabledReason == ShortcutInfo.DISABLED_REASON_NOT_DISABLED) |
| 1694 | && ((flags & ShortcutInfo.FLAG_DISABLED) != 0)) { |
| 1695 | // We didn't used to have the disabled reason, so if a shortcut is disabled |
| 1696 | // and has no reason, we assume it was disabled by publisher. |
| 1697 | disabledReason = ShortcutInfo.DISABLED_REASON_BY_APP; |
| 1698 | } |
| 1699 | |
| 1700 | // All restored shortcuts are initially "shadow". |
| 1701 | if (fromBackup) { |
| 1702 | flags |= ShortcutInfo.FLAG_SHADOW; |
| 1703 | } |
| 1704 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1705 | return new ShortcutInfo( |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1706 | userId, id, packageName, activityComponent, /* icon =*/ null, |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1707 | title, titleResId, titleResName, text, textResId, textResName, |
| 1708 | disabledMessage, disabledMessageResId, disabledMessageResName, |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1709 | categories, |
| 1710 | intents.toArray(new Intent[intents.size()]), |
| 1711 | rank, extras, lastChangedTimestamp, flags, |
Mehdi Alizadeh | ebb4b60 | 2019-02-05 15:52:18 -0800 | [diff] [blame] | 1712 | iconResId, iconResName, bitmapPath, disabledReason, |
| 1713 | persons.toArray(new Person[persons.size()])); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1714 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 1715 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 1716 | private static Intent parseIntent(XmlPullParser parser) |
| 1717 | throws IOException, XmlPullParserException { |
| 1718 | |
| 1719 | Intent intent = ShortcutService.parseIntentAttribute(parser, |
| 1720 | ATTR_INTENT_NO_EXTRA); |
| 1721 | |
| 1722 | final int outerDepth = parser.getDepth(); |
| 1723 | int type; |
| 1724 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1725 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1726 | if (type != XmlPullParser.START_TAG) { |
| 1727 | continue; |
| 1728 | } |
| 1729 | final int depth = parser.getDepth(); |
| 1730 | final String tag = parser.getName(); |
| 1731 | if (ShortcutService.DEBUG_LOAD) { |
| 1732 | Slog.d(TAG, String.format(" depth=%d type=%d name=%s", |
| 1733 | depth, type, tag)); |
| 1734 | } |
| 1735 | switch (tag) { |
| 1736 | case TAG_EXTRAS: |
| 1737 | ShortcutInfo.setIntentExtras(intent, |
| 1738 | PersistableBundle.restoreFromXml(parser)); |
| 1739 | continue; |
| 1740 | } |
| 1741 | throw ShortcutService.throwForInvalidTag(depth, tag); |
| 1742 | } |
| 1743 | return intent; |
| 1744 | } |
| 1745 | |
Mehdi Alizadeh | ebb4b60 | 2019-02-05 15:52:18 -0800 | [diff] [blame] | 1746 | private static Person parsePerson(XmlPullParser parser) |
| 1747 | throws IOException, XmlPullParserException { |
| 1748 | CharSequence name = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_NAME); |
| 1749 | String uri = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_URI); |
| 1750 | String key = ShortcutService.parseStringAttribute(parser, ATTR_PERSON_KEY); |
| 1751 | boolean isBot = ShortcutService.parseBooleanAttribute(parser, ATTR_PERSON_IS_BOT); |
| 1752 | boolean isImportant = ShortcutService.parseBooleanAttribute(parser, |
| 1753 | ATTR_PERSON_IS_IMPORTANT); |
| 1754 | |
| 1755 | Person.Builder builder = new Person.Builder(); |
| 1756 | builder.setName(name).setUri(uri).setKey(key).setBot(isBot).setImportant(isImportant); |
| 1757 | return builder.build(); |
| 1758 | } |
| 1759 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 1760 | @VisibleForTesting |
| 1761 | List<ShortcutInfo> getAllShortcutsForTest() { |
| 1762 | return new ArrayList<>(mShortcuts.values()); |
| 1763 | } |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1764 | |
Mehdi Alizadeh | 3277462 | 2018-11-05 17:32:01 -0800 | [diff] [blame] | 1765 | @VisibleForTesting |
| 1766 | List<ShareTargetInfo> getAllShareTargetsForTest() { |
| 1767 | return new ArrayList<>(mShareTargets); |
| 1768 | } |
| 1769 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1770 | @Override |
| 1771 | public void verifyStates() { |
| 1772 | super.verifyStates(); |
| 1773 | |
| 1774 | boolean failed = false; |
| 1775 | |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 1776 | final ShortcutService s = mShortcutUser.mService; |
| 1777 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1778 | final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all = |
| 1779 | sortShortcutsToActivities(); |
| 1780 | |
| 1781 | // Make sure each activity won't have more than max shortcuts. |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1782 | for (int outer = all.size() - 1; outer >= 0; outer--) { |
| 1783 | final ArrayList<ShortcutInfo> list = all.valueAt(outer); |
| 1784 | if (list.size() > mShortcutUser.mService.getMaxActivityShortcuts()) { |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1785 | failed = true; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1786 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": activity " + all.keyAt(outer) |
| 1787 | + " has " + all.valueAt(outer).size() + " shortcuts."); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1788 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1789 | |
| 1790 | // Sort by rank. |
| 1791 | Collections.sort(list, (a, b) -> Integer.compare(a.getRank(), b.getRank())); |
| 1792 | |
| 1793 | // Split into two arrays for each kind. |
| 1794 | final ArrayList<ShortcutInfo> dynamicList = new ArrayList<>(list); |
| 1795 | dynamicList.removeIf((si) -> !si.isDynamic()); |
| 1796 | |
| 1797 | final ArrayList<ShortcutInfo> manifestList = new ArrayList<>(list); |
| 1798 | dynamicList.removeIf((si) -> !si.isManifestShortcut()); |
| 1799 | |
| 1800 | verifyRanksSequential(dynamicList); |
| 1801 | verifyRanksSequential(manifestList); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1804 | // Verify each shortcut's status. |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1805 | for (int i = mShortcuts.size() - 1; i >= 0; i--) { |
| 1806 | final ShortcutInfo si = mShortcuts.valueAt(i); |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1807 | if (!(si.isDeclaredInManifest() || si.isDynamic() || si.isPinned())) { |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1808 | failed = true; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1809 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1810 | + " is not manifest, dynamic or pinned."); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1811 | } |
Makoto Onuki | ff14f73 | 2016-06-30 17:07:25 -0700 | [diff] [blame] | 1812 | if (si.isDeclaredInManifest() && si.isDynamic()) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1813 | failed = true; |
| 1814 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1815 | + " is both dynamic and manifest at the same time."); |
| 1816 | } |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 1817 | if (si.getActivity() == null && !si.isFloating()) { |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1818 | failed = true; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1819 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 1820 | + " has null activity, but not floating."); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1821 | } |
Makoto Onuki | 9fd9019 | 2017-01-06 18:31:03 +0000 | [diff] [blame] | 1822 | if ((si.isDynamic() || si.isManifestShortcut()) && !si.isEnabled()) { |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1823 | failed = true; |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1824 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1825 | + " is not floating, but is disabled."); |
| 1826 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1827 | if (si.isFloating() && si.getRank() != 0) { |
| 1828 | failed = true; |
| 1829 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1830 | + " is floating, but has rank=" + si.getRank()); |
| 1831 | } |
Makoto Onuki | dd09781 | 2016-06-29 13:10:09 -0700 | [diff] [blame] | 1832 | if (si.getIcon() != null) { |
| 1833 | failed = true; |
| 1834 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1835 | + " still has an icon"); |
| 1836 | } |
Hyunyoung Song | e4179e2 | 2017-03-01 12:51:26 -0800 | [diff] [blame] | 1837 | if (si.hasAdaptiveBitmap() && !si.hasIconFile()) { |
Hyunyoung Song | f281e7a | 2017-02-13 10:57:42 -0800 | [diff] [blame] | 1838 | failed = true; |
| 1839 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
Hyunyoung Song | e4179e2 | 2017-03-01 12:51:26 -0800 | [diff] [blame] | 1840 | + " has adaptive bitmap but was not saved to a file."); |
Hyunyoung Song | f281e7a | 2017-02-13 10:57:42 -0800 | [diff] [blame] | 1841 | } |
Makoto Onuki | dd09781 | 2016-06-29 13:10:09 -0700 | [diff] [blame] | 1842 | if (si.hasIconFile() && si.hasIconResource()) { |
| 1843 | failed = true; |
| 1844 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1845 | + " has both resource and bitmap icons"); |
| 1846 | } |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1847 | if (si.isEnabled() |
| 1848 | != (si.getDisabledReason() == ShortcutInfo.DISABLED_REASON_NOT_DISABLED)) { |
| 1849 | failed = true; |
| 1850 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1851 | + " isEnabled() and getDisabledReason() disagree: " |
| 1852 | + si.isEnabled() + " vs " + si.getDisabledReason()); |
| 1853 | } |
| 1854 | if ((si.getDisabledReason() == ShortcutInfo.DISABLED_REASON_VERSION_LOWER) |
| 1855 | && (getPackageInfo().getBackupSourceVersionCode() |
| 1856 | == ShortcutInfo.VERSION_CODE_UNKNOWN)) { |
| 1857 | failed = true; |
| 1858 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1859 | + " RESTORED_VERSION_LOWER with no backup source version code."); |
| 1860 | } |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 1861 | if (s.isDummyMainActivity(si.getActivity())) { |
| 1862 | failed = true; |
| 1863 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1864 | + " has a dummy target activity"); |
| 1865 | } |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | if (failed) { |
Makoto Onuki | 9fd9019 | 2017-01-06 18:31:03 +0000 | [diff] [blame] | 1869 | throw new IllegalStateException("See logcat for errors"); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1870 | } |
| 1871 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1872 | |
| 1873 | private boolean verifyRanksSequential(List<ShortcutInfo> list) { |
| 1874 | boolean failed = false; |
| 1875 | |
| 1876 | for (int i = 0; i < list.size(); i++) { |
| 1877 | final ShortcutInfo si = list.get(i); |
| 1878 | if (si.getRank() != i) { |
| 1879 | failed = true; |
| 1880 | Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId() |
| 1881 | + " rank=" + si.getRank() + " but expected to be "+ i); |
| 1882 | } |
| 1883 | } |
| 1884 | return failed; |
| 1885 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1886 | } |