use of com.android.launcher3.backup.BackupProtos.Favorite in project Launcher3 by chislon.
the class LauncherBackupHelper method backupIcons.
/**
* Write all the static icon resources we need to render placeholders
* for a package that is not installed.
*
* @param in notes from last backup
* @param data output stream for key/value pairs
* @param out notes about this backup
* @param keys keys to mark as clean in the notes for next backup
* @throws IOException
*/
private void backupIcons(Journal in, BackupDataOutput data, Journal out, ArrayList<Key> keys) throws IOException {
// persist icons that haven't been persisted yet
final LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
if (appState == null) {
// try again later
dataChanged();
if (DEBUG)
Log.d(TAG, "Launcher is not initialized, delaying icon backup");
return;
}
final ContentResolver cr = mContext.getContentResolver();
final IconCache iconCache = appState.getIconCache();
final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
// read the old ID set
Set<String> savedIds = getSavedIdsByType(Key.ICON, in);
if (DEBUG)
Log.d(TAG, "icon savedIds.size()=" + savedIds.size());
int startRows = out.rows;
if (DEBUG)
Log.d(TAG, "starting here: " + startRows);
String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPLICATION;
Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, where, null, null);
Set<String> currentIds = new HashSet<String>(cursor.getCount());
try {
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
final long id = cursor.getLong(ID_INDEX);
final String intentDescription = cursor.getString(INTENT_INDEX);
try {
Intent intent = Intent.parseUri(intentDescription, 0);
ComponentName cn = intent.getComponent();
Key key = null;
String backupKey = null;
if (cn != null) {
key = getKey(Key.ICON, cn.flattenToShortString());
backupKey = keyToBackupKey(key);
currentIds.add(backupKey);
} else {
Log.w(TAG, "empty intent on application favorite: " + id);
}
if (savedIds.contains(backupKey)) {
if (DEBUG)
Log.d(TAG, "already saved icon " + backupKey);
// remember that we already backed this up previously
keys.add(key);
} else if (backupKey != null) {
if (DEBUG)
Log.d(TAG, "I can count this high: " + out.rows);
if ((out.rows - startRows) < MAX_ICONS_PER_PASS) {
if (DEBUG)
Log.d(TAG, "saving icon " + backupKey);
Bitmap icon = iconCache.getIcon(intent);
keys.add(key);
if (icon != null && !iconCache.isDefaultIcon(icon)) {
byte[] blob = packIcon(dpi, icon);
writeRowToBackup(key, blob, out, data);
}
} else {
if (DEBUG)
Log.d(TAG, "scheduling another run for icon " + backupKey);
// too many icons for this pass, request another.
dataChanged();
}
}
} catch (URISyntaxException e) {
Log.w(TAG, "invalid URI on application favorite: " + id);
} catch (IOException e) {
Log.w(TAG, "unable to save application icon for favorite: " + id);
}
}
} finally {
cursor.close();
}
if (DEBUG)
Log.d(TAG, "icon currentIds.size()=" + currentIds.size());
// these IDs must have been deleted
savedIds.removeAll(currentIds);
out.rows += removeDeletedKeysFromBackup(savedIds, data);
}
use of com.android.launcher3.backup.BackupProtos.Favorite in project Launcher3 by chislon.
the class LauncherBackupHelper method packFavorite.
/**
* Serialize a Favorite for persistence, including a checksum wrapper.
*/
private byte[] packFavorite(Cursor c) {
Favorite favorite = new Favorite();
favorite.id = c.getLong(ID_INDEX);
favorite.screen = c.getInt(SCREEN_INDEX);
favorite.container = c.getInt(CONTAINER_INDEX);
favorite.cellX = c.getInt(CELLX_INDEX);
favorite.cellY = c.getInt(CELLY_INDEX);
favorite.spanX = c.getInt(SPANX_INDEX);
favorite.spanY = c.getInt(SPANY_INDEX);
favorite.iconType = c.getInt(ICON_TYPE_INDEX);
if (favorite.iconType == Favorites.ICON_TYPE_RESOURCE) {
String iconPackage = c.getString(ICON_PACKAGE_INDEX);
if (!TextUtils.isEmpty(iconPackage)) {
favorite.iconPackage = iconPackage;
}
String iconResource = c.getString(ICON_RESOURCE_INDEX);
if (!TextUtils.isEmpty(iconResource)) {
favorite.iconResource = iconResource;
}
}
if (favorite.iconType == Favorites.ICON_TYPE_BITMAP) {
byte[] blob = c.getBlob(ICON_INDEX);
if (blob != null && blob.length > 0) {
favorite.icon = blob;
}
}
String title = c.getString(TITLE_INDEX);
if (!TextUtils.isEmpty(title)) {
favorite.title = title;
}
String intent = c.getString(INTENT_INDEX);
if (!TextUtils.isEmpty(intent)) {
favorite.intent = intent;
}
favorite.itemType = c.getInt(ITEM_TYPE_INDEX);
if (favorite.itemType == Favorites.ITEM_TYPE_APPWIDGET) {
favorite.appWidgetId = c.getInt(APPWIDGET_ID_INDEX);
String appWidgetProvider = c.getString(APPWIDGET_PROVIDER_INDEX);
if (!TextUtils.isEmpty(appWidgetProvider)) {
favorite.appWidgetProvider = appWidgetProvider;
}
}
return writeCheckedBytes(favorite);
}
use of com.android.launcher3.backup.BackupProtos.Favorite in project Launcher3 by chislon.
the class LauncherBackupHelper method backupFavorites.
/**
* Write all modified favorites to the data stream.
*
* @param in notes from last backup
* @param data output stream for key/value pairs
* @param out notes about this backup
* @param keys keys to mark as clean in the notes for next backup
* @throws IOException
*/
private void backupFavorites(Journal in, BackupDataOutput data, Journal out, ArrayList<Key> keys) throws IOException {
// read the old ID set
Set<String> savedIds = getSavedIdsByType(Key.FAVORITE, in);
if (DEBUG)
Log.d(TAG, "favorite savedIds.size()=" + savedIds.size());
// persist things that have changed since the last backup
ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, null, null, null);
Set<String> currentIds = new HashSet<String>(cursor.getCount());
try {
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
final long id = cursor.getLong(ID_INDEX);
final long updateTime = cursor.getLong(ID_MODIFIED);
Key key = getKey(Key.FAVORITE, id);
keys.add(key);
currentIds.add(keyToBackupKey(key));
if (updateTime > in.t) {
byte[] blob = packFavorite(cursor);
writeRowToBackup(key, blob, out, data);
}
}
} finally {
cursor.close();
}
if (DEBUG)
Log.d(TAG, "favorite currentIds.size()=" + currentIds.size());
// these IDs must have been deleted
savedIds.removeAll(currentIds);
out.rows += removeDeletedKeysFromBackup(savedIds, data);
}
use of com.android.launcher3.backup.BackupProtos.Favorite in project Launcher3 by chislon.
the class LauncherBackupHelper method unpackFavorite.
/**
* Deserialize a Favorite from persistence, after verifying checksum wrapper.
*/
private Favorite unpackFavorite(byte[] buffer, int offset, int dataSize) throws InvalidProtocolBufferNanoException {
Favorite favorite = new Favorite();
MessageNano.mergeFrom(favorite, readCheckedBytes(buffer, offset, dataSize));
return favorite;
}
Aggregations