blob: 5971a028caa3d01907e9752c9ba6aa5af9aece6a [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;
Sunny Goyal179249d2017-12-19 16:49:24 -080030import android.os.Build.VERSION;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070031import android.os.Bundle;
Sunny Goyalad7ff442017-09-12 12:42:26 -070032import android.os.Process;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070033import android.text.TextUtils;
Rajeev Kumar26453a22017-06-09 16:02:25 -070034import android.util.ArrayMap;
Sunny Goyal0d742312019-03-04 20:22:26 -080035import android.util.AttributeSet;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070036import android.util.Log;
37import android.util.Pair;
38import android.util.Patterns;
Sunny Goyal0d742312019-03-04 20:22:26 -080039import android.util.Xml;
Sunny Goyal36b54222018-07-10 13:50:50 -070040
Samuel Fufaca37b8a2019-08-19 17:04:36 -070041import androidx.annotation.Nullable;
42
Sunny Goyal0fe505b2014-08-06 09:55:36 -070043import com.android.launcher3.LauncherProvider.SqlArguments;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070044import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale62d2bb2018-11-06 10:28:37 -080045import com.android.launcher3.icons.GraphicsUtils;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070046import com.android.launcher3.icons.LauncherIcons;
Sunny Goyale396abf2020-04-06 15:11:17 -070047import com.android.launcher3.model.data.LauncherAppWidgetInfo;
48import com.android.launcher3.model.data.WorkspaceItemInfo;
Samuel Fufaca37b8a2019-08-19 17:04:36 -070049import com.android.launcher3.qsb.QsbContainerView;
Sunny Goyalefb7e842018-10-04 15:11:00 -070050import com.android.launcher3.util.IntArray;
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070051import com.android.launcher3.util.PackageManagerHelper;
Adam Cohen091440a2015-03-18 14:16:05 -070052import com.android.launcher3.util.Thunk;
Sunny Goyal36b54222018-07-10 13:50:50 -070053
54import org.xmlpull.v1.XmlPullParser;
55import org.xmlpull.v1.XmlPullParserException;
56
Sunny Goyal0fe505b2014-08-06 09:55:36 -070057import java.io.IOException;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070058import java.util.Locale;
Sunny Goyalc0f03d92019-03-22 14:13:36 -070059import java.util.function.Supplier;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070060
61/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070062 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070063 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070064public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070065 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070066 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070067
68 /** Marker action used to discover a package which defines launcher customization */
69 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070070 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070071
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070072 /**
73 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
74 */
75 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
76 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070077 private static final String LAYOUT_RES = "default_layout";
78
79 static AutoInstallsLayout get(Context context, AppWidgetHost appWidgetHost,
80 LayoutParserCallback callback) {
Sunny Goyal9dbb27c2019-07-17 15:12:56 -070081 Pair<String, Resources> customizationApkInfo = PackageManagerHelper.findSystemApk(
Sunny Goyal0fe505b2014-08-06 09:55:36 -070082 ACTION_LAUNCHER_CUSTOMIZATION, context.getPackageManager());
83 if (customizationApkInfo == null) {
84 return null;
85 }
Sunny Goyalc0f03d92019-03-22 14:13:36 -070086 String pkg = customizationApkInfo.first;
87 Resources targetRes = customizationApkInfo.second;
Sunny Goyal87f784c2017-01-11 10:48:34 -080088 InvariantDeviceProfile grid = LauncherAppState.getIDP(context);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070089
90 // Try with grid size and hotseat count
91 String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
Samuel Fufaca37b8a2019-08-19 17:04:36 -070092 grid.numColumns, grid.numRows, grid.numHotseatIcons);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070093 int layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
94
95 // Try with only grid size
96 if (layoutId == 0) {
97 Log.d(TAG, "Formatted layout: " + layoutName
98 + " not found. Trying layout without hosteat");
99 layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700100 grid.numColumns, grid.numRows);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700101 layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
102 }
103
104 // Try the default layout
105 if (layoutId == 0) {
106 Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
107 layoutId = targetRes.getIdentifier(LAYOUT_RES, "xml", pkg);
108 }
109
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700110 if (layoutId == 0) {
111 Log.e(TAG, "Layout definition not found in package: " + pkg);
112 return null;
113 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700114 return new AutoInstallsLayout(context, appWidgetHost, callback, targetRes, layoutId,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700115 TAG_WORKSPACE);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700116 }
117
118 // Object Tags
Sunny Goyalb564efb2015-01-23 13:45:20 -0800119 private static final String TAG_INCLUDE = "include";
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700120 public static final String TAG_WORKSPACE = "workspace";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700121 private static final String TAG_APP_ICON = "appicon";
122 private static final String TAG_AUTO_INSTALL = "autoinstall";
123 private static final String TAG_FOLDER = "folder";
124 private static final String TAG_APPWIDGET = "appwidget";
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700125 protected static final String TAG_SEARCH_WIDGET = "searchwidget";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700126 private static final String TAG_SHORTCUT = "shortcut";
127 private static final String TAG_EXTRA = "extra";
128
129 private static final String ATTR_CONTAINER = "container";
130 private static final String ATTR_RANK = "rank";
131
132 private static final String ATTR_PACKAGE_NAME = "packageName";
133 private static final String ATTR_CLASS_NAME = "className";
134 private static final String ATTR_TITLE = "title";
135 private static final String ATTR_SCREEN = "screen";
Sunny Goyal96a09632015-12-16 11:32:54 -0800136
137 // x and y can be specified as negative integers, in which case -1 represents the
138 // last row / column, -2 represents the second last, and so on.
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700139 private static final String ATTR_X = "x";
140 private static final String ATTR_Y = "y";
Sunny Goyal96a09632015-12-16 11:32:54 -0800141
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700142 private static final String ATTR_SPAN_X = "spanX";
143 private static final String ATTR_SPAN_Y = "spanY";
144 private static final String ATTR_ICON = "icon";
145 private static final String ATTR_URL = "url";
146
Sunny Goyalb564efb2015-01-23 13:45:20 -0800147 // Attrs for "Include"
148 private static final String ATTR_WORKSPACE = "workspace";
149
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700150 // Style attrs -- "Extra"
151 private static final String ATTR_KEY = "key";
152 private static final String ATTR_VALUE = "value";
153
154 private static final String HOTSEAT_CONTAINER_NAME =
155 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
156
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700157 @Thunk
158 final Context mContext;
159 @Thunk
160 final AppWidgetHost mAppWidgetHost;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800161 protected final LayoutParserCallback mCallback;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700162
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700163 protected final PackageManager mPackageManager;
164 protected final Resources mSourceRes;
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700165 protected final Supplier<XmlPullParser> mInitialLayoutSupplier;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700166
Sunny Goyalbb011da2016-06-15 15:42:29 -0700167 private final InvariantDeviceProfile mIdp;
Sunny Goyal96a09632015-12-16 11:32:54 -0800168 private final int mRowCount;
169 private final int mColumnCount;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700170
Sunny Goyalefb7e842018-10-04 15:11:00 -0700171 private final int[] mTemp = new int[2];
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700172 @Thunk
173 final ContentValues mValues;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800174 protected final String mRootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700175
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700176 protected SQLiteDatabase mDb;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700177
178 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700179 LayoutParserCallback callback, Resources res,
180 int layoutId, String rootTag) {
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700181 this(context, appWidgetHost, callback, res, () -> res.getXml(layoutId), rootTag);
182 }
183
184 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
185 LayoutParserCallback callback, Resources res,
186 Supplier<XmlPullParser> initialLayoutSupplier, String rootTag) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700187 mContext = context;
188 mAppWidgetHost = appWidgetHost;
189 mCallback = callback;
190
191 mPackageManager = context.getPackageManager();
192 mValues = new ContentValues();
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700193 mRootTag = rootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700194
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700195 mSourceRes = res;
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700196 mInitialLayoutSupplier = initialLayoutSupplier;
Sunny Goyal96a09632015-12-16 11:32:54 -0800197
Sunny Goyal87f784c2017-01-11 10:48:34 -0800198 mIdp = LauncherAppState.getIDP(context);
Sunny Goyalbb011da2016-06-15 15:42:29 -0700199 mRowCount = mIdp.numRows;
200 mColumnCount = mIdp.numColumns;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700201 }
202
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700203 /**
204 * Loads the layout in the db and returns the number of entries added on the desktop.
205 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700206 public int loadLayout(SQLiteDatabase db, IntArray screenIds) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700207 mDb = db;
208 try {
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700209 return parseLayout(mInitialLayoutSupplier.get(), screenIds);
Sameer Padala8fd74832014-09-08 16:00:29 -0700210 } catch (Exception e) {
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700211 Log.e(TAG, "Error parsing layout: ", e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700212 return -1;
213 }
214 }
215
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700216 /**
217 * Parses the layout and returns the number of elements added on the homescreen.
218 */
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700219 protected int parseLayout(XmlPullParser parser, IntArray screenIds)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700220 throws XmlPullParserException, IOException {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700221 beginDocument(parser, mRootTag);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700222 final int depth = parser.getDepth();
223 int type;
Rajeev Kumar26453a22017-06-09 16:02:25 -0700224 ArrayMap<String, TagParser> tagParserMap = getLayoutElementsMap();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700225 int count = 0;
226
227 while (((type = parser.next()) != XmlPullParser.END_TAG ||
228 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
229 if (type != XmlPullParser.START_TAG) {
230 continue;
231 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700232 count += parseAndAddNode(parser, tagParserMap, screenIds);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700233 }
234 return count;
235 }
236
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700237 /**
238 * Parses container and screenId attribute from the current tag, and puts it in the out.
239 * @param out array of size 2.
240 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800241 protected void parseContainerAndScreen(XmlPullParser parser, int[] out) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700242 if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) {
243 out[0] = Favorites.CONTAINER_HOTSEAT;
244 // Hack: hotseat items are stored using screen ids
Sunny Goyalefb7e842018-10-04 15:11:00 -0700245 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_RANK));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700246 } else {
247 out[0] = Favorites.CONTAINER_DESKTOP;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700248 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_SCREEN));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700249 }
250 }
251
252 /**
253 * Parses the current node and returns the number of elements added.
254 */
255 protected int parseAndAddNode(
Sunny Goyal0d742312019-03-04 20:22:26 -0800256 XmlPullParser parser, ArrayMap<String, TagParser> tagParserMap, IntArray screenIds)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700257 throws XmlPullParserException, IOException {
Sunny Goyalb564efb2015-01-23 13:45:20 -0800258
259 if (TAG_INCLUDE.equals(parser.getName())) {
260 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
261 if (resId != 0) {
262 // recursively load some more favorites, why not?
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700263 return parseLayout(mSourceRes.getXml(resId), screenIds);
Sunny Goyalb564efb2015-01-23 13:45:20 -0800264 } else {
265 return 0;
266 }
267 }
268
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700269 mValues.clear();
270 parseContainerAndScreen(parser, mTemp);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700271 final int container = mTemp[0];
272 final int screenId = mTemp[1];
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700273
274 mValues.put(Favorites.CONTAINER, container);
275 mValues.put(Favorites.SCREEN, screenId);
Sunny Goyal96a09632015-12-16 11:32:54 -0800276
277 mValues.put(Favorites.CELLX,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800278 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount));
Sunny Goyal96a09632015-12-16 11:32:54 -0800279 mValues.put(Favorites.CELLY,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800280 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700281
282 TagParser tagParser = tagParserMap.get(parser.getName());
283 if (tagParser == null) {
284 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
285 return 0;
286 }
Sunny Goyalefb7e842018-10-04 15:11:00 -0700287 int newElementId = tagParser.parseAndAdd(parser);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700288 if (newElementId >= 0) {
289 // Keep track of the set of screens which need to be added to the db.
290 if (!screenIds.contains(screenId) &&
291 container == Favorites.CONTAINER_DESKTOP) {
292 screenIds.add(screenId);
293 }
294 return 1;
295 }
296 return 0;
297 }
298
Sunny Goyalefb7e842018-10-04 15:11:00 -0700299 protected int addShortcut(String title, Intent intent, int type) {
300 int id = mCallback.generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700301 mValues.put(Favorites.INTENT, intent.toUri(0));
302 mValues.put(Favorites.TITLE, title);
303 mValues.put(Favorites.ITEM_TYPE, type);
304 mValues.put(Favorites.SPANX, 1);
305 mValues.put(Favorites.SPANY, 1);
306 mValues.put(Favorites._ID, id);
307 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
308 return -1;
309 } else {
310 return id;
311 }
312 }
313
Rajeev Kumar26453a22017-06-09 16:02:25 -0700314 protected ArrayMap<String, TagParser> getFolderElementsMap() {
315 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700316 parsers.put(TAG_APP_ICON, new AppShortcutParser());
317 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700318 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700319 return parsers;
320 }
321
Rajeev Kumar26453a22017-06-09 16:02:25 -0700322 protected ArrayMap<String, TagParser> getLayoutElementsMap() {
323 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700324 parsers.put(TAG_APP_ICON, new AppShortcutParser());
325 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
326 parsers.put(TAG_FOLDER, new FolderParser());
Sunny Goyal86df1382016-08-10 15:03:22 -0700327 parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700328 parsers.put(TAG_SEARCH_WIDGET, new SearchWidgetParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700329 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700330 return parsers;
331 }
332
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700333 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700334 /**
335 * Parses the tag and adds to the db
336 * @return the id of the row added or -1;
337 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800338 int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700339 throws XmlPullParserException, IOException;
340 }
341
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700342 /**
343 * App shortcuts: required attributes packageName and className
344 */
345 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700346
347 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800348 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700349 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
350 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
351
352 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
353 ActivityInfo info;
354 try {
355 ComponentName cn;
356 try {
357 cn = new ComponentName(packageName, className);
358 info = mPackageManager.getActivityInfo(cn, 0);
359 } catch (PackageManager.NameNotFoundException nnfe) {
360 String[] packages = mPackageManager.currentToCanonicalPackageNames(
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700361 new String[]{packageName});
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700362 cn = new ComponentName(packages[0], className);
363 info = mPackageManager.getActivityInfo(cn, 0);
364 }
365 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700366 .addCategory(Intent.CATEGORY_LAUNCHER)
367 .setComponent(cn)
368 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
369 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700370
371 return addShortcut(info.loadLabel(mPackageManager).toString(),
372 intent, Favorites.ITEM_TYPE_APPLICATION);
373 } catch (PackageManager.NameNotFoundException e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700374 Log.e(TAG, "Favorite not found: " + packageName + "/" + className);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700375 }
376 return -1;
377 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700378 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700379 }
380 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700381
382 /**
383 * Helper method to allow extending the parser capabilities
384 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800385 protected int invalidPackageOrClass(XmlPullParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700386 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700387 return -1;
388 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700389 }
390
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700391 /**
392 * AutoInstall: required attributes packageName and className
393 */
394 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700395
396 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800397 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700398 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
399 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
400 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
401 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
402 return -1;
403 }
404
Sunny Goyal95899162019-03-27 16:03:06 -0700405 mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_AUTOINSTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700406 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700407 .addCategory(Intent.CATEGORY_LAUNCHER)
408 .setComponent(new ComponentName(packageName, className))
409 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
410 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700411 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
412 Favorites.ITEM_TYPE_APPLICATION);
413 }
414 }
415
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700416 /**
417 * Parses a web shortcut. Required attributes url, icon, title
418 */
419 protected class ShortcutParser implements TagParser {
420
421 private final Resources mIconRes;
422
423 public ShortcutParser(Resources iconRes) {
424 mIconRes = iconRes;
425 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700426
427 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800428 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700429 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
430 final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0);
431
432 if (titleResId == 0 || iconId == 0) {
433 if (LOGD) Log.d(TAG, "Ignoring shortcut");
434 return -1;
435 }
436
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700437 final Intent intent = parseIntent(parser);
438 if (intent == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700439 return -1;
440 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700441
442 Drawable icon = mIconRes.getDrawable(iconId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700443 if (icon == null) {
444 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon");
445 return -1;
446 }
447
Sunny Goyalad7ff442017-09-12 12:42:26 -0700448 // Auto installs should always support the current platform version.
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800449 LauncherIcons li = LauncherIcons.obtain(mContext);
Sunny Goyale62d2bb2018-11-06 10:28:37 -0800450 mValues.put(LauncherSettings.Favorites.ICON, GraphicsUtils.flattenBitmap(
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800451 li.createBadgedIconBitmap(icon, Process.myUserHandle(), VERSION.SDK_INT).icon));
452 li.recycle();
453
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700454 mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
455 mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));
456
457 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700458 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700459 return addShortcut(mSourceRes.getString(titleResId),
460 intent, Favorites.ITEM_TYPE_SHORTCUT);
461 }
462
Sunny Goyal0d742312019-03-04 20:22:26 -0800463 protected Intent parseIntent(XmlPullParser parser) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700464 final String url = getAttributeValue(parser, ATTR_URL);
465 if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) {
466 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url);
467 return null;
468 }
469 return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700470 }
471 }
472
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700473 /**
474 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
475 * Options child nodes: <extra key=... value=... />
Sunny Goyal86df1382016-08-10 15:03:22 -0700476 * It adds a pending widget which allows the widget to come later. If there are extras, those
477 * are passed to widget options during bind.
478 * The config activity for the widget (if present) is not shown, so any optional configurations
479 * should be passed as extras and the widget should support reading these widget options.
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700480 */
Sunny Goyal86df1382016-08-10 15:03:22 -0700481 protected class PendingWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700482
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700483 @Nullable
484 public ComponentName getComponentName(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700485 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
486 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
487 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700488 return null;
489 }
490 return new ComponentName(packageName, className);
491 }
492
493
494 @Override
495 public int parseAndAdd(XmlPullParser parser)
496 throws XmlPullParserException, IOException {
497 ComponentName cn = getComponentName(parser);
498 if (cn == null) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700499 if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component");
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700500 return -1;
501 }
502
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700503 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
504 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
Sunny Goyal86df1382016-08-10 15:03:22 -0700505 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700506
507 // Read the extras
508 Bundle extras = new Bundle();
509 int widgetDepth = parser.getDepth();
510 int type;
511 while ((type = parser.next()) != XmlPullParser.END_TAG ||
512 parser.getDepth() > widgetDepth) {
513 if (type != XmlPullParser.START_TAG) {
514 continue;
515 }
516
517 if (TAG_EXTRA.equals(parser.getName())) {
518 String key = getAttributeValue(parser, ATTR_KEY);
519 String value = getAttributeValue(parser, ATTR_VALUE);
520 if (key != null && value != null) {
521 extras.putString(key, value);
522 } else {
523 throw new RuntimeException("Widget extras must have a key and value");
524 }
525 } else {
526 throw new RuntimeException("Widgets can contain only extras");
527 }
528 }
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700529 return verifyAndInsert(cn, extras);
Sunny Goyal86df1382016-08-10 15:03:22 -0700530 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700531
Sunny Goyalefb7e842018-10-04 15:11:00 -0700532 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700533 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
534 mValues.put(Favorites.RESTORED,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700535 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID
536 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY
537 | LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
Sunny Goyal86df1382016-08-10 15:03:22 -0700538 mValues.put(Favorites._ID, mCallback.generateNewItemId());
539 if (!extras.isEmpty()) {
540 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700541 }
Sunny Goyal86df1382016-08-10 15:03:22 -0700542
Sunny Goyalefb7e842018-10-04 15:11:00 -0700543 int insertedId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal86df1382016-08-10 15:03:22 -0700544 if (insertedId < 0) {
545 return -1;
546 } else {
547 return insertedId;
548 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700549 }
550 }
551
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700552 protected class SearchWidgetParser extends PendingWidgetParser {
553 @Override
554 @Nullable
555 public ComponentName getComponentName(XmlPullParser parser) {
556 return QsbContainerView.getSearchComponentName(mContext);
557 }
558
559 @Override
560 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
561 mValues.put(Favorites.OPTIONS, LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET);
562 int flags = mValues.getAsInteger(Favorites.RESTORED)
563 | WorkspaceItemInfo.FLAG_RESTORE_STARTED;
564 mValues.put(Favorites.RESTORED, flags);
565 return super.verifyAndInsert(cn, extras);
566 }
567 }
568
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700569 protected class FolderParser implements TagParser {
Rajeev Kumar26453a22017-06-09 16:02:25 -0700570 private final ArrayMap<String, TagParser> mFolderElements;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700571
572 public FolderParser() {
573 this(getFolderElementsMap());
574 }
575
Rajeev Kumar26453a22017-06-09 16:02:25 -0700576 public FolderParser(ArrayMap<String, TagParser> elements) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700577 mFolderElements = elements;
578 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700579
580 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800581 public int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700582 throws XmlPullParserException, IOException {
583 final String title;
584 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
585 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700586 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700587 } else {
Samuel Fufa84245e82019-08-14 13:44:12 -0700588 title = "";
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
Rajeev Kumar26453a22017-06-09 16:02:25 -0700659 protected 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}