下面列出了android.os.Parcel#obtain ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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);
}
@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);
}
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();
}
}
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();
}
}
@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;
}
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;
}
private static int getBundleSizeInBytes(Bundle bundle) {
final Parcel parcel = Parcel.obtain();
parcel.writeBundle(bundle);
final int size = parcel.dataSize();
parcel.recycle();
return size;
}
@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());
}
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;
}
@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);
}
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;
}
@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));
}
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();
}
}
@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());
}
@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());
}
@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;
}
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);
}
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();
}
}
private static Parcel toParcel(Bundle data) {
Parcel serialized = Parcel.obtain();
data.writeToParcel(serialized, 0);
serialized.setDataPosition(0);
return serialized;
}