blob: d82579bf77cfb3642b19ff436c70e158a0281c66 [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;
31import android.os.Bundle;
32import android.text.TextUtils;
Rajeev Kumar26453a22017-06-09 16:02:25 -070033import android.util.ArrayMap;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070034import android.util.Log;
35import android.util.Pair;
36import android.util.Patterns;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070037import com.android.launcher3.LauncherProvider.SqlArguments;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070038import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalbb011da2016-06-15 15:42:29 -070039import com.android.launcher3.config.FeatureFlags;
Sunny Goyal10629b02016-09-01 12:50:11 -070040import com.android.launcher3.graphics.LauncherIcons;
Adam Cohen091440a2015-03-18 14:16:05 -070041import com.android.launcher3.util.Thunk;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070042import java.io.IOException;
43import java.util.ArrayList;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070044import java.util.Locale;
Rajeev Kumar26453a22017-06-09 16:02:25 -070045import org.xmlpull.v1.XmlPullParser;
46import org.xmlpull.v1.XmlPullParserException;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070047
48/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070049 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070050 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070051public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070052 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070053 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070054
55 /** Marker action used to discover a package which defines launcher customization */
56 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070057 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070058
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070059 /**
60 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
61 */
62 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
63 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070064 private static final String LAYOUT_RES = "default_layout";
65
66 static AutoInstallsLayout get(Context context, AppWidgetHost appWidgetHost,
67 LayoutParserCallback callback) {
68 Pair<String, Resources> customizationApkInfo = Utilities.findSystemApk(
69 ACTION_LAUNCHER_CUSTOMIZATION, context.getPackageManager());
70 if (customizationApkInfo == null) {
71 return null;
72 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070073 return get(context, customizationApkInfo.first, customizationApkInfo.second,
74 appWidgetHost, callback);
75 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -070076
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070077 static AutoInstallsLayout get(Context context, String pkg, Resources targetRes,
78 AppWidgetHost appWidgetHost, LayoutParserCallback callback) {
Sunny Goyal87f784c2017-01-11 10:48:34 -080079 InvariantDeviceProfile grid = LauncherAppState.getIDP(context);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070080
81 // Try with grid size and hotseat count
82 String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
Rajeev Kumar26453a22017-06-09 16:02:25 -070083 grid.numColumns, grid.numRows, grid.numHotseatIcons);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070084 int layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
85
86 // Try with only grid size
87 if (layoutId == 0) {
88 Log.d(TAG, "Formatted layout: " + layoutName
89 + " not found. Trying layout without hosteat");
90 layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
Rajeev Kumar26453a22017-06-09 16:02:25 -070091 grid.numColumns, grid.numRows);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070092 layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
93 }
94
95 // Try the default layout
96 if (layoutId == 0) {
97 Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
98 layoutId = targetRes.getIdentifier(LAYOUT_RES, "xml", pkg);
99 }
100
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700101 if (layoutId == 0) {
102 Log.e(TAG, "Layout definition not found in package: " + pkg);
103 return null;
104 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700105 return new AutoInstallsLayout(context, appWidgetHost, callback, targetRes, layoutId,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700106 TAG_WORKSPACE);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700107 }
108
109 // Object Tags
Sunny Goyalb564efb2015-01-23 13:45:20 -0800110 private static final String TAG_INCLUDE = "include";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700111 private static final String TAG_WORKSPACE = "workspace";
112 private static final String TAG_APP_ICON = "appicon";
113 private static final String TAG_AUTO_INSTALL = "autoinstall";
114 private static final String TAG_FOLDER = "folder";
115 private static final String TAG_APPWIDGET = "appwidget";
116 private static final String TAG_SHORTCUT = "shortcut";
117 private static final String TAG_EXTRA = "extra";
118
119 private static final String ATTR_CONTAINER = "container";
120 private static final String ATTR_RANK = "rank";
121
122 private static final String ATTR_PACKAGE_NAME = "packageName";
123 private static final String ATTR_CLASS_NAME = "className";
124 private static final String ATTR_TITLE = "title";
125 private static final String ATTR_SCREEN = "screen";
Sunny Goyal96a09632015-12-16 11:32:54 -0800126
127 // x and y can be specified as negative integers, in which case -1 represents the
128 // last row / column, -2 represents the second last, and so on.
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700129 private static final String ATTR_X = "x";
130 private static final String ATTR_Y = "y";
Sunny Goyal96a09632015-12-16 11:32:54 -0800131
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700132 private static final String ATTR_SPAN_X = "spanX";
133 private static final String ATTR_SPAN_Y = "spanY";
134 private static final String ATTR_ICON = "icon";
135 private static final String ATTR_URL = "url";
136
Sunny Goyalb564efb2015-01-23 13:45:20 -0800137 // Attrs for "Include"
138 private static final String ATTR_WORKSPACE = "workspace";
139
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700140 // Style attrs -- "Extra"
141 private static final String ATTR_KEY = "key";
142 private static final String ATTR_VALUE = "value";
143
144 private static final String HOTSEAT_CONTAINER_NAME =
145 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
146
147 private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
148 "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE";
149
Adam Cohen091440a2015-03-18 14:16:05 -0700150 @Thunk final Context mContext;
151 @Thunk final AppWidgetHost mAppWidgetHost;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800152 protected final LayoutParserCallback mCallback;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700153
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700154 protected final PackageManager mPackageManager;
155 protected final Resources mSourceRes;
156 protected final int mLayoutId;
157
Sunny Goyalbb011da2016-06-15 15:42:29 -0700158 private final InvariantDeviceProfile mIdp;
Sunny Goyal96a09632015-12-16 11:32:54 -0800159 private final int mRowCount;
160 private final int mColumnCount;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700161
162 private final long[] mTemp = new long[2];
Adam Cohen091440a2015-03-18 14:16:05 -0700163 @Thunk final ContentValues mValues;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800164 protected final String mRootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700165
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700166 protected SQLiteDatabase mDb;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700167
168 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700169 LayoutParserCallback callback, Resources res,
170 int layoutId, String rootTag) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700171 mContext = context;
172 mAppWidgetHost = appWidgetHost;
173 mCallback = callback;
174
175 mPackageManager = context.getPackageManager();
176 mValues = new ContentValues();
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700177 mRootTag = rootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700178
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700179 mSourceRes = res;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700180 mLayoutId = layoutId;
Sunny Goyal96a09632015-12-16 11:32:54 -0800181
Sunny Goyal87f784c2017-01-11 10:48:34 -0800182 mIdp = LauncherAppState.getIDP(context);
Sunny Goyalbb011da2016-06-15 15:42:29 -0700183 mRowCount = mIdp.numRows;
184 mColumnCount = mIdp.numColumns;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700185 }
186
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700187 /**
188 * Loads the layout in the db and returns the number of entries added on the desktop.
189 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700190 public int loadLayout(SQLiteDatabase db, ArrayList<Long> screenIds) {
191 mDb = db;
192 try {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700193 return parseLayout(mLayoutId, screenIds);
Sameer Padala8fd74832014-09-08 16:00:29 -0700194 } catch (Exception e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700195 Log.e(TAG, "Error parsing layout: " + e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700196 return -1;
197 }
198 }
199
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700200 /**
201 * Parses the layout and returns the number of elements added on the homescreen.
202 */
203 protected int parseLayout(int layoutId, ArrayList<Long> screenIds)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700204 throws XmlPullParserException, IOException {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700205 XmlResourceParser parser = mSourceRes.getXml(layoutId);
206 beginDocument(parser, mRootTag);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700207 final int depth = parser.getDepth();
208 int type;
Rajeev Kumar26453a22017-06-09 16:02:25 -0700209 ArrayMap<String, TagParser> tagParserMap = getLayoutElementsMap();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700210 int count = 0;
211
212 while (((type = parser.next()) != XmlPullParser.END_TAG ||
213 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
214 if (type != XmlPullParser.START_TAG) {
215 continue;
216 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700217 count += parseAndAddNode(parser, tagParserMap, screenIds);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700218 }
219 return count;
220 }
221
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700222 /**
223 * Parses container and screenId attribute from the current tag, and puts it in the out.
224 * @param out array of size 2.
225 */
226 protected void parseContainerAndScreen(XmlResourceParser parser, long[] out) {
227 if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) {
228 out[0] = Favorites.CONTAINER_HOTSEAT;
229 // Hack: hotseat items are stored using screen ids
230 long rank = Long.parseLong(getAttributeValue(parser, ATTR_RANK));
Sunny Goyalbb011da2016-06-15 15:42:29 -0700231 out[1] = (FeatureFlags.NO_ALL_APPS_ICON || rank < mIdp.getAllAppsButtonRank())
232 ? rank : (rank + 1);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700233 } else {
234 out[0] = Favorites.CONTAINER_DESKTOP;
235 out[1] = Long.parseLong(getAttributeValue(parser, ATTR_SCREEN));
236 }
237 }
238
239 /**
240 * Parses the current node and returns the number of elements added.
241 */
242 protected int parseAndAddNode(
Rajeev Kumar26453a22017-06-09 16:02:25 -0700243 XmlResourceParser parser,
244 ArrayMap<String, TagParser> tagParserMap,
245 ArrayList<Long> screenIds)
246 throws XmlPullParserException, IOException {
Sunny Goyalb564efb2015-01-23 13:45:20 -0800247
248 if (TAG_INCLUDE.equals(parser.getName())) {
249 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
250 if (resId != 0) {
251 // recursively load some more favorites, why not?
252 return parseLayout(resId, screenIds);
253 } else {
254 return 0;
255 }
256 }
257
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700258 mValues.clear();
259 parseContainerAndScreen(parser, mTemp);
260 final long container = mTemp[0];
261 final long screenId = mTemp[1];
262
263 mValues.put(Favorites.CONTAINER, container);
264 mValues.put(Favorites.SCREEN, screenId);
Sunny Goyal96a09632015-12-16 11:32:54 -0800265
266 mValues.put(Favorites.CELLX,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800267 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount));
Sunny Goyal96a09632015-12-16 11:32:54 -0800268 mValues.put(Favorites.CELLY,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800269 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700270
271 TagParser tagParser = tagParserMap.get(parser.getName());
272 if (tagParser == null) {
273 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
274 return 0;
275 }
276 long newElementId = tagParser.parseAndAdd(parser);
277 if (newElementId >= 0) {
278 // Keep track of the set of screens which need to be added to the db.
279 if (!screenIds.contains(screenId) &&
280 container == Favorites.CONTAINER_DESKTOP) {
281 screenIds.add(screenId);
282 }
283 return 1;
284 }
285 return 0;
286 }
287
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700288 protected long addShortcut(String title, Intent intent, int type) {
289 long id = mCallback.generateNewItemId();
290 mValues.put(Favorites.INTENT, intent.toUri(0));
291 mValues.put(Favorites.TITLE, title);
292 mValues.put(Favorites.ITEM_TYPE, type);
293 mValues.put(Favorites.SPANX, 1);
294 mValues.put(Favorites.SPANY, 1);
295 mValues.put(Favorites._ID, id);
296 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
297 return -1;
298 } else {
299 return id;
300 }
301 }
302
Rajeev Kumar26453a22017-06-09 16:02:25 -0700303 protected ArrayMap<String, TagParser> getFolderElementsMap() {
304 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700305 parsers.put(TAG_APP_ICON, new AppShortcutParser());
306 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700307 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700308 return parsers;
309 }
310
Rajeev Kumar26453a22017-06-09 16:02:25 -0700311 protected ArrayMap<String, TagParser> getLayoutElementsMap() {
312 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700313 parsers.put(TAG_APP_ICON, new AppShortcutParser());
314 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
315 parsers.put(TAG_FOLDER, new FolderParser());
Sunny Goyal86df1382016-08-10 15:03:22 -0700316 parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700317 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700318 return parsers;
319 }
320
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700321 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700322 /**
323 * Parses the tag and adds to the db
324 * @return the id of the row added or -1;
325 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700326 long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700327 throws XmlPullParserException, IOException;
328 }
329
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700330 /**
331 * App shortcuts: required attributes packageName and className
332 */
333 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700334
335 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700336 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700337 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
338 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
339
340 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
341 ActivityInfo info;
342 try {
343 ComponentName cn;
344 try {
345 cn = new ComponentName(packageName, className);
346 info = mPackageManager.getActivityInfo(cn, 0);
347 } catch (PackageManager.NameNotFoundException nnfe) {
348 String[] packages = mPackageManager.currentToCanonicalPackageNames(
349 new String[] { packageName });
350 cn = new ComponentName(packages[0], className);
351 info = mPackageManager.getActivityInfo(cn, 0);
352 }
353 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
354 .addCategory(Intent.CATEGORY_LAUNCHER)
355 .setComponent(cn)
356 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
357 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
358
359 return addShortcut(info.loadLabel(mPackageManager).toString(),
360 intent, Favorites.ITEM_TYPE_APPLICATION);
361 } catch (PackageManager.NameNotFoundException e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700362 Log.e(TAG, "Favorite not found: " + packageName + "/" + className);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700363 }
364 return -1;
365 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700366 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700367 }
368 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700369
370 /**
371 * Helper method to allow extending the parser capabilities
372 */
373 protected long invalidPackageOrClass(XmlResourceParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700374 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700375 return -1;
376 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700377 }
378
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700379 /**
380 * AutoInstall: required attributes packageName and className
381 */
382 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700383
384 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700385 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700386 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
387 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
388 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
389 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
390 return -1;
391 }
392
Mario Bertschlerc06af332017-03-28 12:23:22 -0700393 mValues.put(Favorites.RESTORED, ShortcutInfo.FLAG_AUTOINSTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700394 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
395 .addCategory(Intent.CATEGORY_LAUNCHER)
396 .setComponent(new ComponentName(packageName, className))
397 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
398 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
399 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
400 Favorites.ITEM_TYPE_APPLICATION);
401 }
402 }
403
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700404 /**
405 * Parses a web shortcut. Required attributes url, icon, title
406 */
407 protected class ShortcutParser implements TagParser {
408
409 private final Resources mIconRes;
410
411 public ShortcutParser(Resources iconRes) {
412 mIconRes = iconRes;
413 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700414
415 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700416 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700417 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
418 final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0);
419
420 if (titleResId == 0 || iconId == 0) {
421 if (LOGD) Log.d(TAG, "Ignoring shortcut");
422 return -1;
423 }
424
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700425 final Intent intent = parseIntent(parser);
426 if (intent == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700427 return -1;
428 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700429
430 Drawable icon = mIconRes.getDrawable(iconId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700431 if (icon == null) {
432 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon");
433 return -1;
434 }
435
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800436 mValues.put(LauncherSettings.Favorites.ICON,
437 Utilities.flattenBitmap(LauncherIcons.createIconBitmap(icon, mContext)));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700438 mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
439 mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));
440
441 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700442 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700443 return addShortcut(mSourceRes.getString(titleResId),
444 intent, Favorites.ITEM_TYPE_SHORTCUT);
445 }
446
447 protected Intent parseIntent(XmlResourceParser parser) {
448 final String url = getAttributeValue(parser, ATTR_URL);
449 if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) {
450 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url);
451 return null;
452 }
453 return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700454 }
455 }
456
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700457 /**
458 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
459 * Options child nodes: <extra key=... value=... />
Sunny Goyal86df1382016-08-10 15:03:22 -0700460 * It adds a pending widget which allows the widget to come later. If there are extras, those
461 * are passed to widget options during bind.
462 * The config activity for the widget (if present) is not shown, so any optional configurations
463 * should be passed as extras and the widget should support reading these widget options.
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700464 */
Sunny Goyal86df1382016-08-10 15:03:22 -0700465 protected class PendingWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700466
467 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700468 public long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700469 throws XmlPullParserException, IOException {
470 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
471 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
472 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700473 if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component");
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700474 return -1;
475 }
476
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700477 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
478 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
Sunny Goyal86df1382016-08-10 15:03:22 -0700479 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700480
481 // Read the extras
482 Bundle extras = new Bundle();
483 int widgetDepth = parser.getDepth();
484 int type;
485 while ((type = parser.next()) != XmlPullParser.END_TAG ||
486 parser.getDepth() > widgetDepth) {
487 if (type != XmlPullParser.START_TAG) {
488 continue;
489 }
490
491 if (TAG_EXTRA.equals(parser.getName())) {
492 String key = getAttributeValue(parser, ATTR_KEY);
493 String value = getAttributeValue(parser, ATTR_VALUE);
494 if (key != null && value != null) {
495 extras.putString(key, value);
496 } else {
497 throw new RuntimeException("Widget extras must have a key and value");
498 }
499 } else {
500 throw new RuntimeException("Widgets can contain only extras");
501 }
502 }
503
Sunny Goyal86df1382016-08-10 15:03:22 -0700504 return verifyAndInsert(new ComponentName(packageName, className), extras);
505 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700506
Sunny Goyal86df1382016-08-10 15:03:22 -0700507 protected long verifyAndInsert(ComponentName cn, Bundle extras) {
508 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
509 mValues.put(Favorites.RESTORED,
510 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID |
511 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY |
512 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
513 mValues.put(Favorites._ID, mCallback.generateNewItemId());
514 if (!extras.isEmpty()) {
515 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700516 }
Sunny Goyal86df1382016-08-10 15:03:22 -0700517
518 long insertedId = mCallback.insertAndCheck(mDb, mValues);
519 if (insertedId < 0) {
520 return -1;
521 } else {
522 return insertedId;
523 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700524 }
525 }
526
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700527 protected class FolderParser implements TagParser {
Rajeev Kumar26453a22017-06-09 16:02:25 -0700528 private final ArrayMap<String, TagParser> mFolderElements;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700529
530 public FolderParser() {
531 this(getFolderElementsMap());
532 }
533
Rajeev Kumar26453a22017-06-09 16:02:25 -0700534 public FolderParser(ArrayMap<String, TagParser> elements) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700535 mFolderElements = elements;
536 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700537
538 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700539 public long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700540 throws XmlPullParserException, IOException {
541 final String title;
542 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
543 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700544 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700545 } else {
546 title = mContext.getResources().getString(R.string.folder_name);
547 }
548
549 mValues.put(Favorites.TITLE, title);
550 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
551 mValues.put(Favorites.SPANX, 1);
552 mValues.put(Favorites.SPANY, 1);
553 mValues.put(Favorites._ID, mCallback.generateNewItemId());
554 long folderId = mCallback.insertAndCheck(mDb, mValues);
555 if (folderId < 0) {
556 if (LOGD) Log.e(TAG, "Unable to add folder");
557 return -1;
558 }
559
560 final ContentValues myValues = new ContentValues(mValues);
Rajeev Kumar26453a22017-06-09 16:02:25 -0700561 ArrayList<Long> folderItems = new ArrayList<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700562
563 int type;
564 int folderDepth = parser.getDepth();
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700565 int rank = 0;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700566 while ((type = parser.next()) != XmlPullParser.END_TAG ||
567 parser.getDepth() > folderDepth) {
568 if (type != XmlPullParser.START_TAG) {
569 continue;
570 }
571 mValues.clear();
572 mValues.put(Favorites.CONTAINER, folderId);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700573 mValues.put(Favorites.RANK, rank);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700574
575 TagParser tagParser = mFolderElements.get(parser.getName());
576 if (tagParser != null) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700577 final long id = tagParser.parseAndAdd(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700578 if (id >= 0) {
579 folderItems.add(id);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700580 rank++;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700581 }
582 } else {
583 throw new RuntimeException("Invalid folder item " + parser.getName());
584 }
585 }
586
587 long addedId = folderId;
588
589 // We can only have folders with >= 2 items, so we need to remove the
590 // folder and clean up if less than 2 items were included, or some
591 // failed to add, and less than 2 were actually added
592 if (folderItems.size() < 2) {
593 // Delete the folder
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700594 Uri uri = Favorites.getContentUri(folderId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700595 SqlArguments args = new SqlArguments(uri, null, null);
596 mDb.delete(args.table, args.where, args.args);
597 addedId = -1;
598
599 // If we have a single item, promote it to where the folder
600 // would have been.
601 if (folderItems.size() == 1) {
602 final ContentValues childValues = new ContentValues();
603 copyInteger(myValues, childValues, Favorites.CONTAINER);
604 copyInteger(myValues, childValues, Favorites.SCREEN);
605 copyInteger(myValues, childValues, Favorites.CELLX);
606 copyInteger(myValues, childValues, Favorites.CELLY);
607
608 addedId = folderItems.get(0);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700609 mDb.update(Favorites.TABLE_NAME, childValues,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700610 Favorites._ID + "=" + addedId, null);
611 }
612 }
613 return addedId;
614 }
615 }
616
Rajeev Kumar26453a22017-06-09 16:02:25 -0700617 protected static void beginDocument(XmlPullParser parser, String firstElementName)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700618 throws XmlPullParserException, IOException {
619 int type;
620 while ((type = parser.next()) != XmlPullParser.START_TAG
621 && type != XmlPullParser.END_DOCUMENT);
622
623 if (type != XmlPullParser.START_TAG) {
624 throw new XmlPullParserException("No start tag found");
625 }
626
627 if (!parser.getName().equals(firstElementName)) {
628 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
629 ", expected " + firstElementName);
630 }
631 }
632
Sunny Goyal96a09632015-12-16 11:32:54 -0800633 private static String convertToDistanceFromEnd(String value, int endValue) {
634 if (!TextUtils.isEmpty(value)) {
635 int x = Integer.parseInt(value);
636 if (x < 0) {
637 return Integer.toString(endValue + x);
638 }
639 }
640 return value;
641 }
642
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700643 /**
644 * Return attribute value, attempting launcher-specific namespace first
645 * before falling back to anonymous attribute.
646 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700647 protected static String getAttributeValue(XmlResourceParser parser, String attribute) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700648 String value = parser.getAttributeValue(
649 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
650 if (value == null) {
651 value = parser.getAttributeValue(null, attribute);
652 }
653 return value;
654 }
655
656 /**
657 * Return attribute resource value, attempting launcher-specific namespace
658 * first before falling back to anonymous attribute.
659 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700660 protected static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700661 int defaultValue) {
662 int value = parser.getAttributeResourceValue(
663 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
664 defaultValue);
665 if (value == defaultValue) {
666 value = parser.getAttributeResourceValue(null, attribute, defaultValue);
667 }
668 return value;
669 }
670
Rajeev Kumar26453a22017-06-09 16:02:25 -0700671 public interface LayoutParserCallback {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700672 long generateNewItemId();
673
674 long insertAndCheck(SQLiteDatabase db, ContentValues values);
675 }
676
Adam Cohen091440a2015-03-18 14:16:05 -0700677 @Thunk static void copyInteger(ContentValues from, ContentValues to, String key) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700678 to.put(key, from.getAsInteger(key));
679 }
680}