• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.launcher3;
18 
19 import android.appwidget.AppWidgetHost;
20 import android.content.ComponentName;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.ActivityInfo;
25 import android.content.pm.PackageManager;
26 import android.content.res.Resources;
27 import android.database.sqlite.SQLiteDatabase;
28 import android.graphics.drawable.Drawable;
29 import android.net.Uri;
30 import android.os.Build.VERSION;
31 import android.os.Bundle;
32 import android.os.Process;
33 import android.text.TextUtils;
34 import android.util.ArrayMap;
35 import android.util.AttributeSet;
36 import android.util.Log;
37 import android.util.Pair;
38 import android.util.Patterns;
39 import android.util.Xml;
40 
41 import com.android.launcher3.LauncherProvider.SqlArguments;
42 import com.android.launcher3.LauncherSettings.Favorites;
43 import com.android.launcher3.icons.GraphicsUtils;
44 import com.android.launcher3.icons.LauncherIcons;
45 import com.android.launcher3.util.IntArray;
46 import com.android.launcher3.util.Thunk;
47 
48 import org.xmlpull.v1.XmlPullParser;
49 import org.xmlpull.v1.XmlPullParserException;
50 
51 import java.io.IOException;
52 import java.util.Locale;
53 import java.util.function.Supplier;
54 
55 /**
56  * Layout parsing code for auto installs layout
57  */
58 public class AutoInstallsLayout {
59     private static final String TAG = "AutoInstalls";
60     private static final boolean LOGD = false;
61 
62     /** Marker action used to discover a package which defines launcher customization */
63     static final String ACTION_LAUNCHER_CUSTOMIZATION =
64             "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
65 
66     /**
67      * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
68      */
69     private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
70     private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
71     private static final String LAYOUT_RES = "default_layout";
72 
get(Context context, AppWidgetHost appWidgetHost, LayoutParserCallback callback)73     static AutoInstallsLayout get(Context context, AppWidgetHost appWidgetHost,
74             LayoutParserCallback callback) {
75         Pair<String, Resources> customizationApkInfo = Utilities.findSystemApk(
76                 ACTION_LAUNCHER_CUSTOMIZATION, context.getPackageManager());
77         if (customizationApkInfo == null) {
78             return null;
79         }
80         String pkg = customizationApkInfo.first;
81         Resources targetRes = customizationApkInfo.second;
82         InvariantDeviceProfile grid = LauncherAppState.getIDP(context);
83 
84         // Try with grid size and hotseat count
85         String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
86             grid.numColumns, grid.numRows, grid.numHotseatIcons);
87         int layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
88 
89         // Try with only grid size
90         if (layoutId == 0) {
91             Log.d(TAG, "Formatted layout: " + layoutName
92                     + " not found. Trying layout without hosteat");
93             layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
94                 grid.numColumns, grid.numRows);
95             layoutId = targetRes.getIdentifier(layoutName, "xml", pkg);
96         }
97 
98         // Try the default layout
99         if (layoutId == 0) {
100             Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
101             layoutId = targetRes.getIdentifier(LAYOUT_RES, "xml", pkg);
102         }
103 
104         if (layoutId == 0) {
105             Log.e(TAG, "Layout definition not found in package: " + pkg);
106             return null;
107         }
108         return new AutoInstallsLayout(context, appWidgetHost, callback, targetRes, layoutId,
109                 TAG_WORKSPACE);
110     }
111 
112     // Object Tags
113     private static final String TAG_INCLUDE = "include";
114     public static final String TAG_WORKSPACE = "workspace";
115     private static final String TAG_APP_ICON = "appicon";
116     private static final String TAG_AUTO_INSTALL = "autoinstall";
117     private static final String TAG_FOLDER = "folder";
118     private static final String TAG_APPWIDGET = "appwidget";
119     private static final String TAG_SHORTCUT = "shortcut";
120     private static final String TAG_EXTRA = "extra";
121 
122     private static final String ATTR_CONTAINER = "container";
123     private static final String ATTR_RANK = "rank";
124 
125     private static final String ATTR_PACKAGE_NAME = "packageName";
126     private static final String ATTR_CLASS_NAME = "className";
127     private static final String ATTR_TITLE = "title";
128     private static final String ATTR_SCREEN = "screen";
129 
130     // x and y can be specified as negative integers, in which case -1 represents the
131     // last row / column, -2 represents the second last, and so on.
132     private static final String ATTR_X = "x";
133     private static final String ATTR_Y = "y";
134 
135     private static final String ATTR_SPAN_X = "spanX";
136     private static final String ATTR_SPAN_Y = "spanY";
137     private static final String ATTR_ICON = "icon";
138     private static final String ATTR_URL = "url";
139 
140     // Attrs for "Include"
141     private static final String ATTR_WORKSPACE = "workspace";
142 
143     // Style attrs -- "Extra"
144     private static final String ATTR_KEY = "key";
145     private static final String ATTR_VALUE = "value";
146 
147     private static final String HOTSEAT_CONTAINER_NAME =
148             Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
149 
150     @Thunk final Context mContext;
151     @Thunk final AppWidgetHost mAppWidgetHost;
152     protected final LayoutParserCallback mCallback;
153 
154     protected final PackageManager mPackageManager;
155     protected final Resources mSourceRes;
156     protected final Supplier<XmlPullParser> mInitialLayoutSupplier;
157 
158     private final InvariantDeviceProfile mIdp;
159     private final int mRowCount;
160     private final int mColumnCount;
161 
162     private final int[] mTemp = new int[2];
163     @Thunk final ContentValues mValues;
164     protected final String mRootTag;
165 
166     protected SQLiteDatabase mDb;
167 
AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost, LayoutParserCallback callback, Resources res, int layoutId, String rootTag)168     public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
169             LayoutParserCallback callback, Resources res,
170             int layoutId, String rootTag) {
171         this(context, appWidgetHost, callback, res, () -> res.getXml(layoutId), rootTag);
172     }
173 
AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost, LayoutParserCallback callback, Resources res, Supplier<XmlPullParser> initialLayoutSupplier, String rootTag)174     public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
175             LayoutParserCallback callback, Resources res,
176             Supplier<XmlPullParser> initialLayoutSupplier, String rootTag) {
177         mContext = context;
178         mAppWidgetHost = appWidgetHost;
179         mCallback = callback;
180 
181         mPackageManager = context.getPackageManager();
182         mValues = new ContentValues();
183         mRootTag = rootTag;
184 
185         mSourceRes = res;
186         mInitialLayoutSupplier = initialLayoutSupplier;
187 
188         mIdp = LauncherAppState.getIDP(context);
189         mRowCount = mIdp.numRows;
190         mColumnCount = mIdp.numColumns;
191     }
192 
193     /**
194      * Loads the layout in the db and returns the number of entries added on the desktop.
195      */
loadLayout(SQLiteDatabase db, IntArray screenIds)196     public int loadLayout(SQLiteDatabase db, IntArray screenIds) {
197         mDb = db;
198         try {
199             return parseLayout(mInitialLayoutSupplier.get(), screenIds);
200         } catch (Exception e) {
201             Log.e(TAG, "Error parsing layout: ", e);
202             return -1;
203         }
204     }
205 
206     /**
207      * Parses the layout and returns the number of elements added on the homescreen.
208      */
parseLayout(XmlPullParser parser, IntArray screenIds)209     protected int parseLayout(XmlPullParser parser, IntArray screenIds)
210             throws XmlPullParserException, IOException {
211         beginDocument(parser, mRootTag);
212         final int depth = parser.getDepth();
213         int type;
214         ArrayMap<String, TagParser> tagParserMap = getLayoutElementsMap();
215         int count = 0;
216 
217         while (((type = parser.next()) != XmlPullParser.END_TAG ||
218                 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
219             if (type != XmlPullParser.START_TAG) {
220                 continue;
221             }
222             count += parseAndAddNode(parser, tagParserMap, screenIds);
223         }
224         return count;
225     }
226 
227     /**
228      * Parses container and screenId attribute from the current tag, and puts it in the out.
229      * @param out array of size 2.
230      */
parseContainerAndScreen(XmlPullParser parser, int[] out)231     protected void parseContainerAndScreen(XmlPullParser parser, int[] out) {
232         if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) {
233             out[0] = Favorites.CONTAINER_HOTSEAT;
234             // Hack: hotseat items are stored using screen ids
235             out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_RANK));
236         } else {
237             out[0] = Favorites.CONTAINER_DESKTOP;
238             out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_SCREEN));
239         }
240     }
241 
242     /**
243      * Parses the current node and returns the number of elements added.
244      */
parseAndAddNode( XmlPullParser parser, ArrayMap<String, TagParser> tagParserMap, IntArray screenIds)245     protected int parseAndAddNode(
246             XmlPullParser parser, ArrayMap<String, TagParser> tagParserMap, IntArray screenIds)
247         throws XmlPullParserException, IOException {
248 
249         if (TAG_INCLUDE.equals(parser.getName())) {
250             final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
251             if (resId != 0) {
252                 // recursively load some more favorites, why not?
253                 return parseLayout(mSourceRes.getXml(resId), screenIds);
254             } else {
255                 return 0;
256             }
257         }
258 
259         mValues.clear();
260         parseContainerAndScreen(parser, mTemp);
261         final int container = mTemp[0];
262         final int screenId = mTemp[1];
263 
264         mValues.put(Favorites.CONTAINER, container);
265         mValues.put(Favorites.SCREEN, screenId);
266 
267         mValues.put(Favorites.CELLX,
268                 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount));
269         mValues.put(Favorites.CELLY,
270                 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount));
271 
272         TagParser tagParser = tagParserMap.get(parser.getName());
273         if (tagParser == null) {
274             if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
275             return 0;
276         }
277         int newElementId = tagParser.parseAndAdd(parser);
278         if (newElementId >= 0) {
279             // Keep track of the set of screens which need to be added to the db.
280             if (!screenIds.contains(screenId) &&
281                     container == Favorites.CONTAINER_DESKTOP) {
282                 screenIds.add(screenId);
283             }
284             return 1;
285         }
286         return 0;
287     }
288 
addShortcut(String title, Intent intent, int type)289     protected int addShortcut(String title, Intent intent, int type) {
290         int id = mCallback.generateNewItemId();
291         mValues.put(Favorites.INTENT, intent.toUri(0));
292         mValues.put(Favorites.TITLE, title);
293         mValues.put(Favorites.ITEM_TYPE, type);
294         mValues.put(Favorites.SPANX, 1);
295         mValues.put(Favorites.SPANY, 1);
296         mValues.put(Favorites._ID, id);
297         if (mCallback.insertAndCheck(mDb, mValues) < 0) {
298             return -1;
299         } else {
300             return id;
301         }
302     }
303 
getFolderElementsMap()304     protected ArrayMap<String, TagParser> getFolderElementsMap() {
305         ArrayMap<String, TagParser> parsers = new ArrayMap<>();
306         parsers.put(TAG_APP_ICON, new AppShortcutParser());
307         parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
308         parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
309         return parsers;
310     }
311 
getLayoutElementsMap()312     protected ArrayMap<String, TagParser> getLayoutElementsMap() {
313         ArrayMap<String, TagParser> parsers = new ArrayMap<>();
314         parsers.put(TAG_APP_ICON, new AppShortcutParser());
315         parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
316         parsers.put(TAG_FOLDER, new FolderParser());
317         parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
318         parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
319         return parsers;
320     }
321 
322     protected interface TagParser {
323         /**
324          * Parses the tag and adds to the db
325          * @return the id of the row added or -1;
326          */
parseAndAdd(XmlPullParser parser)327         int parseAndAdd(XmlPullParser parser)
328                 throws XmlPullParserException, IOException;
329     }
330 
331     /**
332      * App shortcuts: required attributes packageName and className
333      */
334     protected class AppShortcutParser implements TagParser {
335 
336         @Override
parseAndAdd(XmlPullParser parser)337         public int parseAndAdd(XmlPullParser parser) {
338             final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
339             final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
340 
341             if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
342                 ActivityInfo info;
343                 try {
344                     ComponentName cn;
345                     try {
346                         cn = new ComponentName(packageName, className);
347                         info = mPackageManager.getActivityInfo(cn, 0);
348                     } catch (PackageManager.NameNotFoundException nnfe) {
349                         String[] packages = mPackageManager.currentToCanonicalPackageNames(
350                                 new String[] { packageName });
351                         cn = new ComponentName(packages[0], className);
352                         info = mPackageManager.getActivityInfo(cn, 0);
353                     }
354                     final Intent intent = new Intent(Intent.ACTION_MAIN, null)
355                         .addCategory(Intent.CATEGORY_LAUNCHER)
356                         .setComponent(cn)
357                         .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
358                                 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
359 
360                     return addShortcut(info.loadLabel(mPackageManager).toString(),
361                             intent, Favorites.ITEM_TYPE_APPLICATION);
362                 } catch (PackageManager.NameNotFoundException e) {
363                     Log.e(TAG, "Favorite not found: " + packageName + "/" + className);
364                 }
365                 return -1;
366             } else {
367                 return invalidPackageOrClass(parser);
368             }
369         }
370 
371         /**
372          * Helper method to allow extending the parser capabilities
373          */
invalidPackageOrClass(XmlPullParser parser)374         protected int invalidPackageOrClass(XmlPullParser parser) {
375             Log.w(TAG, "Skipping invalid <favorite> with no component");
376             return -1;
377         }
378     }
379 
380     /**
381      * AutoInstall: required attributes packageName and className
382      */
383     protected class AutoInstallParser implements TagParser {
384 
385         @Override
parseAndAdd(XmlPullParser parser)386         public int parseAndAdd(XmlPullParser parser) {
387             final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
388             final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
389             if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
390                 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
391                 return -1;
392             }
393 
394             mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_AUTOINSTALL_ICON);
395             final Intent intent = new Intent(Intent.ACTION_MAIN, null)
396                 .addCategory(Intent.CATEGORY_LAUNCHER)
397                 .setComponent(new ComponentName(packageName, className))
398                 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
399                         Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
400             return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
401                     Favorites.ITEM_TYPE_APPLICATION);
402         }
403     }
404 
405     /**
406      * Parses a web shortcut. Required attributes url, icon, title
407      */
408     protected class ShortcutParser implements TagParser {
409 
410         private final Resources mIconRes;
411 
ShortcutParser(Resources iconRes)412         public ShortcutParser(Resources iconRes) {
413             mIconRes = iconRes;
414         }
415 
416         @Override
parseAndAdd(XmlPullParser parser)417         public int parseAndAdd(XmlPullParser parser) {
418             final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
419             final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0);
420 
421             if (titleResId == 0 || iconId == 0) {
422                 if (LOGD) Log.d(TAG, "Ignoring shortcut");
423                 return -1;
424             }
425 
426             final Intent intent = parseIntent(parser);
427             if (intent == null) {
428                 return -1;
429             }
430 
431             Drawable icon = mIconRes.getDrawable(iconId);
432             if (icon == null) {
433                 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon");
434                 return -1;
435             }
436 
437             // Auto installs should always support the current platform version.
438             LauncherIcons li = LauncherIcons.obtain(mContext);
439             mValues.put(LauncherSettings.Favorites.ICON, GraphicsUtils.flattenBitmap(
440                     li.createBadgedIconBitmap(icon, Process.myUserHandle(), VERSION.SDK_INT).icon));
441             li.recycle();
442 
443             mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId));
444             mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId));
445 
446             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
447                         Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
448             return addShortcut(mSourceRes.getString(titleResId),
449                     intent, Favorites.ITEM_TYPE_SHORTCUT);
450         }
451 
parseIntent(XmlPullParser parser)452         protected Intent parseIntent(XmlPullParser parser) {
453             final String url = getAttributeValue(parser, ATTR_URL);
454             if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) {
455                 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url);
456                 return null;
457             }
458             return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url));
459         }
460     }
461 
462     /**
463      * AppWidget parser: Required attributes packageName, className, spanX and spanY.
464      * Options child nodes: <extra key=... value=... />
465      * It adds a pending widget which allows the widget to come later. If there are extras, those
466      * are passed to widget options during bind.
467      * The config activity for the widget (if present) is not shown, so any optional configurations
468      * should be passed as extras and the widget should support reading these widget options.
469      */
470     protected class PendingWidgetParser implements TagParser {
471 
472         @Override
parseAndAdd(XmlPullParser parser)473         public int parseAndAdd(XmlPullParser parser)
474                 throws XmlPullParserException, IOException {
475             final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
476             final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
477             if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
478                 if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component");
479                 return -1;
480             }
481 
482             mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
483             mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
484             mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
485 
486             // Read the extras
487             Bundle extras = new Bundle();
488             int widgetDepth = parser.getDepth();
489             int type;
490             while ((type = parser.next()) != XmlPullParser.END_TAG ||
491                     parser.getDepth() > widgetDepth) {
492                 if (type != XmlPullParser.START_TAG) {
493                     continue;
494                 }
495 
496                 if (TAG_EXTRA.equals(parser.getName())) {
497                     String key = getAttributeValue(parser, ATTR_KEY);
498                     String value = getAttributeValue(parser, ATTR_VALUE);
499                     if (key != null && value != null) {
500                         extras.putString(key, value);
501                     } else {
502                         throw new RuntimeException("Widget extras must have a key and value");
503                     }
504                 } else {
505                     throw new RuntimeException("Widgets can contain only extras");
506                 }
507             }
508 
509             return verifyAndInsert(new ComponentName(packageName, className), extras);
510         }
511 
verifyAndInsert(ComponentName cn, Bundle extras)512         protected int verifyAndInsert(ComponentName cn, Bundle extras) {
513             mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
514             mValues.put(Favorites.RESTORED,
515                     LauncherAppWidgetInfo.FLAG_ID_NOT_VALID |
516                             LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY |
517                             LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
518             mValues.put(Favorites._ID, mCallback.generateNewItemId());
519             if (!extras.isEmpty()) {
520                 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
521             }
522 
523             int insertedId = mCallback.insertAndCheck(mDb, mValues);
524             if (insertedId < 0) {
525                 return -1;
526             } else {
527                 return insertedId;
528             }
529         }
530     }
531 
532     protected class FolderParser implements TagParser {
533         private final ArrayMap<String, TagParser> mFolderElements;
534 
FolderParser()535         public FolderParser() {
536             this(getFolderElementsMap());
537         }
538 
FolderParser(ArrayMap<String, TagParser> elements)539         public FolderParser(ArrayMap<String, TagParser> elements) {
540             mFolderElements = elements;
541         }
542 
543         @Override
parseAndAdd(XmlPullParser parser)544         public int parseAndAdd(XmlPullParser parser)
545                 throws XmlPullParserException, IOException {
546             final String title;
547             final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
548             if (titleResId != 0) {
549                 title = mSourceRes.getString(titleResId);
550             } else {
551                 title = mContext.getResources().getString(R.string.folder_name);
552             }
553 
554             mValues.put(Favorites.TITLE, title);
555             mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
556             mValues.put(Favorites.SPANX, 1);
557             mValues.put(Favorites.SPANY, 1);
558             mValues.put(Favorites._ID, mCallback.generateNewItemId());
559             int folderId = mCallback.insertAndCheck(mDb, mValues);
560             if (folderId < 0) {
561                 if (LOGD) Log.e(TAG, "Unable to add folder");
562                 return -1;
563             }
564 
565             final ContentValues myValues = new ContentValues(mValues);
566             IntArray folderItems = new IntArray();
567 
568             int type;
569             int folderDepth = parser.getDepth();
570             int rank = 0;
571             while ((type = parser.next()) != XmlPullParser.END_TAG ||
572                     parser.getDepth() > folderDepth) {
573                 if (type != XmlPullParser.START_TAG) {
574                     continue;
575                 }
576                 mValues.clear();
577                 mValues.put(Favorites.CONTAINER, folderId);
578                 mValues.put(Favorites.RANK, rank);
579 
580                 TagParser tagParser = mFolderElements.get(parser.getName());
581                 if (tagParser != null) {
582                     final int id = tagParser.parseAndAdd(parser);
583                     if (id >= 0) {
584                         folderItems.add(id);
585                         rank++;
586                     }
587                 } else {
588                     throw new RuntimeException("Invalid folder item " + parser.getName());
589                 }
590             }
591 
592             int addedId = folderId;
593 
594             // We can only have folders with >= 2 items, so we need to remove the
595             // folder and clean up if less than 2 items were included, or some
596             // failed to add, and less than 2 were actually added
597             if (folderItems.size() < 2) {
598                 // Delete the folder
599                 Uri uri = Favorites.getContentUri(folderId);
600                 SqlArguments args = new SqlArguments(uri, null, null);
601                 mDb.delete(args.table, args.where, args.args);
602                 addedId = -1;
603 
604                 // If we have a single item, promote it to where the folder
605                 // would have been.
606                 if (folderItems.size() == 1) {
607                     final ContentValues childValues = new ContentValues();
608                     copyInteger(myValues, childValues, Favorites.CONTAINER);
609                     copyInteger(myValues, childValues, Favorites.SCREEN);
610                     copyInteger(myValues, childValues, Favorites.CELLX);
611                     copyInteger(myValues, childValues, Favorites.CELLY);
612 
613                     addedId = folderItems.get(0);
614                     mDb.update(Favorites.TABLE_NAME, childValues,
615                             Favorites._ID + "=" + addedId, null);
616                 }
617             }
618             return addedId;
619         }
620     }
621 
beginDocument(XmlPullParser parser, String firstElementName)622     protected static void beginDocument(XmlPullParser parser, String firstElementName)
623             throws XmlPullParserException, IOException {
624         int type;
625         while ((type = parser.next()) != XmlPullParser.START_TAG
626                 && type != XmlPullParser.END_DOCUMENT);
627 
628         if (type != XmlPullParser.START_TAG) {
629             throw new XmlPullParserException("No start tag found");
630         }
631 
632         if (!parser.getName().equals(firstElementName)) {
633             throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
634                     ", expected " + firstElementName);
635         }
636     }
637 
convertToDistanceFromEnd(String value, int endValue)638     private static String convertToDistanceFromEnd(String value, int endValue) {
639         if (!TextUtils.isEmpty(value)) {
640             int x = Integer.parseInt(value);
641             if (x < 0) {
642                 return Integer.toString(endValue + x);
643             }
644         }
645         return value;
646     }
647 
648     /**
649      * Return attribute value, attempting launcher-specific namespace first
650      * before falling back to anonymous attribute.
651      */
getAttributeValue(XmlPullParser parser, String attribute)652     protected static String getAttributeValue(XmlPullParser parser, String attribute) {
653         String value = parser.getAttributeValue(
654                 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
655         if (value == null) {
656             value = parser.getAttributeValue(null, attribute);
657         }
658         return value;
659     }
660 
661     /**
662      * Return attribute resource value, attempting launcher-specific namespace
663      * first before falling back to anonymous attribute.
664      */
getAttributeResourceValue(XmlPullParser parser, String attribute, int defaultValue)665     protected static int getAttributeResourceValue(XmlPullParser parser, String attribute,
666             int defaultValue) {
667         AttributeSet attrs = Xml.asAttributeSet(parser);
668         int value = attrs.getAttributeResourceValue(
669                 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
670                 defaultValue);
671         if (value == defaultValue) {
672             value = attrs.getAttributeResourceValue(null, attribute, defaultValue);
673         }
674         return value;
675     }
676 
677     public interface LayoutParserCallback {
generateNewItemId()678         int generateNewItemId();
679 
insertAndCheck(SQLiteDatabase db, ContentValues values)680         int insertAndCheck(SQLiteDatabase db, ContentValues values);
681     }
682 
copyInteger(ContentValues from, ContentValues to, String key)683     @Thunk static void copyInteger(ContentValues from, ContentValues to, String key) {
684         to.put(key, from.getAsInteger(key));
685     }
686 }
687