blob: 8b5a8a863ef03125a32d97afb80bf8f57ea67ff6 [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;
20import android.appwidget.AppWidgetManager;
21import android.content.ComponentName;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.ActivityInfo;
26import android.content.pm.PackageManager;
27import android.content.res.Resources;
28import android.content.res.XmlResourceParser;
29import android.database.sqlite.SQLiteDatabase;
30import android.graphics.drawable.Drawable;
31import android.net.Uri;
32import android.os.Bundle;
33import android.text.TextUtils;
34import android.util.Log;
35import android.util.Pair;
36import android.util.Patterns;
37
38import com.android.launcher3.LauncherProvider.SqlArguments;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070039import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalbb011da2016-06-15 15:42:29 -070040import com.android.launcher3.config.FeatureFlags;
Sunny Goyal10629b02016-09-01 12:50:11 -070041import com.android.launcher3.graphics.LauncherIcons;
Adam Cohen091440a2015-03-18 14:16:05 -070042import com.android.launcher3.util.Thunk;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070043
44import org.xmlpull.v1.XmlPullParser;
45import org.xmlpull.v1.XmlPullParserException;
46
47import java.io.IOException;
48import java.util.ArrayList;
49import java.util.HashMap;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070050import java.util.Locale;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070051
52/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070053 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070054 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070055public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070056 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070057 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070058
59 /** Marker action used to discover a package which defines launcher customization */
60 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070061 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070062
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070063 /**
64 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
65 */
66 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
67 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070068 private static final String LAYOUT_RES = "default_layout";
69
70 static AutoInstallsLayout get(Context context, AppWidgetHost appWidgetHost,
71 LayoutParserCallback callback) {
72 Pair<String, Resources> customizationApkInfo = Utilities.findSystemApk(
73 ACTION_LAUNCHER_CUSTOMIZATION, context.getPackageManager());
74 if (customizationApkInfo == null) {
75 return null;
76 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070077 return get(context, customizationApkInfo.first, customizationApkInfo.second,
78 appWidgetHost, callback);
79 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -070080
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070081 static AutoInstallsLayout get(Context context, String pkg, Resources targetRes,
82 AppWidgetHost appWidgetHost, LayoutParserCallback callback) {
Adam Cohen2e6da152015-05-06 11:42:25 -070083 InvariantDeviceProfile grid = LauncherAppState.getInstance().getInvariantDeviceProfile();
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070084
85 // Try with grid size and hotseat count
86 String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
87 (int) grid.numColumns, (int) grid.numRows, (int) grid.numHotseatIcons);
88 int layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
89
90 // Try with only grid size
91 if (layoutId == 0) {
92 Log.d(TAG, "Formatted layout: " + layoutName
93 + " not found. Trying layout without hosteat");
94 layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
95 (int) grid.numColumns, (int) grid.numRows);
96 layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
97 }
98
99 // Try the default layout
100 if (layoutId == 0) {
101 Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
102 layoutId = targetRes.getIdentifier(LAYOUT_RES, "xml", pkg);
103 }
104
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700105 if (layoutId == 0) {
106 Log.e(TAG, "Layout definition not found in package: " + pkg);
107 return null;
108 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700109 return new AutoInstallsLayout(context, appWidgetHost, callback, targetRes, layoutId,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700110 TAG_WORKSPACE);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700111 }
112
113 // Object Tags
Sunny Goyalb564efb2015-01-23 13:45:20 -0800114 private static final String TAG_INCLUDE = "include";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700115 private static final String TAG_WORKSPACE = "workspace";
116 private static final String TAG_APP_ICON = "appicon";
117 private static final String TAG_AUTO_INSTALL = "autoinstall";
118 private static final String TAG_FOLDER = "folder";
119 private static final String TAG_APPWIDGET = "appwidget";
120 private static final String TAG_SHORTCUT = "shortcut";
121 private static final String TAG_EXTRA = "extra";
122
123 private static final String ATTR_CONTAINER = "container";
124 private static final String ATTR_RANK = "rank";
125
126 private static final String ATTR_PACKAGE_NAME = "packageName";
127 private static final String ATTR_CLASS_NAME = "className";
128 private static final String ATTR_TITLE = "title";
129 private static final String ATTR_SCREEN = "screen";
Sunny Goyal96a09632015-12-16 11:32:54 -0800130
131 // x and y can be specified as negative integers, in which case -1 represents the
132 // last row / column, -2 represents the second last, and so on.
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700133 private static final String ATTR_X = "x";
134 private static final String ATTR_Y = "y";
Sunny Goyal96a09632015-12-16 11:32:54 -0800135
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700136 private static final String ATTR_SPAN_X = "spanX";
137 private static final String ATTR_SPAN_Y = "spanY";
138 private static final String ATTR_ICON = "icon";
139 private static final String ATTR_URL = "url";
140
Sunny Goyalb564efb2015-01-23 13:45:20 -0800141 // Attrs for "Include"
142 private static final String ATTR_WORKSPACE = "workspace";
143
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700144 // Style attrs -- "Extra"
145 private static final String ATTR_KEY = "key";
146 private static final String ATTR_VALUE = "value";
147
148 private static final String HOTSEAT_CONTAINER_NAME =
149 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
150
151 private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
152 "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE";
153
Adam Cohen091440a2015-03-18 14:16:05 -0700154 @Thunk final Context mContext;
155 @Thunk final AppWidgetHost mAppWidgetHost;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800156 protected final LayoutParserCallback mCallback;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700157
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700158 protected final PackageManager mPackageManager;
159 protected final Resources mSourceRes;
160 protected final int mLayoutId;
161
Sunny Goyalbb011da2016-06-15 15:42:29 -0700162 private final InvariantDeviceProfile mIdp;
Sunny Goyal96a09632015-12-16 11:32:54 -0800163 private final int mRowCount;
164 private final int mColumnCount;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700165
166 private final long[] mTemp = new long[2];
Adam Cohen091440a2015-03-18 14:16:05 -0700167 @Thunk final ContentValues mValues;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800168 protected final String mRootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700169
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700170 protected SQLiteDatabase mDb;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700171
172 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700173 LayoutParserCallback callback, Resources res,
174 int layoutId, String rootTag) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700175 mContext = context;
176 mAppWidgetHost = appWidgetHost;
177 mCallback = callback;
178
179 mPackageManager = context.getPackageManager();
180 mValues = new ContentValues();
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700181 mRootTag = rootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700182
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700183 mSourceRes = res;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700184 mLayoutId = layoutId;
Sunny Goyal96a09632015-12-16 11:32:54 -0800185
Sunny Goyalbb011da2016-06-15 15:42:29 -0700186 mIdp = LauncherAppState.getInstance().getInvariantDeviceProfile();
187 mRowCount = mIdp.numRows;
188 mColumnCount = mIdp.numColumns;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700189 }
190
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700191 /**
192 * Loads the layout in the db and returns the number of entries added on the desktop.
193 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700194 public int loadLayout(SQLiteDatabase db, ArrayList<Long> screenIds) {
195 mDb = db;
196 try {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700197 return parseLayout(mLayoutId, screenIds);
Sameer Padala8fd74832014-09-08 16:00:29 -0700198 } catch (Exception e) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700199 Log.w(TAG, "Got exception parsing layout.", e);
200 return -1;
201 }
202 }
203
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700204 /**
205 * Parses the layout and returns the number of elements added on the homescreen.
206 */
207 protected int parseLayout(int layoutId, ArrayList<Long> screenIds)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700208 throws XmlPullParserException, IOException {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700209 XmlResourceParser parser = mSourceRes.getXml(layoutId);
210 beginDocument(parser, mRootTag);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700211 final int depth = parser.getDepth();
212 int type;
213 HashMap<String, TagParser> tagParserMap = getLayoutElementsMap();
214 int count = 0;
215
216 while (((type = parser.next()) != XmlPullParser.END_TAG ||
217 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
218 if (type != XmlPullParser.START_TAG) {
219 continue;
220 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700221 count += parseAndAddNode(parser, tagParserMap, screenIds);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700222 }
223 return count;
224 }
225
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700226 /**
227 * Parses container and screenId attribute from the current tag, and puts it in the out.
228 * @param out array of size 2.
229 */
230 protected void parseContainerAndScreen(XmlResourceParser parser, long[] out) {
231 if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) {
232 out[0] = Favorites.CONTAINER_HOTSEAT;
233 // Hack: hotseat items are stored using screen ids
234 long rank = Long.parseLong(getAttributeValue(parser, ATTR_RANK));
Sunny Goyalbb011da2016-06-15 15:42:29 -0700235 out[1] = (FeatureFlags.NO_ALL_APPS_ICON || rank < mIdp.getAllAppsButtonRank())
236 ? rank : (rank + 1);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700237 } else {
238 out[0] = Favorites.CONTAINER_DESKTOP;
239 out[1] = Long.parseLong(getAttributeValue(parser, ATTR_SCREEN));
240 }
241 }
242
243 /**
244 * Parses the current node and returns the number of elements added.
245 */
246 protected int parseAndAddNode(
247 XmlResourceParser parser,
248 HashMap<String, TagParser> tagParserMap,
249 ArrayList<Long> screenIds)
250 throws XmlPullParserException, IOException {
Sunny Goyalb564efb2015-01-23 13:45:20 -0800251
252 if (TAG_INCLUDE.equals(parser.getName())) {
253 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
254 if (resId != 0) {
255 // recursively load some more favorites, why not?
256 return parseLayout(resId, screenIds);
257 } else {
258 return 0;
259 }
260 }
261
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700262 mValues.clear();
263 parseContainerAndScreen(parser, mTemp);
264 final long container = mTemp[0];
265 final long screenId = mTemp[1];
266
267 mValues.put(Favorites.CONTAINER, container);
268 mValues.put(Favorites.SCREEN, screenId);
Sunny Goyal96a09632015-12-16 11:32:54 -0800269
270 mValues.put(Favorites.CELLX,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800271 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount));
Sunny Goyal96a09632015-12-16 11:32:54 -0800272 mValues.put(Favorites.CELLY,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800273 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700274
275 TagParser tagParser = tagParserMap.get(parser.getName());
276 if (tagParser == null) {
277 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
278 return 0;
279 }
280 long newElementId = tagParser.parseAndAdd(parser);
281 if (newElementId >= 0) {
282 // Keep track of the set of screens which need to be added to the db.
283 if (!screenIds.contains(screenId) &&
284 container == Favorites.CONTAINER_DESKTOP) {
285 screenIds.add(screenId);
286 }
287 return 1;
288 }
289 return 0;
290 }
291
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700292 protected long addShortcut(String title, Intent intent, int type) {
293 long id = mCallback.generateNewItemId();
294 mValues.put(Favorites.INTENT, intent.toUri(0));
295 mValues.put(Favorites.TITLE, title);
296 mValues.put(Favorites.ITEM_TYPE, type);
297 mValues.put(Favorites.SPANX, 1);
298 mValues.put(Favorites.SPANY, 1);
299 mValues.put(Favorites._ID, id);
300 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
301 return -1;
302 } else {
303 return id;
304 }
305 }
306
307 protected HashMap<String, TagParser> getFolderElementsMap() {
308 HashMap<String, TagParser> parsers = new HashMap<String, TagParser>();
309 parsers.put(TAG_APP_ICON, new AppShortcutParser());
310 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700311 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700312 return parsers;
313 }
314
315 protected HashMap<String, TagParser> getLayoutElementsMap() {
316 HashMap<String, TagParser> parsers = new HashMap<String, TagParser>();
317 parsers.put(TAG_APP_ICON, new AppShortcutParser());
318 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
319 parsers.put(TAG_FOLDER, new FolderParser());
Sunny Goyal86df1382016-08-10 15:03:22 -0700320 parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700321 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700322 return parsers;
323 }
324
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700325 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700326 /**
327 * Parses the tag and adds to the db
328 * @return the id of the row added or -1;
329 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700330 long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700331 throws XmlPullParserException, IOException;
332 }
333
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700334 /**
335 * App shortcuts: required attributes packageName and className
336 */
337 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700338
339 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700340 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700341 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
342 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
343
344 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
345 ActivityInfo info;
346 try {
347 ComponentName cn;
348 try {
349 cn = new ComponentName(packageName, className);
350 info = mPackageManager.getActivityInfo(cn, 0);
351 } catch (PackageManager.NameNotFoundException nnfe) {
352 String[] packages = mPackageManager.currentToCanonicalPackageNames(
353 new String[] { packageName });
354 cn = new ComponentName(packages[0], className);
355 info = mPackageManager.getActivityInfo(cn, 0);
356 }
357 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
358 .addCategory(Intent.CATEGORY_LAUNCHER)
359 .setComponent(cn)
360 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
361 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
362
363 return addShortcut(info.loadLabel(mPackageManager).toString(),
364 intent, Favorites.ITEM_TYPE_APPLICATION);
365 } catch (PackageManager.NameNotFoundException e) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700366 Log.e(TAG, "Unable to add favorite: " + packageName + "/" + className, e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700367 }
368 return -1;
369 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700370 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700371 }
372 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700373
374 /**
375 * Helper method to allow extending the parser capabilities
376 */
377 protected long invalidPackageOrClass(XmlResourceParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700378 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700379 return -1;
380 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700381 }
382
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700383 /**
384 * AutoInstall: required attributes packageName and className
385 */
386 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700387
388 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700389 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700390 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
391 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
392 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
393 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
394 return -1;
395 }
396
Sunny Goyal34942622014-08-29 17:20:55 -0700397 mValues.put(Favorites.RESTORED, ShortcutInfo.FLAG_AUTOINTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700398 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
399 .addCategory(Intent.CATEGORY_LAUNCHER)
400 .setComponent(new ComponentName(packageName, className))
401 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
402 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
403 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
404 Favorites.ITEM_TYPE_APPLICATION);
405 }
406 }
407
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700408 /**
409 * Parses a web shortcut. Required attributes url, icon, title
410 */
411 protected class ShortcutParser implements TagParser {
412
413 private final Resources mIconRes;
414
415 public ShortcutParser(Resources iconRes) {
416 mIconRes = iconRes;
417 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700418
419 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700420 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700421 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
422 final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0);
423
424 if (titleResId == 0 || iconId == 0) {
425 if (LOGD) Log.d(TAG, "Ignoring shortcut");
426 return -1;
427 }
428
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700429 final Intent intent = parseIntent(parser);
430 if (intent == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700431 return -1;
432 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700433
434 Drawable icon = mIconRes.getDrawable(iconId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700435 if (icon == null) {
436 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon");
437 return -1;
438 }
439
Sunny Goyal10629b02016-09-01 12:50:11 -0700440 ItemInfo.writeBitmap(mValues, LauncherIcons.createIconBitmap(icon, mContext));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700441 mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
442 mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));
443
444 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700445 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700446 return addShortcut(mSourceRes.getString(titleResId),
447 intent, Favorites.ITEM_TYPE_SHORTCUT);
448 }
449
450 protected Intent parseIntent(XmlResourceParser parser) {
451 final String url = getAttributeValue(parser, ATTR_URL);
452 if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) {
453 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url);
454 return null;
455 }
456 return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700457 }
458 }
459
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700460 /**
461 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
462 * Options child nodes: <extra key=... value=... />
Sunny Goyal86df1382016-08-10 15:03:22 -0700463 * It adds a pending widget which allows the widget to come later. If there are extras, those
464 * are passed to widget options during bind.
465 * The config activity for the widget (if present) is not shown, so any optional configurations
466 * should be passed as extras and the widget should support reading these widget options.
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700467 */
Sunny Goyal86df1382016-08-10 15:03:22 -0700468 protected class PendingWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700469
470 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700471 public long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700472 throws XmlPullParserException, IOException {
473 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
474 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
475 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700476 if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component");
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700477 return -1;
478 }
479
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700480 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
481 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
Sunny Goyal86df1382016-08-10 15:03:22 -0700482 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700483
484 // Read the extras
485 Bundle extras = new Bundle();
486 int widgetDepth = parser.getDepth();
487 int type;
488 while ((type = parser.next()) != XmlPullParser.END_TAG ||
489 parser.getDepth() > widgetDepth) {
490 if (type != XmlPullParser.START_TAG) {
491 continue;
492 }
493
494 if (TAG_EXTRA.equals(parser.getName())) {
495 String key = getAttributeValue(parser, ATTR_KEY);
496 String value = getAttributeValue(parser, ATTR_VALUE);
497 if (key != null && value != null) {
498 extras.putString(key, value);
499 } else {
500 throw new RuntimeException("Widget extras must have a key and value");
501 }
502 } else {
503 throw new RuntimeException("Widgets can contain only extras");
504 }
505 }
506
Sunny Goyal86df1382016-08-10 15:03:22 -0700507 return verifyAndInsert(new ComponentName(packageName, className), extras);
508 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700509
Sunny Goyal86df1382016-08-10 15:03:22 -0700510 protected long verifyAndInsert(ComponentName cn, Bundle extras) {
511 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
512 mValues.put(Favorites.RESTORED,
513 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID |
514 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY |
515 LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
516 mValues.put(Favorites._ID, mCallback.generateNewItemId());
517 if (!extras.isEmpty()) {
518 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700519 }
Sunny Goyal86df1382016-08-10 15:03:22 -0700520
521 long insertedId = mCallback.insertAndCheck(mDb, mValues);
522 if (insertedId < 0) {
523 return -1;
524 } else {
525 return insertedId;
526 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700527 }
528 }
529
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700530 protected class FolderParser implements TagParser {
531 private final HashMap<String, TagParser> mFolderElements;
532
533 public FolderParser() {
534 this(getFolderElementsMap());
535 }
536
537 public FolderParser(HashMap<String, TagParser> elements) {
538 mFolderElements = elements;
539 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700540
541 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700542 public long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700543 throws XmlPullParserException, IOException {
544 final String title;
545 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
546 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700547 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700548 } else {
549 title = mContext.getResources().getString(R.string.folder_name);
550 }
551
552 mValues.put(Favorites.TITLE, title);
553 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
554 mValues.put(Favorites.SPANX, 1);
555 mValues.put(Favorites.SPANY, 1);
556 mValues.put(Favorites._ID, mCallback.generateNewItemId());
557 long folderId = mCallback.insertAndCheck(mDb, mValues);
558 if (folderId < 0) {
559 if (LOGD) Log.e(TAG, "Unable to add folder");
560 return -1;
561 }
562
563 final ContentValues myValues = new ContentValues(mValues);
564 ArrayList<Long> folderItems = new ArrayList<Long>();
565
566 int type;
567 int folderDepth = parser.getDepth();
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700568 int rank = 0;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700569 while ((type = parser.next()) != XmlPullParser.END_TAG ||
570 parser.getDepth() > folderDepth) {
571 if (type != XmlPullParser.START_TAG) {
572 continue;
573 }
574 mValues.clear();
575 mValues.put(Favorites.CONTAINER, folderId);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700576 mValues.put(Favorites.RANK, rank);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700577
578 TagParser tagParser = mFolderElements.get(parser.getName());
579 if (tagParser != null) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700580 final long id = tagParser.parseAndAdd(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700581 if (id >= 0) {
582 folderItems.add(id);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700583 rank++;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700584 }
585 } else {
586 throw new RuntimeException("Invalid folder item " + parser.getName());
587 }
588 }
589
590 long addedId = folderId;
591
592 // We can only have folders with >= 2 items, so we need to remove the
593 // folder and clean up if less than 2 items were included, or some
594 // failed to add, and less than 2 were actually added
595 if (folderItems.size() < 2) {
596 // Delete the folder
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700597 Uri uri = Favorites.getContentUri(folderId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700598 SqlArguments args = new SqlArguments(uri, null, null);
599 mDb.delete(args.table, args.where, args.args);
600 addedId = -1;
601
602 // If we have a single item, promote it to where the folder
603 // would have been.
604 if (folderItems.size() == 1) {
605 final ContentValues childValues = new ContentValues();
606 copyInteger(myValues, childValues, Favorites.CONTAINER);
607 copyInteger(myValues, childValues, Favorites.SCREEN);
608 copyInteger(myValues, childValues, Favorites.CELLX);
609 copyInteger(myValues, childValues, Favorites.CELLY);
610
611 addedId = folderItems.get(0);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700612 mDb.update(Favorites.TABLE_NAME, childValues,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700613 Favorites._ID + "=" + addedId, null);
614 }
615 }
616 return addedId;
617 }
618 }
619
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700620 protected static final void beginDocument(XmlPullParser parser, String firstElementName)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700621 throws XmlPullParserException, IOException {
622 int type;
623 while ((type = parser.next()) != XmlPullParser.START_TAG
624 && type != XmlPullParser.END_DOCUMENT);
625
626 if (type != XmlPullParser.START_TAG) {
627 throw new XmlPullParserException("No start tag found");
628 }
629
630 if (!parser.getName().equals(firstElementName)) {
631 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
632 ", expected " + firstElementName);
633 }
634 }
635
Sunny Goyal96a09632015-12-16 11:32:54 -0800636 private static String convertToDistanceFromEnd(String value, int endValue) {
637 if (!TextUtils.isEmpty(value)) {
638 int x = Integer.parseInt(value);
639 if (x < 0) {
640 return Integer.toString(endValue + x);
641 }
642 }
643 return value;
644 }
645
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700646 /**
647 * Return attribute value, attempting launcher-specific namespace first
648 * before falling back to anonymous attribute.
649 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700650 protected static String getAttributeValue(XmlResourceParser parser, String attribute) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700651 String value = parser.getAttributeValue(
652 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
653 if (value == null) {
654 value = parser.getAttributeValue(null, attribute);
655 }
656 return value;
657 }
658
659 /**
660 * Return attribute resource value, attempting launcher-specific namespace
661 * first before falling back to anonymous attribute.
662 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700663 protected static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700664 int defaultValue) {
665 int value = parser.getAttributeResourceValue(
666 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
667 defaultValue);
668 if (value == defaultValue) {
669 value = parser.getAttributeResourceValue(null, attribute, defaultValue);
670 }
671 return value;
672 }
673
674 public static interface LayoutParserCallback {
675 long generateNewItemId();
676
677 long insertAndCheck(SQLiteDatabase db, ContentValues values);
678 }
679
Adam Cohen091440a2015-03-18 14:16:05 -0700680 @Thunk static void copyInteger(ContentValues from, ContentValues to, String key) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700681 to.put(key, from.getAsInteger(key));
682 }
683}