Example usage for android.os Parcel setDataPosition

List of usage examples for android.os Parcel setDataPosition

Introduction

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

Prototype

public final void setDataPosition(int pos) 

Source Link

Document

Move the current read/write position in the parcel.

Usage

From source file:jp.alessandro.android.iab.ItemParcelableTest.java

@Test
public void writeToParcel() throws JSONException {
    Item item = Item.parseJson(String.format(Locale.ENGLISH, DataConverter.SKU_SUBSCRIPTION_DETAILS_JSON, 0));

    // Obtain a Parcel object and write the parcelable object to it
    Parcel parcel = Parcel.obtain();
    item.writeToParcel(parcel, item.describeContents());

    // After you're done with writing, you need to reset the parcel for reading
    parcel.setDataPosition(0);

    Item fromParcel = Item.CREATOR.createFromParcel(parcel);

    assertThat(item.getOriginalJson()).isEqualTo(fromParcel.getOriginalJson());
    assertThat(item.getSku()).isEqualTo(fromParcel.getSku());
    assertThat(item.getType()).isEqualTo(fromParcel.getType());
    assertThat(item.getTitle()).isEqualTo(fromParcel.getTitle());
    assertThat(item.getDescription()).isEqualTo(fromParcel.getDescription());
    assertThat(item.getCurrency()).isEqualTo(fromParcel.getCurrency());
    assertThat(item.getPrice()).isEqualTo(fromParcel.getPrice());
    assertThat(item.getPriceMicros()).isEqualTo(fromParcel.getPriceMicros());
    assertThat(item.getSubscriptionPeriod()).isEqualTo(fromParcel.getSubscriptionPeriod());
    assertThat(item.getFreeTrialPeriod()).isEqualTo(fromParcel.getFreeTrialPeriod());
    assertThat(item.getIntroductoryPrice()).isEqualTo(fromParcel.getIntroductoryPrice());
    assertThat(item.getIntroductoryPriceAmountMicros())
            .isEqualTo(fromParcel.getIntroductoryPriceAmountMicros());
    assertThat(item.getIntroductoryPricePeriod()).isEqualTo(fromParcel.getIntroductoryPricePeriod());
    assertThat(item.getIntroductoryPriceCycles()).isEqualTo(fromParcel.getIntroductoryPriceCycles());
}

From source file:jp.alessandro.android.iab.PurchaseParcelableTest.java

private void writeToParcel(Purchase purchase) {
    // Obtain a Parcel object and write the parcelable object to it
    Parcel parcel = Parcel.obtain();
    purchase.writeToParcel(parcel, purchase.describeContents());

    // After you're done with writing, you need to reset the parcel for reading
    parcel.setDataPosition(0);

    Purchase fromParcel = Purchase.CREATOR.createFromParcel(parcel);

    assertThat(purchase.getOriginalJson()).isEqualTo(fromParcel.getOriginalJson());
    assertThat(purchase.getOrderId()).isEqualTo(fromParcel.getOrderId());
    assertThat(purchase.getPackageName()).isEqualTo(fromParcel.getPackageName());
    assertThat(purchase.getSku()).isEqualTo(fromParcel.getSku());
    assertThat(purchase.getPurchaseTime()).isEqualTo(fromParcel.getPurchaseTime());
    assertThat(purchase.getPurchaseState()).isEqualTo(fromParcel.getPurchaseState());
    assertThat(purchase.getDeveloperPayload()).isEqualTo(fromParcel.getDeveloperPayload());
    assertThat(purchase.getToken()).isEqualTo(fromParcel.getToken());
    assertThat(purchase.isAutoRenewing()).isEqualTo(fromParcel.isAutoRenewing());
    assertThat(purchase.getSignature()).isEqualTo(fromParcel.getSignature());
}

From source file:io.github.data4all.model.DeviceOrientationTest.java

/**
 * Create a new Parcel to save/parcelable the testDeviceOrientation,
 * afterwards a new deviceorientation is created from the parcel and we
 * check if it contains all attributes.//from  w w w  .  ja  va 2s. c  om
 */
@Test
public void test_parcelable_node() {
    Parcel newParcel = Parcel.obtain();
    DeviceOrientation testDeviceOrientation = new DeviceOrientation(10, 20, 30, 1234567);

    testDeviceOrientation.writeToParcel(newParcel, 0);
    newParcel.setDataPosition(0);
    DeviceOrientation deParcelDeviceOrientation = DeviceOrientation.CREATOR.createFromParcel(newParcel);

    assertEquals(testDeviceOrientation.getAzimuth(), deParcelDeviceOrientation.getAzimuth(), 0);
    assertEquals(testDeviceOrientation.getPitch(), deParcelDeviceOrientation.getPitch(), 0);
    assertEquals(testDeviceOrientation.getRoll(), deParcelDeviceOrientation.getRoll(), 0);
    assertEquals(testDeviceOrientation.getTimestamp(), deParcelDeviceOrientation.getTimestamp(), 0);

}

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   www. j a v a2  s .co m*/
     * 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());
    }
}

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);/*from w  w  w. ja  v a  2 s .com*/

            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.oasisfeng.nevo.decorators.media.MediaPlayerDecorator.java

/** Tiny hack to convert IntentSender to PendingIntent */
PendingIntent getPendingIntent(final IntentSender sender) {
    final Parcel parcel = Parcel.obtain();
    try {/*w ww .j a  v  a2 s. co  m*/
        parcel.setDataPosition(0);
        sender.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);
        return PendingIntent.CREATOR.createFromParcel(parcel);
    } finally {
        parcel.recycle();
    }
}

From source file:edu.umich.flowfence.common.ParceledPayload.java

public Object getValue(ClassLoader loader) {
    Parcel p = Parcel.obtain();
    try {/*from ww w  .  j a va2 s  .c o m*/
        p.unmarshall(data, 0, data.length);
        p.setDataPosition(0);
        return p.readValue(loader);
    } finally {
        p.recycle();
    }
}

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

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView vHeaderId = (TextView) view.findViewById(R.id.user_id_header);
    TextView vName = (TextView) view.findViewById(R.id.user_id_item_name);
    TextView vAddresses = (TextView) view.findViewById(R.id.user_id_item_addresses);

    byte[] data = cursor.getBlob(1);
    int isHeader = cursor.getInt(2);
    Parcel p = Parcel.obtain();
    p.unmarshall(data, 0, data.length);// w w  w . j a  va2s.c om
    p.setDataPosition(0);
    ArrayList<String> uids = p.createStringArrayList();
    p.recycle();

    { // first one
        String userId = uids.get(0);
        OpenPgpUtils.UserId splitUserId = KeyRing.splitUserId(userId);
        if (splitUserId.name != null) {
            vName.setText(splitUserId.name);
        } else {
            vName.setText(R.string.user_id_no_name);
        }

        if (isHeader == 1) {
            vHeaderId.setVisibility(View.VISIBLE);
            String message;
            if (splitUserId.name != null) {
                message = mContext.getString(R.string.section_uids_to_certify) + splitUserId.name;
            } else {
                message = mContext.getString(R.string.section_uids_to_certify)
                        + context.getString(R.string.user_id_no_name);
            }
            vHeaderId.setText(message);
        } else {
            vHeaderId.setVisibility(View.GONE);
        }
    }

    StringBuilder lines = new StringBuilder();
    for (String uid : uids) {
        OpenPgpUtils.UserId splitUserId = KeyRing.splitUserId(uid);
        if (splitUserId.email == null) {
            continue;
        }
        lines.append(splitUserId.email);
        if (splitUserId.comment != null) {
            lines.append(" (").append(splitUserId.comment).append(")");
        }
        lines.append('\n');
    }

    // If we have any data here, show it
    if (lines.length() > 0) {
        // delete last newline
        lines.setLength(lines.length() - 1);
        vAddresses.setVisibility(View.VISIBLE);
        vAddresses.setText(lines);
    } else {
        vAddresses.setVisibility(View.GONE);
    }

    final CheckBox vCheckBox = (CheckBox) view.findViewById(R.id.user_id_item_check_box);
    final int position = cursor.getPosition();
    vCheckBox.setOnCheckedChangeListener(null);
    vCheckBox.setChecked(mCheckStates.get(position));
    vCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            mCheckStates.set(position, b);
        }
    });
    vCheckBox.setClickable(false);
    vCheckBox.setVisibility(checkboxVisibility ? View.VISIBLE : View.GONE);

    View vUidBody = view.findViewById(R.id.user_id_body);
    vUidBody.setClickable(true);
    vUidBody.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            vCheckBox.toggle();
        }
    });

}

From source file:com.harlie.android.sunshine.app.ItemChoiceManager.java

public void onRestoreInstanceState(Bundle savedInstanceState) {
    byte[] states = savedInstanceState.getByteArray(SELECTED_ITEMS_KEY);
    if (null != states) {
        Parcel inParcel = Parcel.obtain();
        inParcel.unmarshall(states, 0, states.length);
        inParcel.setDataPosition(0);
        mCheckStates = inParcel.readSparseBooleanArray();
        final int numStates = inParcel.readInt();
        mCheckedIdStates.clear();/*  w  w  w  .j  av  a 2s .  co  m*/
        for (int i = 0; i < numStates; i++) {
            final long key = inParcel.readLong();
            final int value = inParcel.readInt();
            mCheckedIdStates.put(key, value);
        }
        inParcel.recycle();
    }
}

From source file:app.com.example.android.sunshine.ItemChoiceManager.java

public void onRestoreInstanceState(Bundle savedInstanceState) {
    byte[] states = savedInstanceState.getByteArray(SELECTED_ITEMS_KEY);
    if (null != states) {
        Parcel inParcel = Parcel.obtain();
        inParcel.unmarshall(states, 0, states.length);
        inParcel.setDataPosition(0);
        mCheckStates = inParcel.readSparseBooleanArray();
        final int numStates = inParcel.readInt();
        mCheckedIdStates.clear();/*from  www  .  ja va  2s . com*/
        for (int i = 0; i < numStates; i++) {
            final long key = inParcel.readLong();
            final int value = inParcel.readInt();
            mCheckedIdStates.put(key, value);
        }
    }
}