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 get(Context context, AppWidgetHost appWidgetHost, LayoutParserCallback callback)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 get(Context context, String pkg, Resources targetRes, AppWidgetHost appWidgetHost, LayoutParserCallback callback)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 129 // x and y can be specified as negative integers, in which case -1 represents the 130 // last row / column, -2 represents the second last, and so on. 131 private static final String ATTR_X = "x"; 132 private static final String ATTR_Y = "y"; 133 134 private static final String ATTR_SPAN_X = "spanX"; 135 private static final String ATTR_SPAN_Y = "spanY"; 136 private static final String ATTR_ICON = "icon"; 137 private static final String ATTR_URL = "url"; 138 139 // Attrs for "Include" 140 private static final String ATTR_WORKSPACE = "workspace"; 141 142 // Style attrs -- "Extra" 143 private static final String ATTR_KEY = "key"; 144 private static final String ATTR_VALUE = "value"; 145 146 private static final String HOTSEAT_CONTAINER_NAME = 147 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT); 148 149 private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE = 150 "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE"; 151 152 @Thunk final Context mContext; 153 @Thunk final AppWidgetHost mAppWidgetHost; 154 protected final LayoutParserCallback mCallback; 155 156 protected final PackageManager mPackageManager; 157 protected final Resources mSourceRes; 158 protected final int mLayoutId; 159 160 private final int mHotseatAllAppsRank; 161 private final int mRowCount; 162 private final int mColumnCount; 163 164 private final long[] mTemp = new long[2]; 165 @Thunk final ContentValues mValues; 166 protected final String mRootTag; 167 168 protected SQLiteDatabase mDb; 169 AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost, LayoutParserCallback callback, Resources res, int layoutId, String rootTag)170 public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost, 171 LayoutParserCallback callback, Resources res, 172 int layoutId, String rootTag) { 173 mContext = context; 174 mAppWidgetHost = appWidgetHost; 175 mCallback = callback; 176 177 mPackageManager = context.getPackageManager(); 178 mValues = new ContentValues(); 179 mRootTag = rootTag; 180 181 mSourceRes = res; 182 mLayoutId = layoutId; 183 184 InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile(); 185 mHotseatAllAppsRank = idp.hotseatAllAppsRank; 186 mRowCount = idp.numRows; 187 mColumnCount = idp.numColumns; 188 } 189 190 /** 191 * Loads the layout in the db and returns the number of entries added on the desktop. 192 */ loadLayout(SQLiteDatabase db, ArrayList<Long> screenIds)193 public int loadLayout(SQLiteDatabase db, ArrayList<Long> screenIds) { 194 mDb = db; 195 try { 196 return parseLayout(mLayoutId, screenIds); 197 } catch (Exception e) { 198 Log.w(TAG, "Got exception parsing layout.", e); 199 return -1; 200 } 201 } 202 203 /** 204 * Parses the layout and returns the number of elements added on the homescreen. 205 */ parseLayout(int layoutId, ArrayList<Long> screenIds)206 protected int parseLayout(int layoutId, ArrayList<Long> screenIds) 207 throws XmlPullParserException, IOException { 208 XmlResourceParser parser = mSourceRes.getXml(layoutId); 209 beginDocument(parser, mRootTag); 210 final int depth = parser.getDepth(); 211 int type; 212 HashMap<String, TagParser> tagParserMap = getLayoutElementsMap(); 213 int count = 0; 214 215 while (((type = parser.next()) != XmlPullParser.END_TAG || 216 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { 217 if (type != XmlPullParser.START_TAG) { 218 continue; 219 } 220 count += parseAndAddNode(parser, tagParserMap, screenIds); 221 } 222 return count; 223 } 224 225 /** 226 * Parses container and screenId attribute from the current tag, and puts it in the out. 227 * @param out array of size 2. 228 */ parseContainerAndScreen(XmlResourceParser parser, long[] out)229 protected void parseContainerAndScreen(XmlResourceParser parser, long[] out) { 230 if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) { 231 out[0] = Favorites.CONTAINER_HOTSEAT; 232 // Hack: hotseat items are stored using screen ids 233 long rank = Long.parseLong(getAttributeValue(parser, ATTR_RANK)); 234 out[1] = (rank < mHotseatAllAppsRank) ? rank : (rank + 1); 235 } else { 236 out[0] = Favorites.CONTAINER_DESKTOP; 237 out[1] = Long.parseLong(getAttributeValue(parser, ATTR_SCREEN)); 238 } 239 } 240 241 /** 242 * Parses the current node and returns the number of elements added. 243 */ parseAndAddNode( XmlResourceParser parser, HashMap<String, TagParser> tagParserMap, ArrayList<Long> screenIds)244 protected int parseAndAddNode( 245 XmlResourceParser parser, 246 HashMap<String, TagParser> tagParserMap, 247 ArrayList<Long> screenIds) 248 throws XmlPullParserException, IOException { 249 250 if (TAG_INCLUDE.equals(parser.getName())) { 251 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0); 252 if (resId != 0) { 253 // recursively load some more favorites, why not? 254 return parseLayout(resId, screenIds); 255 } else { 256 return 0; 257 } 258 } 259 260 mValues.clear(); 261 parseContainerAndScreen(parser, mTemp); 262 final long container = mTemp[0]; 263 final long screenId = mTemp[1]; 264 265 mValues.put(Favorites.CONTAINER, container); 266 mValues.put(Favorites.SCREEN, screenId); 267 268 mValues.put(Favorites.CELLX, 269 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount)); 270 mValues.put(Favorites.CELLY, 271 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount)); 272 273 TagParser tagParser = tagParserMap.get(parser.getName()); 274 if (tagParser == null) { 275 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName()); 276 return 0; 277 } 278 long newElementId = tagParser.parseAndAdd(parser); 279 if (newElementId >= 0) { 280 // Keep track of the set of screens which need to be added to the db. 281 if (!screenIds.contains(screenId) && 282 container == Favorites.CONTAINER_DESKTOP) { 283 screenIds.add(screenId); 284 } 285 return 1; 286 } 287 return 0; 288 } 289 addShortcut(String title, Intent intent, int type)290 protected long addShortcut(String title, Intent intent, int type) { 291 long id = mCallback.generateNewItemId(); 292 mValues.put(Favorites.INTENT, intent.toUri(0)); 293 mValues.put(Favorites.TITLE, title); 294 mValues.put(Favorites.ITEM_TYPE, type); 295 mValues.put(Favorites.SPANX, 1); 296 mValues.put(Favorites.SPANY, 1); 297 mValues.put(Favorites._ID, id); 298 if (mCallback.insertAndCheck(mDb, mValues) < 0) { 299 return -1; 300 } else { 301 return id; 302 } 303 } 304 getFolderElementsMap()305 protected HashMap<String, TagParser> getFolderElementsMap() { 306 HashMap<String, TagParser> parsers = new HashMap<String, TagParser>(); 307 parsers.put(TAG_APP_ICON, new AppShortcutParser()); 308 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser()); 309 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes)); 310 return parsers; 311 } 312 getLayoutElementsMap()313 protected HashMap<String, TagParser> getLayoutElementsMap() { 314 HashMap<String, TagParser> parsers = new HashMap<String, TagParser>(); 315 parsers.put(TAG_APP_ICON, new AppShortcutParser()); 316 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser()); 317 parsers.put(TAG_FOLDER, new FolderParser()); 318 parsers.put(TAG_APPWIDGET, new AppWidgetParser()); 319 parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes)); 320 return parsers; 321 } 322 323 protected interface TagParser { 324 /** 325 * Parses the tag and adds to the db 326 * @return the id of the row added or -1; 327 */ parseAndAdd(XmlResourceParser parser)328 long parseAndAdd(XmlResourceParser parser) 329 throws XmlPullParserException, IOException; 330 } 331 332 /** 333 * App shortcuts: required attributes packageName and className 334 */ 335 protected class AppShortcutParser implements TagParser { 336 337 @Override parseAndAdd(XmlResourceParser parser)338 public long parseAndAdd(XmlResourceParser parser) { 339 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); 340 final String className = getAttributeValue(parser, ATTR_CLASS_NAME); 341 342 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) { 343 ActivityInfo info; 344 try { 345 ComponentName cn; 346 try { 347 cn = new ComponentName(packageName, className); 348 info = mPackageManager.getActivityInfo(cn, 0); 349 } catch (PackageManager.NameNotFoundException nnfe) { 350 String[] packages = mPackageManager.currentToCanonicalPackageNames( 351 new String[] { packageName }); 352 cn = new ComponentName(packages[0], className); 353 info = mPackageManager.getActivityInfo(cn, 0); 354 } 355 final Intent intent = new Intent(Intent.ACTION_MAIN, null) 356 .addCategory(Intent.CATEGORY_LAUNCHER) 357 .setComponent(cn) 358 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 359 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 360 361 return addShortcut(info.loadLabel(mPackageManager).toString(), 362 intent, Favorites.ITEM_TYPE_APPLICATION); 363 } catch (PackageManager.NameNotFoundException e) { 364 Log.e(TAG, "Unable to add favorite: " + packageName + "/" + className, e); 365 } 366 return -1; 367 } else { 368 return invalidPackageOrClass(parser); 369 } 370 } 371 372 /** 373 * Helper method to allow extending the parser capabilities 374 */ invalidPackageOrClass(XmlResourceParser parser)375 protected long invalidPackageOrClass(XmlResourceParser parser) { 376 Log.w(TAG, "Skipping invalid <favorite> with no component"); 377 return -1; 378 } 379 } 380 381 /** 382 * AutoInstall: required attributes packageName and className 383 */ 384 protected class AutoInstallParser implements TagParser { 385 386 @Override parseAndAdd(XmlResourceParser parser)387 public long parseAndAdd(XmlResourceParser parser) { 388 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); 389 final String className = getAttributeValue(parser, ATTR_CLASS_NAME); 390 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) { 391 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component"); 392 return -1; 393 } 394 395 mValues.put(Favorites.RESTORED, ShortcutInfo.FLAG_AUTOINTALL_ICON); 396 final Intent intent = new Intent(Intent.ACTION_MAIN, null) 397 .addCategory(Intent.CATEGORY_LAUNCHER) 398 .setComponent(new ComponentName(packageName, className)) 399 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 400 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 401 return addShortcut(mContext.getString(R.string.package_state_unknown), intent, 402 Favorites.ITEM_TYPE_APPLICATION); 403 } 404 } 405 406 /** 407 * Parses a web shortcut. Required attributes url, icon, title 408 */ 409 protected class ShortcutParser implements TagParser { 410 411 private final Resources mIconRes; 412 ShortcutParser(Resources iconRes)413 public ShortcutParser(Resources iconRes) { 414 mIconRes = iconRes; 415 } 416 417 @Override parseAndAdd(XmlResourceParser parser)418 public long parseAndAdd(XmlResourceParser parser) { 419 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0); 420 final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0); 421 422 if (titleResId == 0 || iconId == 0) { 423 if (LOGD) Log.d(TAG, "Ignoring shortcut"); 424 return -1; 425 } 426 427 final Intent intent = parseIntent(parser); 428 if (intent == null) { 429 return -1; 430 } 431 432 Drawable icon = mIconRes.getDrawable(iconId); 433 if (icon == null) { 434 if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon"); 435 return -1; 436 } 437 438 ItemInfo.writeBitmap(mValues, Utilities.createIconBitmap(icon, mContext)); 439 mValues.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE); 440 mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId)); 441 mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId)); 442 443 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 444 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 445 return addShortcut(mSourceRes.getString(titleResId), 446 intent, Favorites.ITEM_TYPE_SHORTCUT); 447 } 448 parseIntent(XmlResourceParser parser)449 protected Intent parseIntent(XmlResourceParser parser) { 450 final String url = getAttributeValue(parser, ATTR_URL); 451 if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) { 452 if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url); 453 return null; 454 } 455 return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url)); 456 } 457 } 458 459 /** 460 * AppWidget parser: Required attributes packageName, className, spanX and spanY. 461 * Options child nodes: <extra key=... value=... /> 462 */ 463 protected class AppWidgetParser implements TagParser { 464 465 @Override parseAndAdd(XmlResourceParser parser)466 public long parseAndAdd(XmlResourceParser parser) 467 throws XmlPullParserException, IOException { 468 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); 469 final String className = getAttributeValue(parser, ATTR_CLASS_NAME); 470 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) { 471 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component"); 472 return -1; 473 } 474 475 ComponentName cn = new ComponentName(packageName, className); 476 try { 477 mPackageManager.getReceiverInfo(cn, 0); 478 } catch (Exception e) { 479 String[] packages = mPackageManager.currentToCanonicalPackageNames( 480 new String[] { packageName }); 481 cn = new ComponentName(packages[0], className); 482 try { 483 mPackageManager.getReceiverInfo(cn, 0); 484 } catch (Exception e1) { 485 if (LOGD) Log.d(TAG, "Can't find widget provider: " + className); 486 return -1; 487 } 488 } 489 490 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X)); 491 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y)); 492 493 // Read the extras 494 Bundle extras = new Bundle(); 495 int widgetDepth = parser.getDepth(); 496 int type; 497 while ((type = parser.next()) != XmlPullParser.END_TAG || 498 parser.getDepth() > widgetDepth) { 499 if (type != XmlPullParser.START_TAG) { 500 continue; 501 } 502 503 if (TAG_EXTRA.equals(parser.getName())) { 504 String key = getAttributeValue(parser, ATTR_KEY); 505 String value = getAttributeValue(parser, ATTR_VALUE); 506 if (key != null && value != null) { 507 extras.putString(key, value); 508 } else { 509 throw new RuntimeException("Widget extras must have a key and value"); 510 } 511 } else { 512 throw new RuntimeException("Widgets can contain only extras"); 513 } 514 } 515 516 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); 517 long insertedId = -1; 518 try { 519 int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); 520 521 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn)) { 522 if (LOGD) Log.e(TAG, "Unable to bind app widget id " + cn); 523 return -1; 524 } 525 526 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET); 527 mValues.put(Favorites.APPWIDGET_ID, appWidgetId); 528 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString()); 529 mValues.put(Favorites._ID, mCallback.generateNewItemId()); 530 insertedId = mCallback.insertAndCheck(mDb, mValues); 531 if (insertedId < 0) { 532 mAppWidgetHost.deleteAppWidgetId(appWidgetId); 533 return insertedId; 534 } 535 536 // Send a broadcast to configure the widget 537 if (!extras.isEmpty()) { 538 Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE); 539 intent.setComponent(cn); 540 intent.putExtras(extras); 541 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 542 mContext.sendBroadcast(intent); 543 } 544 } catch (RuntimeException ex) { 545 if (LOGD) Log.e(TAG, "Problem allocating appWidgetId", ex); 546 } 547 return insertedId; 548 } 549 } 550 551 protected class FolderParser implements TagParser { 552 private final HashMap<String, TagParser> mFolderElements; 553 FolderParser()554 public FolderParser() { 555 this(getFolderElementsMap()); 556 } 557 FolderParser(HashMap<String, TagParser> elements)558 public FolderParser(HashMap<String, TagParser> elements) { 559 mFolderElements = elements; 560 } 561 562 @Override parseAndAdd(XmlResourceParser parser)563 public long parseAndAdd(XmlResourceParser parser) 564 throws XmlPullParserException, IOException { 565 final String title; 566 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0); 567 if (titleResId != 0) { 568 title = mSourceRes.getString(titleResId); 569 } else { 570 title = mContext.getResources().getString(R.string.folder_name); 571 } 572 573 mValues.put(Favorites.TITLE, title); 574 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER); 575 mValues.put(Favorites.SPANX, 1); 576 mValues.put(Favorites.SPANY, 1); 577 mValues.put(Favorites._ID, mCallback.generateNewItemId()); 578 long folderId = mCallback.insertAndCheck(mDb, mValues); 579 if (folderId < 0) { 580 if (LOGD) Log.e(TAG, "Unable to add folder"); 581 return -1; 582 } 583 584 final ContentValues myValues = new ContentValues(mValues); 585 ArrayList<Long> folderItems = new ArrayList<Long>(); 586 587 int type; 588 int folderDepth = parser.getDepth(); 589 int rank = 0; 590 while ((type = parser.next()) != XmlPullParser.END_TAG || 591 parser.getDepth() > folderDepth) { 592 if (type != XmlPullParser.START_TAG) { 593 continue; 594 } 595 mValues.clear(); 596 mValues.put(Favorites.CONTAINER, folderId); 597 mValues.put(Favorites.RANK, rank); 598 599 TagParser tagParser = mFolderElements.get(parser.getName()); 600 if (tagParser != null) { 601 final long id = tagParser.parseAndAdd(parser); 602 if (id >= 0) { 603 folderItems.add(id); 604 rank++; 605 } 606 } else { 607 throw new RuntimeException("Invalid folder item " + parser.getName()); 608 } 609 } 610 611 long addedId = folderId; 612 613 // We can only have folders with >= 2 items, so we need to remove the 614 // folder and clean up if less than 2 items were included, or some 615 // failed to add, and less than 2 were actually added 616 if (folderItems.size() < 2) { 617 // Delete the folder 618 Uri uri = Favorites.getContentUri(folderId); 619 SqlArguments args = new SqlArguments(uri, null, null); 620 mDb.delete(args.table, args.where, args.args); 621 addedId = -1; 622 623 // If we have a single item, promote it to where the folder 624 // would have been. 625 if (folderItems.size() == 1) { 626 final ContentValues childValues = new ContentValues(); 627 copyInteger(myValues, childValues, Favorites.CONTAINER); 628 copyInteger(myValues, childValues, Favorites.SCREEN); 629 copyInteger(myValues, childValues, Favorites.CELLX); 630 copyInteger(myValues, childValues, Favorites.CELLY); 631 632 addedId = folderItems.get(0); 633 mDb.update(LauncherProvider.TABLE_FAVORITES, childValues, 634 Favorites._ID + "=" + addedId, null); 635 } 636 } 637 return addedId; 638 } 639 } 640 beginDocument(XmlPullParser parser, String firstElementName)641 protected static final void beginDocument(XmlPullParser parser, String firstElementName) 642 throws XmlPullParserException, IOException { 643 int type; 644 while ((type = parser.next()) != XmlPullParser.START_TAG 645 && type != XmlPullParser.END_DOCUMENT); 646 647 if (type != XmlPullParser.START_TAG) { 648 throw new XmlPullParserException("No start tag found"); 649 } 650 651 if (!parser.getName().equals(firstElementName)) { 652 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() + 653 ", expected " + firstElementName); 654 } 655 } 656 convertToDistanceFromEnd(String value, int endValue)657 private static String convertToDistanceFromEnd(String value, int endValue) { 658 if (!TextUtils.isEmpty(value)) { 659 int x = Integer.parseInt(value); 660 if (x < 0) { 661 return Integer.toString(endValue + x); 662 } 663 } 664 return value; 665 } 666 667 /** 668 * Return attribute value, attempting launcher-specific namespace first 669 * before falling back to anonymous attribute. 670 */ getAttributeValue(XmlResourceParser parser, String attribute)671 protected static String getAttributeValue(XmlResourceParser parser, String attribute) { 672 String value = parser.getAttributeValue( 673 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute); 674 if (value == null) { 675 value = parser.getAttributeValue(null, attribute); 676 } 677 return value; 678 } 679 680 /** 681 * Return attribute resource value, attempting launcher-specific namespace 682 * first before falling back to anonymous attribute. 683 */ getAttributeResourceValue(XmlResourceParser parser, String attribute, int defaultValue)684 protected static int getAttributeResourceValue(XmlResourceParser parser, String attribute, 685 int defaultValue) { 686 int value = parser.getAttributeResourceValue( 687 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute, 688 defaultValue); 689 if (value == defaultValue) { 690 value = parser.getAttributeResourceValue(null, attribute, defaultValue); 691 } 692 return value; 693 } 694 695 public static interface LayoutParserCallback { generateNewItemId()696 long generateNewItemId(); 697 insertAndCheck(SQLiteDatabase db, ContentValues values)698 long insertAndCheck(SQLiteDatabase db, ContentValues values); 699 } 700 copyInteger(ContentValues from, ContentValues to, String key)701 @Thunk static void copyInteger(ContentValues from, ContentValues to, String key) { 702 to.put(key, from.getAsInteger(key)); 703 } 704 } 705