blob: f1920c7e8e227b87c9e5da0ffb1585de8bc95856 [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;
23
24import org.xmlpull.v1.XmlPullParser;
25import org.xmlpull.v1.XmlPullParserException;
26import org.xmlpull.v1.XmlSerializer;
27
28import java.io.IOException;
29import java.io.PrintWriter;
30import java.util.List;
31
32/**
33 * Launcher information used by {@link ShortcutService}.
34 */
35class ShortcutLauncher {
36 private static final String TAG = ShortcutService.TAG;
37
38 static final String TAG_ROOT = "launcher-pins";
39
40 private static final String TAG_PACKAGE = "package";
41 private static final String TAG_PIN = "pin";
42
43 private static final String ATTR_VALUE = "value";
44 private static final String ATTR_PACKAGE_NAME = "package-name";
45
46 @UserIdInt
47 final int mUserId;
48
49 @NonNull
50 final String mPackageName;
51
52 /**
53 * Package name -> IDs.
54 */
55 final private ArrayMap<String, ArraySet<String>> mPinnedShortcuts = new ArrayMap<>();
56
57 ShortcutLauncher(@UserIdInt int userId, @NonNull String packageName) {
58 mUserId = userId;
59 mPackageName = packageName;
60 }
61
62 public void pinShortcuts(@NonNull ShortcutService s, @NonNull String packageName,
63 @NonNull List<String> ids) {
64 final int idSize = ids.size();
65 if (idSize == 0) {
66 mPinnedShortcuts.remove(packageName);
67 } else {
68 final ArraySet<String> prevSet = mPinnedShortcuts.get(packageName);
69
70 // Pin shortcuts. Make sure only pin the ones that were visible to the caller.
71 // i.e. a non-dynamic, pinned shortcut by *other launchers* shouldn't be pinned here.
72
73 final ShortcutPackage packageShortcuts =
74 s.getPackageShortcutsLocked(packageName, mUserId);
75 final ArraySet<String> newSet = new ArraySet<>();
76
77 for (int i = 0; i < idSize; i++) {
78 final String id = ids.get(i);
79 final ShortcutInfo si = packageShortcuts.findShortcutById(id);
80 if (si == null) {
81 continue;
82 }
83 if (si.isDynamic() || (prevSet != null && prevSet.contains(id))) {
84 newSet.add(id);
85 }
86 }
87 mPinnedShortcuts.put(packageName, newSet);
88 }
89 s.getPackageShortcutsLocked(packageName, mUserId).refreshPinnedFlags(s);
90 }
91
92 /**
93 * Return the pinned shortcut IDs for the publisher package.
94 */
95 public ArraySet<String> getPinnedShortcutIds(@NonNull String packageName) {
96 return mPinnedShortcuts.get(packageName);
97 }
98
99 boolean cleanUpPackage(String packageName) {
100 return mPinnedShortcuts.remove(packageName) != null;
101 }
102
103 /**
104 * Persist.
105 */
106 public void saveToXml(XmlSerializer out) throws IOException {
107 final int size = mPinnedShortcuts.size();
108 if (size == 0) {
109 return; // Nothing to write.
110 }
111
112 out.startTag(null, TAG_ROOT);
113 ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME,
114 mPackageName);
115
116 for (int i = 0; i < size; i++) {
117 out.startTag(null, TAG_PACKAGE);
118 ShortcutService.writeAttr(out, ATTR_PACKAGE_NAME,
119 mPinnedShortcuts.keyAt(i));
120
121 final ArraySet<String> ids = mPinnedShortcuts.valueAt(i);
122 final int idSize = ids.size();
123 for (int j = 0; j < idSize; j++) {
124 ShortcutService.writeTagValue(out, TAG_PIN, ids.valueAt(j));
125 }
126 out.endTag(null, TAG_PACKAGE);
127 }
128
129 out.endTag(null, TAG_ROOT);
130 }
131
132 /**
133 * Load.
134 */
135 public static ShortcutLauncher loadFromXml(XmlPullParser parser, int userId)
136 throws IOException, XmlPullParserException {
137 final String launcherPackageName = ShortcutService.parseStringAttribute(parser,
138 ATTR_PACKAGE_NAME);
139
140 final ShortcutLauncher ret = new ShortcutLauncher(userId, launcherPackageName);
141
142 ArraySet<String> ids = null;
143 final int outerDepth = parser.getDepth();
144 int type;
145 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
146 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
147 if (type != XmlPullParser.START_TAG) {
148 continue;
149 }
150 final int depth = parser.getDepth();
151 final String tag = parser.getName();
152 switch (tag) {
153 case TAG_PACKAGE: {
154 final String packageName = ShortcutService.parseStringAttribute(parser,
155 ATTR_PACKAGE_NAME);
156 ids = new ArraySet<>();
157 ret.mPinnedShortcuts.put(packageName, ids);
158 continue;
159 }
160 case TAG_PIN: {
161 ids.add(ShortcutService.parseStringAttribute(parser,
162 ATTR_VALUE));
163 continue;
164 }
165 }
166 throw ShortcutService.throwForInvalidTag(depth, tag);
167 }
168 return ret;
169 }
170
171 public void dump(@NonNull ShortcutService s, @NonNull PrintWriter pw, @NonNull String prefix) {
172 pw.println();
173
174 pw.print(prefix);
175 pw.print("Launcher: ");
176 pw.print(mPackageName);
177 pw.println();
178
179 final int size = mPinnedShortcuts.size();
180 for (int i = 0; i < size; i++) {
181 pw.println();
182
183 pw.print(prefix);
184 pw.print(" ");
185 pw.print("Package: ");
186 pw.println(mPinnedShortcuts.keyAt(i));
187
188 final ArraySet<String> ids = mPinnedShortcuts.valueAt(i);
189 final int idSize = ids.size();
190
191 for (int j = 0; j < idSize; j++) {
192 pw.print(prefix);
193 pw.print(" ");
194 pw.print(ids.valueAt(j));
195 pw.println();
196 }
197 }
198 }
199}