blob: f922ad195ac2377c4533a91851ecefa0c10cfbea [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 Onuki2e210c42016-03-30 08:30:36 -070086 /**
87 * Called when the new package can't receive the backup, due to signature or version mismatch.
88 */
89 @Override
Makoto Onukic51b2872016-05-04 15:24:50 -070090 protected void onRestoreBlocked() {
Makoto Onuki2e210c42016-03-30 08:30:36 -070091 final ArrayList<PackageWithUser> pinnedPackages =
92 new ArrayList<>(mPinnedShortcuts.keySet());
93 mPinnedShortcuts.clear();
94 for (int i = pinnedPackages.size() - 1; i >= 0; i--) {
95 final PackageWithUser pu = pinnedPackages.get(i);
Makoto Onukic51b2872016-05-04 15:24:50 -070096 final ShortcutPackage p = mShortcutUser.getPackageShortcutsIfExists(pu.packageName);
97 if (p != null) {
98 p.refreshPinnedFlags();
99 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700100 }
101 }
102
103 @Override
Makoto Onukic51b2872016-05-04 15:24:50 -0700104 protected void onRestored() {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700105 // Nothing to do.
106 }
107
Makoto Onuki2d895c32016-12-02 15:48:40 -0800108 /**
109 * Pin the given shortcuts, replacing the current pinned ones.
110 */
Makoto Onukic51b2872016-05-04 15:24:50 -0700111 public void pinShortcuts(@UserIdInt int packageUserId,
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700112 @NonNull String packageName, @NonNull List<String> ids) {
113 final ShortcutPackage packageShortcuts =
Makoto Onukic51b2872016-05-04 15:24:50 -0700114 mShortcutUser.getPackageShortcutsIfExists(packageName);
115 if (packageShortcuts == null) {
116 return; // No need to instantiate.
117 }
Makoto Onukid99c6f02016-03-28 11:02:54 -0700118
Makoto Onuki2e210c42016-03-30 08:30:36 -0700119 final PackageWithUser pu = PackageWithUser.of(packageUserId, packageName);
120
Makoto Onuki31459242016-03-22 11:12:18 -0700121 final int idSize = ids.size();
122 if (idSize == 0) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700123 mPinnedShortcuts.remove(pu);
Makoto Onuki31459242016-03-22 11:12:18 -0700124 } else {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700125 final ArraySet<String> prevSet = mPinnedShortcuts.get(pu);
Makoto Onuki31459242016-03-22 11:12:18 -0700126
127 // Pin shortcuts. Make sure only pin the ones that were visible to the caller.
128 // i.e. a non-dynamic, pinned shortcut by *other launchers* shouldn't be pinned here.
129
Makoto Onuki31459242016-03-22 11:12:18 -0700130 final ArraySet<String> newSet = new ArraySet<>();
131
132 for (int i = 0; i < idSize; i++) {
133 final String id = ids.get(i);
134 final ShortcutInfo si = packageShortcuts.findShortcutById(id);
135 if (si == null) {
136 continue;
137 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700138 if (si.isDynamic() || si.isManifestShortcut()
139 || (prevSet != null && prevSet.contains(id))) {
Makoto Onuki31459242016-03-22 11:12:18 -0700140 newSet.add(id);
141 }
142 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700143 mPinnedShortcuts.put(pu, newSet);
Makoto Onuki31459242016-03-22 11:12:18 -0700144 }
Makoto Onukic51b2872016-05-04 15:24:50 -0700145 packageShortcuts.refreshPinnedFlags();
Makoto Onuki31459242016-03-22 11:12:18 -0700146 }
147
148 /**
149 * Return the pinned shortcut IDs for the publisher package.
150 */
Makoto Onuki2d895c32016-12-02 15:48:40 -0800151 @Nullable
Makoto Onuki2e210c42016-03-30 08:30:36 -0700152 public ArraySet<String> getPinnedShortcutIds(@NonNull String packageName,
153 @UserIdInt int packageUserId) {
154 return mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName));
Makoto Onuki31459242016-03-22 11:12:18 -0700155 }
156
Makoto Onuki2d895c32016-12-02 15:48:40 -0800157 /**
158 * Return true if the given shortcut is pinned by this launcher.
159 */
160 public boolean hasPinned(ShortcutInfo shortcut) {
161 final ArraySet<String> pinned =
162 getPinnedShortcutIds(shortcut.getPackage(), shortcut.getUserId());
163 return (pinned != null) && pinned.contains(shortcut.getId());
164 }
165
166 /**
167 * Additionally pin a shortcut. c.f. {@link #pinShortcuts(int, String, List)}
168 */
169 public void addPinnedShortcut(@NonNull String packageName, @UserIdInt int packageUserId,
170 String id) {
171 final ArraySet<String> pinnedSet = getPinnedShortcutIds(packageName, packageUserId);
172 final ArrayList<String> pinnedList;
173 if (pinnedSet != null) {
174 pinnedList = new ArrayList<>(pinnedSet.size() + 1);
175 pinnedList.addAll(pinnedSet);
176 } else {
177 pinnedList = new ArrayList<>(1);
178 }
179 pinnedList.add(id);
180
181 pinShortcuts(packageUserId, packageName, pinnedList);
182 }
183
Makoto Onuki2e210c42016-03-30 08:30:36 -0700184 boolean cleanUpPackage(String packageName, @UserIdInt int packageUserId) {
185 return mPinnedShortcuts.remove(PackageWithUser.of(packageUserId, packageName)) != null;
Makoto Onuki31459242016-03-22 11:12:18 -0700186 }
187
Makoto Onukic8c33292016-09-12 16:36:59 -0700188 public void ensureVersionInfo() {
189 final PackageInfo pi = mShortcutUser.mService.getPackageInfoWithSignatures(
190 getPackageName(), getPackageUserId());
191 if (pi == null) {
192 Slog.w(TAG, "Package not found: " + getPackageName());
193 return;
194 }
195 getPackageInfo().updateVersionInfo(pi);
196 }
197
Makoto Onuki31459242016-03-22 11:12:18 -0700198 /**
199 * Persist.
200 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700201 @Override
202 public void saveToXml(XmlSerializer out, boolean forBackup)
203 throws IOException {
Makoto Onuki31459242016-03-22 11:12:18 -0700204 final int size = mPinnedShortcuts.size();
205 if (size == 0) {
206 return; // Nothing to write.
207 }
208
209 out.startTag(null, TAG_ROOT);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700210 ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, getPackageName());
211 ShortcutService.writeAttr(out, ATTR_LAUNCHER_USER_ID, getPackageUserId());
212 getPackageInfo().saveToXml(out);
Makoto Onuki31459242016-03-22 11:12:18 -0700213
214 for (int i = 0; i < size; i++) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700215 final PackageWithUser pu = mPinnedShortcuts.keyAt(i);
216
217 if (forBackup && (pu.userId != getOwnerUserId())) {
218 continue; // Target package on a different user, skip. (i.e. work profile)
219 }
220
Makoto Onuki31459242016-03-22 11:12:18 -0700221 out.startTag(null, TAG_PACKAGE);
Makoto Onuki2e210c42016-03-30 08:30:36 -0700222 ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, pu.packageName);
223 ShortcutService.writeAttr(out, ATTR_PACKAGE_USER_ID, pu.userId);
Makoto Onuki31459242016-03-22 11:12:18 -0700224
225 final ArraySet<String> ids = mPinnedShortcuts.valueAt(i);
226 final int idSize = ids.size();
227 for (int j = 0; j < idSize; j++) {
228 ShortcutService.writeTagValue(out, TAG_PIN, ids.valueAt(j));
229 }
230 out.endTag(null, TAG_PACKAGE);
231 }
232
233 out.endTag(null, TAG_ROOT);
234 }
235
236 /**
237 * Load.
238 */
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700239 public static ShortcutLauncher loadFromXml(XmlPullParser parser, ShortcutUser shortcutUser,
240 int ownerUserId, boolean fromBackup) throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -0700241 final String launcherPackageName = ShortcutService.parseStringAttribute(parser,
242 ATTR_PACKAGE_NAME);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700243
244 // If restoring, just use the real user ID.
245 final int launcherUserId =
246 fromBackup ? ownerUserId
247 : ShortcutService.parseIntAttribute(parser, ATTR_LAUNCHER_USER_ID, ownerUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700248
Makoto Onukic8c33292016-09-12 16:36:59 -0700249 final ShortcutLauncher ret = new ShortcutLauncher(shortcutUser, ownerUserId,
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700250 launcherPackageName, launcherUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700251
252 ArraySet<String> ids = null;
253 final int outerDepth = parser.getDepth();
254 int type;
255 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
256 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
257 if (type != XmlPullParser.START_TAG) {
258 continue;
259 }
260 final int depth = parser.getDepth();
261 final String tag = parser.getName();
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700262 if (depth == outerDepth + 1) {
263 switch (tag) {
264 case ShortcutPackageInfo.TAG_ROOT:
Makoto Onuki2e210c42016-03-30 08:30:36 -0700265 ret.getPackageInfo().loadFromXml(parser, fromBackup);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700266 continue;
267 case TAG_PACKAGE: {
268 final String packageName = ShortcutService.parseStringAttribute(parser,
269 ATTR_PACKAGE_NAME);
Makoto Onuki2e210c42016-03-30 08:30:36 -0700270 final int packageUserId = fromBackup ? ownerUserId
271 : ShortcutService.parseIntAttribute(parser,
272 ATTR_PACKAGE_USER_ID, ownerUserId);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700273 ids = new ArraySet<>();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700274 ret.mPinnedShortcuts.put(
275 PackageWithUser.of(packageUserId, packageName), ids);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700276 continue;
277 }
Makoto Onuki31459242016-03-22 11:12:18 -0700278 }
279 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700280 if (depth == outerDepth + 2) {
281 switch (tag) {
282 case TAG_PIN: {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700283 if (ids == null) {
284 Slog.w(TAG, TAG_PIN + " in invalid place");
285 } else {
286 ids.add(ShortcutService.parseStringAttribute(parser, ATTR_VALUE));
287 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700288 continue;
289 }
290 }
291 }
292 ShortcutService.warnForInvalidTag(depth, tag);
293 }
Makoto Onuki31459242016-03-22 11:12:18 -0700294 return ret;
295 }
296
Makoto Onuki20b82212017-10-04 15:03:50 -0700297 public void dump(@NonNull PrintWriter pw, @NonNull String prefix, DumpFilter filter) {
Makoto Onuki31459242016-03-22 11:12:18 -0700298 pw.println();
299
300 pw.print(prefix);
301 pw.print("Launcher: ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700302 pw.print(getPackageName());
303 pw.print(" Package user: ");
304 pw.print(getPackageUserId());
Makoto Onuki2e210c42016-03-30 08:30:36 -0700305 pw.print(" Owner user: ");
306 pw.print(getOwnerUserId());
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700307 pw.println();
308
Makoto Onukic51b2872016-05-04 15:24:50 -0700309 getPackageInfo().dump(pw, prefix + " ");
Makoto Onuki31459242016-03-22 11:12:18 -0700310 pw.println();
311
312 final int size = mPinnedShortcuts.size();
313 for (int i = 0; i < size; i++) {
314 pw.println();
315
Makoto Onuki2e210c42016-03-30 08:30:36 -0700316 final PackageWithUser pu = mPinnedShortcuts.keyAt(i);
317
Makoto Onuki31459242016-03-22 11:12:18 -0700318 pw.print(prefix);
319 pw.print(" ");
320 pw.print("Package: ");
Makoto Onuki2e210c42016-03-30 08:30:36 -0700321 pw.print(pu.packageName);
322 pw.print(" User: ");
323 pw.println(pu.userId);
Makoto Onuki31459242016-03-22 11:12:18 -0700324
325 final ArraySet<String> ids = mPinnedShortcuts.valueAt(i);
326 final int idSize = ids.size();
327
328 for (int j = 0; j < idSize; j++) {
329 pw.print(prefix);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700330 pw.print(" Pinned: ");
Makoto Onuki31459242016-03-22 11:12:18 -0700331 pw.print(ids.valueAt(j));
332 pw.println();
333 }
334 }
335 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700336
Makoto Onuki76269922016-07-15 14:58:54 -0700337 @Override
338 public JSONObject dumpCheckin(boolean clear) throws JSONException {
339 final JSONObject result = super.dumpCheckin(clear);
340
341 // Nothing really interesting to dump.
342
343 return result;
344 }
345
Makoto Onuki2e210c42016-03-30 08:30:36 -0700346 @VisibleForTesting
347 ArraySet<String> getAllPinnedShortcutsForTest(String packageName, int packageUserId) {
348 return new ArraySet<>(mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName)));
349 }
Makoto Onuki31459242016-03-22 11:12:18 -0700350}