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