blob: 62bc53a817deb1d349480c6770034140bcc0d0e8 [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
Sunny Goyal0fe505b2014-08-06 09:55:36 -070041import com.android.launcher3.LauncherProvider.SqlArguments;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070042import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyale62d2bb2018-11-06 10:28:37 -080043import com.android.launcher3.icons.GraphicsUtils;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070044import com.android.launcher3.icons.LauncherIcons;
Sunny Goyalefb7e842018-10-04 15:11:00 -070045import com.android.launcher3.util.IntArray;
Adam Cohen091440a2015-03-18 14:16:05 -070046import com.android.launcher3.util.Thunk;
Sunny Goyal36b54222018-07-10 13:50:50 -070047
48import org.xmlpull.v1.XmlPullParser;
49import org.xmlpull.v1.XmlPullParserException;
50
Sunny Goyal0fe505b2014-08-06 09:55:36 -070051import java.io.IOException;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070052import java.util.Locale;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070053
54/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070055 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070056 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070057public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070058 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070059 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070060
61 /** Marker action used to discover a package which defines launcher customization */
62 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070063 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070064
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070065 /**
66 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
67 */
68 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
69 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070070 private static final String LAYOUT_RES = "default_layout";
71
72 static AutoInstallsLayout get(Context context, AppWidgetHost appWidgetHost,
73 LayoutParserCallback callback) {
74 Pair<String, Resources> customizationApkInfo = Utilities.findSystemApk(
75 ACTION_LAUNCHER_CUSTOMIZATION, context.getPackageManager());
76 if (customizationApkInfo == null) {
77 return null;
78 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070079 return get(context, customizationApkInfo.first, customizationApkInfo.second,
80 appWidgetHost, callback);
81 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -070082
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070083 static AutoInstallsLayout get(Context context, String pkg, Resources targetRes,
84 AppWidgetHost appWidgetHost, LayoutParserCallback callback) {
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,
Rajeev Kumar26453a22017-06-09 16:02:25 -070089 grid.numColumns, grid.numRows, grid.numHotseatIcons);
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,
Rajeev Kumar26453a22017-06-09 16:02:25 -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 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700111 return new AutoInstallsLayout(context, appWidgetHost, 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 Goyal0fe505b2014-08-06 09:55:36 -0700117 private static final String TAG_WORKSPACE = "workspace";
118 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";
122 private static final String TAG_SHORTCUT = "shortcut";
123 private static final String TAG_EXTRA = "extra";
124
125 private static final String ATTR_CONTAINER = "container";
126 private static final String ATTR_RANK = "rank";
127
128 private static final String ATTR_PACKAGE_NAME = "packageName";
129 private static final String ATTR_CLASS_NAME = "className";
130 private static final String ATTR_TITLE = "title";
131 private static final String ATTR_SCREEN = "screen";
Sunny Goyal96a09632015-12-16 11:32:54 -0800132
133 // x and y can be specified as negative integers, in which case -1 represents the
134 // last row / column, -2 represents the second last, and so on.
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700135 private static final String ATTR_X = "x";
136 private static final String ATTR_Y = "y";
Sunny Goyal96a09632015-12-16 11:32:54 -0800137
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700138 private static final String ATTR_SPAN_X = "spanX";
139 private static final String ATTR_SPAN_Y = "spanY";
140 private static final String ATTR_ICON = "icon";
141 private static final String ATTR_URL = "url";
142
Sunny Goyalb564efb2015-01-23 13:45:20 -0800143 // Attrs for "Include"
144 private static final String ATTR_WORKSPACE = "workspace";
145
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700146 // Style attrs -- "Extra"
147 private static final String ATTR_KEY = "key";
148 private static final String ATTR_VALUE = "value";
149
150 private static final String HOTSEAT_CONTAINER_NAME =
151 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
152
Adam Cohen091440a2015-03-18 14:16:05 -0700153 @Thunk final Context mContext;
154 @Thunk final AppWidgetHost mAppWidgetHost;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800155 protected final LayoutParserCallback mCallback;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700156
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700157 protected final PackageManager mPackageManager;
158 protected final Resources mSourceRes;
159 protected final int mLayoutId;
160
Sunny Goyalbb011da2016-06-15 15:42:29 -0700161 private final InvariantDeviceProfile mIdp;
Sunny Goyal96a09632015-12-16 11:32:54 -0800162 private final int mRowCount;
163 private final int mColumnCount;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700164
Sunny Goyalefb7e842018-10-04 15:11:00 -0700165 private final int[] mTemp = new int[2];
Adam Cohen091440a2015-03-18 14:16:05 -0700166 @Thunk final ContentValues mValues;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800167 protected final String mRootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700168
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700169 protected SQLiteDatabase mDb;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700170
171 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700172 LayoutParserCallback callback, Resources res,
173 int layoutId, String rootTag) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700174 mContext = context;
175 mAppWidgetHost = appWidgetHost;
176 mCallback = callback;
177
178 mPackageManager = context.getPackageManager();
179 mValues = new ContentValues();
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700180 mRootTag = rootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700181
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700182 mSourceRes = res;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700183 mLayoutId = layoutId;
Sunny Goyal96a09632015-12-16 11:32:54 -0800184
Sunny Goyal87f784c2017-01-11 10:48:34 -0800185 mIdp = LauncherAppState.getIDP(context);
Sunny Goyalbb011da2016-06-15 15:42:29 -0700186 mRowCount = mIdp.numRows;
187 mColumnCount = mIdp.numColumns;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700188 }
189
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700190 /**
191 * Loads the layout in the db and returns the number of entries added on the desktop.
192 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700193 public int loadLayout(SQLiteDatabase db, IntArray screenIds) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700194 mDb = db;
195 try {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700196 return parseLayout(mLayoutId, screenIds);
Sameer Padala8fd74832014-09-08 16:00:29 -0700197 } catch (Exception e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700198 Log.e(TAG, "Error parsing layout: " + e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700199 return -1;
200 }
201 }
202
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700203 /**
204 * Parses the layout and returns the number of elements added on the homescreen.
205 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700206 protected int parseLayout(int layoutId, IntArray screenIds)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700207 throws XmlPullParserException, IOException {
Sunny Goyal0d742312019-03-04 20:22:26 -0800208 XmlPullParser parser = mSourceRes.getXml(layoutId);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700209 beginDocument(parser, mRootTag);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700210 final int depth = parser.getDepth();
211 int type;
Rajeev Kumar26453a22017-06-09 16:02:25 -0700212 ArrayMap<String, TagParser> tagParserMap = getLayoutElementsMap();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700213 int count = 0;
214
215 while (((type = parser.next()) != XmlPullParser.END_TAG ||
216 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
217 if (type != XmlPullParser.START_TAG) {
218 continue;
219 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700220 count += parseAndAddNode(parser, tagParserMap, screenIds);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700221 }
222 return count;
223 }
224
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700225 /**
226 * Parses container and screenId attribute from the current tag, and puts it in the out.
227 * @param out array of size 2.
228 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800229 protected void parseContainerAndScreen(XmlPullParser parser, int[] out) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700230 if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) {
231 out[0] = Favorites.CONTAINER_HOTSEAT;
232 // Hack: hotseat items are stored using screen ids
Sunny Goyalefb7e842018-10-04 15:11:00 -0700233 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_RANK));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700234 } else {
235 out[0] = Favorites.CONTAINER_DESKTOP;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700236 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_SCREEN));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700237 }
238 }
239
240 /**
241 * Parses the current node and returns the number of elements added.
242 */
243 protected int parseAndAddNode(
Sunny Goyal0d742312019-03-04 20:22:26 -0800244 XmlPullParser parser, ArrayMap<String, TagParser> tagParserMap, IntArray screenIds)
Rajeev Kumar26453a22017-06-09 16:02:25 -0700245 throws XmlPullParserException, IOException {
Sunny Goyalb564efb2015-01-23 13:45:20 -0800246
247 if (TAG_INCLUDE.equals(parser.getName())) {
248 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
249 if (resId != 0) {
250 // recursively load some more favorites, why not?
251 return parseLayout(resId, screenIds);
252 } else {
253 return 0;
254 }
255 }
256
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700257 mValues.clear();
258 parseContainerAndScreen(parser, mTemp);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700259 final int container = mTemp[0];
260 final int screenId = mTemp[1];
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700261
262 mValues.put(Favorites.CONTAINER, container);
263 mValues.put(Favorites.SCREEN, screenId);
Sunny Goyal96a09632015-12-16 11:32:54 -0800264
265 mValues.put(Favorites.CELLX,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800266 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount));
Sunny Goyal96a09632015-12-16 11:32:54 -0800267 mValues.put(Favorites.CELLY,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800268 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700269
270 TagParser tagParser = tagParserMap.get(parser.getName());
271 if (tagParser == null) {
272 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
273 return 0;
274 }
Sunny Goyalefb7e842018-10-04 15:11:00 -0700275 int newElementId = tagParser.parseAndAdd(parser);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700276 if (newElementId >= 0) {
277 // Keep track of the set of screens which need to be added to the db.
278 if (!screenIds.contains(screenId) &&
279 container == Favorites.CONTAINER_DESKTOP) {
280 screenIds.add(screenId);
281 }
282 return 1;
283 }
284 return 0;
285 }
286
Sunny Goyalefb7e842018-10-04 15:11:00 -0700287 protected int addShortcut(String title, Intent intent, int type) {
288 int id = mCallback.generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700289 mValues.put(Favorites.INTENT, intent.toUri(0));
290 mValues.put(Favorites.TITLE, title);
291 mValues.put(Favorites.ITEM_TYPE, type);
292 mValues.put(Favorites.SPANX, 1);
293 mValues.put(Favorites.SPANY, 1);
294 mValues.put(Favorites._ID, id);
295 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
296 return -1;
297 } else {
298 return id;
299 }
300 }
301
Rajeev Kumar26453a22017-06-09 16:02:25 -0700302 protected ArrayMap<String, TagParser> getFolderElementsMap() {
303 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700304 parsers.put(TAG_APP_ICON, new AppShortcutParser());
305 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700306 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700307 return parsers;
308 }
309
Rajeev Kumar26453a22017-06-09 16:02:25 -0700310 protected ArrayMap<String, TagParser> getLayoutElementsMap() {
311 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700312 parsers.put(TAG_APP_ICON, new AppShortcutParser());
313 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
314 parsers.put(TAG_FOLDER, new FolderParser());
Sunny Goyal86df1382016-08-10 15:03:22 -0700315 parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
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
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700320 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700321 /**
322 * Parses the tag and adds to the db
323 * @return the id of the row added or -1;
324 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800325 int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700326 throws XmlPullParserException, IOException;
327 }
328
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700329 /**
330 * App shortcuts: required attributes packageName and className
331 */
332 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700333
334 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800335 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700336 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
337 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
338
339 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
340 ActivityInfo info;
341 try {
342 ComponentName cn;
343 try {
344 cn = new ComponentName(packageName, className);
345 info = mPackageManager.getActivityInfo(cn, 0);
346 } catch (PackageManager.NameNotFoundException nnfe) {
347 String[] packages = mPackageManager.currentToCanonicalPackageNames(
348 new String[] { packageName });
349 cn = new ComponentName(packages[0], className);
350 info = mPackageManager.getActivityInfo(cn, 0);
351 }
352 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
353 .addCategory(Intent.CATEGORY_LAUNCHER)
354 .setComponent(cn)
355 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
356 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
357
358 return addShortcut(info.loadLabel(mPackageManager).toString(),
359 intent, Favorites.ITEM_TYPE_APPLICATION);
360 } catch (PackageManager.NameNotFoundException e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700361 Log.e(TAG, "Favorite not found: " + packageName + "/" + className);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700362 }
363 return -1;
364 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700365 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700366 }
367 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700368
369 /**
370 * Helper method to allow extending the parser capabilities
371 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800372 protected int invalidPackageOrClass(XmlPullParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700373 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700374 return -1;
375 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700376 }
377
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700378 /**
379 * AutoInstall: required attributes packageName and className
380 */
381 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700382
383 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800384 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700385 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
386 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
387 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
388 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
389 return -1;
390 }
391
Mario Bertschlerc06af332017-03-28 12:23:22 -0700392 mValues.put(Favorites.RESTORED, ShortcutInfo.FLAG_AUTOINSTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700393 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
394 .addCategory(Intent.CATEGORY_LAUNCHER)
395 .setComponent(new ComponentName(packageName, className))
396 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
397 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
398 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
399 Favorites.ITEM_TYPE_APPLICATION);
400 }
401 }
402
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700403 /**
404 * Parses a web shortcut. Required attributes url, icon, title
405 */
406 protected class ShortcutParser implements TagParser {
407
408 private final Resources mIconRes;
409
410 public ShortcutParser(Resources iconRes) {
411 mIconRes = iconRes;
412 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700413
414 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800415 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700416 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
417 final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0);
418
419 if (titleResId == 0 || iconId == 0) {
420 if (LOGD) Log.d(TAG, "Ignoring shortcut");
421 return -1;
422 }
423
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700424 final Intent intent = parseIntent(parser);
425 if (intent == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700426 return -1;
427 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700428
429 Drawable icon = mIconRes.getDrawable(iconId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700430 if (icon == null) {
431 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon");
432 return -1;
433 }
434
Sunny Goyalad7ff442017-09-12 12:42:26 -0700435 // Auto installs should always support the current platform version.
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800436 LauncherIcons li = LauncherIcons.obtain(mContext);
Sunny Goyale62d2bb2018-11-06 10:28:37 -0800437 mValues.put(LauncherSettings.Favorites.ICON, GraphicsUtils.flattenBitmap(
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800438 li.createBadgedIconBitmap(icon, Process.myUserHandle(), VERSION.SDK_INT).icon));
439 li.recycle();
440
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700441 mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
442 mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));
443
444 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700445 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700446 return addShortcut(mSourceRes.getString(titleResId),
447 intent, Favorites.ITEM_TYPE_SHORTCUT);
448 }
449
Sunny Goyal0d742312019-03-04 20:22:26 -0800450 protected Intent parseIntent(XmlPullParser parser) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700451 final String url = getAttributeValue(parser, ATTR_URL);
452 if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) {
453 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url);
454 return null;
455 }
456 return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700457 }
458 }
459
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700460 /**
461 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
462 * Options child nodes: <extra key=... value=... />
Sunny Goyal86df1382016-08-10 15:03:22 -0700463 * It adds a pending widget which allows the widget to come later. If there are extras, those
464 * are passed to widget options during bind.
465 * The config activity for the widget (if present) is not shown, so any optional configurations
466 * should be passed as extras and the widget should support reading these widget options.
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700467 */
Sunny Goyal86df1382016-08-10 15:03:22 -0700468 protected class PendingWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700469
470 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800471 public int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700472 throws XmlPullParserException, IOException {
473 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
474 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
475 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700476 if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component");
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700477 return -1;
478 }
479
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700480 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
481 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
Sunny Goyal86df1382016-08-10 15:03:22 -0700482 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700483
484 // Read the extras
485 Bundle extras = new Bundle();
486 int widgetDepth = parser.getDepth();
487 int type;
488 while ((type = parser.next()) != XmlPullParser.END_TAG ||
489 parser.getDepth() > widgetDepth) {
490 if (type != XmlPullParser.START_TAG) {
491 continue;
492 }
493
494 if (TAG_EXTRA.equals(parser.getName())) {
495 String key = getAttributeValue(parser, ATTR_KEY);
496 String value = getAttributeValue(parser, ATTR_VALUE);
497 if (key != null && value != null) {
498 extras.putString(key, value);
499 } else {
500 throw new RuntimeException("Widget extras must have a key and value");
501 }
502 } else {
503 throw new RuntimeException("Widgets can contain only extras");
504 }
505 }
506
Sunny Goyal86df1382016-08-10 15:03:22 -0700507 return verifyAndInsert(new ComponentName(packageName, className), extras);
508 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700509
Sunny Goyalefb7e842018-10-04 15:11:00 -0700510 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700511 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
512 mValues.put(Favorites.RESTORED,
513 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID |
514 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY |
515 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
516 mValues.put(Favorites._ID, mCallback.generateNewItemId());
517 if (!extras.isEmpty()) {
518 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700519 }
Sunny Goyal86df1382016-08-10 15:03:22 -0700520
Sunny Goyalefb7e842018-10-04 15:11:00 -0700521 int insertedId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal86df1382016-08-10 15:03:22 -0700522 if (insertedId < 0) {
523 return -1;
524 } else {
525 return insertedId;
526 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700527 }
528 }
529
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700530 protected class FolderParser implements TagParser {
Rajeev Kumar26453a22017-06-09 16:02:25 -0700531 private final ArrayMap<String, TagParser> mFolderElements;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700532
533 public FolderParser() {
534 this(getFolderElementsMap());
535 }
536
Rajeev Kumar26453a22017-06-09 16:02:25 -0700537 public FolderParser(ArrayMap<String, TagParser> elements) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700538 mFolderElements = elements;
539 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700540
541 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800542 public int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700543 throws XmlPullParserException, IOException {
544 final String title;
545 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
546 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700547 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700548 } else {
549 title = mContext.getResources().getString(R.string.folder_name);
550 }
551
552 mValues.put(Favorites.TITLE, title);
553 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
554 mValues.put(Favorites.SPANX, 1);
555 mValues.put(Favorites.SPANY, 1);
556 mValues.put(Favorites._ID, mCallback.generateNewItemId());
Sunny Goyalefb7e842018-10-04 15:11:00 -0700557 int folderId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700558 if (folderId < 0) {
559 if (LOGD) Log.e(TAG, "Unable to add folder");
560 return -1;
561 }
562
563 final ContentValues myValues = new ContentValues(mValues);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700564 IntArray folderItems = new IntArray();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700565
566 int type;
567 int folderDepth = parser.getDepth();
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700568 int rank = 0;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700569 while ((type = parser.next()) != XmlPullParser.END_TAG ||
570 parser.getDepth() > folderDepth) {
571 if (type != XmlPullParser.START_TAG) {
572 continue;
573 }
574 mValues.clear();
575 mValues.put(Favorites.CONTAINER, folderId);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700576 mValues.put(Favorites.RANK, rank);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700577
578 TagParser tagParser = mFolderElements.get(parser.getName());
579 if (tagParser != null) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700580 final int id = tagParser.parseAndAdd(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700581 if (id >= 0) {
582 folderItems.add(id);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700583 rank++;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700584 }
585 } else {
586 throw new RuntimeException("Invalid folder item " + parser.getName());
587 }
588 }
589
Sunny Goyalefb7e842018-10-04 15:11:00 -0700590 int addedId = folderId;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700591
592 // We can only have folders with >= 2 items, so we need to remove the
593 // folder and clean up if less than 2 items were included, or some
594 // failed to add, and less than 2 were actually added
595 if (folderItems.size() < 2) {
596 // Delete the folder
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700597 Uri uri = Favorites.getContentUri(folderId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700598 SqlArguments args = new SqlArguments(uri, null, null);
599 mDb.delete(args.table, args.where, args.args);
600 addedId = -1;
601
602 // If we have a single item, promote it to where the folder
603 // would have been.
604 if (folderItems.size() == 1) {
605 final ContentValues childValues = new ContentValues();
606 copyInteger(myValues, childValues, Favorites.CONTAINER);
607 copyInteger(myValues, childValues, Favorites.SCREEN);
608 copyInteger(myValues, childValues, Favorites.CELLX);
609 copyInteger(myValues, childValues, Favorites.CELLY);
610
611 addedId = folderItems.get(0);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700612 mDb.update(Favorites.TABLE_NAME, childValues,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700613 Favorites._ID + "=" + addedId, null);
614 }
615 }
616 return addedId;
617 }
618 }
619
Rajeev Kumar26453a22017-06-09 16:02:25 -0700620 protected static void beginDocument(XmlPullParser parser, String firstElementName)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700621 throws XmlPullParserException, IOException {
622 int type;
623 while ((type = parser.next()) != XmlPullParser.START_TAG
624 && type != XmlPullParser.END_DOCUMENT);
625
626 if (type != XmlPullParser.START_TAG) {
627 throw new XmlPullParserException("No start tag found");
628 }
629
630 if (!parser.getName().equals(firstElementName)) {
631 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
632 ", expected " + firstElementName);
633 }
634 }
635
Sunny Goyal96a09632015-12-16 11:32:54 -0800636 private static String convertToDistanceFromEnd(String value, int endValue) {
637 if (!TextUtils.isEmpty(value)) {
638 int x = Integer.parseInt(value);
639 if (x < 0) {
640 return Integer.toString(endValue + x);
641 }
642 }
643 return value;
644 }
645
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700646 /**
647 * Return attribute value, attempting launcher-specific namespace first
648 * before falling back to anonymous attribute.
649 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800650 protected static String getAttributeValue(XmlPullParser parser, String attribute) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700651 String value = parser.getAttributeValue(
652 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
653 if (value == null) {
654 value = parser.getAttributeValue(null, attribute);
655 }
656 return value;
657 }
658
659 /**
660 * Return attribute resource value, attempting launcher-specific namespace
661 * first before falling back to anonymous attribute.
662 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800663 protected static int getAttributeResourceValue(XmlPullParser parser, String attribute,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700664 int defaultValue) {
Sunny Goyal0d742312019-03-04 20:22:26 -0800665 AttributeSet attrs = Xml.asAttributeSet(parser);
666 int value = attrs.getAttributeResourceValue(
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700667 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
668 defaultValue);
669 if (value == defaultValue) {
Sunny Goyal0d742312019-03-04 20:22:26 -0800670 value = attrs.getAttributeResourceValue(null, attribute, defaultValue);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700671 }
672 return value;
673 }
674
Rajeev Kumar26453a22017-06-09 16:02:25 -0700675 public interface LayoutParserCallback {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700676 int generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700677
Sunny Goyalefb7e842018-10-04 15:11:00 -0700678 int insertAndCheck(SQLiteDatabase db, ContentValues values);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700679 }
680
Adam Cohen091440a2015-03-18 14:16:05 -0700681 @Thunk static void copyInteger(ContentValues from, ContentValues to, String key) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700682 to.put(key, from.getAsInteger(key));
683 }
684}