blob: c7c1d6ac0e222a365935eabedd6da3981db7b3ff [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;
27import android.content.res.XmlResourceParser;
28import android.database.sqlite.SQLiteDatabase;
29import android.graphics.drawable.Drawable;
30import android.net.Uri;
Sunny Goyal179249d2017-12-19 16:49:24 -080031import android.os.Build.VERSION;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070032import android.os.Bundle;
Sunny Goyalad7ff442017-09-12 12:42:26 -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 Goyal0fe505b2014-08-06 09:55:36 -070036import android.util.Log;
37import android.util.Pair;
38import android.util.Patterns;
Sunny Goyal36b54222018-07-10 13:50:50 -070039
Sunny Goyal0fe505b2014-08-06 09:55:36 -070040import com.android.launcher3.LauncherProvider.SqlArguments;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070041import com.android.launcher3.LauncherSettings.Favorites;
Hyunyoung Song48cb7bc2018-09-25 17:03:34 -070042import com.android.launcher3.icons.LauncherIcons;
Sunny Goyalefb7e842018-10-04 15:11:00 -070043import com.android.launcher3.util.IntArray;
Adam Cohen091440a2015-03-18 14:16:05 -070044import com.android.launcher3.util.Thunk;
Sunny Goyal36b54222018-07-10 13:50:50 -070045
46import org.xmlpull.v1.XmlPullParser;
47import org.xmlpull.v1.XmlPullParserException;
48
Sunny Goyal0fe505b2014-08-06 09:55:36 -070049import java.io.IOException;
50import java.util.ArrayList;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070051import java.util.Locale;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070052
53/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070054 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070055 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070056public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070057 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070058 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070059
60 /** Marker action used to discover a package which defines launcher customization */
61 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070062 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070063
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070064 /**
65 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
66 */
67 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
68 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070069 private static final String LAYOUT_RES = "default_layout";
70
71 static AutoInstallsLayout get(Context context, AppWidgetHost appWidgetHost,
72 LayoutParserCallback callback) {
73 Pair<String, Resources> customizationApkInfo = Utilities.findSystemApk(
74 ACTION_LAUNCHER_CUSTOMIZATION, context.getPackageManager());
75 if (customizationApkInfo == null) {
76 return null;
77 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070078 return get(context, customizationApkInfo.first, customizationApkInfo.second,
79 appWidgetHost, callback);
80 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -070081
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070082 static AutoInstallsLayout get(Context context, String pkg, Resources targetRes,
83 AppWidgetHost appWidgetHost, LayoutParserCallback callback) {
Sunny Goyal87f784c2017-01-11 10:48:34 -080084 InvariantDeviceProfile grid = LauncherAppState.getIDP(context);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070085
86 // Try with grid size and hotseat count
87 String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
Rajeev Kumar26453a22017-06-09 16:02:25 -070088 grid.numColumns, grid.numRows, grid.numHotseatIcons);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070089 int layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
90
91 // Try with only grid size
92 if (layoutId == 0) {
93 Log.d(TAG, "Formatted layout: " + layoutName
94 + " not found. Trying layout without hosteat");
95 layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
Rajeev Kumar26453a22017-06-09 16:02:25 -070096 grid.numColumns, grid.numRows);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070097 layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
98 }
99
100 // Try the default layout
101 if (layoutId == 0) {
102 Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
103 layoutId = targetRes.getIdentifier(LAYOUT_RES, "xml", pkg);
104 }
105
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700106 if (layoutId == 0) {
107 Log.e(TAG, "Layout definition not found in package: " + pkg);
108 return null;
109 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700110 return new AutoInstallsLayout(context, appWidgetHost, callback, targetRes, layoutId,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700111 TAG_WORKSPACE);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700112 }
113
114 // Object Tags
Sunny Goyalb564efb2015-01-23 13:45:20 -0800115 private static final String TAG_INCLUDE = "include";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700116 private static final String TAG_WORKSPACE = "workspace";
117 private static final String TAG_APP_ICON = "appicon";
118 private static final String TAG_AUTO_INSTALL = "autoinstall";
119 private static final String TAG_FOLDER = "folder";
120 private static final String TAG_APPWIDGET = "appwidget";
121 private static final String TAG_SHORTCUT = "shortcut";
122 private static final String TAG_EXTRA = "extra";
123
124 private static final String ATTR_CONTAINER = "container";
125 private static final String ATTR_RANK = "rank";
126
127 private static final String ATTR_PACKAGE_NAME = "packageName";
128 private static final String ATTR_CLASS_NAME = "className";
129 private static final String ATTR_TITLE = "title";
130 private static final String ATTR_SCREEN = "screen";
Sunny Goyal96a09632015-12-16 11:32:54 -0800131
132 // x and y can be specified as negative integers, in which case -1 represents the
133 // last row / column, -2 represents the second last, and so on.
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700134 private static final String ATTR_X = "x";
135 private static final String ATTR_Y = "y";
Sunny Goyal96a09632015-12-16 11:32:54 -0800136
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700137 private static final String ATTR_SPAN_X = "spanX";
138 private static final String ATTR_SPAN_Y = "spanY";
139 private static final String ATTR_ICON = "icon";
140 private static final String ATTR_URL = "url";
141
Sunny Goyalb564efb2015-01-23 13:45:20 -0800142 // Attrs for "Include"
143 private static final String ATTR_WORKSPACE = "workspace";
144
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700145 // Style attrs -- "Extra"
146 private static final String ATTR_KEY = "key";
147 private static final String ATTR_VALUE = "value";
148
149 private static final String HOTSEAT_CONTAINER_NAME =
150 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
151
152 private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
153 "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE";
154
Adam Cohen091440a2015-03-18 14:16:05 -0700155 @Thunk final Context mContext;
156 @Thunk final AppWidgetHost mAppWidgetHost;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800157 protected final LayoutParserCallback mCallback;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700158
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700159 protected final PackageManager mPackageManager;
160 protected final Resources mSourceRes;
161 protected final int mLayoutId;
162
Sunny Goyalbb011da2016-06-15 15:42:29 -0700163 private final InvariantDeviceProfile mIdp;
Sunny Goyal96a09632015-12-16 11:32:54 -0800164 private final int mRowCount;
165 private final int mColumnCount;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700166
Sunny Goyalefb7e842018-10-04 15:11:00 -0700167 private final int[] mTemp = new int[2];
Adam Cohen091440a2015-03-18 14:16:05 -0700168 @Thunk final ContentValues mValues;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800169 protected final String mRootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700170
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700171 protected SQLiteDatabase mDb;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700172
173 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700174 LayoutParserCallback callback, Resources res,
175 int layoutId, String rootTag) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700176 mContext = context;
177 mAppWidgetHost = appWidgetHost;
178 mCallback = callback;
179
180 mPackageManager = context.getPackageManager();
181 mValues = new ContentValues();
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700182 mRootTag = rootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700183
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700184 mSourceRes = res;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700185 mLayoutId = layoutId;
Sunny Goyal96a09632015-12-16 11:32:54 -0800186
Sunny Goyal87f784c2017-01-11 10:48:34 -0800187 mIdp = LauncherAppState.getIDP(context);
Sunny Goyalbb011da2016-06-15 15:42:29 -0700188 mRowCount = mIdp.numRows;
189 mColumnCount = mIdp.numColumns;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700190 }
191
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700192 /**
193 * Loads the layout in the db and returns the number of entries added on the desktop.
194 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700195 public int loadLayout(SQLiteDatabase db, IntArray screenIds) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700196 mDb = db;
197 try {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700198 return parseLayout(mLayoutId, screenIds);
Sameer Padala8fd74832014-09-08 16:00:29 -0700199 } catch (Exception e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700200 Log.e(TAG, "Error parsing layout: " + e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700201 return -1;
202 }
203 }
204
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700205 /**
206 * Parses the layout and returns the number of elements added on the homescreen.
207 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700208 protected int parseLayout(int layoutId, IntArray screenIds)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700209 throws XmlPullParserException, IOException {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700210 XmlResourceParser parser = mSourceRes.getXml(layoutId);
211 beginDocument(parser, mRootTag);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700212 final int depth = parser.getDepth();
213 int type;
Rajeev Kumar26453a22017-06-09 16:02:25 -0700214 ArrayMap<String, TagParser> tagParserMap = getLayoutElementsMap();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700215 int count = 0;
216
217 while (((type = parser.next()) != XmlPullParser.END_TAG ||
218 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
219 if (type != XmlPullParser.START_TAG) {
220 continue;
221 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700222 count += parseAndAddNode(parser, tagParserMap, screenIds);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700223 }
224 return count;
225 }
226
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700227 /**
228 * Parses container and screenId attribute from the current tag, and puts it in the out.
229 * @param out array of size 2.
230 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700231 protected void parseContainerAndScreen(XmlResourceParser parser, int[] out) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700232 if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) {
233 out[0] = Favorites.CONTAINER_HOTSEAT;
234 // Hack: hotseat items are stored using screen ids
Sunny Goyalefb7e842018-10-04 15:11:00 -0700235 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_RANK));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700236 } else {
237 out[0] = Favorites.CONTAINER_DESKTOP;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700238 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_SCREEN));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700239 }
240 }
241
242 /**
243 * Parses the current node and returns the number of elements added.
244 */
245 protected int parseAndAddNode(
Sunny Goyalefb7e842018-10-04 15:11:00 -0700246 XmlResourceParser parser, ArrayMap<String, TagParser> tagParserMap, IntArray screenIds)
Rajeev Kumar26453a22017-06-09 16:02:25 -0700247 throws XmlPullParserException, IOException {
Sunny Goyalb564efb2015-01-23 13:45:20 -0800248
249 if (TAG_INCLUDE.equals(parser.getName())) {
250 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
251 if (resId != 0) {
252 // recursively load some more favorites, why not?
253 return parseLayout(resId, screenIds);
254 } else {
255 return 0;
256 }
257 }
258
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700259 mValues.clear();
260 parseContainerAndScreen(parser, mTemp);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700261 final int container = mTemp[0];
262 final int screenId = mTemp[1];
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700263
264 mValues.put(Favorites.CONTAINER, container);
265 mValues.put(Favorites.SCREEN, screenId);
Sunny Goyal96a09632015-12-16 11:32:54 -0800266
267 mValues.put(Favorites.CELLX,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800268 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount));
Sunny Goyal96a09632015-12-16 11:32:54 -0800269 mValues.put(Favorites.CELLY,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800270 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700271
272 TagParser tagParser = tagParserMap.get(parser.getName());
273 if (tagParser == null) {
274 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
275 return 0;
276 }
Sunny Goyalefb7e842018-10-04 15:11:00 -0700277 int newElementId = tagParser.parseAndAdd(parser);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700278 if (newElementId >= 0) {
279 // Keep track of the set of screens which need to be added to the db.
280 if (!screenIds.contains(screenId) &&
281 container == Favorites.CONTAINER_DESKTOP) {
282 screenIds.add(screenId);
283 }
284 return 1;
285 }
286 return 0;
287 }
288
Sunny Goyalefb7e842018-10-04 15:11:00 -0700289 protected int addShortcut(String title, Intent intent, int type) {
290 int id = mCallback.generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700291 mValues.put(Favorites.INTENT, intent.toUri(0));
292 mValues.put(Favorites.TITLE, title);
293 mValues.put(Favorites.ITEM_TYPE, type);
294 mValues.put(Favorites.SPANX, 1);
295 mValues.put(Favorites.SPANY, 1);
296 mValues.put(Favorites._ID, id);
297 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
298 return -1;
299 } else {
300 return id;
301 }
302 }
303
Rajeev Kumar26453a22017-06-09 16:02:25 -0700304 protected ArrayMap<String, TagParser> getFolderElementsMap() {
305 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700306 parsers.put(TAG_APP_ICON, new AppShortcutParser());
307 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700308 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700309 return parsers;
310 }
311
Rajeev Kumar26453a22017-06-09 16:02:25 -0700312 protected ArrayMap<String, TagParser> getLayoutElementsMap() {
313 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700314 parsers.put(TAG_APP_ICON, new AppShortcutParser());
315 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
316 parsers.put(TAG_FOLDER, new FolderParser());
Sunny Goyal86df1382016-08-10 15:03:22 -0700317 parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
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
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700322 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700323 /**
324 * Parses the tag and adds to the db
325 * @return the id of the row added or -1;
326 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700327 int parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700328 throws XmlPullParserException, IOException;
329 }
330
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700331 /**
332 * App shortcuts: required attributes packageName and className
333 */
334 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700335
336 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -0700337 public int parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700338 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
339 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
340
341 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
342 ActivityInfo info;
343 try {
344 ComponentName cn;
345 try {
346 cn = new ComponentName(packageName, className);
347 info = mPackageManager.getActivityInfo(cn, 0);
348 } catch (PackageManager.NameNotFoundException nnfe) {
349 String[] packages = mPackageManager.currentToCanonicalPackageNames(
350 new String[] { packageName });
351 cn = new ComponentName(packages[0], className);
352 info = mPackageManager.getActivityInfo(cn, 0);
353 }
354 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
355 .addCategory(Intent.CATEGORY_LAUNCHER)
356 .setComponent(cn)
357 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
358 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
359
360 return addShortcut(info.loadLabel(mPackageManager).toString(),
361 intent, Favorites.ITEM_TYPE_APPLICATION);
362 } catch (PackageManager.NameNotFoundException e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700363 Log.e(TAG, "Favorite not found: " + packageName + "/" + className);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700364 }
365 return -1;
366 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700367 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700368 }
369 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700370
371 /**
372 * Helper method to allow extending the parser capabilities
373 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700374 protected int invalidPackageOrClass(XmlResourceParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700375 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700376 return -1;
377 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700378 }
379
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700380 /**
381 * AutoInstall: required attributes packageName and className
382 */
383 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700384
385 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -0700386 public int parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700387 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
388 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
389 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
390 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
391 return -1;
392 }
393
Mario Bertschlerc06af332017-03-28 12:23:22 -0700394 mValues.put(Favorites.RESTORED, ShortcutInfo.FLAG_AUTOINSTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700395 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
396 .addCategory(Intent.CATEGORY_LAUNCHER)
397 .setComponent(new ComponentName(packageName, className))
398 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
399 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
400 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
401 Favorites.ITEM_TYPE_APPLICATION);
402 }
403 }
404
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700405 /**
406 * Parses a web shortcut. Required attributes url, icon, title
407 */
408 protected class ShortcutParser implements TagParser {
409
410 private final Resources mIconRes;
411
412 public ShortcutParser(Resources iconRes) {
413 mIconRes = iconRes;
414 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700415
416 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -0700417 public int parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700418 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
419 final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0);
420
421 if (titleResId == 0 || iconId == 0) {
422 if (LOGD) Log.d(TAG, "Ignoring shortcut");
423 return -1;
424 }
425
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700426 final Intent intent = parseIntent(parser);
427 if (intent == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700428 return -1;
429 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700430
431 Drawable icon = mIconRes.getDrawable(iconId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700432 if (icon == null) {
433 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon");
434 return -1;
435 }
436
Sunny Goyalad7ff442017-09-12 12:42:26 -0700437 // Auto installs should always support the current platform version.
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800438 LauncherIcons li = LauncherIcons.obtain(mContext);
Sunny Goyalad7ff442017-09-12 12:42:26 -0700439 mValues.put(LauncherSettings.Favorites.ICON, Utilities.flattenBitmap(
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800440 li.createBadgedIconBitmap(icon, Process.myUserHandle(), VERSION.SDK_INT).icon));
441 li.recycle();
442
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700443 mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
444 mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));
445
446 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700447 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700448 return addShortcut(mSourceRes.getString(titleResId),
449 intent, Favorites.ITEM_TYPE_SHORTCUT);
450 }
451
452 protected Intent parseIntent(XmlResourceParser parser) {
453 final String url = getAttributeValue(parser, ATTR_URL);
454 if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) {
455 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url);
456 return null;
457 }
458 return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700459 }
460 }
461
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700462 /**
463 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
464 * Options child nodes: <extra key=... value=... />
Sunny Goyal86df1382016-08-10 15:03:22 -0700465 * It adds a pending widget which allows the widget to come later. If there are extras, those
466 * are passed to widget options during bind.
467 * The config activity for the widget (if present) is not shown, so any optional configurations
468 * should be passed as extras and the widget should support reading these widget options.
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700469 */
Sunny Goyal86df1382016-08-10 15:03:22 -0700470 protected class PendingWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700471
472 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -0700473 public int parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700474 throws XmlPullParserException, IOException {
475 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
476 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
477 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
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 }
508
Sunny Goyal86df1382016-08-10 15:03:22 -0700509 return verifyAndInsert(new ComponentName(packageName, className), extras);
510 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700511
Sunny Goyalefb7e842018-10-04 15:11:00 -0700512 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700513 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
514 mValues.put(Favorites.RESTORED,
515 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID |
516 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY |
517 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
518 mValues.put(Favorites._ID, mCallback.generateNewItemId());
519 if (!extras.isEmpty()) {
520 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700521 }
Sunny Goyal86df1382016-08-10 15:03:22 -0700522
Sunny Goyalefb7e842018-10-04 15:11:00 -0700523 int insertedId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal86df1382016-08-10 15:03:22 -0700524 if (insertedId < 0) {
525 return -1;
526 } else {
527 return insertedId;
528 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700529 }
530 }
531
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700532 protected class FolderParser implements TagParser {
Rajeev Kumar26453a22017-06-09 16:02:25 -0700533 private final ArrayMap<String, TagParser> mFolderElements;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700534
535 public FolderParser() {
536 this(getFolderElementsMap());
537 }
538
Rajeev Kumar26453a22017-06-09 16:02:25 -0700539 public FolderParser(ArrayMap<String, TagParser> elements) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700540 mFolderElements = elements;
541 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700542
543 @Override
Sunny Goyalefb7e842018-10-04 15:11:00 -0700544 public int parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700545 throws XmlPullParserException, IOException {
546 final String title;
547 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
548 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700549 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700550 } else {
551 title = mContext.getResources().getString(R.string.folder_name);
552 }
553
554 mValues.put(Favorites.TITLE, title);
555 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
556 mValues.put(Favorites.SPANX, 1);
557 mValues.put(Favorites.SPANY, 1);
558 mValues.put(Favorites._ID, mCallback.generateNewItemId());
Sunny Goyalefb7e842018-10-04 15:11:00 -0700559 int folderId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700560 if (folderId < 0) {
561 if (LOGD) Log.e(TAG, "Unable to add folder");
562 return -1;
563 }
564
565 final ContentValues myValues = new ContentValues(mValues);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700566 IntArray folderItems = new IntArray();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700567
568 int type;
569 int folderDepth = parser.getDepth();
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700570 int rank = 0;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700571 while ((type = parser.next()) != XmlPullParser.END_TAG ||
572 parser.getDepth() > folderDepth) {
573 if (type != XmlPullParser.START_TAG) {
574 continue;
575 }
576 mValues.clear();
577 mValues.put(Favorites.CONTAINER, folderId);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700578 mValues.put(Favorites.RANK, rank);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700579
580 TagParser tagParser = mFolderElements.get(parser.getName());
581 if (tagParser != null) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700582 final int id = tagParser.parseAndAdd(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700583 if (id >= 0) {
584 folderItems.add(id);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700585 rank++;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700586 }
587 } else {
588 throw new RuntimeException("Invalid folder item " + parser.getName());
589 }
590 }
591
Sunny Goyalefb7e842018-10-04 15:11:00 -0700592 int addedId = folderId;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700593
594 // We can only have folders with >= 2 items, so we need to remove the
595 // folder and clean up if less than 2 items were included, or some
596 // failed to add, and less than 2 were actually added
597 if (folderItems.size() < 2) {
598 // Delete the folder
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700599 Uri uri = Favorites.getContentUri(folderId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700600 SqlArguments args = new SqlArguments(uri, null, null);
601 mDb.delete(args.table, args.where, args.args);
602 addedId = -1;
603
604 // If we have a single item, promote it to where the folder
605 // would have been.
606 if (folderItems.size() == 1) {
607 final ContentValues childValues = new ContentValues();
608 copyInteger(myValues, childValues, Favorites.CONTAINER);
609 copyInteger(myValues, childValues, Favorites.SCREEN);
610 copyInteger(myValues, childValues, Favorites.CELLX);
611 copyInteger(myValues, childValues, Favorites.CELLY);
612
613 addedId = folderItems.get(0);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700614 mDb.update(Favorites.TABLE_NAME, childValues,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700615 Favorites._ID + "=" + addedId, null);
616 }
617 }
618 return addedId;
619 }
620 }
621
Rajeev Kumar26453a22017-06-09 16:02:25 -0700622 protected static void beginDocument(XmlPullParser parser, String firstElementName)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700623 throws XmlPullParserException, IOException {
624 int type;
625 while ((type = parser.next()) != XmlPullParser.START_TAG
626 && type != XmlPullParser.END_DOCUMENT);
627
628 if (type != XmlPullParser.START_TAG) {
629 throw new XmlPullParserException("No start tag found");
630 }
631
632 if (!parser.getName().equals(firstElementName)) {
633 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
634 ", expected " + firstElementName);
635 }
636 }
637
Sunny Goyal96a09632015-12-16 11:32:54 -0800638 private static String convertToDistanceFromEnd(String value, int endValue) {
639 if (!TextUtils.isEmpty(value)) {
640 int x = Integer.parseInt(value);
641 if (x < 0) {
642 return Integer.toString(endValue + x);
643 }
644 }
645 return value;
646 }
647
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700648 /**
649 * Return attribute value, attempting launcher-specific namespace first
650 * before falling back to anonymous attribute.
651 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700652 protected static String getAttributeValue(XmlResourceParser parser, String attribute) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700653 String value = parser.getAttributeValue(
654 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
655 if (value == null) {
656 value = parser.getAttributeValue(null, attribute);
657 }
658 return value;
659 }
660
661 /**
662 * Return attribute resource value, attempting launcher-specific namespace
663 * first before falling back to anonymous attribute.
664 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700665 protected static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700666 int defaultValue) {
667 int value = parser.getAttributeResourceValue(
668 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
669 defaultValue);
670 if (value == defaultValue) {
671 value = parser.getAttributeResourceValue(null, attribute, defaultValue);
672 }
673 return value;
674 }
675
Rajeev Kumar26453a22017-06-09 16:02:25 -0700676 public interface LayoutParserCallback {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700677 int generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700678
Sunny Goyalefb7e842018-10-04 15:11:00 -0700679 int insertAndCheck(SQLiteDatabase db, ContentValues values);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700680 }
681
Adam Cohen091440a2015-03-18 14:16:05 -0700682 @Thunk static void copyInteger(ContentValues from, ContentValues to, String key) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700683 to.put(key, from.getAsInteger(key));
684 }
685}