Example usage for android.os Parcel obtain

List of usage examples for android.os Parcel obtain

Introduction

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

Prototype

public static Parcel obtain() 

Source Link

Document

Retrieve a new Parcel object from the pool.

Usage

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

public static byte[] bundleToBytes(Bundle bundle) {
    Parcel accountInfoParcel = Parcel.obtain();
    bundle.writeToParcel(accountInfoParcel, 0);
    byte[] bytes = accountInfoParcel.marshall();

    accountInfoParcel.recycle();/*ww w  .j a v a 2  s .  c  o  m*/
    return bytes;
}

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;
    }/*ww w .j  av a2s  .c om*/

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

    return bundle;
}

From source file:org.droid2droid.internal.AbstractRemoteAndroidImpl.java

protected final void updateData(Parcel data) // TODO: a placer plutot cot serveur
{
    if (UPDATE_PARCEL) {
        int v = VERSION.SDK_INT;
        data.setDataPosition(0);/*from w w w.j a  va2s. co m*/
        if (v >= 10) // Gingerbread_MR1+
        {
            data.readInt();
        }
        String enforceInterfaceName = data.readString(); // Read the interface name (see Parcel.cpp)
        assert (enforceInterfaceName != null);
        byte[] bufDatas = data.marshall(); // Return all the buffer (with the specific enforceInterface
        int startDatas = data.dataPosition(); // Position after the first string

        // Create a new one with interface name + buffers
        Parcel p = Parcel.obtain();
        p.setDataPosition(0);
        p.writeString(enforceInterfaceName);
        int sizeInterface = p.dataPosition();
        byte[] bufInterface = p.marshall(); // Part of buffer only for the string
        p.recycle();

        // Extract the rest of the buffer
        byte[] result = new byte[sizeInterface + bufDatas.length - startDatas];
        System.arraycopy(bufInterface, 0, result, 0, sizeInterface);
        System.arraycopy(bufDatas, startDatas, result, sizeInterface, bufDatas.length - startDatas);
        data.unmarshall(result, 0, result.length);
    }
}

From source file:org.dmfs.tasks.notification.NotificationUpdaterService.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void resolveUndoAction(Intent intent) {
    if (!intent.hasExtra(NotificationActionUtils.EXTRA_NOTIFICATION_ACTION)) {
        return;//from  w  w  w.  j  av a 2  s  . 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 String action = intent.getAction();
    final byte[] data = intent.getByteArrayExtra(NotificationActionUtils.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);
    } else {
        return;
    }

    if (NotificationActionUtils.ACTION_UNDO.equals(action)) {
        NotificationActionUtils.cancelUndoTimeout(this, notificationAction);
        NotificationActionUtils.cancelUndoNotification(this, notificationAction);
        resendNotification(notificationAction);
    } else if (ACTION_COMPLETE.equals(action)) {
        // All we need to do is switch to an Undo notification
        NotificationActionUtils.createUndoNotification(this, notificationAction);
        NotificationActionUtils.registerUndoTimeout(this, notificationAction);
    } else {
        if (NotificationActionUtils.ACTION_UNDO_TIMEOUT.equals(action)
                || NotificationActionUtils.ACTION_DESTRUCT.equals(action)) {
            // Process the action
            NotificationActionUtils.cancelUndoTimeout(this, notificationAction);
            NotificationActionUtils.processUndoNotification(this, notificationAction);
            processDesctructiveNotification(notificationAction);
        }
    }
}

From source file:com.bmd.android.collection.SparseArrayCompatTest.java

public void testParcelable() {

    final ParcelableIntSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableIntSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableIntSparseObjectEntry<String>> parcelableList = AndroidCollections.iterate(mArray)
            .but().keys(Arrays.asList(1, 2, 3)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(4, "4"),
            SparseEntries.parcelableEntry(0, "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);/* w  w w.  j ava2  s. c  o m*/

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableIntSparseObjectEntry<String>> filledList = new ArrayList<ParcelableIntSparseObjectEntry<String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableIntSparseObjectEntry[] filledArray = new ParcelableIntSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2, "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2, "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:com.bmd.android.collection.LongSparseArrayTest.java

public void testParcelable() {

    if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN) {

        return;/*from  ww w .  j a va  2 s .c om*/
    }

    final ParcelableLongSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableLongSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableLongSparseObjectEntry<String>> parcelableList = AndroidCollections.iterate(mArray)
            .but().keys(Arrays.asList(1L, 2L, 3L)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(4L, "4"),
            SparseEntries.parcelableEntry(0L, "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableLongSparseObjectEntry<String>> filledList = new ArrayList<ParcelableLongSparseObjectEntry<String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableLongSparseObjectEntry[] filledArray = new ParcelableLongSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:com.bmd.android.collection.SimpleArrayMapTest.java

public void testParcelable() {

    final ParcelableObjectSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableObjectSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>> parcelableList = AndroidCollections
            .iterate(mArray).but().keys(Arrays.asList(1, 2, 3)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(Integer.valueOf(4), "4"),
            SparseEntries.parcelableEntry(Integer.valueOf(0), "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);//from  ww  w  .j a va  2 s .  co m

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>> filledList = new ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableObjectSparseObjectEntry[] filledArray = new ParcelableObjectSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(Integer.valueOf(2), "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(Integer.valueOf(2), "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:com.bmd.android.collection.SupportLongSparseArrayTest.java

public void testParcelable() {

    final ParcelableLongSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableLongSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableLongSparseObjectEntry<String>> parcelableList = AndroidCollections.iterate(mArray)
            .but().keys(Arrays.asList(1L, 2L, 3L)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(4L, "4"),
            SparseEntries.parcelableEntry(0L, "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);/*from ww  w.  j  a va  2 s . com*/

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableLongSparseObjectEntry<String>> filledList = new ArrayList<ParcelableLongSparseObjectEntry<String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableLongSparseObjectEntry[] filledArray = new ParcelableLongSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:com.android.mail.utils.NotificationActionUtils.java

/**
 * <p>/*from w w w  . j av  a 2  s  . c o  m*/
 * This is a slight hack to avoid an exception in the remote AlarmManagerService process. The
 * AlarmManager adds extra data to this Intent which causes it to inflate. Since the remote
 * process does not know about the NotificationAction class, it throws a ClassNotFoundException.
 * </p>
 * <p>
 * To avoid this, we marshall the data ourselves and then parcel a plain byte[] array. The
 * NotificationActionIntentService class knows to build the NotificationAction object from the
 * byte[] array.
 * </p>
 */
private static void putNotificationActionExtra(final Intent intent,
        final NotificationAction notificationAction) {
    final Parcel out = Parcel.obtain();
    notificationAction.writeToParcel(out, 0);
    out.setDataPosition(0);
    intent.putExtra(NotificationActionIntentService.EXTRA_NOTIFICATION_ACTION, out.marshall());
}

From source file:android.car.ui.provider.CarDrawerLayout.java

@Override
protected void onRestoreInstanceState(Parcelable state) {
    SavedState ss = null;//from w w  w. j a  va 2 s.  co m
    if (state.getClass().getClassLoader() != getClass().getClassLoader()) {
        // Class loader mismatch, recreate from parcel.
        Parcel stateParcel = Parcel.obtain();
        state.writeToParcel(stateParcel, 0);
        ss = SavedState.CREATOR.createFromParcel(stateParcel);
    } else {
        ss = (SavedState) state;
    }
    super.onRestoreInstanceState(ss.getSuperState());

    if (ss.openDrawerGravity != Gravity.NO_GRAVITY) {
        openDrawer();
    }

    setDrawerLockMode(ss.lockModeLeft, Gravity.LEFT);
    setDrawerLockMode(ss.lockModeRight, Gravity.RIGHT);
}