blob: 440e4e7b99f8a0f79734f458f43a984efdbc2969 [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;
Adam Cohen091440a2015-03-18 14:16:05 -070040import com.android.launcher3.util.Thunk;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070041
42import org.xmlpull.v1.XmlPullParser;
43import org.xmlpull.v1.XmlPullParserException;
44
45import java.io.IOException;
46import java.util.ArrayList;
47import java.util.HashMap;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070048import java.util.Locale;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070049
50/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070051 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070052 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070053public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070054 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070055 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070056
57 /** Marker action used to discover a package which defines launcher customization */
58 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070059 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070060
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070061 /**
62 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
63 */
64 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
65 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070066 private static final String LAYOUT_RES = "default_layout";
67
68 static AutoInstallsLayout get(Context context, AppWidgetHost appWidgetHost,
69 LayoutParserCallback callback) {
70 Pair<String, Resources> customizationApkInfo = Utilities.findSystemApk(
71 ACTION_LAUNCHER_CUSTOMIZATION, context.getPackageManager());
72 if (customizationApkInfo == null) {
73 return null;
74 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070075 return get(context, customizationApkInfo.first, customizationApkInfo.second,
76 appWidgetHost, callback);
77 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -070078
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070079 static AutoInstallsLayout get(Context context, String pkg, Resources targetRes,
80 AppWidgetHost appWidgetHost, LayoutParserCallback callback) {
Adam Cohen2e6da152015-05-06 11:42:25 -070081 InvariantDeviceProfile grid = LauncherAppState.getInstance().getInvariantDeviceProfile();
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070082
83 // Try with grid size and hotseat count
84 String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
85 (int) grid.numColumns, (int) grid.numRows, (int) grid.numHotseatIcons);
86 int layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
87
88 // Try with only grid size
89 if (layoutId == 0) {
90 Log.d(TAG, "Formatted layout: " + layoutName
91 + " not found. Trying layout without hosteat");
92 layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
93 (int) grid.numColumns, (int) grid.numRows);
94 layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
95 }
96
97 // Try the default layout
98 if (layoutId == 0) {
99 Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
100 layoutId = targetRes.getIdentifier(LAYOUT_RES, "xml", pkg);
101 }
102
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700103 if (layoutId == 0) {
104 Log.e(TAG, "Layout definition not found in package: " + pkg);
105 return null;
106 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700107 return new AutoInstallsLayout(context, appWidgetHost, callback, targetRes, layoutId,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700108 TAG_WORKSPACE);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700109 }
110
111 // Object Tags
Sunny Goyalb564efb2015-01-23 13:45:20 -0800112 private static final String TAG_INCLUDE = "include";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700113 private static final String TAG_WORKSPACE = "workspace";
114 private static final String TAG_APP_ICON = "appicon";
115 private static final String TAG_AUTO_INSTALL = "autoinstall";
116 private static final String TAG_FOLDER = "folder";
117 private static final String TAG_APPWIDGET = "appwidget";
118 private static final String TAG_SHORTCUT = "shortcut";
119 private static final String TAG_EXTRA = "extra";
120
121 private static final String ATTR_CONTAINER = "container";
122 private static final String ATTR_RANK = "rank";
123
124 private static final String ATTR_PACKAGE_NAME = "packageName";
125 private static final String ATTR_CLASS_NAME = "className";
126 private static final String ATTR_TITLE = "title";
127 private static final String ATTR_SCREEN = "screen";
128 private static final String ATTR_X = "x";
129 private static final String ATTR_Y = "y";
130 private static final String ATTR_SPAN_X = "spanX";
131 private static final String ATTR_SPAN_Y = "spanY";
132 private static final String ATTR_ICON = "icon";
133 private static final String ATTR_URL = "url";
134
Sunny Goyalb564efb2015-01-23 13:45:20 -0800135 // Attrs for "Include"
136 private static final String ATTR_WORKSPACE = "workspace";
137
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700138 // Style attrs -- "Extra"
139 private static final String ATTR_KEY = "key";
140 private static final String ATTR_VALUE = "value";
141
142 private static final String HOTSEAT_CONTAINER_NAME =
143 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
144
145 private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
146 "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE";
147
Adam Cohen091440a2015-03-18 14:16:05 -0700148 @Thunk final Context mContext;
149 @Thunk final AppWidgetHost mAppWidgetHost;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800150 protected final LayoutParserCallback mCallback;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700151
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700152 protected final PackageManager mPackageManager;
153 protected final Resources mSourceRes;
154 protected final int mLayoutId;
155
156 private final int mHotseatAllAppsRank;
157
158 private final long[] mTemp = new long[2];
Adam Cohen091440a2015-03-18 14:16:05 -0700159 @Thunk final ContentValues mValues;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800160 protected final String mRootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700161
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700162 protected SQLiteDatabase mDb;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700163
164 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700165 LayoutParserCallback callback, Resources res,
166 int layoutId, String rootTag) {
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800167 this(context, appWidgetHost, callback, res, layoutId, rootTag,
Adam Cohen2e6da152015-05-06 11:42:25 -0700168 LauncherAppState.getInstance().getInvariantDeviceProfile().hotseatAllAppsRank);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800169 }
170
171 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
172 LayoutParserCallback callback, Resources res,
173 int layoutId, String rootTag, int hotseatAllAppsRank) {
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 Goyalbb3b02f2015-01-15 12:00:14 -0800184 mHotseatAllAppsRank = hotseatAllAppsRank;
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 Goyal0fe505b2014-08-06 09:55:36 -0700195 Log.w(TAG, "Got exception parsing layout.", e);
196 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;
209 HashMap<String, TagParser> tagParserMap = getLayoutElementsMap();
210 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));
231 out[1] = (rank < mHotseatAllAppsRank) ? rank : (rank + 1);
232 } else {
233 out[0] = Favorites.CONTAINER_DESKTOP;
234 out[1] = Long.parseLong(getAttributeValue(parser, ATTR_SCREEN));
235 }
236 }
237
238 /**
239 * Parses the current node and returns the number of elements added.
240 */
241 protected int parseAndAddNode(
242 XmlResourceParser parser,
243 HashMap<String, TagParser> tagParserMap,
244 ArrayList<Long> screenIds)
245 throws XmlPullParserException, IOException {
Sunny Goyalb564efb2015-01-23 13:45:20 -0800246
247 if (TAG_INCLUDE.equals(parser.getName())) {
248 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
249 if (resId != 0) {
250 // recursively load some more favorites, why not?
251 return parseLayout(resId, screenIds);
252 } else {
253 return 0;
254 }
255 }
256
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700257 mValues.clear();
258 parseContainerAndScreen(parser, mTemp);
259 final long container = mTemp[0];
260 final long screenId = mTemp[1];
261
262 mValues.put(Favorites.CONTAINER, container);
263 mValues.put(Favorites.SCREEN, screenId);
264 mValues.put(Favorites.CELLX, getAttributeValue(parser, ATTR_X));
265 mValues.put(Favorites.CELLY, getAttributeValue(parser, ATTR_Y));
266
267 TagParser tagParser = tagParserMap.get(parser.getName());
268 if (tagParser == null) {
269 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
270 return 0;
271 }
272 long newElementId = tagParser.parseAndAdd(parser);
273 if (newElementId >= 0) {
274 // Keep track of the set of screens which need to be added to the db.
275 if (!screenIds.contains(screenId) &&
276 container == Favorites.CONTAINER_DESKTOP) {
277 screenIds.add(screenId);
278 }
279 return 1;
280 }
281 return 0;
282 }
283
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700284 protected long addShortcut(String title, Intent intent, int type) {
285 long id = mCallback.generateNewItemId();
286 mValues.put(Favorites.INTENT, intent.toUri(0));
287 mValues.put(Favorites.TITLE, title);
288 mValues.put(Favorites.ITEM_TYPE, type);
289 mValues.put(Favorites.SPANX, 1);
290 mValues.put(Favorites.SPANY, 1);
291 mValues.put(Favorites._ID, id);
292 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
293 return -1;
294 } else {
295 return id;
296 }
297 }
298
299 protected HashMap<String, TagParser> getFolderElementsMap() {
300 HashMap<String, TagParser> parsers = new HashMap<String, TagParser>();
301 parsers.put(TAG_APP_ICON, new AppShortcutParser());
302 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700303 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700304 return parsers;
305 }
306
307 protected HashMap<String, TagParser> getLayoutElementsMap() {
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());
311 parsers.put(TAG_FOLDER, new FolderParser());
312 parsers.put(TAG_APPWIDGET, new AppWidgetParser());
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700313 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700314 return parsers;
315 }
316
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700317 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700318 /**
319 * Parses the tag and adds to the db
320 * @return the id of the row added or -1;
321 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700322 long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700323 throws XmlPullParserException, IOException;
324 }
325
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700326 /**
327 * App shortcuts: required attributes packageName and className
328 */
329 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700330
331 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700332 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700333 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
334 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
335
336 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
337 ActivityInfo info;
338 try {
339 ComponentName cn;
340 try {
341 cn = new ComponentName(packageName, className);
342 info = mPackageManager.getActivityInfo(cn, 0);
343 } catch (PackageManager.NameNotFoundException nnfe) {
344 String[] packages = mPackageManager.currentToCanonicalPackageNames(
345 new String[] { packageName });
346 cn = new ComponentName(packages[0], className);
347 info = mPackageManager.getActivityInfo(cn, 0);
348 }
349 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
350 .addCategory(Intent.CATEGORY_LAUNCHER)
351 .setComponent(cn)
352 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
353 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
354
355 return addShortcut(info.loadLabel(mPackageManager).toString(),
356 intent, Favorites.ITEM_TYPE_APPLICATION);
357 } catch (PackageManager.NameNotFoundException e) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700358 Log.e(TAG, "Unable to add favorite: " + packageName + "/" + className, e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700359 }
360 return -1;
361 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700362 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700363 }
364 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700365
366 /**
367 * Helper method to allow extending the parser capabilities
368 */
369 protected long invalidPackageOrClass(XmlResourceParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700370 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700371 return -1;
372 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700373 }
374
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700375 /**
376 * AutoInstall: required attributes packageName and className
377 */
378 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700379
380 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700381 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700382 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
383 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
384 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
385 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
386 return -1;
387 }
388
Sunny Goyal34942622014-08-29 17:20:55 -0700389 mValues.put(Favorites.RESTORED, ShortcutInfo.FLAG_AUTOINTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700390 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
391 .addCategory(Intent.CATEGORY_LAUNCHER)
392 .setComponent(new ComponentName(packageName, className))
393 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
394 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
395 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
396 Favorites.ITEM_TYPE_APPLICATION);
397 }
398 }
399
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700400 /**
401 * Parses a web shortcut. Required attributes url, icon, title
402 */
403 protected class ShortcutParser implements TagParser {
404
405 private final Resources mIconRes;
406
407 public ShortcutParser(Resources iconRes) {
408 mIconRes = iconRes;
409 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700410
411 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700412 public long parseAndAdd(XmlResourceParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700413 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
414 final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0);
415
416 if (titleResId == 0 || iconId == 0) {
417 if (LOGD) Log.d(TAG, "Ignoring shortcut");
418 return -1;
419 }
420
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700421 final Intent intent = parseIntent(parser);
422 if (intent == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700423 return -1;
424 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700425
426 Drawable icon = mIconRes.getDrawable(iconId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700427 if (icon == null) {
428 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon");
429 return -1;
430 }
431
432 ItemInfo.writeBitmap(mValues, Utilities.createIconBitmap(icon, mContext));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700433 mValues.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
434 mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
435 mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));
436
437 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700438 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700439 return addShortcut(mSourceRes.getString(titleResId),
440 intent, Favorites.ITEM_TYPE_SHORTCUT);
441 }
442
443 protected Intent parseIntent(XmlResourceParser parser) {
444 final String url = getAttributeValue(parser, ATTR_URL);
445 if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) {
446 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url);
447 return null;
448 }
449 return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700450 }
451 }
452
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700453 /**
454 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
455 * Options child nodes: <extra key=... value=... />
456 */
457 protected class AppWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700458
459 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700460 public long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700461 throws XmlPullParserException, IOException {
462 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
463 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
464 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
465 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
466 return -1;
467 }
468
469 ComponentName cn = new ComponentName(packageName, className);
470 try {
471 mPackageManager.getReceiverInfo(cn, 0);
472 } catch (Exception e) {
473 String[] packages = mPackageManager.currentToCanonicalPackageNames(
474 new String[] { packageName });
475 cn = new ComponentName(packages[0], className);
476 try {
477 mPackageManager.getReceiverInfo(cn, 0);
478 } catch (Exception e1) {
479 if (LOGD) Log.d(TAG, "Can't find widget provider: " + className);
480 return -1;
481 }
482 }
483
484 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
485 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
486
487 // Read the extras
488 Bundle extras = new Bundle();
489 int widgetDepth = parser.getDepth();
490 int type;
491 while ((type = parser.next()) != XmlPullParser.END_TAG ||
492 parser.getDepth() > widgetDepth) {
493 if (type != XmlPullParser.START_TAG) {
494 continue;
495 }
496
497 if (TAG_EXTRA.equals(parser.getName())) {
498 String key = getAttributeValue(parser, ATTR_KEY);
499 String value = getAttributeValue(parser, ATTR_VALUE);
500 if (key != null && value != null) {
501 extras.putString(key, value);
502 } else {
503 throw new RuntimeException("Widget extras must have a key and value");
504 }
505 } else {
506 throw new RuntimeException("Widgets can contain only extras");
507 }
508 }
509
510 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
511 long insertedId = -1;
512 try {
513 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
514
515 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn)) {
516 if (LOGD) Log.e(TAG, "Unable to bind app widget id " + cn);
517 return -1;
518 }
519
520 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
521 mValues.put(Favorites.APPWIDGET_ID, appWidgetId);
522 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
523 mValues.put(Favorites._ID, mCallback.generateNewItemId());
524 insertedId = mCallback.insertAndCheck(mDb, mValues);
525 if (insertedId < 0) {
526 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
527 return insertedId;
528 }
529
530 // Send a broadcast to configure the widget
531 if (!extras.isEmpty()) {
532 Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE);
533 intent.setComponent(cn);
534 intent.putExtras(extras);
535 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
536 mContext.sendBroadcast(intent);
537 }
538 } catch (RuntimeException ex) {
539 if (LOGD) Log.e(TAG, "Problem allocating appWidgetId", ex);
540 }
541 return insertedId;
542 }
543 }
544
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700545 protected class FolderParser implements TagParser {
546 private final HashMap<String, TagParser> mFolderElements;
547
548 public FolderParser() {
549 this(getFolderElementsMap());
550 }
551
552 public FolderParser(HashMap<String, TagParser> elements) {
553 mFolderElements = elements;
554 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700555
556 @Override
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700557 public long parseAndAdd(XmlResourceParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700558 throws XmlPullParserException, IOException {
559 final String title;
560 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
561 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700562 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700563 } else {
564 title = mContext.getResources().getString(R.string.folder_name);
565 }
566
567 mValues.put(Favorites.TITLE, title);
568 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
569 mValues.put(Favorites.SPANX, 1);
570 mValues.put(Favorites.SPANY, 1);
571 mValues.put(Favorites._ID, mCallback.generateNewItemId());
572 long folderId = mCallback.insertAndCheck(mDb, mValues);
573 if (folderId < 0) {
574 if (LOGD) Log.e(TAG, "Unable to add folder");
575 return -1;
576 }
577
578 final ContentValues myValues = new ContentValues(mValues);
579 ArrayList<Long> folderItems = new ArrayList<Long>();
580
581 int type;
582 int folderDepth = parser.getDepth();
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700583 int rank = 0;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700584 while ((type = parser.next()) != XmlPullParser.END_TAG ||
585 parser.getDepth() > folderDepth) {
586 if (type != XmlPullParser.START_TAG) {
587 continue;
588 }
589 mValues.clear();
590 mValues.put(Favorites.CONTAINER, folderId);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700591 mValues.put(Favorites.RANK, rank);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700592
593 TagParser tagParser = mFolderElements.get(parser.getName());
594 if (tagParser != null) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700595 final long id = tagParser.parseAndAdd(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700596 if (id >= 0) {
597 folderItems.add(id);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700598 rank++;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700599 }
600 } else {
601 throw new RuntimeException("Invalid folder item " + parser.getName());
602 }
603 }
604
605 long addedId = folderId;
606
607 // We can only have folders with >= 2 items, so we need to remove the
608 // folder and clean up if less than 2 items were included, or some
609 // failed to add, and less than 2 were actually added
610 if (folderItems.size() < 2) {
611 // Delete the folder
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700612 Uri uri = Favorites.getContentUri(folderId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700613 SqlArguments args = new SqlArguments(uri, null, null);
614 mDb.delete(args.table, args.where, args.args);
615 addedId = -1;
616
617 // If we have a single item, promote it to where the folder
618 // would have been.
619 if (folderItems.size() == 1) {
620 final ContentValues childValues = new ContentValues();
621 copyInteger(myValues, childValues, Favorites.CONTAINER);
622 copyInteger(myValues, childValues, Favorites.SCREEN);
623 copyInteger(myValues, childValues, Favorites.CELLX);
624 copyInteger(myValues, childValues, Favorites.CELLY);
625
626 addedId = folderItems.get(0);
627 mDb.update(LauncherProvider.TABLE_FAVORITES, childValues,
628 Favorites._ID + "=" + addedId, null);
629 }
630 }
631 return addedId;
632 }
633 }
634
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700635 protected static final void beginDocument(XmlPullParser parser, String firstElementName)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700636 throws XmlPullParserException, IOException {
637 int type;
638 while ((type = parser.next()) != XmlPullParser.START_TAG
639 && type != XmlPullParser.END_DOCUMENT);
640
641 if (type != XmlPullParser.START_TAG) {
642 throw new XmlPullParserException("No start tag found");
643 }
644
645 if (!parser.getName().equals(firstElementName)) {
646 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
647 ", expected " + firstElementName);
648 }
649 }
650
651 /**
652 * Return attribute value, attempting launcher-specific namespace first
653 * before falling back to anonymous attribute.
654 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700655 protected static String getAttributeValue(XmlResourceParser parser, String attribute) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700656 String value = parser.getAttributeValue(
657 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
658 if (value == null) {
659 value = parser.getAttributeValue(null, attribute);
660 }
661 return value;
662 }
663
664 /**
665 * Return attribute resource value, attempting launcher-specific namespace
666 * first before falling back to anonymous attribute.
667 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700668 protected static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700669 int defaultValue) {
670 int value = parser.getAttributeResourceValue(
671 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
672 defaultValue);
673 if (value == defaultValue) {
674 value = parser.getAttributeResourceValue(null, attribute, defaultValue);
675 }
676 return value;
677 }
678
679 public static interface LayoutParserCallback {
680 long generateNewItemId();
681
682 long insertAndCheck(SQLiteDatabase db, ContentValues values);
683 }
684
Adam Cohen091440a2015-03-18 14:16:05 -0700685 @Thunk static void copyInteger(ContentValues from, ContentValues to, String key) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700686 to.put(key, from.getAsInteger(key));
687 }
688}