blob: 64666b00410794db41ebac87d239801437730853 [file] [log] [blame]
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001/*
2 * Copyright (C) 2014 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 */
16
17package com.android.launcher3;
18
19import android.appwidget.AppWidgetHost;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070020import android.content.ComponentName;
21import android.content.ContentValues;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.ActivityInfo;
25import android.content.pm.PackageManager;
26import android.content.res.Resources;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070027import android.database.sqlite.SQLiteDatabase;
28import android.graphics.drawable.Drawable;
29import android.net.Uri;
30import android.os.Bundle;
31import android.text.TextUtils;
Rajeev Kumar26453a22017-06-09 16:02:25 -070032import android.util.ArrayMap;
Sunny Goyal0d742312019-03-04 20:22:26 -080033import android.util.AttributeSet;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070034import android.util.Log;
35import android.util.Pair;
36import android.util.Patterns;
Sunny Goyal0d742312019-03-04 20:22:26 -080037import android.util.Xml;
Sunny Goyal36b54222018-07-10 13:50:50 -070038
Samuel Fufaca37b8a2019-08-19 17:04:36 -070039import androidx.annotation.Nullable;
40
Sunny Goyal0fe505b2014-08-06 09:55:36 -070041import com.android.launcher3.LauncherProvider.SqlArguments;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070042import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale62d2bb2018-11-06 10:28:37 -080043import com.android.launcher3.icons.GraphicsUtils;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070044import com.android.launcher3.icons.LauncherIcons;
Sunny Goyale396abf2020-04-06 15:11:17 -070045import com.android.launcher3.model.data.LauncherAppWidgetInfo;
46import com.android.launcher3.model.data.WorkspaceItemInfo;
Samuel Fufaca37b8a2019-08-19 17:04:36 -070047import com.android.launcher3.qsb.QsbContainerView;
Sunny Goyalefb7e842018-10-04 15:11:00 -070048import com.android.launcher3.util.IntArray;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070049import com.android.launcher3.util.PackageManagerHelper;
Adam Cohen091440a2015-03-18 14:16:05 -070050import com.android.launcher3.util.Thunk;
Sunny Goyal36b54222018-07-10 13:50:50 -070051
52import org.xmlpull.v1.XmlPullParser;
53import org.xmlpull.v1.XmlPullParserException;
54
Sunny Goyal0fe505b2014-08-06 09:55:36 -070055import java.io.IOException;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070056import java.util.Locale;
Sunny Goyalc0f03d92019-03-22 14:13:36 -070057import java.util.function.Supplier;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070058
59/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070060 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070061 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070062public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070063 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070064 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070065
66 /** Marker action used to discover a package which defines launcher customization */
67 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070068 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070069
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070070 /**
71 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
72 */
73 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
74 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070075 private static final String LAYOUT_RES = "default_layout";
76
77 static AutoInstallsLayout get(Context context, AppWidgetHost appWidgetHost,
78 LayoutParserCallback callback) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070079 Pair<String, Resources> customizationApkInfo = PackageManagerHelper.findSystemApk(
Sunny Goyal0fe505b2014-08-06 09:55:36 -070080 ACTION_LAUNCHER_CUSTOMIZATION, context.getPackageManager());
81 if (customizationApkInfo == null) {
82 return null;
83 }
Sunny Goyalc0f03d92019-03-22 14:13:36 -070084 String pkg = customizationApkInfo.first;
85 Resources targetRes = customizationApkInfo.second;
Sunny Goyal87f784c2017-01-11 10:48:34 -080086 InvariantDeviceProfile grid = LauncherAppState.getIDP(context);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070087
88 // Try with grid size and hotseat count
89 String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
Tony Wickhamb87f3cd2021-04-07 15:02:37 -070090 grid.numColumns, grid.numRows, grid.numDatabaseHotseatIcons);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070091 int layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
92
93 // Try with only grid size
94 if (layoutId == 0) {
95 Log.d(TAG, "Formatted layout: " + layoutName
96 + " not found. Trying layout without hosteat");
97 layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
Samuel Fufaca37b8a2019-08-19 17:04:36 -070098 grid.numColumns, grid.numRows);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070099 layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
100 }
101
102 // Try the default layout
103 if (layoutId == 0) {
104 Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
105 layoutId = targetRes.getIdentifier(LAYOUT_RES, "xml", pkg);
106 }
107
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700108 if (layoutId == 0) {
109 Log.e(TAG, "Layout definition not found in package: " + pkg);
110 return null;
111 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700112 return new AutoInstallsLayout(context, appWidgetHost, callback, targetRes, layoutId,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700113 TAG_WORKSPACE);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700114 }
115
116 // Object Tags
Sunny Goyalb564efb2015-01-23 13:45:20 -0800117 private static final String TAG_INCLUDE = "include";
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700118 public static final String TAG_WORKSPACE = "workspace";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700119 private static final String TAG_APP_ICON = "appicon";
120 private static final String TAG_AUTO_INSTALL = "autoinstall";
121 private static final String TAG_FOLDER = "folder";
122 private static final String TAG_APPWIDGET = "appwidget";
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700123 protected static final String TAG_SEARCH_WIDGET = "searchwidget";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700124 private static final String TAG_SHORTCUT = "shortcut";
125 private static final String TAG_EXTRA = "extra";
126
127 private static final String ATTR_CONTAINER = "container";
128 private static final String ATTR_RANK = "rank";
129
130 private static final String ATTR_PACKAGE_NAME = "packageName";
131 private static final String ATTR_CLASS_NAME = "className";
132 private static final String ATTR_TITLE = "title";
Sunny Goyal55e2b162020-06-09 15:44:48 -0700133 private static final String ATTR_TITLE_TEXT = "titleText";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700134 private static final String ATTR_SCREEN = "screen";
Sunny Goyal96a09632015-12-16 11:32:54 -0800135
136 // x and y can be specified as negative integers, in which case -1 represents the
137 // last row / column, -2 represents the second last, and so on.
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700138 private static final String ATTR_X = "x";
139 private static final String ATTR_Y = "y";
Sunny Goyal96a09632015-12-16 11:32:54 -0800140
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700141 private static final String ATTR_SPAN_X = "spanX";
142 private static final String ATTR_SPAN_Y = "spanY";
143 private static final String ATTR_ICON = "icon";
144 private static final String ATTR_URL = "url";
145
Sunny Goyalb564efb2015-01-23 13:45:20 -0800146 // Attrs for "Include"
147 private static final String ATTR_WORKSPACE = "workspace";
148
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700149 // Style attrs -- "Extra"
150 private static final String ATTR_KEY = "key";
151 private static final String ATTR_VALUE = "value";
152
153 private static final String HOTSEAT_CONTAINER_NAME =
154 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
155
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700156 @Thunk
157 final Context mContext;
158 @Thunk
159 final AppWidgetHost mAppWidgetHost;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800160 protected final LayoutParserCallback mCallback;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700161
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700162 protected final PackageManager mPackageManager;
163 protected final Resources mSourceRes;
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700164 protected final Supplier<XmlPullParser> mInitialLayoutSupplier;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700165
Sunny Goyalbb011da2016-06-15 15:42:29 -0700166 private final InvariantDeviceProfile mIdp;
Sunny Goyal96a09632015-12-16 11:32:54 -0800167 private final int mRowCount;
168 private final int mColumnCount;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700169
Sunny Goyalefb7e842018-10-04 15:11:00 -0700170 private final int[] mTemp = new int[2];
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700171 @Thunk
172 final ContentValues mValues;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800173 protected final String mRootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700174
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700175 protected SQLiteDatabase mDb;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700176
177 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700178 LayoutParserCallback callback, Resources res,
179 int layoutId, String rootTag) {
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700180 this(context, appWidgetHost, callback, res, () -> res.getXml(layoutId), rootTag);
181 }
182
183 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
184 LayoutParserCallback callback, Resources res,
185 Supplier<XmlPullParser> initialLayoutSupplier, String rootTag) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700186 mContext = context;
187 mAppWidgetHost = appWidgetHost;
188 mCallback = callback;
189
190 mPackageManager = context.getPackageManager();
191 mValues = new ContentValues();
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700192 mRootTag = rootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700193
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700194 mSourceRes = res;
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700195 mInitialLayoutSupplier = initialLayoutSupplier;
Sunny Goyal96a09632015-12-16 11:32:54 -0800196
Sunny Goyal87f784c2017-01-11 10:48:34 -0800197 mIdp = LauncherAppState.getIDP(context);
Sunny Goyalbb011da2016-06-15 15:42:29 -0700198 mRowCount = mIdp.numRows;
199 mColumnCount = mIdp.numColumns;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700200 }
201
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700202 /**
203 * Loads the layout in the db and returns the number of entries added on the desktop.
204 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700205 public int loadLayout(SQLiteDatabase db, IntArray screenIds) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700206 mDb = db;
207 try {
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700208 return parseLayout(mInitialLayoutSupplier.get(), screenIds);
Sameer Padala8fd74832014-09-08 16:00:29 -0700209 } catch (Exception e) {
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700210 Log.e(TAG, "Error parsing layout: ", e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700211 return -1;
212 }
213 }
214
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700215 /**
216 * Parses the layout and returns the number of elements added on the homescreen.
217 */
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700218 protected int parseLayout(XmlPullParser parser, IntArray screenIds)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700219 throws XmlPullParserException, IOException {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700220 beginDocument(parser, mRootTag);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700221 final int depth = parser.getDepth();
222 int type;
Rajeev Kumar26453a22017-06-09 16:02:25 -0700223 ArrayMap<String, TagParser> tagParserMap = getLayoutElementsMap();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700224 int count = 0;
225
226 while (((type = parser.next()) != XmlPullParser.END_TAG ||
227 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
228 if (type != XmlPullParser.START_TAG) {
229 continue;
230 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700231 count += parseAndAddNode(parser, tagParserMap, screenIds);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700232 }
233 return count;
234 }
235
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700236 /**
237 * Parses container and screenId attribute from the current tag, and puts it in the out.
238 * @param out array of size 2.
239 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800240 protected void parseContainerAndScreen(XmlPullParser parser, int[] out) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700241 if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) {
242 out[0] = Favorites.CONTAINER_HOTSEAT;
243 // Hack: hotseat items are stored using screen ids
Sunny Goyalefb7e842018-10-04 15:11:00 -0700244 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_RANK));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700245 } else {
246 out[0] = Favorites.CONTAINER_DESKTOP;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700247 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_SCREEN));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700248 }
249 }
250
251 /**
252 * Parses the current node and returns the number of elements added.
253 */
254 protected int parseAndAddNode(
Sunny Goyal0d742312019-03-04 20:22:26 -0800255 XmlPullParser parser, ArrayMap<String, TagParser> tagParserMap, IntArray screenIds)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700256 throws XmlPullParserException, IOException {
Sunny Goyalb564efb2015-01-23 13:45:20 -0800257
258 if (TAG_INCLUDE.equals(parser.getName())) {
259 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
260 if (resId != 0) {
261 // recursively load some more favorites, why not?
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700262 return parseLayout(mSourceRes.getXml(resId), screenIds);
Sunny Goyalb564efb2015-01-23 13:45:20 -0800263 } else {
264 return 0;
265 }
266 }
267
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700268 mValues.clear();
269 parseContainerAndScreen(parser, mTemp);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700270 final int container = mTemp[0];
271 final int screenId = mTemp[1];
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700272
273 mValues.put(Favorites.CONTAINER, container);
274 mValues.put(Favorites.SCREEN, screenId);
Sunny Goyal96a09632015-12-16 11:32:54 -0800275
276 mValues.put(Favorites.CELLX,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800277 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount));
Sunny Goyal96a09632015-12-16 11:32:54 -0800278 mValues.put(Favorites.CELLY,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800279 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700280
281 TagParser tagParser = tagParserMap.get(parser.getName());
282 if (tagParser == null) {
283 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
284 return 0;
285 }
Sunny Goyalefb7e842018-10-04 15:11:00 -0700286 int newElementId = tagParser.parseAndAdd(parser);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700287 if (newElementId >= 0) {
288 // Keep track of the set of screens which need to be added to the db.
289 if (!screenIds.contains(screenId) &&
290 container == Favorites.CONTAINER_DESKTOP) {
291 screenIds.add(screenId);
292 }
293 return 1;
294 }
295 return 0;
296 }
297
Sunny Goyalefb7e842018-10-04 15:11:00 -0700298 protected int addShortcut(String title, Intent intent, int type) {
299 int id = mCallback.generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700300 mValues.put(Favorites.INTENT, intent.toUri(0));
301 mValues.put(Favorites.TITLE, title);
302 mValues.put(Favorites.ITEM_TYPE, type);
303 mValues.put(Favorites.SPANX, 1);
304 mValues.put(Favorites.SPANY, 1);
305 mValues.put(Favorites._ID, id);
306 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
307 return -1;
308 } else {
309 return id;
310 }
311 }
312
Rajeev Kumar26453a22017-06-09 16:02:25 -0700313 protected ArrayMap<String, TagParser> getFolderElementsMap() {
314 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700315 parsers.put(TAG_APP_ICON, new AppShortcutParser());
316 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700317 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700318 return parsers;
319 }
320
Rajeev Kumar26453a22017-06-09 16:02:25 -0700321 protected ArrayMap<String, TagParser> getLayoutElementsMap() {
322 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700323 parsers.put(TAG_APP_ICON, new AppShortcutParser());
324 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
325 parsers.put(TAG_FOLDER, new FolderParser());
Sunny Goyal86df1382016-08-10 15:03:22 -0700326 parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700327 parsers.put(TAG_SEARCH_WIDGET, new SearchWidgetParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700328 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700329 return parsers;
330 }
331
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700332 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700333 /**
334 * Parses the tag and adds to the db
335 * @return the id of the row added or -1;
336 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800337 int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700338 throws XmlPullParserException, IOException;
339 }
340
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700341 /**
342 * App shortcuts: required attributes packageName and className
343 */
344 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700345
346 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800347 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700348 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
349 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
350
351 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
352 ActivityInfo info;
353 try {
354 ComponentName cn;
355 try {
356 cn = new ComponentName(packageName, className);
357 info = mPackageManager.getActivityInfo(cn, 0);
358 } catch (PackageManager.NameNotFoundException nnfe) {
359 String[] packages = mPackageManager.currentToCanonicalPackageNames(
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700360 new String[]{packageName});
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700361 cn = new ComponentName(packages[0], className);
362 info = mPackageManager.getActivityInfo(cn, 0);
363 }
364 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700365 .addCategory(Intent.CATEGORY_LAUNCHER)
366 .setComponent(cn)
367 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
368 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700369
370 return addShortcut(info.loadLabel(mPackageManager).toString(),
371 intent, Favorites.ITEM_TYPE_APPLICATION);
372 } catch (PackageManager.NameNotFoundException e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700373 Log.e(TAG, "Favorite not found: " + packageName + "/" + className);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700374 }
375 return -1;
376 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700377 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700378 }
379 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700380
381 /**
382 * Helper method to allow extending the parser capabilities
383 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800384 protected int invalidPackageOrClass(XmlPullParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700385 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700386 return -1;
387 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700388 }
389
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700390 /**
391 * AutoInstall: required attributes packageName and className
392 */
393 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700394
395 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800396 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700397 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
398 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
399 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
400 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
401 return -1;
402 }
403
Sunny Goyal95899162019-03-27 16:03:06 -0700404 mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_AUTOINSTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700405 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700406 .addCategory(Intent.CATEGORY_LAUNCHER)
407 .setComponent(new ComponentName(packageName, className))
408 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
409 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700410 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
411 Favorites.ITEM_TYPE_APPLICATION);
412 }
413 }
414
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700415 /**
416 * Parses a web shortcut. Required attributes url, icon, title
417 */
418 protected class ShortcutParser implements TagParser {
419
420 private final Resources mIconRes;
421
422 public ShortcutParser(Resources iconRes) {
423 mIconRes = iconRes;
424 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700425
426 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800427 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700428 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
429 final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0);
430
431 if (titleResId == 0 || iconId == 0) {
432 if (LOGD) Log.d(TAG, "Ignoring shortcut");
433 return -1;
434 }
435
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700436 final Intent intent = parseIntent(parser);
437 if (intent == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700438 return -1;
439 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700440
441 Drawable icon = mIconRes.getDrawable(iconId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700442 if (icon == null) {
443 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon");
444 return -1;
445 }
446
Sunny Goyalad7ff442017-09-12 12:42:26 -0700447 // Auto installs should always support the current platform version.
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800448 LauncherIcons li = LauncherIcons.obtain(mContext);
Sunny Goyale62d2bb2018-11-06 10:28:37 -0800449 mValues.put(LauncherSettings.Favorites.ICON, GraphicsUtils.flattenBitmap(
Sunny Goyald872a972021-11-24 18:07:04 -0800450 li.createBadgedIconBitmap(icon).icon));
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800451 li.recycle();
452
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700453 mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
454 mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));
455
456 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700457 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700458 return addShortcut(mSourceRes.getString(titleResId),
459 intent, Favorites.ITEM_TYPE_SHORTCUT);
460 }
461
Sunny Goyal0d742312019-03-04 20:22:26 -0800462 protected Intent parseIntent(XmlPullParser parser) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700463 final String url = getAttributeValue(parser, ATTR_URL);
464 if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) {
465 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url);
466 return null;
467 }
468 return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700469 }
470 }
471
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700472 /**
473 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
474 * Options child nodes: <extra key=... value=... />
Sunny Goyal86df1382016-08-10 15:03:22 -0700475 * It adds a pending widget which allows the widget to come later. If there are extras, those
476 * are passed to widget options during bind.
477 * The config activity for the widget (if present) is not shown, so any optional configurations
478 * should be passed as extras and the widget should support reading these widget options.
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700479 */
Sunny Goyal86df1382016-08-10 15:03:22 -0700480 protected class PendingWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700481
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700482 @Nullable
483 public ComponentName getComponentName(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700484 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
485 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
486 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700487 return null;
488 }
489 return new ComponentName(packageName, className);
490 }
491
492
493 @Override
494 public int parseAndAdd(XmlPullParser parser)
495 throws XmlPullParserException, IOException {
496 ComponentName cn = getComponentName(parser);
497 if (cn == null) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700498 if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component");
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700499 return -1;
500 }
501
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700502 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
503 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
Sunny Goyal86df1382016-08-10 15:03:22 -0700504 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700505
506 // Read the extras
507 Bundle extras = new Bundle();
508 int widgetDepth = parser.getDepth();
509 int type;
510 while ((type = parser.next()) != XmlPullParser.END_TAG ||
511 parser.getDepth() > widgetDepth) {
512 if (type != XmlPullParser.START_TAG) {
513 continue;
514 }
515
516 if (TAG_EXTRA.equals(parser.getName())) {
517 String key = getAttributeValue(parser, ATTR_KEY);
518 String value = getAttributeValue(parser, ATTR_VALUE);
519 if (key != null && value != null) {
520 extras.putString(key, value);
521 } else {
522 throw new RuntimeException("Widget extras must have a key and value");
523 }
524 } else {
525 throw new RuntimeException("Widgets can contain only extras");
526 }
527 }
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700528 return verifyAndInsert(cn, extras);
Sunny Goyal86df1382016-08-10 15:03:22 -0700529 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700530
Sunny Goyalefb7e842018-10-04 15:11:00 -0700531 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700532 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
533 mValues.put(Favorites.RESTORED,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700534 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID
535 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY
536 | LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
Sunny Goyal86df1382016-08-10 15:03:22 -0700537 mValues.put(Favorites._ID, mCallback.generateNewItemId());
538 if (!extras.isEmpty()) {
539 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700540 }
Sunny Goyal86df1382016-08-10 15:03:22 -0700541
Sunny Goyalefb7e842018-10-04 15:11:00 -0700542 int insertedId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal86df1382016-08-10 15:03:22 -0700543 if (insertedId < 0) {
544 return -1;
545 } else {
546 return insertedId;
547 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700548 }
549 }
550
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700551 protected class SearchWidgetParser extends PendingWidgetParser {
552 @Override
553 @Nullable
554 public ComponentName getComponentName(XmlPullParser parser) {
555 return QsbContainerView.getSearchComponentName(mContext);
556 }
557
558 @Override
559 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
560 mValues.put(Favorites.OPTIONS, LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET);
561 int flags = mValues.getAsInteger(Favorites.RESTORED)
562 | WorkspaceItemInfo.FLAG_RESTORE_STARTED;
563 mValues.put(Favorites.RESTORED, flags);
564 return super.verifyAndInsert(cn, extras);
565 }
566 }
567
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700568 protected class FolderParser implements TagParser {
Rajeev Kumar26453a22017-06-09 16:02:25 -0700569 private final ArrayMap<String, TagParser> mFolderElements;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700570
571 public FolderParser() {
572 this(getFolderElementsMap());
573 }
574
Rajeev Kumar26453a22017-06-09 16:02:25 -0700575 public FolderParser(ArrayMap<String, TagParser> elements) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700576 mFolderElements = elements;
577 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700578
579 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800580 public int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700581 throws XmlPullParserException, IOException {
582 final String title;
583 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
584 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700585 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700586 } else {
Sunny Goyal55e2b162020-06-09 15:44:48 -0700587 String titleText = getAttributeValue(parser, ATTR_TITLE_TEXT);
588 title = TextUtils.isEmpty(titleText) ? "" : titleText;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700589 }
590
591 mValues.put(Favorites.TITLE, title);
592 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
593 mValues.put(Favorites.SPANX, 1);
594 mValues.put(Favorites.SPANY, 1);
595 mValues.put(Favorites._ID, mCallback.generateNewItemId());
Sunny Goyalefb7e842018-10-04 15:11:00 -0700596 int folderId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700597 if (folderId < 0) {
598 if (LOGD) Log.e(TAG, "Unable to add folder");
599 return -1;
600 }
601
602 final ContentValues myValues = new ContentValues(mValues);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700603 IntArray folderItems = new IntArray();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700604
605 int type;
606 int folderDepth = parser.getDepth();
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700607 int rank = 0;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700608 while ((type = parser.next()) != XmlPullParser.END_TAG ||
609 parser.getDepth() > folderDepth) {
610 if (type != XmlPullParser.START_TAG) {
611 continue;
612 }
613 mValues.clear();
614 mValues.put(Favorites.CONTAINER, folderId);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700615 mValues.put(Favorites.RANK, rank);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700616
617 TagParser tagParser = mFolderElements.get(parser.getName());
618 if (tagParser != null) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700619 final int id = tagParser.parseAndAdd(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700620 if (id >= 0) {
621 folderItems.add(id);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700622 rank++;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700623 }
624 } else {
625 throw new RuntimeException("Invalid folder item " + parser.getName());
626 }
627 }
628
Sunny Goyalefb7e842018-10-04 15:11:00 -0700629 int addedId = folderId;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700630
631 // We can only have folders with >= 2 items, so we need to remove the
632 // folder and clean up if less than 2 items were included, or some
633 // failed to add, and less than 2 were actually added
634 if (folderItems.size() < 2) {
635 // Delete the folder
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700636 Uri uri = Favorites.getContentUri(folderId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700637 SqlArguments args = new SqlArguments(uri, null, null);
638 mDb.delete(args.table, args.where, args.args);
639 addedId = -1;
640
641 // If we have a single item, promote it to where the folder
642 // would have been.
643 if (folderItems.size() == 1) {
644 final ContentValues childValues = new ContentValues();
645 copyInteger(myValues, childValues, Favorites.CONTAINER);
646 copyInteger(myValues, childValues, Favorites.SCREEN);
647 copyInteger(myValues, childValues, Favorites.CELLX);
648 copyInteger(myValues, childValues, Favorites.CELLY);
649
650 addedId = folderItems.get(0);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700651 mDb.update(Favorites.TABLE_NAME, childValues,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700652 Favorites._ID + "=" + addedId, null);
653 }
654 }
655 return addedId;
656 }
657 }
658
Sunny Goyal762d0612020-07-29 15:03:46 -0700659 public static void beginDocument(XmlPullParser parser, String firstElementName)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700660 throws XmlPullParserException, IOException {
661 int type;
662 while ((type = parser.next()) != XmlPullParser.START_TAG
663 && type != XmlPullParser.END_DOCUMENT);
664
665 if (type != XmlPullParser.START_TAG) {
666 throw new XmlPullParserException("No start tag found");
667 }
668
669 if (!parser.getName().equals(firstElementName)) {
670 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
671 ", expected " + firstElementName);
672 }
673 }
674
Sunny Goyal96a09632015-12-16 11:32:54 -0800675 private static String convertToDistanceFromEnd(String value, int endValue) {
676 if (!TextUtils.isEmpty(value)) {
677 int x = Integer.parseInt(value);
678 if (x < 0) {
679 return Integer.toString(endValue + x);
680 }
681 }
682 return value;
683 }
684
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700685 /**
686 * Return attribute value, attempting launcher-specific namespace first
687 * before falling back to anonymous attribute.
688 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800689 protected static String getAttributeValue(XmlPullParser parser, String attribute) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700690 String value = parser.getAttributeValue(
691 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
692 if (value == null) {
693 value = parser.getAttributeValue(null, attribute);
694 }
695 return value;
696 }
697
698 /**
699 * Return attribute resource value, attempting launcher-specific namespace
700 * first before falling back to anonymous attribute.
701 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800702 protected static int getAttributeResourceValue(XmlPullParser parser, String attribute,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700703 int defaultValue) {
Sunny Goyal0d742312019-03-04 20:22:26 -0800704 AttributeSet attrs = Xml.asAttributeSet(parser);
705 int value = attrs.getAttributeResourceValue(
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700706 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
707 defaultValue);
708 if (value == defaultValue) {
Sunny Goyal0d742312019-03-04 20:22:26 -0800709 value = attrs.getAttributeResourceValue(null, attribute, defaultValue);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700710 }
711 return value;
712 }
713
Rajeev Kumar26453a22017-06-09 16:02:25 -0700714 public interface LayoutParserCallback {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700715 int generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700716
Sunny Goyalefb7e842018-10-04 15:11:00 -0700717 int insertAndCheck(SQLiteDatabase db, ContentValues values);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700718 }
719
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700720 @Thunk
721 static void copyInteger(ContentValues from, ContentValues to, String key) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700722 to.put(key, from.getAsInteger(key));
723 }
724}