blob: 469b8bbcea54a30dfd6eece1c635a7f1fc43aad9 [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 Goyalad7ff442017-09-12 12:42:26 -070031import android.os.Build;
Sunny Goyal179249d2017-12-19 16:49:24 -080032import android.os.Build.VERSION;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070033import android.os.Bundle;
Sunny Goyalad7ff442017-09-12 12:42:26 -070034import android.os.Process;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070035import android.text.TextUtils;
Rajeev Kumar26453a22017-06-09 16:02:25 -070036import android.util.ArrayMap;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070037import android.util.Log;
38import android.util.Pair;
39import android.util.Patterns;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070040import com.android.launcher3.LauncherProvider.SqlArguments;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070041import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalbb011da2016-06-15 15:42:29 -070042import com.android.launcher3.config.FeatureFlags;
Sunny Goyal10629b02016-09-01 12:50:11 -070043import com.android.launcher3.graphics.LauncherIcons;
Adam Cohen091440a2015-03-18 14:16:05 -070044import com.android.launcher3.util.Thunk;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070045import java.io.IOException;
46import java.util.ArrayList;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070047import java.util.Locale;
Rajeev Kumar26453a22017-06-09 16:02:25 -070048import org.xmlpull.v1.XmlPullParser;
49import org.xmlpull.v1.XmlPullParserException;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070050
51/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070052 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070053 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070054public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070055 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070056 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070057
58 /** Marker action used to discover a package which defines launcher customization */
59 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070060 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070061
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070062 /**
63 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
64 */
65 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
66 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070067 private static final String LAYOUT_RES = "default_layout";
68
69 static AutoInstallsLayout get(Context context, AppWidgetHost appWidgetHost,
70 LayoutParserCallback callback) {
71 Pair<String, Resources> customizationApkInfo = Utilities.findSystemApk(
72 ACTION_LAUNCHER_CUSTOMIZATION, context.getPackageManager());
73 if (customizationApkInfo == null) {
74 return null;
75 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070076 return get(context, customizationApkInfo.first, customizationApkInfo.second,
77 appWidgetHost, callback);
78 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -070079
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070080 static AutoInstallsLayout get(Context context, String pkg, Resources targetRes,
81 AppWidgetHost appWidgetHost, LayoutParserCallback callback) {
Sunny Goyal87f784c2017-01-11 10:48:34 -080082 InvariantDeviceProfile grid = LauncherAppState.getIDP(context);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070083
84 // Try with grid size and hotseat count
85 String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
Rajeev Kumar26453a22017-06-09 16:02:25 -070086 grid.numColumns, grid.numRows, grid.numHotseatIcons);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070087 int layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
88
89 // Try with only grid size
90 if (layoutId == 0) {
91 Log.d(TAG, "Formatted layout: " + layoutName
92 + " not found. Trying layout without hosteat");
93 layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
Rajeev Kumar26453a22017-06-09 16:02:25 -070094 grid.numColumns, grid.numRows);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070095 layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
96 }
97
98 // Try the default layout
99 if (layoutId == 0) {
100 Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
101 layoutId = targetRes.getIdentifier(LAYOUT_RES, "xml", pkg);
102 }
103
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700104 if (layoutId == 0) {
105 Log.e(TAG, "Layout definition not found in package: " + pkg);
106 return null;
107 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700108 return new AutoInstallsLayout(context, appWidgetHost, callback, targetRes, layoutId,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700109 TAG_WORKSPACE);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700110 }
111
112 // Object Tags
Sunny Goyalb564efb2015-01-23 13:45:20 -0800113 private static final String TAG_INCLUDE = "include";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700114 private static final String TAG_WORKSPACE = "workspace";
115 private static final String TAG_APP_ICON = "appicon";
116 private static final String TAG_AUTO_INSTALL = "autoinstall";
117 private static final String TAG_FOLDER = "folder";
118 private static final String TAG_APPWIDGET = "appwidget";
119 private static final String TAG_SHORTCUT = "shortcut";
120 private static final String TAG_EXTRA = "extra";
121
122 private static final String ATTR_CONTAINER = "container";
123 private static final String ATTR_RANK = "rank";
124
125 private static final String ATTR_PACKAGE_NAME = "packageName";
126 private static final String ATTR_CLASS_NAME = "className";
127 private static final String ATTR_TITLE = "title";
128 private static final String ATTR_SCREEN = "screen";
Sunny Goyal96a09632015-12-16 11:32:54 -0800129
130 // x and y can be specified as negative integers, in which case -1 represents the
131 // last row / column, -2 represents the second last, and so on.
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700132 private static final String ATTR_X = "x";
133 private static final String ATTR_Y = "y";
Sunny Goyal96a09632015-12-16 11:32:54 -0800134
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700135 private static final String ATTR_SPAN_X = "spanX";
136 private static final String ATTR_SPAN_Y = "spanY";
137 private static final String ATTR_ICON = "icon";
138 private static final String ATTR_URL = "url";
139
Sunny Goyalb564efb2015-01-23 13:45:20 -0800140 // Attrs for "Include"
141 private static final String ATTR_WORKSPACE = "workspace";
142
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700143 // Style attrs -- "Extra"
144 private static final String ATTR_KEY = "key";
145 private static final String ATTR_VALUE = "value";
146
147 private static final String HOTSEAT_CONTAINER_NAME =
148 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
149
150 private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
151 "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE";
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
165 private final long[] mTemp = new long[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 Goyal0fe505b2014-08-06 09:55:36 -0700193 public int loadLayout(SQLiteDatabase db, ArrayList<Long> screenIds) {
194 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 */
206 protected int parseLayout(int layoutId, ArrayList<Long> screenIds)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700207 throws XmlPullParserException, IOException {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700208 XmlResourceParser parser = mSourceRes.getXml(layoutId);
209 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 */
229 protected void parseContainerAndScreen(XmlResourceParser parser, long[] out) {
230 if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) {
231 out[0] = Favorites.CONTAINER_HOTSEAT;
232 // Hack: hotseat items are stored using screen ids
233 long rank = Long.parseLong(getAttributeValue(parser, ATTR_RANK));
Sunny Goyalbb011da2016-06-15 15:42:29 -0700234 out[1] = (FeatureFlags.NO_ALL_APPS_ICON || rank < mIdp.getAllAppsButtonRank())
235 ? rank : (rank + 1);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700236 } else {
237 out[0] = Favorites.CONTAINER_DESKTOP;
238 out[1] = Long.parseLong(getAttributeValue(parser, ATTR_SCREEN));
239 }
240 }
241
242 /**
243 * Parses the current node and returns the number of elements added.
244 */
245 protected int parseAndAddNode(
Rajeev Kumar26453a22017-06-09 16:02:25 -0700246 XmlResourceParser parser,
247 ArrayMap<String, TagParser> tagParserMap,
248 ArrayList<Long> screenIds)
249 throws XmlPullParserException, IOException {
Sunny Goyalb564efb2015-01-23 13:45:20 -0800250
251 if (TAG_INCLUDE.equals(parser.getName())) {
252 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
253 if (resId != 0) {
254 // recursively load some more favorites, why not?
255 return parseLayout(resId, screenIds);
256 } else {
257 return 0;
258 }
259 }
260
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700261 mValues.clear();
262 parseContainerAndScreen(parser, mTemp);
263 final long container = mTemp[0];
264 final long screenId = mTemp[1];
265
266 mValues.put(Favorites.CONTAINER, container);
267 mValues.put(Favorites.SCREEN, screenId);
Sunny Goyal96a09632015-12-16 11:32:54 -0800268
269 mValues.put(Favorites.CELLX,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800270 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount));
Sunny Goyal96a09632015-12-16 11:32:54 -0800271 mValues.put(Favorites.CELLY,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800272 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700273
274 TagParser tagParser = tagParserMap.get(parser.getName());
275 if (tagParser == null) {
276 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
277 return 0;
278 }
279 long newElementId = tagParser.parseAndAdd(parser);
280 if (newElementId >= 0) {
281 // Keep track of the set of screens which need to be added to the db.
282 if (!screenIds.contains(screenId) &&
283 container == Favorites.CONTAINER_DESKTOP) {
284 screenIds.add(screenId);
285 }
286 return 1;
287 }
288 return 0;
289 }
290
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700291 protected long addShortcut(String title, Intent intent, int type) {
292 long id = mCallback.generateNewItemId();
293 mValues.put(Favorites.INTENT, intent.toUri(0));
294 mValues.put(Favorites.TITLE, title);
295 mValues.put(Favorites.ITEM_TYPE, type);
296 mValues.put(Favorites.SPANX, 1);
297 mValues.put(Favorites.SPANY, 1);
298 mValues.put(Favorites._ID, id);
299 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
300 return -1;
301 } else {
302 return id;
303 }
304 }
305
Rajeev Kumar26453a22017-06-09 16:02:25 -0700306 protected ArrayMap<String, TagParser> getFolderElementsMap() {
307 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700308 parsers.put(TAG_APP_ICON, new AppShortcutParser());
309 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700310 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700311 return parsers;
312 }
313
Rajeev Kumar26453a22017-06-09 16:02:25 -0700314 protected ArrayMap<String, TagParser> getLayoutElementsMap() {
315 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700316 parsers.put(TAG_APP_ICON, new AppShortcutParser());
317 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
318 parsers.put(TAG_FOLDER, new FolderParser());
Sunny Goyal86df1382016-08-10 15:03:22 -0700319 parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700320 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700321 return parsers;
322 }
323
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700324 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700325 /**
326 * Parses the tag and adds to the db
327 * @return the id of the row added or -1;
328 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700329 long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700330 throws XmlPullParserException, IOException;
331 }
332
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700333 /**
334 * App shortcuts: required attributes packageName and className
335 */
336 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700337
338 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700339 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700340 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
341 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
342
343 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
344 ActivityInfo info;
345 try {
346 ComponentName cn;
347 try {
348 cn = new ComponentName(packageName, className);
349 info = mPackageManager.getActivityInfo(cn, 0);
350 } catch (PackageManager.NameNotFoundException nnfe) {
351 String[] packages = mPackageManager.currentToCanonicalPackageNames(
352 new String[] { packageName });
353 cn = new ComponentName(packages[0], className);
354 info = mPackageManager.getActivityInfo(cn, 0);
355 }
356 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
357 .addCategory(Intent.CATEGORY_LAUNCHER)
358 .setComponent(cn)
359 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
360 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
361
362 return addShortcut(info.loadLabel(mPackageManager).toString(),
363 intent, Favorites.ITEM_TYPE_APPLICATION);
364 } catch (PackageManager.NameNotFoundException e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700365 Log.e(TAG, "Favorite not found: " + packageName + "/" + className);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700366 }
367 return -1;
368 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700369 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700370 }
371 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700372
373 /**
374 * Helper method to allow extending the parser capabilities
375 */
376 protected long invalidPackageOrClass(XmlResourceParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700377 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700378 return -1;
379 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700380 }
381
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700382 /**
383 * AutoInstall: required attributes packageName and className
384 */
385 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700386
387 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700388 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700389 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
390 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
391 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
392 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
393 return -1;
394 }
395
Mario Bertschlerc06af332017-03-28 12:23:22 -0700396 mValues.put(Favorites.RESTORED, ShortcutInfo.FLAG_AUTOINSTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700397 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
398 .addCategory(Intent.CATEGORY_LAUNCHER)
399 .setComponent(new ComponentName(packageName, className))
400 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
401 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
402 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
403 Favorites.ITEM_TYPE_APPLICATION);
404 }
405 }
406
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700407 /**
408 * Parses a web shortcut. Required attributes url, icon, title
409 */
410 protected class ShortcutParser implements TagParser {
411
412 private final Resources mIconRes;
413
414 public ShortcutParser(Resources iconRes) {
415 mIconRes = iconRes;
416 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700417
418 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700419 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700420 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
421 final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0);
422
423 if (titleResId == 0 || iconId == 0) {
424 if (LOGD) Log.d(TAG, "Ignoring shortcut");
425 return -1;
426 }
427
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700428 final Intent intent = parseIntent(parser);
429 if (intent == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700430 return -1;
431 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700432
433 Drawable icon = mIconRes.getDrawable(iconId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700434 if (icon == null) {
435 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon");
436 return -1;
437 }
438
Sunny Goyalad7ff442017-09-12 12:42:26 -0700439 // Auto installs should always support the current platform version.
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800440 LauncherIcons li = LauncherIcons.obtain(mContext);
Sunny Goyalad7ff442017-09-12 12:42:26 -0700441 mValues.put(LauncherSettings.Favorites.ICON, Utilities.flattenBitmap(
Sunny Goyal18a4e5a2018-01-09 15:34:38 -0800442 li.createBadgedIconBitmap(icon, Process.myUserHandle(), VERSION.SDK_INT).icon));
443 li.recycle();
444
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700445 mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
446 mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));
447
448 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700449 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700450 return addShortcut(mSourceRes.getString(titleResId),
451 intent, Favorites.ITEM_TYPE_SHORTCUT);
452 }
453
454 protected Intent parseIntent(XmlResourceParser parser) {
455 final String url = getAttributeValue(parser, ATTR_URL);
456 if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) {
457 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url);
458 return null;
459 }
460 return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700461 }
462 }
463
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700464 /**
465 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
466 * Options child nodes: <extra key=... value=... />
Sunny Goyal86df1382016-08-10 15:03:22 -0700467 * It adds a pending widget which allows the widget to come later. If there are extras, those
468 * are passed to widget options during bind.
469 * The config activity for the widget (if present) is not shown, so any optional configurations
470 * should be passed as extras and the widget should support reading these widget options.
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700471 */
Sunny Goyal86df1382016-08-10 15:03:22 -0700472 protected class PendingWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700473
474 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700475 public long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700476 throws XmlPullParserException, IOException {
477 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
478 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
479 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700480 if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component");
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700481 return -1;
482 }
483
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700484 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
485 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
Sunny Goyal86df1382016-08-10 15:03:22 -0700486 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700487
488 // Read the extras
489 Bundle extras = new Bundle();
490 int widgetDepth = parser.getDepth();
491 int type;
492 while ((type = parser.next()) != XmlPullParser.END_TAG ||
493 parser.getDepth() > widgetDepth) {
494 if (type != XmlPullParser.START_TAG) {
495 continue;
496 }
497
498 if (TAG_EXTRA.equals(parser.getName())) {
499 String key = getAttributeValue(parser, ATTR_KEY);
500 String value = getAttributeValue(parser, ATTR_VALUE);
501 if (key != null && value != null) {
502 extras.putString(key, value);
503 } else {
504 throw new RuntimeException("Widget extras must have a key and value");
505 }
506 } else {
507 throw new RuntimeException("Widgets can contain only extras");
508 }
509 }
510
Sunny Goyal86df1382016-08-10 15:03:22 -0700511 return verifyAndInsert(new ComponentName(packageName, className), extras);
512 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700513
Sunny Goyal86df1382016-08-10 15:03:22 -0700514 protected long verifyAndInsert(ComponentName cn, Bundle extras) {
515 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
516 mValues.put(Favorites.RESTORED,
517 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID |
518 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY |
519 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
520 mValues.put(Favorites._ID, mCallback.generateNewItemId());
521 if (!extras.isEmpty()) {
522 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700523 }
Sunny Goyal86df1382016-08-10 15:03:22 -0700524
525 long insertedId = mCallback.insertAndCheck(mDb, mValues);
526 if (insertedId < 0) {
527 return -1;
528 } else {
529 return insertedId;
530 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700531 }
532 }
533
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700534 protected class FolderParser implements TagParser {
Rajeev Kumar26453a22017-06-09 16:02:25 -0700535 private final ArrayMap<String, TagParser> mFolderElements;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700536
537 public FolderParser() {
538 this(getFolderElementsMap());
539 }
540
Rajeev Kumar26453a22017-06-09 16:02:25 -0700541 public FolderParser(ArrayMap<String, TagParser> elements) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700542 mFolderElements = elements;
543 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700544
545 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700546 public long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700547 throws XmlPullParserException, IOException {
548 final String title;
549 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
550 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700551 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700552 } else {
553 title = mContext.getResources().getString(R.string.folder_name);
554 }
555
556 mValues.put(Favorites.TITLE, title);
557 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
558 mValues.put(Favorites.SPANX, 1);
559 mValues.put(Favorites.SPANY, 1);
560 mValues.put(Favorites._ID, mCallback.generateNewItemId());
561 long folderId = mCallback.insertAndCheck(mDb, mValues);
562 if (folderId < 0) {
563 if (LOGD) Log.e(TAG, "Unable to add folder");
564 return -1;
565 }
566
567 final ContentValues myValues = new ContentValues(mValues);
Rajeev Kumar26453a22017-06-09 16:02:25 -0700568 ArrayList<Long> folderItems = new ArrayList<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700569
570 int type;
571 int folderDepth = parser.getDepth();
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700572 int rank = 0;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700573 while ((type = parser.next()) != XmlPullParser.END_TAG ||
574 parser.getDepth() > folderDepth) {
575 if (type != XmlPullParser.START_TAG) {
576 continue;
577 }
578 mValues.clear();
579 mValues.put(Favorites.CONTAINER, folderId);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700580 mValues.put(Favorites.RANK, rank);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700581
582 TagParser tagParser = mFolderElements.get(parser.getName());
583 if (tagParser != null) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700584 final long id = tagParser.parseAndAdd(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700585 if (id >= 0) {
586 folderItems.add(id);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700587 rank++;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700588 }
589 } else {
590 throw new RuntimeException("Invalid folder item " + parser.getName());
591 }
592 }
593
594 long addedId = folderId;
595
596 // We can only have folders with >= 2 items, so we need to remove the
597 // folder and clean up if less than 2 items were included, or some
598 // failed to add, and less than 2 were actually added
599 if (folderItems.size() < 2) {
600 // Delete the folder
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700601 Uri uri = Favorites.getContentUri(folderId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700602 SqlArguments args = new SqlArguments(uri, null, null);
603 mDb.delete(args.table, args.where, args.args);
604 addedId = -1;
605
606 // If we have a single item, promote it to where the folder
607 // would have been.
608 if (folderItems.size() == 1) {
609 final ContentValues childValues = new ContentValues();
610 copyInteger(myValues, childValues, Favorites.CONTAINER);
611 copyInteger(myValues, childValues, Favorites.SCREEN);
612 copyInteger(myValues, childValues, Favorites.CELLX);
613 copyInteger(myValues, childValues, Favorites.CELLY);
614
615 addedId = folderItems.get(0);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700616 mDb.update(Favorites.TABLE_NAME, childValues,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700617 Favorites._ID + "=" + addedId, null);
618 }
619 }
620 return addedId;
621 }
622 }
623
Rajeev Kumar26453a22017-06-09 16:02:25 -0700624 protected static void beginDocument(XmlPullParser parser, String firstElementName)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700625 throws XmlPullParserException, IOException {
626 int type;
627 while ((type = parser.next()) != XmlPullParser.START_TAG
628 && type != XmlPullParser.END_DOCUMENT);
629
630 if (type != XmlPullParser.START_TAG) {
631 throw new XmlPullParserException("No start tag found");
632 }
633
634 if (!parser.getName().equals(firstElementName)) {
635 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
636 ", expected " + firstElementName);
637 }
638 }
639
Sunny Goyal96a09632015-12-16 11:32:54 -0800640 private static String convertToDistanceFromEnd(String value, int endValue) {
641 if (!TextUtils.isEmpty(value)) {
642 int x = Integer.parseInt(value);
643 if (x < 0) {
644 return Integer.toString(endValue + x);
645 }
646 }
647 return value;
648 }
649
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700650 /**
651 * Return attribute value, attempting launcher-specific namespace first
652 * before falling back to anonymous attribute.
653 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700654 protected static String getAttributeValue(XmlResourceParser parser, String attribute) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700655 String value = parser.getAttributeValue(
656 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
657 if (value == null) {
658 value = parser.getAttributeValue(null, attribute);
659 }
660 return value;
661 }
662
663 /**
664 * Return attribute resource value, attempting launcher-specific namespace
665 * first before falling back to anonymous attribute.
666 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700667 protected static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700668 int defaultValue) {
669 int value = parser.getAttributeResourceValue(
670 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
671 defaultValue);
672 if (value == defaultValue) {
673 value = parser.getAttributeResourceValue(null, attribute, defaultValue);
674 }
675 return value;
676 }
677
Rajeev Kumar26453a22017-06-09 16:02:25 -0700678 public interface LayoutParserCallback {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700679 long generateNewItemId();
680
681 long insertAndCheck(SQLiteDatabase db, ContentValues values);
682 }
683
Adam Cohen091440a2015-03-18 14:16:05 -0700684 @Thunk static void copyInteger(ContentValues from, ContentValues to, String key) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700685 to.put(key, from.getAsInteger(key));
686 }
687}