Java Code Examples for android.os.Parcel#marshall()

The following examples show how to use android.os.Parcel#marshall() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: dropbox-sdk-java   File: OpenWithActivity.java    License: MIT License 6 votes vote down vote up
protected String encodeOpenWithIntent(Intent owpIntent) {
    /*
     * Encode OpenWith intent
     * Real 3rd party apps should never run this function as DropboxApp does this entirely
     */

    Bundle extras = owpIntent.getExtras();
    extras.putString("_action", owpIntent.getAction());
    extras.putParcelable("_uri", owpIntent.getData());
    extras.putString("_type", owpIntent.getType());

    // marshall it!
    final Parcel parcel = Parcel.obtain();
    parcel.writeBundle(extras);
    byte[] b = parcel.marshall();
    parcel.recycle();
    return new String(Base64.encode(b, 0));
}
 
Example 2
Source Project: android_9.0.0_r45   File: CryptoHelper.java    License: Apache License 2.0 6 votes vote down vote up
@NonNull
/* default */ Bundle encryptBundle(@NonNull Bundle bundle) throws GeneralSecurityException {
    Preconditions.checkNotNull(bundle, "Cannot encrypt null bundle.");
    Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);
    byte[] clearBytes = parcel.marshall();
    parcel.recycle();

    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, mEncryptionKey);
    byte[] encryptedBytes = cipher.doFinal(clearBytes);
    byte[] iv = cipher.getIV();
    byte[] mac = createMac(encryptedBytes, iv);

    Bundle encryptedBundle = new Bundle();
    encryptedBundle.putByteArray(KEY_CIPHER, encryptedBytes);
    encryptedBundle.putByteArray(KEY_MAC, mac);
    encryptedBundle.putByteArray(KEY_IV, iv);

    return encryptedBundle;
}
 
Example 3
@Override
public int calculateMarshalSize(T value) {
    Parcel parcel = Parcel.obtain();
    try {
        value.writeToParcel(parcel, /*flags*/0);
        int length = parcel.marshall().length;

        if (DEBUG) {
            Log.v(TAG, "calculateMarshalSize, length when parceling "
                    + value + " is " + length);
        }

        return length;
    } finally {
        parcel.recycle();
    }
}
 
Example 4
public MasterSecret parcelClone() {
  Parcel thisParcel = Parcel.obtain();
  Parcel thatParcel = Parcel.obtain();
  byte[] bytes      = null;

  thisParcel.writeValue(this);
  bytes = thisParcel.marshall();

  thatParcel.unmarshall(bytes, 0, bytes.length);
  thatParcel.setDataPosition(0);

  MasterSecret that = (MasterSecret)thatParcel.readValue(MasterSecret.class.getClassLoader());

  thisParcel.recycle();
  thatParcel.recycle();

  return that;
}
 
Example 5
public MockConnection(final CharSequence text, final int cursorPosition) {
    super(null, false);
    // Interaction of spans with Parcels is completely non-trivial, but in the actual case
    // the CharSequences do go through Parcels because they go through IPC. There
    // are some significant differences between the behavior of Spanned objects that
    // have and that have not gone through parceling, so it's much easier to simulate
    // the environment with Parcels than try to emulate things by hand.
    final Parcel p = Parcel.obtain();
    TextUtils.writeToParcel(text.subSequence(0, cursorPosition), p, 0 /* flags */);
    TextUtils.writeToParcel(text.subSequence(cursorPosition, text.length()), p,
            0 /* flags */);
    final byte[] marshalled = p.marshall();
    p.unmarshall(marshalled, 0, marshalled.length);
    p.setDataPosition(0);
    mTextBefore = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
    mTextAfter = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
    mExtractedText = null;
    p.recycle();
}
 
Example 6
@Test
public void testParcelling() {
    PersistableBundle bundle = new PersistableBundle();
    bundle.putString("string", "string");
    bundle.putInt("int", 0);
    bundle.putLong("long", 0);
    bundle.putBoolean("boolean", true);
    // Can't use double or any array, as the instances would be different and equals() would fail.
    Parcel parcel = Parcel.obtain();
    parcel.writeValue(bundle);
    byte[] bytes = parcel.marshall();
    parcel.recycle();
    parcel = Parcel.obtain();
    parcel.unmarshall(bytes, 0, bytes.length);
    parcel.setDataPosition(0);
    PersistableBundle parcelledBundle =
            (PersistableBundle) parcel.readValue(PersistableBundle.class.getClassLoader());

    assertEquals(bundle.toMap(1), parcelledBundle.toMap(1));
}
 
Example 7
Source Project: LLApp   File: CacheUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static byte[] parcelable2Bytes(Parcelable parcelable) {
    if (parcelable == null) return null;
    Parcel parcel = Parcel.obtain();
    parcelable.writeToParcel(parcel, 0);
    byte[] bytes = parcel.marshall();
    parcel.recycle();
    return bytes;
}
 
Example 8
public static byte[] marshall(Parcelable parceable) {
    Parcel parcel = Parcel.obtain();
    parceable.writeToParcel(parcel, 0);
    byte[] bytes = parcel.marshall();
    parcel.recycle();
    return bytes;
}
 
Example 9
public void onSaveInstanceState(Bundle outState) {
    Parcel outParcel = Parcel.obtain();
    outParcel.writeSparseBooleanArray(mCheckStates);
    final int numStates = mCheckedIdStates.size();
    outParcel.writeInt(numStates);
    for (int i=0; i<numStates; i++) {
        outParcel.writeLong(mCheckedIdStates.keyAt(i));
        outParcel.writeInt(mCheckedIdStates.valueAt(i));
    }
    byte[] states = outParcel.marshall();
    outState.putByteArray(SELECTED_ITEMS_KEY, states);
    outParcel.recycle();
}
 
Example 10
Source Project: BusWear   File: WearBusTools.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Converts the Parcelable object to a byte[]
 *
 * @param parcelable
 * @return
 */
public static byte[] parcelToByte(@NonNull Parcelable parcelable) {
    Parcel parcel = Parcel.obtain();
    parcelable.writeToParcel(parcel, 0);
    byte[] bytes = parcel.marshall();
    parcel.recycle();
    return bytes;
}
 
Example 11
public static byte[] serialize(Parcelable parceable) {
  Parcel parcel = Parcel.obtain();
  parceable.writeToParcel(parcel, 0);
  byte[] bytes = parcel.marshall();
  parcel.recycle();
  return bytes;
}
 
Example 12
public byte[] getCurrentStats(List<ParcelFileDescriptor> historic) {
    mAm.mContext.enforceCallingOrSelfPermission(
            android.Manifest.permission.PACKAGE_USAGE_STATS, null);
    Parcel current = Parcel.obtain();
    synchronized (mAm) {
        long now = SystemClock.uptimeMillis();
        mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
        mProcessStats.mTimePeriodEndUptime = now;
        mProcessStats.writeToParcel(current, now, 0);
    }
    mWriteLock.lock();
    try {
        if (historic != null) {
            ArrayList<String> files = getCommittedFiles(0, false, true);
            if (files != null) {
                for (int i=files.size()-1; i>=0; i--) {
                    try {
                        ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
                                new File(files.get(i)), ParcelFileDescriptor.MODE_READ_ONLY);
                        historic.add(pfd);
                    } catch (IOException e) {
                        Slog.w(TAG, "Failure opening procstat file " + files.get(i), e);
                    }
                }
            }
        }
    } finally {
        mWriteLock.unlock();
    }
    return current.marshall();
}
 
Example 13
private static byte[] marshall(final InputMethodSubtype[] array) {
    Parcel parcel = null;
    try {
        parcel = Parcel.obtain();
        parcel.writeTypedArray(array, 0);
        return parcel.marshall();
    } finally {
        if (parcel != null) {
            parcel.recycle();
            parcel = null;
        }
    }
}
 
Example 14
public ServiceTransport clone() {
    Parcel p = Parcel.obtain();
    Parcel p2 = Parcel.obtain();
    getMap().writeToParcel(p, 0);
    byte[] bytes = p.marshall();
    p2.unmarshall(bytes, 0, bytes.length);
    p2.setDataPosition(0);
    Bundle b = p2.readBundle();
    ServiceTransport rval = new ServiceTransport();
    rval.setMap(b);
    return rval;
}
 
Example 15
Source Project: OpenYOLO-Android   File: IntentUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Serializes the provided intent to a byte array.
 */
@NonNull
public static byte[] toBytes(@NonNull Intent intent) {
    require(intent, notNullValue());

    Parcel parcel = Parcel.obtain();
    parcel.writeParcelable(intent, 0);
    byte[] intentBytes = parcel.marshall();
    parcel.recycle();

    return intentBytes;
}
 
Example 16
public static byte[] getAddressAsBytes(Address address) {
    if (address == null) {
        return null;
    }
    final Parcel parcel = Parcel.obtain();
    address.writeToParcel(parcel, 0);
    byte[] addressBytes = parcel.marshall();
    parcel.recycle();
    return addressBytes;
}
 
Example 17
/**
 * Marshalls a list of {@link Parcelable} into parcel bytes for adding into an {@link Intent}
 * extras. Workaround for bug: https://code.google.com/p/android/issues/detail?id=6822
 */
public static byte[] marshallParcelableList(ArrayList<? extends Parcelable> parcelables) {
  Parcel parcel = Parcel.obtain();
  parcel.writeList(parcelables);
  parcel.setDataPosition(0);
  return parcel.marshall();
}
 
Example 18
private static byte[] marshall(final CharSequence cahrSequence) {
    Parcel parcel = null;
    try {
        parcel = Parcel.obtain();
        TextUtils.writeToParcel(cahrSequence, parcel, 0);
        return parcel.marshall();
    } finally {
        if (parcel != null) {
            parcel.recycle();
            parcel = null;
        }
    }
}
 
Example 19
Source Project: JobSchedulerCompat   File: JobStore.java    License: MIT License 5 votes vote down vote up
private <T extends Parcelable> byte[] parcelableToByteArray(T parcelable) {
    Parcel parcel = Parcel.obtain();
    parcel.writeParcelable(parcelable, 0);
    byte[] data = parcel.marshall();
    parcel.recycle();
    return data;
}
 
Example 20
private byte[] marshallRequest(int token, byte data[]) {
    Parcel p = Parcel.obtain();
    p.writeInt(RIL_REQUEST_OEM_RAW);
    p.writeInt(token);
    p.writeByteArray(data);
    byte[] res = p.marshall();
    p.recycle();
    return res;
}