Home | History | Annotate | Download | only in launcher3
      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.appwidget.AppWidgetManager;
     21 import android.content.ComponentName;
     22 import android.content.ContentValues;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.pm.ActivityInfo;
     26 import android.content.pm.PackageManager;
     27 import android.content.res.Resources;
     28 import android.content.res.XmlResourceParser;
     29 import android.database.sqlite.SQLiteDatabase;
     30 import android.graphics.drawable.Drawable;
     31 import android.net.Uri;
     32 import android.os.Bundle;
     33 import android.text.TextUtils;
     34 import android.util.Log;
     35 import android.util.Pair;
     36 import android.util.Patterns;
     37 
     38 import com.android.launcher3.LauncherProvider.SqlArguments;
     39 import com.android.launcher3.LauncherSettings.Favorites;
     40 import com.android.launcher3.util.Thunk;
     41 
     42 import org.xmlpull.v1.XmlPullParser;
     43 import org.xmlpull.v1.XmlPullParserException;
     44 
     45 import java.io.IOException;
     46 import java.util.ArrayList;
     47 import java.util.HashMap;
     48 import java.util.Locale;
     49 
     50 /**
     51  * Layout parsing code for auto installs layout
     52  */
     53 public class AutoInstallsLayout {
     54     private static final String TAG = "AutoInstalls";
     55     private static final boolean LOGD = false;
     56 
     57     /** Marker action used to discover a package which defines launcher customization */
     58     static final String ACTION_LAUNCHER_CUSTOMIZATION =
     59             "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
     60 
     61     /**
     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";
     66     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         }
     75         return get(context, customizationApkInfo.first, customizationApkInfo.second,
     76                 appWidgetHost, callback);
     77     }
     78 
     79     static AutoInstallsLayout get(Context context, String pkg, Resources targetRes,
     80             AppWidgetHost appWidgetHost, LayoutParserCallback callback) {
     81         InvariantDeviceProfile grid = LauncherAppState.getInstance().getInvariantDeviceProfile();
     82 
     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 
    103         if (layoutId == 0) {
    104             Log.e(TAG, "Layout definition not found in package: " + pkg);
    105             return null;
    106         }
    107         return new AutoInstallsLayout(context, appWidgetHost, callback, targetRes, layoutId,
    108                 TAG_WORKSPACE);
    109     }
    110 
    111     // Object Tags
    112     private static final String TAG_INCLUDE = "include";
    113     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 
    135     // Attrs for "Include"
    136     private static final String ATTR_WORKSPACE = "workspace";
    137 
    138     // 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 
    148     @Thunk final Context mContext;
    149     @Thunk final AppWidgetHost mAppWidgetHost;
    150     protected final LayoutParserCallback mCallback;
    151 
    152     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];
    159     @Thunk final ContentValues mValues;
    160     protected final String mRootTag;
    161 
    162     protected SQLiteDatabase mDb;
    163 
    164     public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
    165             LayoutParserCallback callback, Resources res,
    166             int layoutId, String rootTag) {
    167         this(context, appWidgetHost, callback, res, layoutId, rootTag,
    168                 LauncherAppState.getInstance().getInvariantDeviceProfile().hotseatAllAppsRank);
    169     }
    170 
    171     public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost,
    172             LayoutParserCallback callback, Resources res,
    173             int layoutId, String rootTag, int hotseatAllAppsRank) {
    174         mContext = context;
    175         mAppWidgetHost = appWidgetHost;
    176         mCallback = callback;
    177 
    178         mPackageManager = context.getPackageManager();
    179         mValues = new ContentValues();
    180         mRootTag = rootTag;
    181 
    182         mSourceRes = res;
    183         mLayoutId = layoutId;
    184         mHotseatAllAppsRank = hotseatAllAppsRank;
    185     }
    186 
    187     /**
    188      * Loads the layout in the db and returns the number of entries added on the desktop.
    189      */
    190     public int loadLayout(SQLiteDatabase db, ArrayList<Long> screenIds) {
    191         mDb = db;
    192         try {
    193             return parseLayout(mLayoutId, screenIds);
    194         } catch (Exception e) {
    195             Log.w(TAG, "Got exception parsing layout.", e);
    196             return -1;
    197         }
    198     }
    199 
    200     /**
    201      * Parses the layout and returns the number of elements added on the homescreen.
    202      */
    203     protected int parseLayout(int layoutId, ArrayList<Long> screenIds)
    204             throws XmlPullParserException, IOException {
    205         XmlResourceParser parser = mSourceRes.getXml(layoutId);
    206         beginDocument(parser, mRootTag);
    207         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             }
    217             count += parseAndAddNode(parser, tagParserMap, screenIds);
    218         }
    219         return count;
    220     }
    221 
    222     /**
    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 {
    246 
    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 
    257         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 
    284     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());
    303         parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
    304         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());
    313         parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes));
    314         return parsers;
    315     }
    316 
    317     protected interface TagParser {
    318         /**
    319          * Parses the tag and adds to the db
    320          * @return the id of the row added or -1;
    321          */
    322         long parseAndAdd(XmlResourceParser parser)
    323                 throws XmlPullParserException, IOException;
    324     }
    325 
    326     /**
    327      * App shortcuts: required attributes packageName and className
    328      */
    329     protected class AppShortcutParser implements TagParser {
    330 
    331         @Override
    332         public long parseAndAdd(XmlResourceParser parser) {
    333             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) {
    358                     if (LOGD) Log.w(TAG, "Unable to add favorite: " + packageName + "/" + className, e);
    359                 }
    360                 return -1;
    361             } else {
    362                 return invalidPackageOrClass(parser);
    363             }
    364         }
    365 
    366         /**
    367          * Helper method to allow extending the parser capabilities
    368          */
    369         protected long invalidPackageOrClass(XmlResourceParser parser) {
    370             if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
    371             return -1;
    372         }
    373     }
    374 
    375     /**
    376      * AutoInstall: required attributes packageName and className
    377      */
    378     protected class AutoInstallParser implements TagParser {
    379 
    380         @Override
    381         public long parseAndAdd(XmlResourceParser parser) {
    382             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 
    389             mValues.put(Favorites.RESTORED, ShortcutInfo.FLAG_AUTOINTALL_ICON);
    390             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 
    400     /**
    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         }
    410 
    411         @Override
    412         public long parseAndAdd(XmlResourceParser parser) {
    413             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 
    421             final Intent intent = parseIntent(parser);
    422             if (intent == null) {
    423                 return -1;
    424             }
    425 
    426             Drawable icon = mIconRes.getDrawable(iconId);
    427             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));
    433             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 |
    438                         Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    439             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));
    450         }
    451     }
    452 
    453     /**
    454      * AppWidget parser: Required attributes packageName, className, spanX and spanY.
    455      * Options child nodes: <extra key=... value=... />
    456      */
    457     protected class AppWidgetParser implements TagParser {
    458 
    459         @Override
    460         public long parseAndAdd(XmlResourceParser parser)
    461                 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 
    545     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         }
    555 
    556         @Override
    557         public long parseAndAdd(XmlResourceParser parser)
    558                 throws XmlPullParserException, IOException {
    559             final String title;
    560             final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
    561             if (titleResId != 0) {
    562                 title = mSourceRes.getString(titleResId);
    563             } 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();
    583             int rank = 0;
    584             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);
    591                 mValues.put(Favorites.RANK, rank);
    592 
    593                 TagParser tagParser = mFolderElements.get(parser.getName());
    594                 if (tagParser != null) {
    595                     final long id = tagParser.parseAndAdd(parser);
    596                     if (id >= 0) {
    597                         folderItems.add(id);
    598                         rank++;
    599                     }
    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
    612                 Uri uri = Favorites.getContentUri(folderId);
    613                 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 
    635     protected static final void beginDocument(XmlPullParser parser, String firstElementName)
    636             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      */
    655     protected static String getAttributeValue(XmlResourceParser parser, String attribute) {
    656         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      */
    668     protected static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
    669             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 
    685     @Thunk static void copyInteger(ContentValues from, ContentValues to, String key) {
    686         to.put(key, from.getAsInteger(key));
    687     }
    688 }
    689