Example usage for android.os Parcel unmarshall

List of usage examples for android.os Parcel unmarshall

Introduction

In this page you can find the example usage for android.os Parcel unmarshall.

Prototype

public final void unmarshall(byte[] data, int offset, int length) 

Source Link

Document

Set the bytes in data to be the raw bytes of this Parcel.

Usage

From source file:org.gearvrf.weartouchpad.MessageListenerService.java

private static <T> T unmarshall(byte[] bytes, Parcelable.Creator<T> creator) {
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0);//  w w w .j a va  2  s . co  m
    T result = creator.createFromParcel(parcel);
    parcel.recycle();
    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Parcelable readParcelable(Context context, String fileName, ClassLoader classLoader) {
    Parcelable parcelable = null;/*ww  w  .j  av  a 2 s  .c  o  m*/
    FileInputStream fis = null;
    ByteArrayOutputStream bos = null;
    try {
        fis = context.openFileInput(fileName);
        if (fis != null) {
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
            }

            byte[] data = bos.toByteArray();

            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            parcelable = parcel.readParcelable(classLoader);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        parcelable = null;
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        if (bos != null)
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return parcelable;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static List<Parcelable> readParcelableList(Context context, String fileName, ClassLoader classLoader) {
    List<Parcelable> results = null;
    FileInputStream fis = null;/*from  w w w.ja  v a 2 s.  c  o m*/
    ByteArrayOutputStream bos = null;
    try {
        fis = context.openFileInput(fileName);
        if (fis != null) {
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
            }

            byte[] data = bos.toByteArray();

            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            results = parcel.readArrayList(classLoader);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        results = null;
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        if (bos != null)
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return results;
}

From source file:Main.java

public static Bundle fromBase64(String serialized) {
    Bundle bundle = null;//from w  ww . ja  v  a 2 s. c o  m
    if (serialized != null) {
        Parcel parcel = Parcel.obtain();
        try {
            byte[] data = Base64.decode(serialized, 0);
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            bundle = parcel.readBundle();
        } finally {
            parcel.recycle();
        }
    }
    return bundle;
}

From source file:at.bitfire.davdroid.resource.LocalGroup.java

/**
 * Processes all groups with non-null {@link #COLUMN_PENDING_MEMBERS}: the pending memberships
 * are (if possible) applied, keeping cached memberships in sync.
 * @param addressBook    address book to take groups from
 * @throws ContactsStorageException on contact provider errors
 *///from w  w  w . jav  a 2 s .  c o m
public static void applyPendingMemberships(LocalAddressBook addressBook) throws ContactsStorageException {
    try {
        @Cleanup
        Cursor cursor = addressBook.provider.query(addressBook.syncAdapterURI(Groups.CONTENT_URI),
                new String[] { Groups._ID, COLUMN_PENDING_MEMBERS }, COLUMN_PENDING_MEMBERS + " IS NOT NULL",
                new String[] {}, null);

        BatchOperation batch = new BatchOperation(addressBook.provider);
        while (cursor != null && cursor.moveToNext()) {
            long id = cursor.getLong(0);
            Constants.log.fine("Assigning members to group " + id);

            // delete all memberships and cached memberships for this group
            batch.enqueue(new BatchOperation.Operation(ContentProviderOperation
                    .newDelete(addressBook.syncAdapterURI(ContactsContract.Data.CONTENT_URI))
                    .withSelection(
                            "(" + GroupMembership.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID
                                    + "=?) OR (" + CachedGroupMembership.MIMETYPE + "=? AND "
                                    + CachedGroupMembership.GROUP_ID + "=?)",
                            new String[] { GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(id),
                                    CachedGroupMembership.CONTENT_ITEM_TYPE, String.valueOf(id) })
                    .withYieldAllowed(true)));

            // extract list of member UIDs
            List<String> members = new LinkedList<>();
            byte[] raw = cursor.getBlob(1);
            @Cleanup("recycle")
            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(raw, 0, raw.length);
            parcel.setDataPosition(0);
            parcel.readStringList(members);

            // insert memberships
            for (String uid : members) {
                Constants.log.fine("Assigning member: " + uid);
                try {
                    LocalContact member = addressBook.findContactByUID(uid);
                    member.addToGroup(batch, id);
                } catch (FileNotFoundException e) {
                    Constants.log.log(Level.WARNING, "Group member not found: " + uid, e);
                }
            }

            // remove pending memberships
            batch.enqueue(new BatchOperation.Operation(ContentProviderOperation
                    .newUpdate(addressBook.syncAdapterURI(ContentUris.withAppendedId(Groups.CONTENT_URI, id)))
                    .withValue(COLUMN_PENDING_MEMBERS, null).withYieldAllowed(true)));

            batch.commit();
        }
    } catch (RemoteException e) {
        throw new ContactsStorageException("Couldn't get pending memberships", e);
    }
}

From source file:com.facebook.TestUtils.java

public static <E extends Parcelable> E parcelAndUnparcel(final E object) {
    final Parcel writeParcel = Parcel.obtain();
    final Parcel readParcel = Parcel.obtain();
    try {//from   w  w  w .java  2 s  . co  m
        writeParcel.writeParcelable(object, 0);
        final byte[] bytes = writeParcel.marshall();
        readParcel.unmarshall(bytes, 0, bytes.length);
        readParcel.setDataPosition(0);
        return readParcel.readParcelable(object.getClass().getClassLoader());
    } finally {
        writeParcel.recycle();
        readParcel.recycle();
    }
}

From source file:com.marianhello.bgloc.Config.java

public static Config fromByteArray(byte[] byteArray) {
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(byteArray, 0, byteArray.length);
    parcel.setDataPosition(0);// ww w.j av a 2  s. c  om
    return Config.CREATOR.createFromParcel(parcel);
}

From source file:com.jungle.base.utils.MiscUtils.java

public static Bundle bytesToBundle(byte[] bytes) {
    Bundle bundle = new Bundle();
    if (bytes == null || bytes.length == 0) {
        return bundle;
    }//  w  ww. ja  v a  2s  . c o  m

    Parcel accountInfoParcel = Parcel.obtain();
    accountInfoParcel.unmarshall(bytes, 0, bytes.length);
    accountInfoParcel.setDataPosition(0);
    bundle.readFromParcel(accountInfoParcel);
    accountInfoParcel.recycle();

    return bundle;
}

From source file:org.sufficientlysecure.keychain.ui.adapter.MultiUserIdsAdapter.java

public ArrayList<CertifyAction> getSelectedCertifyActions() {
    LongSparseArray<CertifyAction> actions = new LongSparseArray<>();
    for (int i = 0; i < mCheckStates.size(); i++) {
        if (mCheckStates.get(i)) {
            mCursor.moveToPosition(i);//ww  w  . ja  va2s. co m

            long keyId = mCursor.getLong(0);
            byte[] data = mCursor.getBlob(1);

            Parcel p = Parcel.obtain();
            p.unmarshall(data, 0, data.length);
            p.setDataPosition(0);
            ArrayList<String> uids = p.createStringArrayList();
            p.recycle();

            CertifyAction action = actions.get(keyId);
            if (actions.get(keyId) == null) {
                actions.put(keyId, new CertifyAction(keyId, uids, null));
            } else {
                action.mUserIds.addAll(uids);
            }
        }
    }

    ArrayList<CertifyAction> result = new ArrayList<>(actions.size());
    for (int i = 0; i < actions.size(); i++) {
        result.add(actions.valueAt(i));
    }
    return result;
}

From source file:com.android.mail.NotificationActionIntentService.java

@Override
protected void onHandleIntent(final Intent intent) {
    final Context context = this;
    final String action = intent.getAction();

    /*/*from w  w  w . j  a  va 2s.c  om*/
     * Grab the alarm from the intent. Since the remote AlarmManagerService fills in the Intent
     * to add some extra data, it must unparcel the NotificationAction object. It throws a
     * ClassNotFoundException when unparcelling.
     * To avoid this, do the marshalling ourselves.
     */
    final NotificationAction notificationAction;
    final byte[] data = intent.getByteArrayExtra(EXTRA_NOTIFICATION_ACTION);
    if (data != null) {
        final Parcel in = Parcel.obtain();
        in.unmarshall(data, 0, data.length);
        in.setDataPosition(0);
        notificationAction = NotificationAction.CREATOR.createFromParcel(in,
                NotificationAction.class.getClassLoader());
    } else {
        LogUtils.wtf(LOG_TAG, "data was null trying to unparcel the NotificationAction");
        return;
    }

    final Message message = notificationAction.getMessage();

    final ContentResolver contentResolver = getContentResolver();

    LogUtils.i(LOG_TAG, "Handling %s", action);

    logNotificationAction(action, notificationAction);

    if (notificationAction.getSource() == NotificationAction.SOURCE_REMOTE) {
        // Skip undo if the action is bridged from remote node.  This should be similar to the
        // logic after the Undo notification expires in a regular flow.
        LogUtils.d(LOG_TAG, "Canceling %s", notificationAction.getNotificationId());
        NotificationManagerCompat.from(context).cancel(notificationAction.getNotificationId());
        NotificationActionUtils.processDestructiveAction(this, notificationAction);
        NotificationActionUtils.resendNotifications(context, notificationAction.getAccount(),
                notificationAction.getFolder());
        return;
    }

    if (ACTION_UNDO.equals(action)) {
        NotificationActionUtils.cancelUndoTimeout(context, notificationAction);
        NotificationActionUtils.cancelUndoNotification(context, notificationAction);
    } else if (ACTION_ARCHIVE_REMOVE_LABEL.equals(action) || ACTION_DELETE.equals(action)) {
        // All we need to do is switch to an Undo notification
        NotificationActionUtils.createUndoNotification(context, notificationAction);

        NotificationActionUtils.registerUndoTimeout(context, notificationAction);
    } else {
        if (ACTION_UNDO_TIMEOUT.equals(action) || ACTION_DESTRUCT.equals(action)) {
            // Process the action
            NotificationActionUtils.cancelUndoTimeout(this, notificationAction);
            NotificationActionUtils.processUndoNotification(this, notificationAction);
        } else if (ACTION_MARK_READ.equals(action)) {
            final Uri uri = message.uri;

            final ContentValues values = new ContentValues(1);
            values.put(UIProvider.MessageColumns.READ, 1);

            contentResolver.update(uri, values, null, null);
        }

        NotificationActionUtils.resendNotifications(context, notificationAction.getAccount(),
                notificationAction.getFolder());
    }
}