blob: df51923c97d37d33032f5f5344481cf06475754f [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
Makoto Onuki76269922016-07-15 14:58:54 -070028import org.json.JSONException;
29import org.json.JSONObject;
Makoto Onuki31459242016-03-22 11:12:18 -070030import org.xmlpull.v1.XmlPullParser;
31import org.xmlpull.v1.XmlPullParserException;
32import org.xmlpull.v1.XmlSerializer;
33
34import java.io.IOException;
35import java.io.PrintWriter;
Makoto Onuki2e210c42016-03-30 08:30:36 -070036import java.util.ArrayList;
Makoto Onuki31459242016-03-22 11:12:18 -070037import java.util.List;
38
39/**
40 * Launcher information used by {@link ShortcutService}.
Makoto Onuki22fcc682016-05-17 14:52:19 -070041 *
42 * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
Makoto Onuki31459242016-03-22 11:12:18 -070043 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -070044class ShortcutLauncher extends ShortcutPackageItem {
Makoto Onuki31459242016-03-22 11:12:18 -070045 private static final String TAG = ShortcutService.TAG;
46
47 static final String TAG_ROOT = "launcher-pins";
48
49 private static final String TAG_PACKAGE = "package";
50 private static final String TAG_PIN = "pin";
51
Makoto Onukid99c6f02016-03-28 11:02:54 -070052 private static final String ATTR_LAUNCHER_USER_ID = "launcher-user";
Makoto Onuki31459242016-03-22 11:12:18 -070053 private static final String ATTR_VALUE = "value";
54 private static final String ATTR_PACKAGE_NAME = "package-name";
Makoto Onuki2e210c42016-03-30 08:30:36 -070055 private static final String ATTR_PACKAGE_USER_ID = "package-user";
Makoto Onuki31459242016-03-22 11:12:18 -070056
Makoto Onuki9da23fc2016-03-29 11:14:42 -070057 private final int mOwnerUserId;
Makoto Onukid99c6f02016-03-28 11:02:54 -070058
Makoto Onuki31459242016-03-22 11:12:18 -070059 /**
60 * Package name -> IDs.
61 */
Makoto Onuki2e210c42016-03-30 08:30:36 -070062 final private ArrayMap<PackageWithUser, ArraySet<String>> mPinnedShortcuts = new ArrayMap<>();
Makoto Onuki31459242016-03-22 11:12:18 -070063
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070064 private ShortcutLauncher(@NonNull ShortcutUser shortcutUser,
65 @UserIdInt int ownerUserId, @NonNull String packageName,
Makoto Onuki9da23fc2016-03-29 11:14:42 -070066 @UserIdInt int launcherUserId, ShortcutPackageInfo spi) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070067 super(shortcutUser, launcherUserId, packageName,
68 spi != null ? spi : ShortcutPackageInfo.newEmpty());
Makoto Onuki9da23fc2016-03-29 11:14:42 -070069 mOwnerUserId = ownerUserId;
70 }
71
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070072 public ShortcutLauncher(@NonNull ShortcutUser shortcutUser,
73 @UserIdInt int ownerUserId, @NonNull String packageName,
Makoto Onukid99c6f02016-03-28 11:02:54 -070074 @UserIdInt int launcherUserId) {
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070075 this(shortcutUser, ownerUserId, packageName, launcherUserId, null);
Makoto Onuki31459242016-03-22 11:12:18 -070076 }
77
Makoto Onuki9da23fc2016-03-29 11:14:42 -070078 @Override
79 public int getOwnerUserId() {
80 return mOwnerUserId;
Makoto Onuki0acbb142016-03-22 17:02:57 -070081 }
82
Makoto Onuki2e210c42016-03-30 08:30:36 -070083 /**
84 * Called when the new package can't receive the backup, due to signature or version mismatch.
85 */
86 @Override
Makoto Onukic51b2872016-05-04 15:24:50 -070087 protected void onRestoreBlocked() {
Makoto Onuki2e210c42016-03-30 08:30:36 -070088 final ArrayList<PackageWithUser> pinnedPackages =
89 new ArrayList<>(mPinnedShortcuts.keySet());
90 mPinnedShortcuts.clear();
91 for (int i = pinnedPackages.size() - 1; i >= 0; i--) {
92 final PackageWithUser pu = pinnedPackages.get(i);
Makoto Onukic51b2872016-05-04 15:24:50 -070093 final ShortcutPackage p = mShortcutUser.getPackageShortcutsIfExists(pu.packageName);
94 if (p != null) {
95 p.refreshPinnedFlags();
96 }
Makoto Onuki2e210c42016-03-30 08:30:36 -070097 }
98 }
99
100 @Override
Makoto Onukic51b2872016-05-04 15:24:50 -0700101 protected void onRestored() {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700102 // Nothing to do.
103 }
104
Makoto Onukic51b2872016-05-04 15:24:50 -0700105 public void pinShortcuts(@UserIdInt int packageUserId,
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700106 @NonNull String packageName, @NonNull List<String> ids) {
107 final ShortcutPackage packageShortcuts =
Makoto Onukic51b2872016-05-04 15:24:50 -0700108 mShortcutUser.getPackageShortcutsIfExists(packageName);
109 if (packageShortcuts == null) {
110 return; // No need to instantiate.
111 }
Makoto Onukid99c6f02016-03-28 11:02:54 -0700112
Makoto Onuki2e210c42016-03-30 08:30:36 -0700113 final PackageWithUser pu = PackageWithUser.of(packageUserId, packageName);
114
Makoto Onuki31459242016-03-22 11:12:18 -0700115 final int idSize = ids.size();
116 if (idSize == 0) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700117 mPinnedShortcuts.remove(pu);
Makoto Onuki31459242016-03-22 11:12:18 -0700118 } else {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700119 final ArraySet<String> prevSet = mPinnedShortcuts.get(pu);
Makoto Onuki31459242016-03-22 11:12:18 -0700120
121 // Pin shortcuts. Make sure only pin the ones that were visible to the caller.
122 // i.e. a non-dynamic, pinned shortcut by *other launchers* shouldn't be pinned here.
123
Makoto Onuki31459242016-03-22 11:12:18 -0700124 final ArraySet<String> newSet = new ArraySet<>();
125
126 for (int i = 0; i < idSize; i++) {
127 final String id = ids.get(i);
128 final ShortcutInfo si = packageShortcuts.findShortcutById(id);
129 if (si == null) {
130 continue;
131 }
Makoto Onuki22fcc682016-05-17 14:52:19 -0700132 if (si.isDynamic() || si.isManifestShortcut()
133 || (prevSet != null && prevSet.contains(id))) {
Makoto Onuki31459242016-03-22 11:12:18 -0700134 newSet.add(id);
135 }
136 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700137 mPinnedShortcuts.put(pu, newSet);
Makoto Onuki31459242016-03-22 11:12:18 -0700138 }
Makoto Onukic51b2872016-05-04 15:24:50 -0700139 packageShortcuts.refreshPinnedFlags();
Makoto Onuki31459242016-03-22 11:12:18 -0700140 }
141
142 /**
143 * Return the pinned shortcut IDs for the publisher package.
144 */
Makoto Onuki2e210c42016-03-30 08:30:36 -0700145 public ArraySet<String> getPinnedShortcutIds(@NonNull String packageName,
146 @UserIdInt int packageUserId) {
147 return mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName));
Makoto Onuki31459242016-03-22 11:12:18 -0700148 }
149
Makoto Onuki2e210c42016-03-30 08:30:36 -0700150 boolean cleanUpPackage(String packageName, @UserIdInt int packageUserId) {
151 return mPinnedShortcuts.remove(PackageWithUser.of(packageUserId, packageName)) != null;
Makoto Onuki31459242016-03-22 11:12:18 -0700152 }
153
154 /**
155 * Persist.
156 */
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700157 @Override
158 public void saveToXml(XmlSerializer out, boolean forBackup)
159 throws IOException {
Makoto Onuki31459242016-03-22 11:12:18 -0700160 final int size = mPinnedShortcuts.size();
161 if (size == 0) {
162 return; // Nothing to write.
163 }
164
165 out.startTag(null, TAG_ROOT);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700166 ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, getPackageName());
167 ShortcutService.writeAttr(out, ATTR_LAUNCHER_USER_ID, getPackageUserId());
168 getPackageInfo().saveToXml(out);
Makoto Onuki31459242016-03-22 11:12:18 -0700169
170 for (int i = 0; i < size; i++) {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700171 final PackageWithUser pu = mPinnedShortcuts.keyAt(i);
172
173 if (forBackup && (pu.userId != getOwnerUserId())) {
174 continue; // Target package on a different user, skip. (i.e. work profile)
175 }
176
Makoto Onuki31459242016-03-22 11:12:18 -0700177 out.startTag(null, TAG_PACKAGE);
Makoto Onuki2e210c42016-03-30 08:30:36 -0700178 ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME, pu.packageName);
179 ShortcutService.writeAttr(out, ATTR_PACKAGE_USER_ID, pu.userId);
Makoto Onuki31459242016-03-22 11:12:18 -0700180
181 final ArraySet<String> ids = mPinnedShortcuts.valueAt(i);
182 final int idSize = ids.size();
183 for (int j = 0; j < idSize; j++) {
184 ShortcutService.writeTagValue(out, TAG_PIN, ids.valueAt(j));
185 }
186 out.endTag(null, TAG_PACKAGE);
187 }
188
189 out.endTag(null, TAG_ROOT);
190 }
191
192 /**
193 * Load.
194 */
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700195 public static ShortcutLauncher loadFromXml(XmlPullParser parser, ShortcutUser shortcutUser,
196 int ownerUserId, boolean fromBackup) throws IOException, XmlPullParserException {
Makoto Onuki31459242016-03-22 11:12:18 -0700197 final String launcherPackageName = ShortcutService.parseStringAttribute(parser,
198 ATTR_PACKAGE_NAME);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700199
200 // If restoring, just use the real user ID.
201 final int launcherUserId =
202 fromBackup ? ownerUserId
203 : ShortcutService.parseIntAttribute(parser, ATTR_LAUNCHER_USER_ID, ownerUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700204
Makoto Onuki4d36b3a2016-04-27 12:00:17 -0700205 final ShortcutLauncher ret = new ShortcutLauncher(shortcutUser, launcherUserId,
206 launcherPackageName, launcherUserId);
Makoto Onuki31459242016-03-22 11:12:18 -0700207
208 ArraySet<String> ids = null;
209 final int outerDepth = parser.getDepth();
210 int type;
211 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
212 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
213 if (type != XmlPullParser.START_TAG) {
214 continue;
215 }
216 final int depth = parser.getDepth();
217 final String tag = parser.getName();
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700218 if (depth == outerDepth + 1) {
219 switch (tag) {
220 case ShortcutPackageInfo.TAG_ROOT:
Makoto Onuki2e210c42016-03-30 08:30:36 -0700221 ret.getPackageInfo().loadFromXml(parser, fromBackup);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700222 continue;
223 case TAG_PACKAGE: {
224 final String packageName = ShortcutService.parseStringAttribute(parser,
225 ATTR_PACKAGE_NAME);
Makoto Onuki2e210c42016-03-30 08:30:36 -0700226 final int packageUserId = fromBackup ? ownerUserId
227 : ShortcutService.parseIntAttribute(parser,
228 ATTR_PACKAGE_USER_ID, ownerUserId);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700229 ids = new ArraySet<>();
Makoto Onuki2e210c42016-03-30 08:30:36 -0700230 ret.mPinnedShortcuts.put(
231 PackageWithUser.of(packageUserId, packageName), ids);
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700232 continue;
233 }
Makoto Onuki31459242016-03-22 11:12:18 -0700234 }
235 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700236 if (depth == outerDepth + 2) {
237 switch (tag) {
238 case TAG_PIN: {
Makoto Onuki2e210c42016-03-30 08:30:36 -0700239 if (ids == null) {
240 Slog.w(TAG, TAG_PIN + " in invalid place");
241 } else {
242 ids.add(ShortcutService.parseStringAttribute(parser, ATTR_VALUE));
243 }
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700244 continue;
245 }
246 }
247 }
248 ShortcutService.warnForInvalidTag(depth, tag);
249 }
Makoto Onuki31459242016-03-22 11:12:18 -0700250 return ret;
251 }
252
Makoto Onukic51b2872016-05-04 15:24:50 -0700253 public void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
Makoto Onuki31459242016-03-22 11:12:18 -0700254 pw.println();
255
256 pw.print(prefix);
257 pw.print("Launcher: ");
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700258 pw.print(getPackageName());
259 pw.print(" Package user: ");
260 pw.print(getPackageUserId());
Makoto Onuki2e210c42016-03-30 08:30:36 -0700261 pw.print(" Owner user: ");
262 pw.print(getOwnerUserId());
Makoto Onuki9da23fc2016-03-29 11:14:42 -0700263 pw.println();
264
Makoto Onukic51b2872016-05-04 15:24:50 -0700265 getPackageInfo().dump(pw, prefix + " ");
Makoto Onuki31459242016-03-22 11:12:18 -0700266 pw.println();
267
268 final int size = mPinnedShortcuts.size();
269 for (int i = 0; i < size; i++) {
270 pw.println();
271
Makoto Onuki2e210c42016-03-30 08:30:36 -0700272 final PackageWithUser pu = mPinnedShortcuts.keyAt(i);
273
Makoto Onuki31459242016-03-22 11:12:18 -0700274 pw.print(prefix);
275 pw.print(" ");
276 pw.print("Package: ");
Makoto Onuki2e210c42016-03-30 08:30:36 -0700277 pw.print(pu.packageName);
278 pw.print(" User: ");
279 pw.println(pu.userId);
Makoto Onuki31459242016-03-22 11:12:18 -0700280
281 final ArraySet<String> ids = mPinnedShortcuts.valueAt(i);
282 final int idSize = ids.size();
283
284 for (int j = 0; j < idSize; j++) {
285 pw.print(prefix);
Makoto Onuki0acbb142016-03-22 17:02:57 -0700286 pw.print(" Pinned: ");
Makoto Onuki31459242016-03-22 11:12:18 -0700287 pw.print(ids.valueAt(j));
288 pw.println();
289 }
290 }
291 }
Makoto Onuki2e210c42016-03-30 08:30:36 -0700292
Makoto Onuki76269922016-07-15 14:58:54 -0700293 @Override
294 public JSONObject dumpCheckin(boolean clear) throws JSONException {
295 final JSONObject result = super.dumpCheckin(clear);
296
297 // Nothing really interesting to dump.
298
299 return result;
300 }
301
Makoto Onuki2e210c42016-03-30 08:30:36 -0700302 @VisibleForTesting
303 ArraySet<String> getAllPinnedShortcutsForTest(String packageName, int packageUserId) {
304 return new ArraySet<>(mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName)));
305 }
Makoto Onuki31459242016-03-22 11:12:18 -0700306}