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