private void normalizeIcons(SQLiteDatabase db) { Log.d(TAG, "normalizing icons"); db.beginTransaction(); Cursor c = null; SQLiteStatement update = null; try { boolean logged = false; update = db.compileStatement("UPDATE favorites " + "SET icon=? WHERE _id=?"); c = db.rawQuery( "SELECT _id, icon FROM favorites WHERE iconType=" + Favorites.ICON_TYPE_BITMAP, null); final int idIndex = c.getColumnIndexOrThrow(Favorites._ID); final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON); while (c.moveToNext()) { long id = c.getLong(idIndex); byte[] data = c.getBlob(iconIndex); try { Bitmap bitmap = Utilities.resampleIconBitmap( BitmapFactory.decodeByteArray(data, 0, data.length), mContext); if (bitmap != null) { update.bindLong(1, id); data = ItemInfo.flattenBitmap(bitmap); if (data != null) { update.bindBlob(2, data); update.execute(); } bitmap.recycle(); } } catch (Exception e) { if (!logged) { Log.e(TAG, "Failed normalizing icon " + id, e); } else { Log.e(TAG, "Also failed normalizing icon " + id); } logged = true; } } db.setTransactionSuccessful(); } catch (SQLException ex) { Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex); } finally { db.endTransaction(); if (update != null) { update.close(); } if (c != null) { c.close(); } } }
public String encodeToString() { if (activityInfo != null) { try { // If it a launcher target, we only need component name, and user to // recreate this. return new JSONStringer() .object() .key(LAUNCH_INTENT_KEY) .value(launchIntent.toUri(0)) .key(APP_SHORTCUT_TYPE_KEY) .value(true) .key(USER_HANDLE_KEY) .value(UserManagerCompat.getInstance(mContext).getSerialNumberForUser(user)) .endObject() .toString(); } catch (JSONException e) { Log.d(TAG, "Exception when adding shortcut: " + e); return null; } } if (launchIntent.getAction() == null) { launchIntent.setAction(Intent.ACTION_VIEW); } else if (launchIntent.getAction().equals(Intent.ACTION_MAIN) && launchIntent.getCategories() != null && launchIntent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) { launchIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); } // This name is only used for comparisons and notifications, so fall back to activity // name if not supplied String name = ensureValidName(mContext, launchIntent, label).toString(); Bitmap icon = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); Intent.ShortcutIconResource iconResource = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); // Only encode the parameters which are supported by the API. try { JSONStringer json = new JSONStringer() .object() .key(LAUNCH_INTENT_KEY) .value(launchIntent.toUri(0)) .key(NAME_KEY) .value(name); if (icon != null) { byte[] iconByteArray = ItemInfo.flattenBitmap(icon); json = json.key(ICON_KEY) .value( Base64.encodeToString( iconByteArray, 0, iconByteArray.length, Base64.DEFAULT)); } if (iconResource != null) { json = json.key(ICON_RESOURCE_NAME_KEY).value(iconResource.resourceName); json = json.key(ICON_RESOURCE_PACKAGE_NAME_KEY).value(iconResource.packageName); } return json.endObject().toString(); } catch (JSONException e) { Log.d(TAG, "Exception when adding shortcut: " + e); } return null; }