Search in sources :

Example 6 with Key

use of com.android.launcher3.backup.BackupProtos.Key in project Launcher3 by chislon.

the class LauncherBackupHelper method getKey.

/** create a new key, with an integer ID.
     *
     * <P> Keys contain their own checksum instead of using
     * the heavy-weight CheckedMessage wrapper.
     */
private Key getKey(int type, long id) {
    Key key = new Key();
    key.type = type;
    key.id = id;
    key.checksum = checkKey(key);
    return key;
}
Also used : Key(com.android.launcher3.backup.BackupProtos.Key)

Example 7 with Key

use of com.android.launcher3.backup.BackupProtos.Key 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);
}
Also used : Intent(android.content.Intent) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) Bitmap(android.graphics.Bitmap) ComponentName(android.content.ComponentName) Key(com.android.launcher3.backup.BackupProtos.Key) HashSet(java.util.HashSet)

Example 8 with Key

use of com.android.launcher3.backup.BackupProtos.Key in project Launcher3 by chislon.

the class LauncherBackupHelper method backupScreens.

/**
     * Write all modified screens 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 backupScreens(Journal in, BackupDataOutput data, Journal out, ArrayList<Key> keys) throws IOException {
    // read the old ID set
    Set<String> savedIds = getSavedIdsByType(Key.SCREEN, in);
    if (DEBUG)
        Log.d(TAG, "screen savedIds.size()=" + savedIds.size());
    // persist things that have changed since the last backup
    ContentResolver cr = mContext.getContentResolver();
    Cursor cursor = cr.query(WorkspaceScreens.CONTENT_URI, SCREEN_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.SCREEN, id);
            keys.add(key);
            currentIds.add(keyToBackupKey(key));
            if (updateTime > in.t) {
                byte[] blob = packScreen(cursor);
                writeRowToBackup(key, blob, out, data);
            }
        }
    } finally {
        cursor.close();
    }
    if (DEBUG)
        Log.d(TAG, "screen currentIds.size()=" + currentIds.size());
    // these IDs must have been deleted
    savedIds.removeAll(currentIds);
    out.rows += removeDeletedKeysFromBackup(savedIds, data);
}
Also used : Cursor(android.database.Cursor) Key(com.android.launcher3.backup.BackupProtos.Key) ContentResolver(android.content.ContentResolver) HashSet(java.util.HashSet)

Example 9 with Key

use of com.android.launcher3.backup.BackupProtos.Key in project Launcher3 by chislon.

the class LauncherBackupHelper method restoreEntity.

/**
     * Restore launcher configuration from the restored data stream.
     *
     * <P>Keys may arrive in any order.
     *
     * @param data the key/value pair from the server
     */
@Override
public void restoreEntity(BackupDataInputStream data) {
    Log.v(TAG, "restoreEntity");
    if (mKeys == null) {
        mKeys = new ArrayList<Key>();
    }
    byte[] buffer = new byte[512];
    String backupKey = data.getKey();
    int dataSize = data.size();
    if (buffer.length < dataSize) {
        buffer = new byte[dataSize];
    }
    Key key = null;
    int bytesRead = 0;
    try {
        bytesRead = data.read(buffer, 0, dataSize);
        if (DEBUG)
            Log.d(TAG, "read " + bytesRead + " of " + dataSize + " available");
    } catch (IOException e) {
        Log.d(TAG, "failed to read entity from restore data", e);
    }
    try {
        key = backupKeyToKey(backupKey);
        switch(key.type) {
            case Key.FAVORITE:
                restoreFavorite(key, buffer, dataSize, mKeys);
                break;
            case Key.SCREEN:
                restoreScreen(key, buffer, dataSize, mKeys);
                break;
            case Key.ICON:
                restoreIcon(key, buffer, dataSize, mKeys);
                break;
            case Key.WIDGET:
                restoreWidget(key, buffer, dataSize, mKeys);
                break;
            default:
                Log.w(TAG, "unknown restore entity type: " + key.type);
                break;
        }
    } catch (KeyParsingException e) {
        Log.w(TAG, "ignoring unparsable backup key: " + backupKey);
    }
}
Also used : IOException(java.io.IOException) Key(com.android.launcher3.backup.BackupProtos.Key)

Example 10 with Key

use of com.android.launcher3.backup.BackupProtos.Key in project Launcher3 by chislon.

the class DecoderRing method main.

public static void main(String[] args) throws Exception {
    File source = null;
    Class type = Key.class;
    int skip = 0;
    for (int i = 0; i < args.length; i++) {
        if ("-k".equals(args[i])) {
            type = Key.class;
        } else if ("-f".equals(args[i])) {
            type = Favorite.class;
        } else if ("-j".equals(args[i])) {
            type = Journal.class;
        } else if ("-i".equals(args[i])) {
            type = Resource.class;
        } else if ("-s".equals(args[i])) {
            type = Screen.class;
        } else if ("-w".equals(args[i])) {
            type = Widget.class;
        } else if ("-S".equals(args[i])) {
            if ((i + 1) < args.length) {
                skip = Integer.valueOf(args[++i]);
            } else {
                usage(args);
            }
        } else if (args[i] != null && !args[i].startsWith("-")) {
            source = new File(args[i]);
        } else {
            System.err.println("Unsupported flag: " + args[i]);
            usage(args);
        }
    }
    // read in the bytes
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    BufferedInputStream input = null;
    if (source == null) {
        input = new BufferedInputStream(System.in);
    } else {
        try {
            input = new BufferedInputStream(new FileInputStream(source));
        } catch (FileNotFoundException e) {
            System.err.println("failed to open file: " + source + ", " + e);
            System.exit(1);
        }
    }
    byte[] buffer = new byte[1024];
    try {
        while (input.available() > 0) {
            int n = input.read(buffer);
            int offset = 0;
            if (skip > 0) {
                offset = Math.min(skip, n);
                n -= offset;
                skip -= offset;
            }
            if (n > 0) {
                byteStream.write(buffer, offset, n);
            }
        }
    } catch (IOException e) {
        System.err.println("failed to read input: " + e);
        System.exit(1);
    }
    System.err.println("read this many bytes: " + byteStream.size());
    MessageNano proto = null;
    if (type == Key.class) {
        Key key = new Key();
        try {
            key = Key.parseFrom(byteStream.toByteArray());
        } catch (InvalidProtocolBufferNanoException e) {
            System.err.println("failed to parse proto: " + e);
            System.exit(1);
        }
        // keys are self-checked
        if (key.checksum != checkKey(key)) {
            System.err.println("key ckecksum failed");
            System.exit(1);
        }
        proto = key;
    } else {
        // other types are wrapped in a checksum message
        CheckedMessage wrapper = new CheckedMessage();
        try {
            MessageNano.mergeFrom(wrapper, byteStream.toByteArray());
        } catch (InvalidProtocolBufferNanoException e) {
            System.err.println("failed to parse wrapper: " + e);
            System.exit(1);
        }
        CRC32 checksum = new CRC32();
        checksum.update(wrapper.payload);
        if (wrapper.checksum != checksum.getValue()) {
            System.err.println("wrapper ckecksum failed");
            System.exit(1);
        }
        // decode the actual message
        proto = (MessageNano) type.newInstance();
        try {
            MessageNano.mergeFrom(proto, wrapper.payload);
        } catch (InvalidProtocolBufferNanoException e) {
            System.err.println("failed to parse proto: " + e);
            System.exit(1);
        }
    }
    // Generic string output
    System.out.println(proto.toString());
    // save off the icon bits in a file for inspection
    if (proto instanceof Resource) {
        Resource icon = (Resource) proto;
        final String path = "icon.webp";
        FileOutputStream iconFile = new FileOutputStream(path);
        iconFile.write(icon.data);
        iconFile.close();
        System.err.println("wrote " + path);
    }
    // save off the widget icon and preview bits in files for inspection
    if (proto instanceof Widget) {
        Widget widget = (Widget) proto;
        if (widget.icon != null) {
            final String path = "widget_icon.webp";
            FileOutputStream iconFile = new FileOutputStream(path);
            iconFile.write(widget.icon.data);
            iconFile.close();
            System.err.println("wrote " + path);
        }
        if (widget.preview != null) {
            final String path = "widget_preview.webp";
            FileOutputStream iconFile = new FileOutputStream(path);
            iconFile.write(widget.preview.data);
            iconFile.close();
            System.err.println("wrote " + path);
        }
    }
    // success
    System.exit(0);
}
Also used : Favorite(com.android.launcher3.backup.BackupProtos.Favorite) CRC32(java.util.zip.CRC32) MessageNano(com.google.protobuf.nano.MessageNano) Resource(com.android.launcher3.backup.BackupProtos.Resource) Widget(com.android.launcher3.backup.BackupProtos.Widget) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InvalidProtocolBufferNanoException(com.google.protobuf.nano.InvalidProtocolBufferNanoException) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) CheckedMessage(com.android.launcher3.backup.BackupProtos.CheckedMessage) File(java.io.File) Key(com.android.launcher3.backup.BackupProtos.Key)

Aggregations

Key (com.android.launcher3.backup.BackupProtos.Key)9 ContentResolver (android.content.ContentResolver)4 Cursor (android.database.Cursor)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 ComponentName (android.content.ComponentName)2 Bitmap (android.graphics.Bitmap)2 Resource (com.android.launcher3.backup.BackupProtos.Resource)2 InvalidProtocolBufferNanoException (com.google.protobuf.nano.InvalidProtocolBufferNanoException)2 Intent (android.content.Intent)1 CheckedMessage (com.android.launcher3.backup.BackupProtos.CheckedMessage)1 Favorite (com.android.launcher3.backup.BackupProtos.Favorite)1 Journal (com.android.launcher3.backup.BackupProtos.Journal)1 Widget (com.android.launcher3.backup.BackupProtos.Widget)1 MessageNano (com.google.protobuf.nano.MessageNano)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1