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.UserIdInt; |
| 20 | import android.content.pm.ShortcutInfo; |
| 21 | import android.util.ArrayMap; |
| 22 | import android.util.ArraySet; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 23 | import android.util.Slog; |
| 24 | |
| 25 | import com.android.internal.annotations.VisibleForTesting; |
| 26 | import com.android.server.pm.ShortcutUser.PackageWithUser; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 27 | |
| 28 | import org.xmlpull.v1.XmlPullParser; |
| 29 | import org.xmlpull.v1.XmlPullParserException; |
| 30 | import org.xmlpull.v1.XmlSerializer; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | import java.io.PrintWriter; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 35 | import java.util.List; |
| 36 | |
| 37 | /** |
| 38 | * Launcher information used by {@link ShortcutService}. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame^] | 39 | * |
| 40 | * All methods should be guarded by {@code #mShortcutUser.mService.mLock}. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 41 | */ |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 42 | class ShortcutLauncher extends ShortcutPackageItem { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 43 | private static final String TAG = ShortcutService.TAG; |
| 44 | |
| 45 | static final String TAG_ROOT = "launcher-pins"; |
| 46 | |
| 47 | private static final String TAG_PACKAGE = "package"; |
| 48 | private static final String TAG_PIN = "pin"; |
| 49 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 50 | private static final String ATTR_LAUNCHER_USER_ID = "launcher-user"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 51 | private static final String ATTR_VALUE = "value"; |
| 52 | private static final String ATTR_PACKAGE_NAME = "package-name"; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 53 | private static final String ATTR_PACKAGE_USER_ID = "package-user"; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 54 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 55 | private final int mOwnerUserId; |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 56 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Package name -> IDs. |
| 59 | */ |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 60 | final private ArrayMap<PackageWithUser, ArraySet<String>> mPinnedShortcuts = new ArrayMap<>(); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 61 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 62 | private ShortcutLauncher(@NonNull ShortcutUser shortcutUser, |
| 63 | @UserIdInt int ownerUserId, @NonNull String packageName, |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 64 | @UserIdInt int launcherUserId, ShortcutPackageInfo spi) { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 65 | super(shortcutUser, launcherUserId, packageName, |
| 66 | spi != null ? spi : ShortcutPackageInfo.newEmpty()); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 67 | mOwnerUserId = ownerUserId; |
| 68 | } |
| 69 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 70 | public ShortcutLauncher(@NonNull ShortcutUser shortcutUser, |
| 71 | @UserIdInt int ownerUserId, @NonNull String packageName, |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 72 | @UserIdInt int launcherUserId) { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 73 | this(shortcutUser, ownerUserId, packageName, launcherUserId, null); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 76 | @Override |
| 77 | public int getOwnerUserId() { |
| 78 | return mOwnerUserId; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Called when the new package can't receive the backup, due to signature or version mismatch. |
| 83 | */ |
| 84 | @Override |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 85 | protected void onRestoreBlocked() { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 86 | final ArrayList<PackageWithUser> pinnedPackages = |
| 87 | new ArrayList<>(mPinnedShortcuts.keySet()); |
| 88 | mPinnedShortcuts.clear(); |
| 89 | for (int i = pinnedPackages.size() - 1; i >= 0; i--) { |
| 90 | final PackageWithUser pu = pinnedPackages.get(i); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 91 | final ShortcutPackage p = mShortcutUser.getPackageShortcutsIfExists(pu.packageName); |
| 92 | if (p != null) { |
| 93 | p.refreshPinnedFlags(); |
| 94 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | @Override |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 99 | protected void onRestored() { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 100 | // Nothing to do. |
| 101 | } |
| 102 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 103 | public void pinShortcuts(@UserIdInt int packageUserId, |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 104 | @NonNull String packageName, @NonNull List<String> ids) { |
| 105 | final ShortcutPackage packageShortcuts = |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 106 | mShortcutUser.getPackageShortcutsIfExists(packageName); |
| 107 | if (packageShortcuts == null) { |
| 108 | return; // No need to instantiate. |
| 109 | } |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 110 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 111 | final PackageWithUser pu = PackageWithUser.of(packageUserId, packageName); |
| 112 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 113 | final int idSize = ids.size(); |
| 114 | if (idSize == 0) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 115 | mPinnedShortcuts.remove(pu); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 116 | } else { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 117 | final ArraySet<String> prevSet = mPinnedShortcuts.get(pu); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 118 | |
| 119 | // Pin shortcuts. Make sure only pin the ones that were visible to the caller. |
| 120 | // i.e. a non-dynamic, pinned shortcut by *other launchers* shouldn't be pinned here. |
| 121 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 122 | final ArraySet<String> newSet = new ArraySet<>(); |
| 123 | |
| 124 | for (int i = 0; i < idSize; i++) { |
| 125 | final String id = ids.get(i); |
| 126 | final ShortcutInfo si = packageShortcuts.findShortcutById(id); |
| 127 | if (si == null) { |
| 128 | continue; |
| 129 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame^] | 130 | if (si.isDynamic() || si.isManifestShortcut() |
| 131 | || (prevSet != null && prevSet.contains(id))) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 132 | newSet.add(id); |
| 133 | } |
| 134 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 135 | mPinnedShortcuts.put(pu, newSet); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 136 | } |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 137 | packageShortcuts.refreshPinnedFlags(); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Return the pinned shortcut IDs for the publisher package. |
| 142 | */ |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 143 | public ArraySet<String> getPinnedShortcutIds(@NonNull String packageName, |
| 144 | @UserIdInt int packageUserId) { |
| 145 | return mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName)); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 148 | boolean cleanUpPackage(String packageName, @UserIdInt int packageUserId) { |
| 149 | return mPinnedShortcuts.remove(PackageWithUser.of(packageUserId, packageName)) != null; |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Persist. |
| 154 | */ |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public void saveToXml(XmlSerializer out, boolean forBackup) |
| 157 | throws IOException { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 158 | final int size = mPinnedShortcuts.size(); |
| 159 | if (size == 0) { |
| 160 | return; // Nothing to write. |
| 161 | } |
| 162 | |
| 163 | out.startTag(null, TAG_ROOT); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 164 | ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, getPackageName()); |
| 165 | ShortcutService.writeAttr(out, ATTR_LAUNCHER_USER_ID, getPackageUserId()); |
| 166 | getPackageInfo().saveToXml(out); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 167 | |
| 168 | for (int i = 0; i < size; i++) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 169 | final PackageWithUser pu = mPinnedShortcuts.keyAt(i); |
| 170 | |
| 171 | if (forBackup && (pu.userId != getOwnerUserId())) { |
| 172 | continue; // Target package on a different user, skip. (i.e. work profile) |
| 173 | } |
| 174 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 175 | out.startTag(null, TAG_PACKAGE); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 176 | ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, pu.packageName); |
| 177 | ShortcutService.writeAttr(out, ATTR_PACKAGE_USER_ID, pu.userId); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 178 | |
| 179 | final ArraySet<String> ids = mPinnedShortcuts.valueAt(i); |
| 180 | final int idSize = ids.size(); |
| 181 | for (int j = 0; j < idSize; j++) { |
| 182 | ShortcutService.writeTagValue(out, TAG_PIN, ids.valueAt(j)); |
| 183 | } |
| 184 | out.endTag(null, TAG_PACKAGE); |
| 185 | } |
| 186 | |
| 187 | out.endTag(null, TAG_ROOT); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Load. |
| 192 | */ |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 193 | public static ShortcutLauncher loadFromXml(XmlPullParser parser, ShortcutUser shortcutUser, |
| 194 | int ownerUserId, boolean fromBackup) throws IOException, XmlPullParserException { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 195 | final String launcherPackageName = ShortcutService.parseStringAttribute(parser, |
| 196 | ATTR_PACKAGE_NAME); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 197 | |
| 198 | // If restoring, just use the real user ID. |
| 199 | final int launcherUserId = |
| 200 | fromBackup ? ownerUserId |
| 201 | : ShortcutService.parseIntAttribute(parser, ATTR_LAUNCHER_USER_ID, ownerUserId); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 202 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 203 | final ShortcutLauncher ret = new ShortcutLauncher(shortcutUser, launcherUserId, |
| 204 | launcherPackageName, launcherUserId); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 205 | |
| 206 | ArraySet<String> ids = null; |
| 207 | final int outerDepth = parser.getDepth(); |
| 208 | int type; |
| 209 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 210 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 211 | if (type != XmlPullParser.START_TAG) { |
| 212 | continue; |
| 213 | } |
| 214 | final int depth = parser.getDepth(); |
| 215 | final String tag = parser.getName(); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 216 | if (depth == outerDepth + 1) { |
| 217 | switch (tag) { |
| 218 | case ShortcutPackageInfo.TAG_ROOT: |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 219 | ret.getPackageInfo().loadFromXml(parser, fromBackup); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 220 | continue; |
| 221 | case TAG_PACKAGE: { |
| 222 | final String packageName = ShortcutService.parseStringAttribute(parser, |
| 223 | ATTR_PACKAGE_NAME); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 224 | final int packageUserId = fromBackup ? ownerUserId |
| 225 | : ShortcutService.parseIntAttribute(parser, |
| 226 | ATTR_PACKAGE_USER_ID, ownerUserId); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 227 | ids = new ArraySet<>(); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 228 | ret.mPinnedShortcuts.put( |
| 229 | PackageWithUser.of(packageUserId, packageName), ids); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 230 | continue; |
| 231 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 234 | if (depth == outerDepth + 2) { |
| 235 | switch (tag) { |
| 236 | case TAG_PIN: { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 237 | if (ids == null) { |
| 238 | Slog.w(TAG, TAG_PIN + " in invalid place"); |
| 239 | } else { |
| 240 | ids.add(ShortcutService.parseStringAttribute(parser, ATTR_VALUE)); |
| 241 | } |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 242 | continue; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | ShortcutService.warnForInvalidTag(depth, tag); |
| 247 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 248 | return ret; |
| 249 | } |
| 250 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 251 | public void dump(@NonNull PrintWriter pw, @NonNull String prefix) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 252 | pw.println(); |
| 253 | |
| 254 | pw.print(prefix); |
| 255 | pw.print("Launcher: "); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 256 | pw.print(getPackageName()); |
| 257 | pw.print(" Package user: "); |
| 258 | pw.print(getPackageUserId()); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 259 | pw.print(" Owner user: "); |
| 260 | pw.print(getOwnerUserId()); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 261 | pw.println(); |
| 262 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 263 | getPackageInfo().dump(pw, prefix + " "); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 264 | pw.println(); |
| 265 | |
| 266 | final int size = mPinnedShortcuts.size(); |
| 267 | for (int i = 0; i < size; i++) { |
| 268 | pw.println(); |
| 269 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 270 | final PackageWithUser pu = mPinnedShortcuts.keyAt(i); |
| 271 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 272 | pw.print(prefix); |
| 273 | pw.print(" "); |
| 274 | pw.print("Package: "); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 275 | pw.print(pu.packageName); |
| 276 | pw.print(" User: "); |
| 277 | pw.println(pu.userId); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 278 | |
| 279 | final ArraySet<String> ids = mPinnedShortcuts.valueAt(i); |
| 280 | final int idSize = ids.size(); |
| 281 | |
| 282 | for (int j = 0; j < idSize; j++) { |
| 283 | pw.print(prefix); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 284 | pw.print(" Pinned: "); |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 285 | pw.print(ids.valueAt(j)); |
| 286 | pw.println(); |
| 287 | } |
| 288 | } |
| 289 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 290 | |
| 291 | @VisibleForTesting |
| 292 | ArraySet<String> getAllPinnedShortcutsForTest(String packageName, int packageUserId) { |
| 293 | return new ArraySet<>(mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName))); |
| 294 | } |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 295 | } |