blob: c6d66fe7fee587dc2c6ba2c330e32468ec36ae34 [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;
19import android.annotation.UserIdInt;
20import android.content.pm.ShortcutInfo;
21import android.util.ArrayMap;
22import android.util.ArraySet;
Makoto Onuki2e210c42016-03-30 08:30:36 -070023import android.util.Slog;
24
25import com.android.internal.annotations.VisibleForTesting;
26import com.android.server.pm.ShortcutUser.PackageWithUser;
Makoto Onuki31459242016-03-22 11:12:18 -070027
28import org.xmlpull.v1.XmlPullParser;
29import org.xmlpull.v1.XmlPullParserException;
30import org.xmlpull.v1.XmlSerializer;
31
32import java.io.IOException;
33import java.io.PrintWriter;
Makoto Onuki2e210c42016-03-30 08:30:36 -070034import java.util.ArrayList;
Makoto Onuki31459242016-03-22 11:12:18 -070035import java.util.List;
36
37/**
38 * Launcher information used by {@link ShortcutService}.
39 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070040class ShortcutLauncher extends ShortcutPackageItem {
Makoto Onuki31459242016-03-22 11:12:18 -070041 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 Onukid99c6f02016-03-28 11:02:54 -070048 private static final String ATTR_LAUNCHER_USER_ID = "launcher-user";
Makoto Onuki31459242016-03-22 11:12:18 -070049 private static final String ATTR_VALUE = "value";
50 private static final String ATTR_PACKAGE_NAME = "package-name";
Makoto Onuki2e210c42016-03-30 08:30:36 -070051 private static final String ATTR_PACKAGE_USER_ID = "package-user";
Makoto Onuki31459242016-03-22 11:12:18 -070052
Makoto Onuki9da23fc2016-03-29 11:14:42 -070053 private final int mOwnerUserId;
Makoto Onukid99c6f02016-03-28 11:02:54 -070054
Makoto Onuki31459242016-03-22 11:12:18 -070055 /**
56 * Package name -> IDs.
57 */
Makoto Onuki2e210c42016-03-30 08:30:36 -070058 final private ArrayMap<PackageWithUser, ArraySet<String>> mPinnedShortcuts = new ArrayMap<>();
Makoto Onuki31459242016-03-22 11:12:18 -070059
Makoto Onuki2e210c42016-03-30 08:30:36 -070060 private ShortcutLauncher(@UserIdInt int ownerUserId, @NonNull String packageName,
Makoto Onuki9da23fc2016-03-29 11:14:42 -070061 @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 Onukid99c6f02016-03-28 11:02:54 -070067 @UserIdInt int launcherUserId) {
Makoto Onuki2e210c42016-03-30 08:30:36 -070068 this(ownerUserId, packageName, launcherUserId, null);
Makoto Onuki31459242016-03-22 11:12:18 -070069 }
70
Makoto Onuki9da23fc2016-03-29 11:14:42 -070071 @Override
72 public int getOwnerUserId() {
73 return mOwnerUserId;
Makoto Onuki0acbb142016-03-22 17:02:57 -070074 }
75
Makoto Onuki2e210c42016-03-30 08:30:36 -070076 /**
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 Onuki9da23fc2016-03-29 11:14:42 -070096 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 Onukid99c6f02016-03-28 11:02:54 -0700100
Makoto Onuki2e210c42016-03-30 08:30:36 -0700101 final PackageWithUser pu = PackageWithUser.of(packageUserId, packageName);
102
Makoto Onuki31459242016-03-22 11:12:18 -0700103 final int idSize = ids.size();
104 if (idSize == 0) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700105 mPinnedShortcuts.remove(pu);
Makoto Onuki31459242016-03-22 11:12:18 -0700106 } else {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700107 final ArraySet<String> prevSet = mPinnedShortcuts.get(pu);
Makoto Onuki31459242016-03-22 11:12:18 -0700108
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 Onuki31459242016-03-22 11:12:18 -0700112 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 Onuki2e210c42016-03-30 08:30:36 -0700124 mPinnedShortcuts.put(pu, newSet);
Makoto Onuki31459242016-03-22 11:12:18 -0700125 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700126 packageShortcuts.refreshPinnedFlags(s);
Makoto Onuki31459242016-03-22 11:12:18 -0700127 }
128
129 /**
130 * Return the pinned shortcut IDs for the publisher package.
131 */
Makoto Onuki2e210c42016-03-30 08:30:36 -0700132 public ArraySet<String> getPinnedShortcutIds(@NonNull String packageName,
133 @UserIdInt int packageUserId) {
134 return mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName));
Makoto Onuki31459242016-03-22 11:12:18 -0700135 }
136
Makoto Onuki2e210c42016-03-30 08:30:36 -0700137 boolean cleanUpPackage(String packageName, @UserIdInt int packageUserId) {
138 return mPinnedShortcuts.remove(PackageWithUser.of(packageUserId, packageName)) != null;
Makoto Onuki31459242016-03-22 11:12:18 -0700139 }
140
141 /**
142 * Persist.
143 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700144 @Override
145 public void saveToXml(XmlSerializer out, boolean forBackup)
146 throws IOException {
Makoto Onuki31459242016-03-22 11:12:18 -0700147 final int size = mPinnedShortcuts.size();
148 if (size == 0) {
149 return; // Nothing to write.
150 }
151
152 out.startTag(null, TAG_ROOT);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700153 ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, getPackageName());
154 ShortcutService.writeAttr(out, ATTR_LAUNCHER_USER_ID, getPackageUserId());
155 getPackageInfo().saveToXml(out);
Makoto Onuki31459242016-03-22 11:12:18 -0700156
157 for (int i = 0; i < size; i++) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700158 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 Onuki31459242016-03-22 11:12:18 -0700164 out.startTag(null, TAG_PACKAGE);
Makoto Onuki2e210c42016-03-30 08:30:36 -0700165 ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, pu.packageName);
166 ShortcutService.writeAttr(out, ATTR_PACKAGE_USER_ID, pu.userId);
Makoto Onuki31459242016-03-22 11:12:18 -0700167
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 Onuki9da23fc2016-03-29 11:14:42 -0700182 public static ShortcutLauncher loadFromXml(XmlPullParser parser, int ownerUserId,
183 boolean fromBackup) throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -0700184 final String launcherPackageName = ShortcutService.parseStringAttribute(parser,
185 ATTR_PACKAGE_NAME);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700186
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 Onuki31459242016-03-22 11:12:18 -0700191
Makoto Onukid99c6f02016-03-28 11:02:54 -0700192 final ShortcutLauncher ret = new ShortcutLauncher(launcherUserId, launcherPackageName,
193 launcherUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700194
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 Onuki9da23fc2016-03-29 11:14:42 -0700205 if (depth == outerDepth + 1) {
206 switch (tag) {
207 case ShortcutPackageInfo.TAG_ROOT:
Makoto Onuki2e210c42016-03-30 08:30:36 -0700208 ret.getPackageInfo().loadFromXml(parser, fromBackup);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700209 continue;
210 case TAG_PACKAGE: {
211 final String packageName = ShortcutService.parseStringAttribute(parser,
212 ATTR_PACKAGE_NAME);
Makoto Onuki2e210c42016-03-30 08:30:36 -0700213 final int packageUserId = fromBackup ? ownerUserId
214 : ShortcutService.parseIntAttribute(parser,
215 ATTR_PACKAGE_USER_ID, ownerUserId);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700216 ids = new ArraySet<>();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700217 ret.mPinnedShortcuts.put(
218 PackageWithUser.of(packageUserId, packageName), ids);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700219 continue;
220 }
Makoto Onuki31459242016-03-22 11:12:18 -0700221 }
222 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700223 if (depth == outerDepth + 2) {
224 switch (tag) {
225 case TAG_PIN: {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700226 if (ids == null) {
227 Slog.w(TAG, TAG_PIN + " in invalid place");
228 } else {
229 ids.add(ShortcutService.parseStringAttribute(parser, ATTR_VALUE));
230 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700231 continue;
232 }
233 }
234 }
235 ShortcutService.warnForInvalidTag(depth, tag);
236 }
Makoto Onuki31459242016-03-22 11:12:18 -0700237 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 Onuki9da23fc2016-03-29 11:14:42 -0700245 pw.print(getPackageName());
246 pw.print(" Package user: ");
247 pw.print(getPackageUserId());
Makoto Onuki2e210c42016-03-30 08:30:36 -0700248 pw.print(" Owner user: ");
249 pw.print(getOwnerUserId());
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700250 pw.println();
251
252 getPackageInfo().dump(s, pw, prefix + " ");
Makoto Onuki31459242016-03-22 11:12:18 -0700253 pw.println();
254
255 final int size = mPinnedShortcuts.size();
256 for (int i = 0; i < size; i++) {
257 pw.println();
258
Makoto Onuki2e210c42016-03-30 08:30:36 -0700259 final PackageWithUser pu = mPinnedShortcuts.keyAt(i);
260
Makoto Onuki31459242016-03-22 11:12:18 -0700261 pw.print(prefix);
262 pw.print(" ");
263 pw.print("Package: ");
Makoto Onuki2e210c42016-03-30 08:30:36 -0700264 pw.print(pu.packageName);
265 pw.print(" User: ");
266 pw.println(pu.userId);
Makoto Onuki31459242016-03-22 11:12:18 -0700267
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 Onuki0acbb142016-03-22 17:02:57 -0700273 pw.print(" Pinned: ");
Makoto Onuki31459242016-03-22 11:12:18 -0700274 pw.print(ids.valueAt(j));
275 pw.println();
276 }
277 }
278 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700279
280 @VisibleForTesting
281 ArraySet<String> getAllPinnedShortcutsForTest(String packageName, int packageUserId) {
282 return new ArraySet<>(mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName)));
283 }
Makoto Onuki31459242016-03-22 11:12:18 -0700284}