blob: 2083726a05e48c3f8a36a55cac9ab6937784fa51 [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
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000019import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
20
Sunny Goyal0fe505b2014-08-06 09:55:36 -070021import android.content.ComponentName;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.ActivityInfo;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000026import android.content.pm.LauncherActivityInfo;
Sunny Goyalafaa8f02023-03-30 12:34:43 -070027import android.content.pm.LauncherApps;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070028import android.content.pm.PackageManager;
29import android.content.res.Resources;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070030import android.database.sqlite.SQLiteDatabase;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070031import android.net.Uri;
32import android.os.Bundle;
Sunny Goyalafaa8f02023-03-30 12:34:43 -070033import android.os.Process;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070034import android.text.TextUtils;
Rajeev Kumar26453a22017-06-09 16:02:25 -070035import android.util.ArrayMap;
Sunny Goyal0d742312019-03-04 20:22:26 -080036import android.util.AttributeSet;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070037import android.util.Log;
Sunny Goyal0d742312019-03-04 20:22:26 -080038import android.util.Xml;
Sunny Goyal36b54222018-07-10 13:50:50 -070039
Samuel Fufaca37b8a2019-08-19 17:04:36 -070040import androidx.annotation.Nullable;
41
Sunny Goyal0fe505b2014-08-06 09:55:36 -070042import com.android.launcher3.LauncherProvider.SqlArguments;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070043import com.android.launcher3.LauncherSettings.Favorites;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000044import com.android.launcher3.model.data.AppInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070045import com.android.launcher3.model.data.LauncherAppWidgetInfo;
46import com.android.launcher3.model.data.WorkspaceItemInfo;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000047import com.android.launcher3.pm.UserCache;
Samuel Fufaca37b8a2019-08-19 17:04:36 -070048import com.android.launcher3.qsb.QsbContainerView;
Sunny Goyalafaa8f02023-03-30 12:34:43 -070049import com.android.launcher3.shortcuts.ShortcutKey;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000050import com.android.launcher3.uioverrides.ApiWrapper;
Sunny Goyalefb7e842018-10-04 15:11:00 -070051import com.android.launcher3.util.IntArray;
Sunny Goyal3e58eea2022-11-14 14:30:07 -080052import com.android.launcher3.util.Partner;
Adam Cohen091440a2015-03-18 14:16:05 -070053import com.android.launcher3.util.Thunk;
Sihua Ma8bbfcb62022-11-08 16:46:07 -080054import com.android.launcher3.widget.LauncherWidgetHolder;
Sunny Goyal36b54222018-07-10 13:50:50 -070055
56import org.xmlpull.v1.XmlPullParser;
57import org.xmlpull.v1.XmlPullParserException;
58
Sunny Goyal0fe505b2014-08-06 09:55:36 -070059import java.io.IOException;
Sunny Goyalafaa8f02023-03-30 12:34:43 -070060import java.util.Collections;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070061import java.util.Locale;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000062import java.util.Map;
Sunny Goyalc0f03d92019-03-22 14:13:36 -070063import java.util.function.Supplier;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070064
65/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070066 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070067 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070068public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070069 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070070 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070071
72 /** Marker action used to discover a package which defines launcher customization */
73 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070074 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070075
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070076 /**
77 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
78 */
79 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
80 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070081 private static final String LAYOUT_RES = "default_layout";
82
Sihua Maaa2b8722022-10-25 15:17:58 -070083 static AutoInstallsLayout get(Context context, LauncherWidgetHolder appWidgetHolder,
Sunny Goyal0fe505b2014-08-06 09:55:36 -070084 LayoutParserCallback callback) {
Sunny Goyal3e58eea2022-11-14 14:30:07 -080085 Partner partner = Partner.get(context.getPackageManager(), ACTION_LAUNCHER_CUSTOMIZATION);
86 if (partner == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070087 return null;
88 }
Sunny Goyal87f784c2017-01-11 10:48:34 -080089 InvariantDeviceProfile grid = LauncherAppState.getIDP(context);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070090
91 // Try with grid size and hotseat count
92 String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
Tony Wickhamb87f3cd2021-04-07 15:02:37 -070093 grid.numColumns, grid.numRows, grid.numDatabaseHotseatIcons);
Sunny Goyal3e58eea2022-11-14 14:30:07 -080094 int layoutId = partner.getXmlResId(layoutName);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070095
96 // Try with only grid size
97 if (layoutId == 0) {
98 Log.d(TAG, "Formatted layout: " + layoutName
99 + " not found. Trying layout without hosteat");
100 layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700101 grid.numColumns, grid.numRows);
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800102 layoutId = partner.getXmlResId(layoutName);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700103 }
104
105 // Try the default layout
106 if (layoutId == 0) {
107 Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800108 layoutId = partner.getXmlResId(LAYOUT_RES);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700109 }
110
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700111 if (layoutId == 0) {
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800112 Log.e(TAG, "Layout definition not found in package: " + partner.getPackageName());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700113 return null;
114 }
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800115 return new AutoInstallsLayout(context, appWidgetHolder, callback, partner.getResources(),
116 layoutId, TAG_WORKSPACE);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700117 }
118
119 // Object Tags
Sunny Goyalb564efb2015-01-23 13:45:20 -0800120 private static final String TAG_INCLUDE = "include";
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700121 public static final String TAG_WORKSPACE = "workspace";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700122 private static final String TAG_APP_ICON = "appicon";
123 private static final String TAG_AUTO_INSTALL = "autoinstall";
124 private static final String TAG_FOLDER = "folder";
125 private static final String TAG_APPWIDGET = "appwidget";
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700126 protected static final String TAG_SEARCH_WIDGET = "searchwidget";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700127 private static final String TAG_SHORTCUT = "shortcut";
128 private static final String TAG_EXTRA = "extra";
129
130 private static final String ATTR_CONTAINER = "container";
131 private static final String ATTR_RANK = "rank";
132
133 private static final String ATTR_PACKAGE_NAME = "packageName";
134 private static final String ATTR_CLASS_NAME = "className";
135 private static final String ATTR_TITLE = "title";
Sunny Goyal55e2b162020-06-09 15:44:48 -0700136 private static final String ATTR_TITLE_TEXT = "titleText";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700137 private static final String ATTR_SCREEN = "screen";
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700138 private static final String ATTR_SHORTCUT_ID = "shortcutId";
Sunny Goyal96a09632015-12-16 11:32:54 -0800139
140 // x and y can be specified as negative integers, in which case -1 represents the
141 // last row / column, -2 represents the second last, and so on.
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700142 private static final String ATTR_X = "x";
143 private static final String ATTR_Y = "y";
Sunny Goyal96a09632015-12-16 11:32:54 -0800144
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700145 private static final String ATTR_SPAN_X = "spanX";
146 private static final String ATTR_SPAN_Y = "spanY";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700147
Sunny Goyalb564efb2015-01-23 13:45:20 -0800148 // Attrs for "Include"
149 private static final String ATTR_WORKSPACE = "workspace";
150
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700151 // Style attrs -- "Extra"
152 private static final String ATTR_KEY = "key";
153 private static final String ATTR_VALUE = "value";
154
155 private static final String HOTSEAT_CONTAINER_NAME =
156 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
157
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700158 protected final Context mContext;
159 protected final LauncherWidgetHolder mAppWidgetHolder;
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;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000169 private final Map<String, LauncherActivityInfo> mActivityOverride;
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
Sihua Maaa2b8722022-10-25 15:17:58 -0700177 public AutoInstallsLayout(Context context, LauncherWidgetHolder appWidgetHolder,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700178 LayoutParserCallback callback, Resources res,
179 int layoutId, String rootTag) {
Sihua Maaa2b8722022-10-25 15:17:58 -0700180 this(context, appWidgetHolder, callback, res, () -> res.getXml(layoutId), rootTag);
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700181 }
182
Sihua Maaa2b8722022-10-25 15:17:58 -0700183 public AutoInstallsLayout(Context context, LauncherWidgetHolder appWidgetHolder,
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700184 LayoutParserCallback callback, Resources res,
185 Supplier<XmlPullParser> initialLayoutSupplier, String rootTag) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700186 mContext = context;
Sihua Maaa2b8722022-10-25 15:17:58 -0700187 mAppWidgetHolder = appWidgetHolder;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700188 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;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000200 mActivityOverride = ApiWrapper.getActivityOverrides(context);
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);
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000307
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700308 if (type == ITEM_TYPE_APPLICATION) {
309 ComponentName cn = intent.getComponent();
310 if (cn != null && mActivityOverride.containsKey(cn.getPackageName())) {
311 LauncherActivityInfo replacementInfo = mActivityOverride.get(cn.getPackageName());
312 mValues.put(Favorites.PROFILE_ID, UserCache.INSTANCE.get(mContext)
313 .getSerialNumberForUser(replacementInfo.getUser()));
314 mValues.put(Favorites.INTENT, AppInfo.makeLaunchIntent(replacementInfo).toUri(0));
315 }
316 }
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000317
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700318 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
319 return -1;
320 } else {
321 return id;
322 }
323 }
324
Rajeev Kumar26453a22017-06-09 16:02:25 -0700325 protected ArrayMap<String, TagParser> getFolderElementsMap() {
326 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700327 parsers.put(TAG_APP_ICON, new AppShortcutParser());
328 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700329 parsers.put(TAG_SHORTCUT, new ShortcutParser());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700330 return parsers;
331 }
332
Rajeev Kumar26453a22017-06-09 16:02:25 -0700333 protected ArrayMap<String, TagParser> getLayoutElementsMap() {
334 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700335 parsers.put(TAG_APP_ICON, new AppShortcutParser());
336 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
337 parsers.put(TAG_FOLDER, new FolderParser());
Sunny Goyal86df1382016-08-10 15:03:22 -0700338 parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700339 parsers.put(TAG_SEARCH_WIDGET, new SearchWidgetParser());
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700340 parsers.put(TAG_SHORTCUT, new ShortcutParser());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700341 return parsers;
342 }
343
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700344 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700345 /**
346 * Parses the tag and adds to the db
347 * @return the id of the row added or -1;
348 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800349 int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700350 throws XmlPullParserException, IOException;
351 }
352
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700353 /**
354 * App shortcuts: required attributes packageName and className
355 */
356 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700357
358 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800359 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700360 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
361 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
362
363 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
364 ActivityInfo info;
365 try {
366 ComponentName cn;
367 try {
368 cn = new ComponentName(packageName, className);
369 info = mPackageManager.getActivityInfo(cn, 0);
370 } catch (PackageManager.NameNotFoundException nnfe) {
371 String[] packages = mPackageManager.currentToCanonicalPackageNames(
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700372 new String[]{packageName});
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700373 cn = new ComponentName(packages[0], className);
374 info = mPackageManager.getActivityInfo(cn, 0);
375 }
376 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700377 .addCategory(Intent.CATEGORY_LAUNCHER)
378 .setComponent(cn)
379 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
380 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700381
382 return addShortcut(info.loadLabel(mPackageManager).toString(),
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000383 intent, ITEM_TYPE_APPLICATION);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700384 } catch (PackageManager.NameNotFoundException e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700385 Log.e(TAG, "Favorite not found: " + packageName + "/" + className);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700386 }
387 return -1;
388 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700389 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700390 }
391 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700392
393 /**
394 * Helper method to allow extending the parser capabilities
395 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800396 protected int invalidPackageOrClass(XmlPullParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700397 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700398 return -1;
399 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700400 }
401
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700402 /**
403 * AutoInstall: required attributes packageName and className
404 */
405 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700406
407 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800408 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700409 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
410 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
411 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
412 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
413 return -1;
414 }
415
Sunny Goyal95899162019-03-27 16:03:06 -0700416 mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_AUTOINSTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700417 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700418 .addCategory(Intent.CATEGORY_LAUNCHER)
419 .setComponent(new ComponentName(packageName, className))
420 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
421 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700422 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000423 ITEM_TYPE_APPLICATION);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700424 }
425 }
426
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700427 /**
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700428 * Parses a deep shortcut. Required attributes packageName and shortcutId
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700429 */
430 protected class ShortcutParser implements TagParser {
431
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700432 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800433 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700434 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
435 final String shortcutId = getAttributeValue(parser, ATTR_SHORTCUT_ID);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700436
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700437 try {
438 LauncherApps launcherApps = mContext.getSystemService(LauncherApps.class);
439 launcherApps.pinShortcuts(packageName, Collections.singletonList(shortcutId),
440 Process.myUserHandle());
441 Intent intent = ShortcutKey.makeIntent(shortcutId, packageName);
442 mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_RESTORED_ICON);
443 return addShortcut(null, intent, Favorites.ITEM_TYPE_DEEP_SHORTCUT);
444 } catch (Exception e) {
445 Log.e(TAG, "Unable to pin the shortcut for shortcut id = " + shortcutId
446 + " and package name = " + packageName, e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700447 }
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700448 return -1;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700449 }
450 }
451
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700452 /**
453 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
454 * Options child nodes: <extra key=... value=... />
Sunny Goyal86df1382016-08-10 15:03:22 -0700455 * It adds a pending widget which allows the widget to come later. If there are extras, those
456 * are passed to widget options during bind.
457 * The config activity for the widget (if present) is not shown, so any optional configurations
458 * should be passed as extras and the widget should support reading these widget options.
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700459 */
Sunny Goyal86df1382016-08-10 15:03:22 -0700460 protected class PendingWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700461
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700462 @Nullable
463 public ComponentName getComponentName(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700464 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
465 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
466 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700467 return null;
468 }
469 return new ComponentName(packageName, className);
470 }
471
472
473 @Override
474 public int parseAndAdd(XmlPullParser parser)
475 throws XmlPullParserException, IOException {
476 ComponentName cn = getComponentName(parser);
477 if (cn == null) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700478 if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component");
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700479 return -1;
480 }
481
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700482 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
483 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
Sunny Goyal86df1382016-08-10 15:03:22 -0700484 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700485
486 // Read the extras
487 Bundle extras = new Bundle();
488 int widgetDepth = parser.getDepth();
489 int type;
490 while ((type = parser.next()) != XmlPullParser.END_TAG ||
491 parser.getDepth() > widgetDepth) {
492 if (type != XmlPullParser.START_TAG) {
493 continue;
494 }
495
496 if (TAG_EXTRA.equals(parser.getName())) {
497 String key = getAttributeValue(parser, ATTR_KEY);
498 String value = getAttributeValue(parser, ATTR_VALUE);
499 if (key != null && value != null) {
500 extras.putString(key, value);
501 } else {
502 throw new RuntimeException("Widget extras must have a key and value");
503 }
504 } else {
505 throw new RuntimeException("Widgets can contain only extras");
506 }
507 }
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700508 return verifyAndInsert(cn, extras);
Sunny Goyal86df1382016-08-10 15:03:22 -0700509 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700510
Sunny Goyalefb7e842018-10-04 15:11:00 -0700511 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700512 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
513 mValues.put(Favorites.RESTORED,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700514 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID
515 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY
516 | LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
Sunny Goyal86df1382016-08-10 15:03:22 -0700517 mValues.put(Favorites._ID, mCallback.generateNewItemId());
518 if (!extras.isEmpty()) {
519 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700520 }
Sunny Goyal86df1382016-08-10 15:03:22 -0700521
Sunny Goyalefb7e842018-10-04 15:11:00 -0700522 int insertedId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal86df1382016-08-10 15:03:22 -0700523 if (insertedId < 0) {
524 return -1;
525 } else {
526 return insertedId;
527 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700528 }
529 }
530
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700531 protected class SearchWidgetParser extends PendingWidgetParser {
532 @Override
533 @Nullable
534 public ComponentName getComponentName(XmlPullParser parser) {
535 return QsbContainerView.getSearchComponentName(mContext);
536 }
537
538 @Override
539 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
540 mValues.put(Favorites.OPTIONS, LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET);
541 int flags = mValues.getAsInteger(Favorites.RESTORED)
542 | WorkspaceItemInfo.FLAG_RESTORE_STARTED;
543 mValues.put(Favorites.RESTORED, flags);
544 return super.verifyAndInsert(cn, extras);
545 }
546 }
547
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700548 protected class FolderParser implements TagParser {
Rajeev Kumar26453a22017-06-09 16:02:25 -0700549 private final ArrayMap<String, TagParser> mFolderElements;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700550
551 public FolderParser() {
552 this(getFolderElementsMap());
553 }
554
Rajeev Kumar26453a22017-06-09 16:02:25 -0700555 public FolderParser(ArrayMap<String, TagParser> elements) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700556 mFolderElements = elements;
557 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700558
559 @Override
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000560 public int parseAndAdd(XmlPullParser parser) throws XmlPullParserException, IOException {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700561 final String title;
562 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
563 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700564 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700565 } else {
Sunny Goyal55e2b162020-06-09 15:44:48 -0700566 String titleText = getAttributeValue(parser, ATTR_TITLE_TEXT);
567 title = TextUtils.isEmpty(titleText) ? "" : titleText;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700568 }
569
570 mValues.put(Favorites.TITLE, title);
571 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
572 mValues.put(Favorites.SPANX, 1);
573 mValues.put(Favorites.SPANY, 1);
574 mValues.put(Favorites._ID, mCallback.generateNewItemId());
Sunny Goyalefb7e842018-10-04 15:11:00 -0700575 int folderId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700576 if (folderId < 0) {
577 if (LOGD) Log.e(TAG, "Unable to add folder");
578 return -1;
579 }
580
581 final ContentValues myValues = new ContentValues(mValues);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700582 IntArray folderItems = new IntArray();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700583
584 int type;
585 int folderDepth = parser.getDepth();
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700586 int rank = 0;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700587 while ((type = parser.next()) != XmlPullParser.END_TAG ||
588 parser.getDepth() > folderDepth) {
589 if (type != XmlPullParser.START_TAG) {
590 continue;
591 }
592 mValues.clear();
593 mValues.put(Favorites.CONTAINER, folderId);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700594 mValues.put(Favorites.RANK, rank);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700595
596 TagParser tagParser = mFolderElements.get(parser.getName());
597 if (tagParser != null) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700598 final int id = tagParser.parseAndAdd(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700599 if (id >= 0) {
600 folderItems.add(id);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700601 rank++;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700602 }
603 } else {
604 throw new RuntimeException("Invalid folder item " + parser.getName());
605 }
606 }
607
Sunny Goyalefb7e842018-10-04 15:11:00 -0700608 int addedId = folderId;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700609
610 // We can only have folders with >= 2 items, so we need to remove the
611 // folder and clean up if less than 2 items were included, or some
612 // failed to add, and less than 2 were actually added
613 if (folderItems.size() < 2) {
614 // Delete the folder
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700615 Uri uri = Favorites.getContentUri(folderId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700616 SqlArguments args = new SqlArguments(uri, null, null);
617 mDb.delete(args.table, args.where, args.args);
618 addedId = -1;
619
620 // If we have a single item, promote it to where the folder
621 // would have been.
622 if (folderItems.size() == 1) {
623 final ContentValues childValues = new ContentValues();
624 copyInteger(myValues, childValues, Favorites.CONTAINER);
625 copyInteger(myValues, childValues, Favorites.SCREEN);
626 copyInteger(myValues, childValues, Favorites.CELLX);
627 copyInteger(myValues, childValues, Favorites.CELLY);
628
629 addedId = folderItems.get(0);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700630 mDb.update(Favorites.TABLE_NAME, childValues,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700631 Favorites._ID + "=" + addedId, null);
632 }
633 }
634 return addedId;
635 }
636 }
637
Sunny Goyal762d0612020-07-29 15:03:46 -0700638 public static void beginDocument(XmlPullParser parser, String firstElementName)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700639 throws XmlPullParserException, IOException {
640 int type;
641 while ((type = parser.next()) != XmlPullParser.START_TAG
642 && type != XmlPullParser.END_DOCUMENT);
643
644 if (type != XmlPullParser.START_TAG) {
645 throw new XmlPullParserException("No start tag found");
646 }
647
648 if (!parser.getName().equals(firstElementName)) {
649 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
650 ", expected " + firstElementName);
651 }
652 }
653
Sunny Goyal96a09632015-12-16 11:32:54 -0800654 private static String convertToDistanceFromEnd(String value, int endValue) {
655 if (!TextUtils.isEmpty(value)) {
656 int x = Integer.parseInt(value);
657 if (x < 0) {
658 return Integer.toString(endValue + x);
659 }
660 }
661 return value;
662 }
663
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700664 /**
665 * Return attribute value, attempting launcher-specific namespace first
666 * before falling back to anonymous attribute.
667 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800668 protected static String getAttributeValue(XmlPullParser parser, String attribute) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700669 String value = parser.getAttributeValue(
670 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
671 if (value == null) {
672 value = parser.getAttributeValue(null, attribute);
673 }
674 return value;
675 }
676
677 /**
678 * Return attribute resource value, attempting launcher-specific namespace
679 * first before falling back to anonymous attribute.
680 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800681 protected static int getAttributeResourceValue(XmlPullParser parser, String attribute,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700682 int defaultValue) {
Sunny Goyal0d742312019-03-04 20:22:26 -0800683 AttributeSet attrs = Xml.asAttributeSet(parser);
684 int value = attrs.getAttributeResourceValue(
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700685 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
686 defaultValue);
687 if (value == defaultValue) {
Sunny Goyal0d742312019-03-04 20:22:26 -0800688 value = attrs.getAttributeResourceValue(null, attribute, defaultValue);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700689 }
690 return value;
691 }
692
Rajeev Kumar26453a22017-06-09 16:02:25 -0700693 public interface LayoutParserCallback {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700694 int generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700695
Sunny Goyalefb7e842018-10-04 15:11:00 -0700696 int insertAndCheck(SQLiteDatabase db, ContentValues values);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700697 }
698
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700699 @Thunk
700 static void copyInteger(ContentValues from, ContentValues to, String key) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700701 to.put(key, from.getAsInteger(key));
702 }
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000703
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700704}