blob: 0003510ab1144461467e90d001f3762f8cc1ff00 [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
Sunny Goyal0fe505b2014-08-06 09:55:36 -070019import android.content.ComponentName;
20import android.content.ContentValues;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ActivityInfo;
24import android.content.pm.PackageManager;
25import android.content.res.Resources;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070026import android.database.sqlite.SQLiteDatabase;
27import android.graphics.drawable.Drawable;
28import android.net.Uri;
29import android.os.Bundle;
30import android.text.TextUtils;
Rajeev Kumar26453a22017-06-09 16:02:25 -070031import android.util.ArrayMap;
Sunny Goyal0d742312019-03-04 20:22:26 -080032import android.util.AttributeSet;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070033import android.util.Log;
34import android.util.Pair;
35import android.util.Patterns;
Sunny Goyal0d742312019-03-04 20:22:26 -080036import android.util.Xml;
Sunny Goyal36b54222018-07-10 13:50:50 -070037
Samuel Fufaca37b8a2019-08-19 17:04:36 -070038import androidx.annotation.Nullable;
39
Sunny Goyal0fe505b2014-08-06 09:55:36 -070040import com.android.launcher3.LauncherProvider.SqlArguments;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070041import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale62d2bb2018-11-06 10:28:37 -080042import com.android.launcher3.icons.GraphicsUtils;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070043import com.android.launcher3.icons.LauncherIcons;
Sunny Goyale396abf2020-04-06 15:11:17 -070044import com.android.launcher3.model.data.LauncherAppWidgetInfo;
45import com.android.launcher3.model.data.WorkspaceItemInfo;
Samuel Fufaca37b8a2019-08-19 17:04:36 -070046import com.android.launcher3.qsb.QsbContainerView;
Sunny Goyalefb7e842018-10-04 15:11:00 -070047import com.android.launcher3.util.IntArray;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070048import com.android.launcher3.util.PackageManagerHelper;
Adam Cohen091440a2015-03-18 14:16:05 -070049import com.android.launcher3.util.Thunk;
Sunny Goyal36b54222018-07-10 13:50:50 -070050
51import org.xmlpull.v1.XmlPullParser;
52import org.xmlpull.v1.XmlPullParserException;
53
Sunny Goyal0fe505b2014-08-06 09:55:36 -070054import java.io.IOException;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070055import java.util.Locale;
Sunny Goyalc0f03d92019-03-22 14:13:36 -070056import java.util.function.Supplier;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070057
58/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070059 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070060 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070061public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070062 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070063 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070064
65 /** Marker action used to discover a package which defines launcher customization */
66 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070067 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070068
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070069 /**
70 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
71 */
72 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
73 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070074 private static final String LAYOUT_RES = "default_layout";
75
Sihua Maaa2b8722022-10-25 15:17:58 -070076 static AutoInstallsLayout get(Context context, LauncherWidgetHolder appWidgetHolder,
Sunny Goyal0fe505b2014-08-06 09:55:36 -070077 LayoutParserCallback callback) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070078 Pair<String, Resources> customizationApkInfo = PackageManagerHelper.findSystemApk(
Sunny Goyal0fe505b2014-08-06 09:55:36 -070079 ACTION_LAUNCHER_CUSTOMIZATION, context.getPackageManager());
80 if (customizationApkInfo == null) {
81 return null;
82 }
Sunny Goyalc0f03d92019-03-22 14:13:36 -070083 String pkg = customizationApkInfo.first;
84 Resources targetRes = customizationApkInfo.second;
Sunny Goyal87f784c2017-01-11 10:48:34 -080085 InvariantDeviceProfile grid = LauncherAppState.getIDP(context);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070086
87 // Try with grid size and hotseat count
88 String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
Tony Wickhamb87f3cd2021-04-07 15:02:37 -070089 grid.numColumns, grid.numRows, grid.numDatabaseHotseatIcons);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070090 int layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
91
92 // Try with only grid size
93 if (layoutId == 0) {
94 Log.d(TAG, "Formatted layout: " + layoutName
95 + " not found. Trying layout without hosteat");
96 layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
Samuel Fufaca37b8a2019-08-19 17:04:36 -070097 grid.numColumns, grid.numRows);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070098 layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
99 }
100
101 // Try the default layout
102 if (layoutId == 0) {
103 Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
104 layoutId = targetRes.getIdentifier(LAYOUT_RES, "xml", pkg);
105 }
106
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700107 if (layoutId == 0) {
108 Log.e(TAG, "Layout definition not found in package: " + pkg);
109 return null;
110 }
Sihua Maaa2b8722022-10-25 15:17:58 -0700111 return new AutoInstallsLayout(context, appWidgetHolder, callback, targetRes, layoutId,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700112 TAG_WORKSPACE);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700113 }
114
115 // Object Tags
Sunny Goyalb564efb2015-01-23 13:45:20 -0800116 private static final String TAG_INCLUDE = "include";
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700117 public static final String TAG_WORKSPACE = "workspace";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700118 private static final String TAG_APP_ICON = "appicon";
119 private static final String TAG_AUTO_INSTALL = "autoinstall";
120 private static final String TAG_FOLDER = "folder";
121 private static final String TAG_APPWIDGET = "appwidget";
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700122 protected static final String TAG_SEARCH_WIDGET = "searchwidget";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700123 private static final String TAG_SHORTCUT = "shortcut";
124 private static final String TAG_EXTRA = "extra";
125
126 private static final String ATTR_CONTAINER = "container";
127 private static final String ATTR_RANK = "rank";
128
129 private static final String ATTR_PACKAGE_NAME = "packageName";
130 private static final String ATTR_CLASS_NAME = "className";
131 private static final String ATTR_TITLE = "title";
Sunny Goyal55e2b162020-06-09 15:44:48 -0700132 private static final String ATTR_TITLE_TEXT = "titleText";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700133 private static final String ATTR_SCREEN = "screen";
Sunny Goyal96a09632015-12-16 11:32:54 -0800134
135 // x and y can be specified as negative integers, in which case -1 represents the
136 // last row / column, -2 represents the second last, and so on.
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700137 private static final String ATTR_X = "x";
138 private static final String ATTR_Y = "y";
Sunny Goyal96a09632015-12-16 11:32:54 -0800139
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700140 private static final String ATTR_SPAN_X = "spanX";
141 private static final String ATTR_SPAN_Y = "spanY";
142 private static final String ATTR_ICON = "icon";
143 private static final String ATTR_URL = "url";
144
Sunny Goyalb564efb2015-01-23 13:45:20 -0800145 // Attrs for "Include"
146 private static final String ATTR_WORKSPACE = "workspace";
147
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700148 // Style attrs -- "Extra"
149 private static final String ATTR_KEY = "key";
150 private static final String ATTR_VALUE = "value";
151
152 private static final String HOTSEAT_CONTAINER_NAME =
153 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
154
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700155 @Thunk
156 final Context mContext;
157 @Thunk
Sihua Maaa2b8722022-10-25 15:17:58 -0700158 final LauncherWidgetHolder mAppWidgetHolder;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800159 protected final LayoutParserCallback mCallback;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700160
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700161 protected final PackageManager mPackageManager;
162 protected final Resources mSourceRes;
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700163 protected final Supplier<XmlPullParser> mInitialLayoutSupplier;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700164
Sunny Goyalbb011da2016-06-15 15:42:29 -0700165 private final InvariantDeviceProfile mIdp;
Sunny Goyal96a09632015-12-16 11:32:54 -0800166 private final int mRowCount;
167 private final int mColumnCount;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700168
Sunny Goyalefb7e842018-10-04 15:11:00 -0700169 private final int[] mTemp = new int[2];
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700170 @Thunk
171 final ContentValues mValues;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800172 protected final String mRootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700173
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700174 protected SQLiteDatabase mDb;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700175
Sihua Maaa2b8722022-10-25 15:17:58 -0700176 public AutoInstallsLayout(Context context, LauncherWidgetHolder appWidgetHolder,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700177 LayoutParserCallback callback, Resources res,
178 int layoutId, String rootTag) {
Sihua Maaa2b8722022-10-25 15:17:58 -0700179 this(context, appWidgetHolder, callback, res, () -> res.getXml(layoutId), rootTag);
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700180 }
181
Sihua Maaa2b8722022-10-25 15:17:58 -0700182 public AutoInstallsLayout(Context context, LauncherWidgetHolder appWidgetHolder,
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700183 LayoutParserCallback callback, Resources res,
184 Supplier<XmlPullParser> initialLayoutSupplier, String rootTag) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700185 mContext = context;
Sihua Maaa2b8722022-10-25 15:17:58 -0700186 mAppWidgetHolder = appWidgetHolder;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700187 mCallback = callback;
188
189 mPackageManager = context.getPackageManager();
190 mValues = new ContentValues();
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700191 mRootTag = rootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700192
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700193 mSourceRes = res;
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700194 mInitialLayoutSupplier = initialLayoutSupplier;
Sunny Goyal96a09632015-12-16 11:32:54 -0800195
Sunny Goyal87f784c2017-01-11 10:48:34 -0800196 mIdp = LauncherAppState.getIDP(context);
Sunny Goyalbb011da2016-06-15 15:42:29 -0700197 mRowCount = mIdp.numRows;
198 mColumnCount = mIdp.numColumns;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700199 }
200
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700201 /**
202 * Loads the layout in the db and returns the number of entries added on the desktop.
203 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700204 public int loadLayout(SQLiteDatabase db, IntArray screenIds) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700205 mDb = db;
206 try {
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700207 return parseLayout(mInitialLayoutSupplier.get(), screenIds);
Sameer Padala8fd74832014-09-08 16:00:29 -0700208 } catch (Exception e) {
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700209 Log.e(TAG, "Error parsing layout: ", e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700210 return -1;
211 }
212 }
213
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700214 /**
215 * Parses the layout and returns the number of elements added on the homescreen.
216 */
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700217 protected int parseLayout(XmlPullParser parser, IntArray screenIds)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700218 throws XmlPullParserException, IOException {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700219 beginDocument(parser, mRootTag);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700220 final int depth = parser.getDepth();
221 int type;
Rajeev Kumar26453a22017-06-09 16:02:25 -0700222 ArrayMap<String, TagParser> tagParserMap = getLayoutElementsMap();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700223 int count = 0;
224
225 while (((type = parser.next()) != XmlPullParser.END_TAG ||
226 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
227 if (type != XmlPullParser.START_TAG) {
228 continue;
229 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700230 count += parseAndAddNode(parser, tagParserMap, screenIds);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700231 }
232 return count;
233 }
234
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700235 /**
236 * Parses container and screenId attribute from the current tag, and puts it in the out.
237 * @param out array of size 2.
238 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800239 protected void parseContainerAndScreen(XmlPullParser parser, int[] out) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700240 if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) {
241 out[0] = Favorites.CONTAINER_HOTSEAT;
242 // Hack: hotseat items are stored using screen ids
Sunny Goyalefb7e842018-10-04 15:11:00 -0700243 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_RANK));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700244 } else {
245 out[0] = Favorites.CONTAINER_DESKTOP;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700246 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_SCREEN));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700247 }
248 }
249
250 /**
251 * Parses the current node and returns the number of elements added.
252 */
253 protected int parseAndAddNode(
Sunny Goyal0d742312019-03-04 20:22:26 -0800254 XmlPullParser parser, ArrayMap<String, TagParser> tagParserMap, IntArray screenIds)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700255 throws XmlPullParserException, IOException {
Sunny Goyalb564efb2015-01-23 13:45:20 -0800256
257 if (TAG_INCLUDE.equals(parser.getName())) {
258 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
259 if (resId != 0) {
260 // recursively load some more favorites, why not?
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700261 return parseLayout(mSourceRes.getXml(resId), screenIds);
Sunny Goyalb564efb2015-01-23 13:45:20 -0800262 } else {
263 return 0;
264 }
265 }
266
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700267 mValues.clear();
268 parseContainerAndScreen(parser, mTemp);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700269 final int container = mTemp[0];
270 final int screenId = mTemp[1];
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700271
272 mValues.put(Favorites.CONTAINER, container);
273 mValues.put(Favorites.SCREEN, screenId);
Sunny Goyal96a09632015-12-16 11:32:54 -0800274
275 mValues.put(Favorites.CELLX,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800276 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount));
Sunny Goyal96a09632015-12-16 11:32:54 -0800277 mValues.put(Favorites.CELLY,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800278 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700279
280 TagParser tagParser = tagParserMap.get(parser.getName());
281 if (tagParser == null) {
282 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
283 return 0;
284 }
Sunny Goyalefb7e842018-10-04 15:11:00 -0700285 int newElementId = tagParser.parseAndAdd(parser);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700286 if (newElementId >= 0) {
287 // Keep track of the set of screens which need to be added to the db.
288 if (!screenIds.contains(screenId) &&
289 container == Favorites.CONTAINER_DESKTOP) {
290 screenIds.add(screenId);
291 }
292 return 1;
293 }
294 return 0;
295 }
296
Sunny Goyalefb7e842018-10-04 15:11:00 -0700297 protected int addShortcut(String title, Intent intent, int type) {
298 int id = mCallback.generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700299 mValues.put(Favorites.INTENT, intent.toUri(0));
300 mValues.put(Favorites.TITLE, title);
301 mValues.put(Favorites.ITEM_TYPE, type);
302 mValues.put(Favorites.SPANX, 1);
303 mValues.put(Favorites.SPANY, 1);
304 mValues.put(Favorites._ID, id);
305 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
306 return -1;
307 } else {
308 return id;
309 }
310 }
311
Rajeev Kumar26453a22017-06-09 16:02:25 -0700312 protected ArrayMap<String, TagParser> getFolderElementsMap() {
313 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700314 parsers.put(TAG_APP_ICON, new AppShortcutParser());
315 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700316 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700317 return parsers;
318 }
319
Rajeev Kumar26453a22017-06-09 16:02:25 -0700320 protected ArrayMap<String, TagParser> getLayoutElementsMap() {
321 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700322 parsers.put(TAG_APP_ICON, new AppShortcutParser());
323 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
324 parsers.put(TAG_FOLDER, new FolderParser());
Sunny Goyal86df1382016-08-10 15:03:22 -0700325 parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700326 parsers.put(TAG_SEARCH_WIDGET, new SearchWidgetParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700327 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700328 return parsers;
329 }
330
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700331 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700332 /**
333 * Parses the tag and adds to the db
334 * @return the id of the row added or -1;
335 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800336 int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700337 throws XmlPullParserException, IOException;
338 }
339
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700340 /**
341 * App shortcuts: required attributes packageName and className
342 */
343 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700344
345 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800346 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700347 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
348 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
349
350 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
351 ActivityInfo info;
352 try {
353 ComponentName cn;
354 try {
355 cn = new ComponentName(packageName, className);
356 info = mPackageManager.getActivityInfo(cn, 0);
357 } catch (PackageManager.NameNotFoundException nnfe) {
358 String[] packages = mPackageManager.currentToCanonicalPackageNames(
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700359 new String[]{packageName});
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700360 cn = new ComponentName(packages[0], className);
361 info = mPackageManager.getActivityInfo(cn, 0);
362 }
363 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700364 .addCategory(Intent.CATEGORY_LAUNCHER)
365 .setComponent(cn)
366 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
367 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700368
369 return addShortcut(info.loadLabel(mPackageManager).toString(),
370 intent, Favorites.ITEM_TYPE_APPLICATION);
371 } catch (PackageManager.NameNotFoundException e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700372 Log.e(TAG, "Favorite not found: " + packageName + "/" + className);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700373 }
374 return -1;
375 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700376 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700377 }
378 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700379
380 /**
381 * Helper method to allow extending the parser capabilities
382 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800383 protected int invalidPackageOrClass(XmlPullParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700384 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700385 return -1;
386 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700387 }
388
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700389 /**
390 * AutoInstall: required attributes packageName and className
391 */
392 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700393
394 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800395 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700396 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
397 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
398 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
399 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
400 return -1;
401 }
402
Sunny Goyal95899162019-03-27 16:03:06 -0700403 mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_AUTOINSTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700404 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700405 .addCategory(Intent.CATEGORY_LAUNCHER)
406 .setComponent(new ComponentName(packageName, className))
407 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
408 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700409 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
410 Favorites.ITEM_TYPE_APPLICATION);
411 }
412 }
413
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700414 /**
415 * Parses a web shortcut. Required attributes url, icon, title
416 */
417 protected class ShortcutParser implements TagParser {
418
419 private final Resources mIconRes;
420
421 public ShortcutParser(Resources iconRes) {
422 mIconRes = iconRes;
423 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700424
425 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800426 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700427 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
428 final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0);
429
430 if (titleResId == 0 || iconId == 0) {
431 if (LOGD) Log.d(TAG, "Ignoring shortcut");
432 return -1;
433 }
434
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700435 final Intent intent = parseIntent(parser);
436 if (intent == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700437 return -1;
438 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700439
440 Drawable icon = mIconRes.getDrawable(iconId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700441 if (icon == null) {
442 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon");
443 return -1;
444 }
445
Sunny Goyalad7ff442017-09-12 12:42:26 -0700446 // Auto installs should always support the current platform version.
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800447 LauncherIcons li = LauncherIcons.obtain(mContext);
Sunny Goyale62d2bb2018-11-06 10:28:37 -0800448 mValues.put(LauncherSettings.Favorites.ICON, GraphicsUtils.flattenBitmap(
Sunny Goyald872a972021-11-24 18:07:04 -0800449 li.createBadgedIconBitmap(icon).icon));
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800450 li.recycle();
451
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700452 mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
453 mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));
454
455 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700456 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700457 return addShortcut(mSourceRes.getString(titleResId),
458 intent, Favorites.ITEM_TYPE_SHORTCUT);
459 }
460
Sunny Goyal0d742312019-03-04 20:22:26 -0800461 protected Intent parseIntent(XmlPullParser parser) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700462 final String url = getAttributeValue(parser, ATTR_URL);
463 if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) {
464 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url);
465 return null;
466 }
467 return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700468 }
469 }
470
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700471 /**
472 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
473 * Options child nodes: <extra key=... value=... />
Sunny Goyal86df1382016-08-10 15:03:22 -0700474 * It adds a pending widget which allows the widget to come later. If there are extras, those
475 * are passed to widget options during bind.
476 * The config activity for the widget (if present) is not shown, so any optional configurations
477 * should be passed as extras and the widget should support reading these widget options.
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700478 */
Sunny Goyal86df1382016-08-10 15:03:22 -0700479 protected class PendingWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700480
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700481 @Nullable
482 public ComponentName getComponentName(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700483 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
484 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
485 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700486 return null;
487 }
488 return new ComponentName(packageName, className);
489 }
490
491
492 @Override
493 public int parseAndAdd(XmlPullParser parser)
494 throws XmlPullParserException, IOException {
495 ComponentName cn = getComponentName(parser);
496 if (cn == null) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700497 if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component");
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700498 return -1;
499 }
500
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700501 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
502 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
Sunny Goyal86df1382016-08-10 15:03:22 -0700503 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700504
505 // Read the extras
506 Bundle extras = new Bundle();
507 int widgetDepth = parser.getDepth();
508 int type;
509 while ((type = parser.next()) != XmlPullParser.END_TAG ||
510 parser.getDepth() > widgetDepth) {
511 if (type != XmlPullParser.START_TAG) {
512 continue;
513 }
514
515 if (TAG_EXTRA.equals(parser.getName())) {
516 String key = getAttributeValue(parser, ATTR_KEY);
517 String value = getAttributeValue(parser, ATTR_VALUE);
518 if (key != null && value != null) {
519 extras.putString(key, value);
520 } else {
521 throw new RuntimeException("Widget extras must have a key and value");
522 }
523 } else {
524 throw new RuntimeException("Widgets can contain only extras");
525 }
526 }
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700527 return verifyAndInsert(cn, extras);
Sunny Goyal86df1382016-08-10 15:03:22 -0700528 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700529
Sunny Goyalefb7e842018-10-04 15:11:00 -0700530 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700531 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
532 mValues.put(Favorites.RESTORED,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700533 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID
534 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY
535 | LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
Sunny Goyal86df1382016-08-10 15:03:22 -0700536 mValues.put(Favorites._ID, mCallback.generateNewItemId());
537 if (!extras.isEmpty()) {
538 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700539 }
Sunny Goyal86df1382016-08-10 15:03:22 -0700540
Sunny Goyalefb7e842018-10-04 15:11:00 -0700541 int insertedId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal86df1382016-08-10 15:03:22 -0700542 if (insertedId < 0) {
543 return -1;
544 } else {
545 return insertedId;
546 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700547 }
548 }
549
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700550 protected class SearchWidgetParser extends PendingWidgetParser {
551 @Override
552 @Nullable
553 public ComponentName getComponentName(XmlPullParser parser) {
554 return QsbContainerView.getSearchComponentName(mContext);
555 }
556
557 @Override
558 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
559 mValues.put(Favorites.OPTIONS, LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET);
560 int flags = mValues.getAsInteger(Favorites.RESTORED)
561 | WorkspaceItemInfo.FLAG_RESTORE_STARTED;
562 mValues.put(Favorites.RESTORED, flags);
563 return super.verifyAndInsert(cn, extras);
564 }
565 }
566
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700567 protected class FolderParser implements TagParser {
Rajeev Kumar26453a22017-06-09 16:02:25 -0700568 private final ArrayMap<String, TagParser> mFolderElements;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700569
570 public FolderParser() {
571 this(getFolderElementsMap());
572 }
573
Rajeev Kumar26453a22017-06-09 16:02:25 -0700574 public FolderParser(ArrayMap<String, TagParser> elements) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700575 mFolderElements = elements;
576 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700577
578 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800579 public int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700580 throws XmlPullParserException, IOException {
581 final String title;
582 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
583 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700584 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700585 } else {
Sunny Goyal55e2b162020-06-09 15:44:48 -0700586 String titleText = getAttributeValue(parser, ATTR_TITLE_TEXT);
587 title = TextUtils.isEmpty(titleText) ? "" : titleText;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700588 }
589
590 mValues.put(Favorites.TITLE, title);
591 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
592 mValues.put(Favorites.SPANX, 1);
593 mValues.put(Favorites.SPANY, 1);
594 mValues.put(Favorites._ID, mCallback.generateNewItemId());
Sunny Goyalefb7e842018-10-04 15:11:00 -0700595 int folderId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700596 if (folderId < 0) {
597 if (LOGD) Log.e(TAG, "Unable to add folder");
598 return -1;
599 }
600
601 final ContentValues myValues = new ContentValues(mValues);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700602 IntArray folderItems = new IntArray();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700603
604 int type;
605 int folderDepth = parser.getDepth();
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700606 int rank = 0;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700607 while ((type = parser.next()) != XmlPullParser.END_TAG ||
608 parser.getDepth() > folderDepth) {
609 if (type != XmlPullParser.START_TAG) {
610 continue;
611 }
612 mValues.clear();
613 mValues.put(Favorites.CONTAINER, folderId);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700614 mValues.put(Favorites.RANK, rank);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700615
616 TagParser tagParser = mFolderElements.get(parser.getName());
617 if (tagParser != null) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700618 final int id = tagParser.parseAndAdd(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700619 if (id >= 0) {
620 folderItems.add(id);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700621 rank++;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700622 }
623 } else {
624 throw new RuntimeException("Invalid folder item " + parser.getName());
625 }
626 }
627
Sunny Goyalefb7e842018-10-04 15:11:00 -0700628 int addedId = folderId;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700629
630 // We can only have folders with >= 2 items, so we need to remove the
631 // folder and clean up if less than 2 items were included, or some
632 // failed to add, and less than 2 were actually added
633 if (folderItems.size() < 2) {
634 // Delete the folder
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700635 Uri uri = Favorites.getContentUri(folderId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700636 SqlArguments args = new SqlArguments(uri, null, null);
637 mDb.delete(args.table, args.where, args.args);
638 addedId = -1;
639
640 // If we have a single item, promote it to where the folder
641 // would have been.
642 if (folderItems.size() == 1) {
643 final ContentValues childValues = new ContentValues();
644 copyInteger(myValues, childValues, Favorites.CONTAINER);
645 copyInteger(myValues, childValues, Favorites.SCREEN);
646 copyInteger(myValues, childValues, Favorites.CELLX);
647 copyInteger(myValues, childValues, Favorites.CELLY);
648
649 addedId = folderItems.get(0);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700650 mDb.update(Favorites.TABLE_NAME, childValues,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700651 Favorites._ID + "=" + addedId, null);
652 }
653 }
654 return addedId;
655 }
656 }
657
Sunny Goyal762d0612020-07-29 15:03:46 -0700658 public static void beginDocument(XmlPullParser parser, String firstElementName)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700659 throws XmlPullParserException, IOException {
660 int type;
661 while ((type = parser.next()) != XmlPullParser.START_TAG
662 && type != XmlPullParser.END_DOCUMENT);
663
664 if (type != XmlPullParser.START_TAG) {
665 throw new XmlPullParserException("No start tag found");
666 }
667
668 if (!parser.getName().equals(firstElementName)) {
669 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
670 ", expected " + firstElementName);
671 }
672 }
673
Sunny Goyal96a09632015-12-16 11:32:54 -0800674 private static String convertToDistanceFromEnd(String value, int endValue) {
675 if (!TextUtils.isEmpty(value)) {
676 int x = Integer.parseInt(value);
677 if (x < 0) {
678 return Integer.toString(endValue + x);
679 }
680 }
681 return value;
682 }
683
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700684 /**
685 * Return attribute value, attempting launcher-specific namespace first
686 * before falling back to anonymous attribute.
687 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800688 protected static String getAttributeValue(XmlPullParser parser, String attribute) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700689 String value = parser.getAttributeValue(
690 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
691 if (value == null) {
692 value = parser.getAttributeValue(null, attribute);
693 }
694 return value;
695 }
696
697 /**
698 * Return attribute resource value, attempting launcher-specific namespace
699 * first before falling back to anonymous attribute.
700 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800701 protected static int getAttributeResourceValue(XmlPullParser parser, String attribute,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700702 int defaultValue) {
Sunny Goyal0d742312019-03-04 20:22:26 -0800703 AttributeSet attrs = Xml.asAttributeSet(parser);
704 int value = attrs.getAttributeResourceValue(
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700705 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
706 defaultValue);
707 if (value == defaultValue) {
Sunny Goyal0d742312019-03-04 20:22:26 -0800708 value = attrs.getAttributeResourceValue(null, attribute, defaultValue);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700709 }
710 return value;
711 }
712
Rajeev Kumar26453a22017-06-09 16:02:25 -0700713 public interface LayoutParserCallback {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700714 int generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700715
Sunny Goyalefb7e842018-10-04 15:11:00 -0700716 int insertAndCheck(SQLiteDatabase db, ContentValues values);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700717 }
718
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700719 @Thunk
720 static void copyInteger(ContentValues from, ContentValues to, String key) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700721 to.put(key, from.getAsInteger(key));
722 }
723}