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