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

The following examples show how to use android.os.Parcel#obtain() . 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: droidconat-2016   File: ScheduleTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void should_restore_from_parcelable() {
    // Given
    Schedule schedule = new Schedule();
    ScheduleDay day1 = new ScheduleDay(null, null);
    ScheduleDay day2 = new ScheduleDay(null, null);
    schedule.add(day1);
    schedule.add(day2);

    // When
    Parcel parcel = Parcel.obtain();
    schedule.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    Schedule fromParcel = Schedule.CREATOR.createFromParcel(parcel);

    // Then
    assertThat(schedule).isEqualTo(fromParcel);
    assertThat(fromParcel).hasSize(2);
}
 
Example 2
@Test
public void testEventToParcelableTest_basicTestCaseParcelable() {

  String className = "Class";
  String methodName = "Method";
  TestCase testCase = new TestCase(className, methodName, new ArrayList<>(), new ArrayList<>());

  TestRunEvent testRunEvent = new TestRunEvent(testCase);
  Parcel parcel = Parcel.obtain();
  testRunEvent.writeToParcel(parcel, 0);

  parcel.setDataPosition(0);

  TestRunEvent testRunEventFromParcel = TestRunEvent.CREATOR.createFromParcel(parcel);

  assertThat(testRunEventFromParcel.getTestCase().getClassName()).isEqualTo(className);
  assertThat(testRunEventFromParcel.getTestCase().getMethodName()).isEqualTo(methodName);
}
 
Example 3
@Test
public void prepareJob() {
  Intent intent = new Intent();

  Bundle encode = encodeContentUriJob(getContentUriTrigger(), TestUtil.JOB_CODER);
  intent.putExtra(GooglePlayJobWriter.REQUEST_PARAM_EXTRAS, encode);

  Parcel container = Parcel.obtain();
  container.writeStrongBinder(new Binder());
  PendingCallback pcb = new PendingCallback(container);
  intent.putExtra("callback", pcb);

  ArrayList<Uri> uris = new ArrayList<>();
  uris.add(ContactsContract.AUTHORITY_URI);
  uris.add(Media.EXTERNAL_CONTENT_URI);
  intent.putParcelableArrayListExtra(BundleProtocol.PACKED_PARAM_TRIGGERED_URIS, uris);

  JobInvocation jobInvocation = receiver.prepareJob(intent);
  assertEquals(jobInvocation.getTriggerReason().getTriggeredContentUris(), uris);
}
 
Example 4
Source Project: letv   File: RemoteDeviceManager.java    License: Apache License 2.0 6 votes vote down vote up
public DeviceInfo getLastUsedDevice(int deviceType) throws RemoteException {
    Parcel _data = Parcel.obtain();
    Parcel _reply = Parcel.obtain();
    try {
        DeviceInfo _result;
        _data.writeInterfaceToken(Stub.DESCRIPTOR);
        _data.writeInt(deviceType);
        this.mRemote.transact(4, _data, _reply, 0);
        ExceptionUtils.readExceptionFromParcel(_reply);
        if (_reply.readInt() != 0) {
            _result = (DeviceInfo) DeviceInfo.CREATOR.createFromParcel(_reply);
        } else {
            _result = null;
        }
        _reply.recycle();
        _data.recycle();
        return _result;
    } catch (Throwable th) {
        _reply.recycle();
        _data.recycle();
    }
}
 
Example 5
Source Project: letv   File: IMediaSession.java    License: Apache License 2.0 6 votes vote down vote up
public void sendCustomAction(String action, Bundle args) throws RemoteException {
    Parcel _data = Parcel.obtain();
    Parcel _reply = Parcel.obtain();
    try {
        _data.writeInterfaceToken(Stub.DESCRIPTOR);
        _data.writeString(action);
        if (args != null) {
            _data.writeInt(1);
            args.writeToParcel(_data, 0);
        } else {
            _data.writeInt(0);
        }
        this.mRemote.transact(26, _data, _reply, 0);
        _reply.readException();
    } finally {
        _reply.recycle();
        _data.recycle();
    }
}
 
Example 6
Source Project: android_9.0.0_r45   File: CryptoHelper.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
/* default */ Bundle decryptBundle(@NonNull Bundle bundle) throws GeneralSecurityException {
    Preconditions.checkNotNull(bundle, "Cannot decrypt null bundle.");
    byte[] iv = bundle.getByteArray(KEY_IV);
    byte[] encryptedBytes = bundle.getByteArray(KEY_CIPHER);
    byte[] mac = bundle.getByteArray(KEY_MAC);
    if (!verifyMac(encryptedBytes, iv, mac)) {
        Log.w(TAG, "Escrow mac mismatched!");
        return null;
    }

    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, mEncryptionKey, ivSpec);
    byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

    Parcel decryptedParcel = Parcel.obtain();
    decryptedParcel.unmarshall(decryptedBytes, 0, decryptedBytes.length);
    decryptedParcel.setDataPosition(0);
    Bundle decryptedBundle = new Bundle();
    decryptedBundle.readFromParcel(decryptedParcel);
    decryptedParcel.recycle();
    return decryptedBundle;
}
 
Example 7
public int postMessage(ICustomTabsCallback callback, String message, Bundle extras) throws RemoteException {
    Parcel _data = Parcel.obtain();
    Parcel _reply = Parcel.obtain();

    int _result;
    try {
        _data.writeInterfaceToken("android.support.customtabs.ICustomTabsService");
        _data.writeStrongBinder(callback != null?callback.asBinder():null);
        _data.writeString(message);
        if(extras != null) {
            _data.writeInt(1);
            extras.writeToParcel(_data, 0);
        } else {
            _data.writeInt(0);
        }

        this.mRemote.transact(8, _data, _reply, 0);
        _reply.readException();
        _result = _reply.readInt();
    } finally {
        _reply.recycle();
        _data.recycle();
    }

    return _result;
}
 
Example 8
Source Project: xDrip   File: BitmapLoader.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static int getBundleSizeInBytes(Bundle bundle) {
    final Parcel parcel = Parcel.obtain();
    parcel.writeBundle(bundle);
    final int size = parcel.dataSize();
    parcel.recycle();
    return size;
}
 
Example 9
@Test
public void testViewDataIsParcelable() {
    final GuidanceSpeedData data = new GuidanceSpeedData(VELOCITY, SPEED_LIMIT);
    GuidanceSpeedLimitView.SavedState savedState = new GuidanceSpeedLimitView.SavedState(AbsSavedState.EMPTY_STATE);
    savedState.setStateToSave(data);
    Parcel parcel = Parcel.obtain();
    savedState.writeToParcel(parcel, savedState.describeContents());
    parcel.setDataPosition(0);

    GuidanceSpeedLimitView.SavedState createdFromParcel = GuidanceSpeedLimitView.SavedState.CREATOR.createFromParcel(
            parcel);
    assertNotNull(createdFromParcel.getSavedState());
}
 
Example 10
Source Project: postman   File: ParcelTestUtils.java    License: MIT License 5 votes vote down vote up
public static <T extends Parcelable> T writeAndReadParcelable(Parcelable in) {
    Parcel parcel = Parcel.obtain();
    parcel.writeParcelable(in, 0);

    parcel.setDataPosition(0);
    T value = parcel.readParcelable(RuntimeEnvironment.application.getClassLoader());

    parcel.recycle();

    return value;
}
 
Example 11
@Test
public void shouldCorrectlySaveAndRestoreAParcelWithMinTime() {
    Timepoint minTime = new Timepoint(1, 2, 3);

    DefaultTimepointLimiter limiter = new DefaultTimepointLimiter();
    limiter.setMinTime(minTime);

    Parcel limiterParcel = Parcel.obtain();
    limiter.writeToParcel(limiterParcel, 0);
    limiterParcel.setDataPosition(0);

    DefaultTimepointLimiter clonedLimiter = DefaultTimepointLimiter.CREATOR.createFromParcel(limiterParcel);

    assertEquals(clonedLimiter.getMinTime(), minTime);
}
 
Example 12
public HealthStats getHealthStats() {
    if (mWriter != null) {
        final Parcel parcel = Parcel.obtain();
        mWriter.flattenToParcel(parcel);
        parcel.setDataPosition(0);
        mHealthStats = new HealthStats(parcel);
        parcel.recycle();
    }

    return mHealthStats;
}
 
Example 13
@Test
public void testWriteToParcel() {
    final Parcelable parcelable = new View.BaseSavedState(AbsSavedState.EMPTY_STATE);
    final ExpandableSavedState ss = new ExpandableSavedState(parcelable);
    ss.setSize(1000);
    ss.setWeight(0.5f);

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

    final ExpandableSavedState target = ExpandableSavedState.CREATOR.createFromParcel(parcel);
    assertThat(target.getSize(), is(1000));
    assertThat(target.getWeight(), is(0.5f));
}
 
Example 14
Source Project: letv   File: IMediaSession.java    License: Apache License 2.0 5 votes vote down vote up
public void setVolumeTo(int value, int flags, String packageName) throws RemoteException {
    Parcel _data = Parcel.obtain();
    Parcel _reply = Parcel.obtain();
    try {
        _data.writeInterfaceToken(Stub.DESCRIPTOR);
        _data.writeInt(value);
        _data.writeInt(flags);
        _data.writeString(packageName);
        this.mRemote.transact(12, _data, _reply, 0);
        _reply.readException();
    } finally {
        _reply.recycle();
        _data.recycle();
    }
}
 
Example 15
@Test
public void testCallIntDelegation() throws Exception {
    Parcel p = Parcel.obtain();
    mockStatic(ABridge.class);
    when(ABridge.callInt(eq("android.os.Parcel.nativeReadInt(long)"), any(Object.class), any(Object[].class))).thenReturn(42);
    assertEquals(42, p.readInt());
}
 
Example 16
@Test
public void shouldCorrectlySaveAndRestoreAParcel() {
    Timepoint input = new Timepoint(1, 2, 3);
    Parcel timepointParcel = Parcel.obtain();
    input.writeToParcel(timepointParcel, 0);
    timepointParcel.setDataPosition(0);

    Timepoint output = Timepoint.CREATOR.createFromParcel(timepointParcel);
    assertEquals(input.getHour(), output.getHour());
    assertEquals(input.getMinute(), output.getMinute());
    assertEquals(input.getSecond(), output.getSecond());
}
 
Example 17
Source Project: px-android   File: ParcelableUtil.java    License: MIT License 5 votes vote down vote up
@NonNull
public static byte[] marshall(@NonNull final Parcelable parcelable) {
    final Parcel parcel = Parcel.obtain();
    parcelable.writeToParcel(parcel, 0);
    final byte[] bytes = parcel.marshall();
    parcel.recycle();
    return bytes;
}
 
Example 18
public void writeStateLocked(boolean sync, final boolean commit) {
    final long totalTime;
    synchronized (mPendingWriteLock) {
        final long now = SystemClock.uptimeMillis();
        if (mPendingWrite == null || !mPendingWriteCommitted) {
            mPendingWrite = Parcel.obtain();
            mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
            mProcessStats.mTimePeriodEndUptime = now;
            if (commit) {
                mProcessStats.mFlags |= ProcessStats.FLAG_COMPLETE;
            }
            mProcessStats.writeToParcel(mPendingWrite, 0);
            mPendingWriteFile = new AtomicFile(mFile.getBaseFile());
            mPendingWriteCommitted = commit;
        }
        if (commit) {
            mProcessStats.resetSafely();
            updateFile();
            mAm.requestPssAllProcsLocked(SystemClock.uptimeMillis(), true, false);
        }
        mLastWriteTime = SystemClock.uptimeMillis();
        totalTime = SystemClock.uptimeMillis() - now;
        if (DEBUG) Slog.d(TAG, "Prepared write state in " + now + "ms");
        if (!sync) {
            BackgroundThread.getHandler().post(new Runnable() {
                @Override public void run() {
                    performWriteState(totalTime);
                }
            });
            return;
        }
    }

    performWriteState(totalTime);
}
 
Example 19
Source Project: letv   File: ICdeBinder.java    License: Apache License 2.0 5 votes vote down vote up
public long getStateTotalDuration(String url) throws RemoteException {
    Parcel _data = Parcel.obtain();
    Parcel _reply = Parcel.obtain();
    try {
        _data.writeInterfaceToken(Stub.DESCRIPTOR);
        _data.writeString(url);
        this.mRemote.transact(7, _data, _reply, 0);
        _reply.readException();
        long _result = _reply.readLong();
        return _result;
    } finally {
        _reply.recycle();
        _data.recycle();
    }
}
 
Example 20
private static Parcel toParcel(Bundle data) {
  Parcel serialized = Parcel.obtain();
  data.writeToParcel(serialized, 0);
  serialized.setDataPosition(0);
  return serialized;
}