Search in sources :

Example 1 with Resource

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

the class LauncherBackupHelper method unpackIcon.

/**
 * Deserialize an icon resource from persistence, after verifying checksum wrapper.
 */
private Resource unpackIcon(byte[] buffer, int offset, int dataSize) throws InvalidProtocolBufferNanoException {
    Resource res = new Resource();
    MessageNano.mergeFrom(res, readCheckedBytes(buffer, offset, dataSize));
    return res;
}
Also used : Resource(com.android.launcher3.backup.BackupProtos.Resource)

Example 2 with Resource

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

the class LauncherBackupHelper method restoreIcon.

/**
 * Read an icon from the stream.
 *
 * <P>Keys arrive in any order, so shortcuts that use this icon may already exist.
 *
 * @param key identifier for the row
 * @param buffer the serialized proto from the stream, may be larger than dataSize
 * @param dataSize the size of the proto from the stream
 * @param keys keys to mark as clean in the notes for next backup
 */
private void restoreIcon(Key key, byte[] buffer, int dataSize, ArrayList<Key> keys) {
    Log.v(TAG, "unpacking icon " + key.id);
    if (DEBUG)
        Log.d(TAG, "read (" + buffer.length + "): " + Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
    try {
        Resource res = unpackIcon(buffer, 0, dataSize);
        if (DEBUG)
            Log.d(TAG, "unpacked " + res.dpi);
        if (DEBUG)
            Log.d(TAG, "read " + Base64.encodeToString(res.data, 0, res.data.length, Base64.NO_WRAP));
        Bitmap icon = BitmapFactory.decodeByteArray(res.data, 0, res.data.length);
        if (icon == null) {
            Log.w(TAG, "failed to unpack icon for " + key.name);
        }
    } catch (InvalidProtocolBufferNanoException e) {
        Log.w(TAG, "failed to decode proto", e);
    }
}
Also used : Bitmap(android.graphics.Bitmap) Resource(com.android.launcher3.backup.BackupProtos.Resource) InvalidProtocolBufferNanoException(com.google.protobuf.nano.InvalidProtocolBufferNanoException)

Example 3 with Resource

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

the class LauncherBackupHelper method packIcon.

/**
 * Serialize an icon Resource for persistence, including a checksum wrapper.
 */
private byte[] packIcon(int dpi, Bitmap icon) {
    Resource res = new Resource();
    res.dpi = dpi;
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    if (icon.compress(IMAGE_FORMAT, IMAGE_COMPRESSION_QUALITY, os)) {
        res.data = os.toByteArray();
    }
    return writeCheckedBytes(res);
}
Also used : Resource(com.android.launcher3.backup.BackupProtos.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with Resource

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

the class LauncherBackupHelper method packWidget.

/**
 * Serialize a widget for persistence, including a checksum wrapper.
 */
private byte[] packWidget(int dpi, WidgetPreviewLoader previewLoader, IconCache iconCache, ComponentName provider) {
    final AppWidgetProviderInfo info = findAppWidgetProviderInfo(provider);
    Widget widget = new Widget();
    widget.provider = provider.flattenToShortString();
    widget.label = info.label;
    widget.configure = info.configure != null;
    if (info.icon != 0) {
        widget.icon = new Resource();
        Drawable fullResIcon = iconCache.getFullResIcon(provider.getPackageName(), info.icon);
        Bitmap icon = Utilities.createIconBitmap(fullResIcon, mContext);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        if (icon.compress(IMAGE_FORMAT, IMAGE_COMPRESSION_QUALITY, os)) {
            widget.icon.data = os.toByteArray();
            widget.icon.dpi = dpi;
        }
    }
    if (info.previewImage != 0) {
        widget.preview = new Resource();
        Bitmap preview = previewLoader.generateWidgetPreview(info, null);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        if (preview.compress(IMAGE_FORMAT, IMAGE_COMPRESSION_QUALITY, os)) {
            widget.preview.data = os.toByteArray();
            widget.preview.dpi = dpi;
        }
    }
    return writeCheckedBytes(widget);
}
Also used : Bitmap(android.graphics.Bitmap) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) Widget(com.android.launcher3.backup.BackupProtos.Widget) Resource(com.android.launcher3.backup.BackupProtos.Resource) Drawable(android.graphics.drawable.Drawable) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with Resource

use of com.android.launcher3.backup.BackupProtos.Resource 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

Resource (com.android.launcher3.backup.BackupProtos.Resource)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Bitmap (android.graphics.Bitmap)2 Widget (com.android.launcher3.backup.BackupProtos.Widget)2 InvalidProtocolBufferNanoException (com.google.protobuf.nano.InvalidProtocolBufferNanoException)2 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)1 Drawable (android.graphics.drawable.Drawable)1 CheckedMessage (com.android.launcher3.backup.BackupProtos.CheckedMessage)1 Favorite (com.android.launcher3.backup.BackupProtos.Favorite)1 Key (com.android.launcher3.backup.BackupProtos.Key)1 MessageNano (com.google.protobuf.nano.MessageNano)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 CRC32 (java.util.zip.CRC32)1