blob: 0fecb631a182a1f02da24fbb99b0246b92e67b3f [file] [log] [blame]
Makoto Onuki31459242016-03-22 11:12:18 -07001/*
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 */
16package com.android.server.pm;
17
18import android.annotation.NonNull;
Makoto Onuki2d895c32016-12-02 15:48:40 -080019import android.annotation.Nullable;
Makoto Onuki31459242016-03-22 11:12:18 -070020import android.annotation.UserIdInt;
Makoto Onukic8c33292016-09-12 16:36:59 -070021import android.content.pm.PackageInfo;
Makoto Onuki31459242016-03-22 11:12:18 -070022import android.content.pm.ShortcutInfo;
23import android.util.ArrayMap;
24import android.util.ArraySet;
Makoto Onuki2e210c42016-03-30 08:30:36 -070025import android.util.Slog;
26
27import com.android.internal.annotations.VisibleForTesting;
Makoto Onuki20b82212017-10-04 15:03:50 -070028import com.android.server.pm.ShortcutService.DumpFilter;
Makoto Onuki2e210c42016-03-30 08:30:36 -070029import com.android.server.pm.ShortcutUser.PackageWithUser;
Makoto Onuki31459242016-03-22 11:12:18 -070030
Makoto Onuki76269922016-07-15 14:58:54 -070031import org.json.JSONException;
32import org.json.JSONObject;
Makoto Onuki31459242016-03-22 11:12:18 -070033import org.xmlpull.v1.XmlPullParser;
34import org.xmlpull.v1.XmlPullParserException;
35import org.xmlpull.v1.XmlSerializer;
36
37import java.io.IOException;
38import java.io.PrintWriter;
Makoto Onuki2e210c42016-03-30 08:30:36 -070039import java.util.ArrayList;
Makoto Onuki31459242016-03-22 11:12:18 -070040import java.util.List;
41
42/**
43 * Launcher information used by {@link ShortcutService}.
Makoto Onuki22fcc682016-05-17 14:52:19 -070044 *
45 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
Makoto Onuki31459242016-03-22 11:12:18 -070046 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070047class ShortcutLauncher extends ShortcutPackageItem {
Makoto Onuki31459242016-03-22 11:12:18 -070048 private static final String TAG = ShortcutService.TAG;
49
50 static final String TAG_ROOT = "launcher-pins";
51
52 private static final String TAG_PACKAGE = "package";
53 private static final String TAG_PIN = "pin";
54
Makoto Onukid99c6f02016-03-28 11:02:54 -070055 private static final String ATTR_LAUNCHER_USER_ID = "launcher-user";
Makoto Onuki31459242016-03-22 11:12:18 -070056 private static final String ATTR_VALUE = "value";
57 private static final String ATTR_PACKAGE_NAME = "package-name";
Makoto Onuki2e210c42016-03-30 08:30:36 -070058 private static final String ATTR_PACKAGE_USER_ID = "package-user";
Makoto Onuki31459242016-03-22 11:12:18 -070059
Makoto Onuki9da23fc2016-03-29 11:14:42 -070060 private final int mOwnerUserId;
Makoto Onukid99c6f02016-03-28 11:02:54 -070061
Makoto Onuki31459242016-03-22 11:12:18 -070062 /**
63 * Package name -> IDs.
64 */
Makoto Onuki2e210c42016-03-30 08:30:36 -070065 final private ArrayMap<PackageWithUser, ArraySet<String>> mPinnedShortcuts = new ArrayMap<>();
Makoto Onuki31459242016-03-22 11:12:18 -070066
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070067 private ShortcutLauncher(@NonNull ShortcutUser shortcutUser,
68 @UserIdInt int ownerUserId, @NonNull String packageName,
Makoto Onuki9da23fc2016-03-29 11:14:42 -070069 @UserIdInt int launcherUserId, ShortcutPackageInfo spi) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070070 super(shortcutUser, launcherUserId, packageName,
71 spi != null ? spi : ShortcutPackageInfo.newEmpty());
Makoto Onuki9da23fc2016-03-29 11:14:42 -070072 mOwnerUserId = ownerUserId;
73 }
74
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070075 public ShortcutLauncher(@NonNull ShortcutUser shortcutUser,
76 @UserIdInt int ownerUserId, @NonNull String packageName,
Makoto Onukid99c6f02016-03-28 11:02:54 -070077 @UserIdInt int launcherUserId) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070078 this(shortcutUser, ownerUserId, packageName, launcherUserId, null);
Makoto Onuki31459242016-03-22 11:12:18 -070079 }
80
Makoto Onuki9da23fc2016-03-29 11:14:42 -070081 @Override
82 public int getOwnerUserId() {
83 return mOwnerUserId;
Makoto Onuki0acbb142016-03-22 17:02:57 -070084 }
85
Makoto Onukia4f89b12017-10-05 10:37:55 -070086 @Override
87 protected boolean canRestoreAnyVersion() {
88 // Launcher's pinned shortcuts can be restored to an older version.
89 return true;
90 }
91
Makoto Onuki2e210c42016-03-30 08:30:36 -070092 /**
93 * Called when the new package can't receive the backup, due to signature or version mismatch.
94 */
Makoto Onukia4f89b12017-10-05 10:37:55 -070095 private void onRestoreBlocked() {
Makoto Onuki2e210c42016-03-30 08:30:36 -070096 final ArrayList<PackageWithUser> pinnedPackages =
97 new ArrayList<>(mPinnedShortcuts.keySet());
98 mPinnedShortcuts.clear();
99 for (int i = pinnedPackages.size() - 1; i >= 0; i--) {
100 final PackageWithUser pu = pinnedPackages.get(i);
Makoto Onukic51b2872016-05-04 15:24:50 -0700101 final ShortcutPackage p = mShortcutUser.getPackageShortcutsIfExists(pu.packageName);
102 if (p != null) {
103 p.refreshPinnedFlags();
104 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700105 }
106 }
107
108 @Override
Makoto Onukia4f89b12017-10-05 10:37:55 -0700109 protected void onRestored(int restoreBlockReason) {
110 // For launcher, possible reasons here are DISABLED_REASON_SIGNATURE_MISMATCH or
111 // DISABLED_REASON_BACKUP_NOT_SUPPORTED.
112 // DISABLED_REASON_VERSION_LOWER will NOT happen because we don't check version
113 // code for launchers.
114 if (restoreBlockReason != ShortcutInfo.DISABLED_REASON_NOT_DISABLED) {
115 onRestoreBlocked();
116 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700117 }
118
Makoto Onuki2d895c32016-12-02 15:48:40 -0800119 /**
120 * Pin the given shortcuts, replacing the current pinned ones.
121 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700122 public void pinShortcuts(@UserIdInt int packageUserId,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700123 @NonNull String packageName, @NonNull List<String> ids, boolean forPinRequest) {
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700124 final ShortcutPackage packageShortcuts =
Makoto Onukic51b2872016-05-04 15:24:50 -0700125 mShortcutUser.getPackageShortcutsIfExists(packageName);
126 if (packageShortcuts == null) {
127 return; // No need to instantiate.
128 }
Makoto Onukid99c6f02016-03-28 11:02:54 -0700129
Makoto Onuki2e210c42016-03-30 08:30:36 -0700130 final PackageWithUser pu = PackageWithUser.of(packageUserId, packageName);
131
Makoto Onuki31459242016-03-22 11:12:18 -0700132 final int idSize = ids.size();
133 if (idSize == 0) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700134 mPinnedShortcuts.remove(pu);
Makoto Onuki31459242016-03-22 11:12:18 -0700135 } else {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700136 final ArraySet<String> prevSet = mPinnedShortcuts.get(pu);
Makoto Onuki31459242016-03-22 11:12:18 -0700137
Makoto Onukia4f89b12017-10-05 10:37:55 -0700138 // Actually pin shortcuts.
139 // This logic here is to make sure a launcher cannot pin a shortcut that is floating
140 // (i.e. not dynamic nor manifest but is pinned) and pinned by another launcher.
141 // In this case, technically the shortcut doesn't exist to this launcher, so it can't
142 // pin it.
143 // (Maybe unnecessarily strict...)
Makoto Onuki31459242016-03-22 11:12:18 -0700144
Makoto Onuki31459242016-03-22 11:12:18 -0700145 final ArraySet<String> newSet = new ArraySet<>();
146
147 for (int i = 0; i < idSize; i++) {
148 final String id = ids.get(i);
149 final ShortcutInfo si = packageShortcuts.findShortcutById(id);
150 if (si == null) {
151 continue;
152 }
Makoto Onukia4f89b12017-10-05 10:37:55 -0700153 if (si.isDynamic()
154 || si.isManifestShortcut()
155 || (prevSet != null && prevSet.contains(id))
156 || forPinRequest) {
Makoto Onuki31459242016-03-22 11:12:18 -0700157 newSet.add(id);
158 }
159 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700160 mPinnedShortcuts.put(pu, newSet);
Makoto Onuki31459242016-03-22 11:12:18 -0700161 }
Makoto Onukic51b2872016-05-04 15:24:50 -0700162 packageShortcuts.refreshPinnedFlags();
Makoto Onuki31459242016-03-22 11:12:18 -0700163 }
164
165 /**
166 * Return the pinned shortcut IDs for the publisher package.
167 */
Makoto Onuki2d895c32016-12-02 15:48:40 -0800168 @Nullable
Makoto Onuki2e210c42016-03-30 08:30:36 -0700169 public ArraySet<String> getPinnedShortcutIds(@NonNull String packageName,
170 @UserIdInt int packageUserId) {
171 return mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName));
Makoto Onuki31459242016-03-22 11:12:18 -0700172 }
173
Makoto Onuki2d895c32016-12-02 15:48:40 -0800174 /**
Makoto Onukia4f89b12017-10-05 10:37:55 -0700175 * Return true if the given shortcut is pinned by this launcher.<code></code>
Makoto Onuki2d895c32016-12-02 15:48:40 -0800176 */
177 public boolean hasPinned(ShortcutInfo shortcut) {
178 final ArraySet<String> pinned =
179 getPinnedShortcutIds(shortcut.getPackage(), shortcut.getUserId());
180 return (pinned != null) && pinned.contains(shortcut.getId());
181 }
182
183 /**
Makoto Onukia4f89b12017-10-05 10:37:55 -0700184 * Additionally pin a shortcut. c.f. {@link #pinShortcuts(int, String, List, boolean)}
Makoto Onuki2d895c32016-12-02 15:48:40 -0800185 */
186 public void addPinnedShortcut(@NonNull String packageName, @UserIdInt int packageUserId,
Makoto Onukia4f89b12017-10-05 10:37:55 -0700187 String id, boolean forPinRequest) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800188 final ArraySet<String> pinnedSet = getPinnedShortcutIds(packageName, packageUserId);
189 final ArrayList<String> pinnedList;
190 if (pinnedSet != null) {
191 pinnedList = new ArrayList<>(pinnedSet.size() + 1);
192 pinnedList.addAll(pinnedSet);
193 } else {
194 pinnedList = new ArrayList<>(1);
195 }
196 pinnedList.add(id);
197
Makoto Onukia4f89b12017-10-05 10:37:55 -0700198 pinShortcuts(packageUserId, packageName, pinnedList, forPinRequest);
Makoto Onuki2d895c32016-12-02 15:48:40 -0800199 }
200
Makoto Onuki2e210c42016-03-30 08:30:36 -0700201 boolean cleanUpPackage(String packageName, @UserIdInt int packageUserId) {
202 return mPinnedShortcuts.remove(PackageWithUser.of(packageUserId, packageName)) != null;
Makoto Onuki31459242016-03-22 11:12:18 -0700203 }
204
Makoto Onukia4f89b12017-10-05 10:37:55 -0700205 public void ensurePackageInfo() {
Makoto Onukic8c33292016-09-12 16:36:59 -0700206 final PackageInfo pi = mShortcutUser.mService.getPackageInfoWithSignatures(
207 getPackageName(), getPackageUserId());
208 if (pi == null) {
209 Slog.w(TAG, "Package not found: " + getPackageName());
210 return;
211 }
Makoto Onukia4f89b12017-10-05 10:37:55 -0700212 getPackageInfo().updateFromPackageInfo(pi);
Makoto Onukic8c33292016-09-12 16:36:59 -0700213 }
214
Makoto Onuki31459242016-03-22 11:12:18 -0700215 /**
216 * Persist.
217 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700218 @Override
219 public void saveToXml(XmlSerializer out, boolean forBackup)
220 throws IOException {
Makoto Onukia4f89b12017-10-05 10:37:55 -0700221 if (forBackup && !getPackageInfo().isBackupAllowed()) {
222 // If an launcher app doesn't support backup&restore, then nothing to do.
223 return;
224 }
Makoto Onuki31459242016-03-22 11:12:18 -0700225 final int size = mPinnedShortcuts.size();
226 if (size == 0) {
227 return; // Nothing to write.
228 }
229
230 out.startTag(null, TAG_ROOT);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700231 ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, getPackageName());
232 ShortcutService.writeAttr(out, ATTR_LAUNCHER_USER_ID, getPackageUserId());
Makoto Onukie3fffa92018-02-28 16:25:40 -0800233 getPackageInfo().saveToXml(mShortcutUser.mService, out, forBackup);
Makoto Onuki31459242016-03-22 11:12:18 -0700234
235 for (int i = 0; i < size; i++) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700236 final PackageWithUser pu = mPinnedShortcuts.keyAt(i);
237
238 if (forBackup && (pu.userId != getOwnerUserId())) {
239 continue; // Target package on a different user, skip. (i.e. work profile)
240 }
241
Makoto Onuki31459242016-03-22 11:12:18 -0700242 out.startTag(null, TAG_PACKAGE);
Makoto Onuki2e210c42016-03-30 08:30:36 -0700243 ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, pu.packageName);
244 ShortcutService.writeAttr(out, ATTR_PACKAGE_USER_ID, pu.userId);
Makoto Onuki31459242016-03-22 11:12:18 -0700245
246 final ArraySet<String> ids = mPinnedShortcuts.valueAt(i);
247 final int idSize = ids.size();
248 for (int j = 0; j < idSize; j++) {
249 ShortcutService.writeTagValue(out, TAG_PIN, ids.valueAt(j));
250 }
251 out.endTag(null, TAG_PACKAGE);
252 }
253
254 out.endTag(null, TAG_ROOT);
255 }
256
257 /**
258 * Load.
259 */
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700260 public static ShortcutLauncher loadFromXml(XmlPullParser parser, ShortcutUser shortcutUser,
261 int ownerUserId, boolean fromBackup) throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -0700262 final String launcherPackageName = ShortcutService.parseStringAttribute(parser,
263 ATTR_PACKAGE_NAME);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700264
265 // If restoring, just use the real user ID.
266 final int launcherUserId =
267 fromBackup ? ownerUserId
268 : ShortcutService.parseIntAttribute(parser, ATTR_LAUNCHER_USER_ID, ownerUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700269
Makoto Onukic8c33292016-09-12 16:36:59 -0700270 final ShortcutLauncher ret = new ShortcutLauncher(shortcutUser, ownerUserId,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700271 launcherPackageName, launcherUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700272
273 ArraySet<String> ids = null;
274 final int outerDepth = parser.getDepth();
275 int type;
276 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
277 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
278 if (type != XmlPullParser.START_TAG) {
279 continue;
280 }
281 final int depth = parser.getDepth();
282 final String tag = parser.getName();
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700283 if (depth == outerDepth + 1) {
284 switch (tag) {
285 case ShortcutPackageInfo.TAG_ROOT:
Makoto Onuki2e210c42016-03-30 08:30:36 -0700286 ret.getPackageInfo().loadFromXml(parser, fromBackup);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700287 continue;
288 case TAG_PACKAGE: {
289 final String packageName = ShortcutService.parseStringAttribute(parser,
290 ATTR_PACKAGE_NAME);
Makoto Onuki2e210c42016-03-30 08:30:36 -0700291 final int packageUserId = fromBackup ? ownerUserId
292 : ShortcutService.parseIntAttribute(parser,
293 ATTR_PACKAGE_USER_ID, ownerUserId);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700294 ids = new ArraySet<>();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700295 ret.mPinnedShortcuts.put(
296 PackageWithUser.of(packageUserId, packageName), ids);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700297 continue;
298 }
Makoto Onuki31459242016-03-22 11:12:18 -0700299 }
300 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700301 if (depth == outerDepth + 2) {
302 switch (tag) {
303 case TAG_PIN: {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700304 if (ids == null) {
305 Slog.w(TAG, TAG_PIN + " in invalid place");
306 } else {
307 ids.add(ShortcutService.parseStringAttribute(parser, ATTR_VALUE));
308 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700309 continue;
310 }
311 }
312 }
313 ShortcutService.warnForInvalidTag(depth, tag);
314 }
Makoto Onuki31459242016-03-22 11:12:18 -0700315 return ret;
316 }
317
Makoto Onuki20b82212017-10-04 15:03:50 -0700318 public void dump(@NonNull PrintWriter pw, @NonNull String prefix, DumpFilter filter) {
Makoto Onuki31459242016-03-22 11:12:18 -0700319 pw.println();
320
321 pw.print(prefix);
322 pw.print("Launcher: ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700323 pw.print(getPackageName());
324 pw.print(" Package user: ");
325 pw.print(getPackageUserId());
Makoto Onuki2e210c42016-03-30 08:30:36 -0700326 pw.print(" Owner user: ");
327 pw.print(getOwnerUserId());
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700328 pw.println();
329
Makoto Onukic51b2872016-05-04 15:24:50 -0700330 getPackageInfo().dump(pw, prefix + " ");
Makoto Onuki31459242016-03-22 11:12:18 -0700331 pw.println();
332
333 final int size = mPinnedShortcuts.size();
334 for (int i = 0; i < size; i++) {
335 pw.println();
336
Makoto Onuki2e210c42016-03-30 08:30:36 -0700337 final PackageWithUser pu = mPinnedShortcuts.keyAt(i);
338
Makoto Onuki31459242016-03-22 11:12:18 -0700339 pw.print(prefix);
340 pw.print(" ");
341 pw.print("Package: ");
Makoto Onuki2e210c42016-03-30 08:30:36 -0700342 pw.print(pu.packageName);
343 pw.print(" User: ");
344 pw.println(pu.userId);
Makoto Onuki31459242016-03-22 11:12:18 -0700345
346 final ArraySet<String> ids = mPinnedShortcuts.valueAt(i);
347 final int idSize = ids.size();
348
349 for (int j = 0; j < idSize; j++) {
350 pw.print(prefix);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700351 pw.print(" Pinned: ");
Makoto Onuki31459242016-03-22 11:12:18 -0700352 pw.print(ids.valueAt(j));
353 pw.println();
354 }
355 }
356 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700357
Makoto Onuki76269922016-07-15 14:58:54 -0700358 @Override
359 public JSONObject dumpCheckin(boolean clear) throws JSONException {
360 final JSONObject result = super.dumpCheckin(clear);
361
362 // Nothing really interesting to dump.
363
364 return result;
365 }
366
Makoto Onuki2e210c42016-03-30 08:30:36 -0700367 @VisibleForTesting
368 ArraySet<String> getAllPinnedShortcutsForTest(String packageName, int packageUserId) {
369 return new ArraySet<>(mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName)));
370 }
Makoto Onuki31459242016-03-22 11:12:18 -0700371}