下面列出了android.os.Parcel#recycle ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void onMessageChannelReady(Bundle extras) throws RemoteException {
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
try {
_data.writeInterfaceToken("android.support.customtabs.ICustomTabsCallback");
if(extras != null) {
_data.writeInt(1);
extras.writeToParcel(_data, 0);
} else {
_data.writeInt(0);
}
this.mRemote.transact(4, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
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();
}
public Bundle getIncomingCallout(Intent incoming) {
byte[] incomingCallout = incoming.getByteArrayExtra(EXTRA_KEY_CALLOUT);
byte[] incomingKey = incoming.getByteArrayExtra(EXTRA_KEY_CALLOUT_SYMETRIC_KEY);
Parcel p = Parcel.obtain();
byte[] decoded;
try {
byte[] aesKey = CryptUtil.decrypt(incomingKey, CryptUtil.getPrivateKeyCipher(this.privateKey));
decoded = CryptUtil.decrypt(incomingCallout, CryptUtil.getAesKeyCipher(aesKey));
} catch (GeneralSecurityException gse) {
Logger.exception("Error getting incoming callout", gse);
return null;
}
p.unmarshall(decoded, 0, decoded.length);
p.setDataPosition(0);
Bundle result = p.readBundle();
p.recycle();
return result;
}
public void testServiceErrorParcelableCreator() {
final int code = 1;
final String type = "type";
final String message = "message";
final ServiceError error = new ServiceError(code, type, message);
final Parcel parcel = Parcel.obtain();
error.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
final ServiceError parceled = ServiceError.CREATOR.createFromParcel(parcel);
assertEquals(code, parceled.getCode());
assertEquals(type, parceled.getType());
assertEquals(message, parceled.getMessage());
parcel.recycle();
}
@Override
public void addBook(Book book) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
try {
data.writeInterfaceToken(DESCRIPTOR);
if ((book != null)) {
data.writeInt(1);
book.writeToParcel(data, 0);
} else {
data.writeInt(0);
}
mRemote.transact(TRANSACTION_addBook, data, reply, 0);
reply.readException();
} finally {
reply.recycle();
data.recycle();
}
}
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;
}
@Override
public AssetFileDescriptor openAssetFile(
String callingPkg, Uri url, String mode, ICancellationSignal signal)
throws RemoteException, FileNotFoundException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
try {
data.writeInterfaceToken(IContentProvider.descriptor);
data.writeString(callingPkg);
url.writeToParcel(data, 0);
data.writeString(mode);
data.writeStrongBinder(signal != null ? signal.asBinder() : null);
mRemote.transact(IContentProvider.OPEN_ASSET_FILE_TRANSACTION, data, reply, 0);
DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(reply);
int has = reply.readInt();
AssetFileDescriptor fd = has != 0
? AssetFileDescriptor.CREATOR.createFromParcel(reply) : null;
return fd;
} finally {
data.recycle();
reply.recycle();
}
}
@Override
public List<Book> getBookList() throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
List<Book> result;
try {
data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(TRANSACTION_getBookList, data, reply, 0);
reply.readException();
result = reply.createTypedArrayList(Book.CREATOR);
} finally {
reply.recycle();
data.recycle();
}
return result;
}
public void onPostMessage(String message, Bundle extras) throws RemoteException {
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
try {
_data.writeInterfaceToken("android.support.customtabs.ICustomTabsCallback");
_data.writeString(message);
if(extras != null) {
_data.writeInt(1);
extras.writeToParcel(_data, 0);
} else {
_data.writeInt(0);
}
this.mRemote.transact(5, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
/**
* Constructor by copy
* @param callInfo
*/
public SipCallSession(SipCallSession callInfo) {
Parcel p = Parcel.obtain();
callInfo.writeToParcel(p, 0);
p.setDataPosition(0);
initFromParcel(p);
p.recycle();
}
public void setChannelSeekPosition(String url, double pos) throws RemoteException {
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
try {
_data.writeInterfaceToken(Stub.DESCRIPTOR);
_data.writeString(url);
_data.writeDouble(pos);
this.mRemote.transact(10, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
public void onEncryptStartedError(int errorCode) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
try {
data.writeInt(errorCode);
this.mRemote.transact(2, data, reply, 0);
reply.readException();
} finally {
data.recycle();
reply.recycle();
}
}
public void cancel(String packageName, int id, String tag) throws RemoteException {
Parcel _data = Parcel.obtain();
try {
_data.writeInterfaceToken(Stub.DESCRIPTOR);
_data.writeString(packageName);
_data.writeInt(id);
_data.writeString(tag);
this.mRemote.transact(2, _data, null, 1);
} finally {
_data.recycle();
}
}
public void skipToQueueItem(long id) throws RemoteException {
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
try {
_data.writeInterfaceToken(Stub.DESCRIPTOR);
_data.writeLong(id);
this.mRemote.transact(17, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
public void seekTo(long pos) throws RemoteException {
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
try {
_data.writeInterfaceToken(Stub.DESCRIPTOR);
_data.writeLong(pos);
this.mRemote.transact(24, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
public static byte[] serialize(Parcelable parceable) {
Parcel parcel = Parcel.obtain();
parceable.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle();
return bytes;
}
void performWriteState(long initialTime) {
if (DEBUG) Slog.d(TAG, "Performing write to " + mFile.getBaseFile());
Parcel data;
AtomicFile file;
synchronized (mPendingWriteLock) {
data = mPendingWrite;
file = mPendingWriteFile;
mPendingWriteCommitted = false;
if (data == null) {
return;
}
mPendingWrite = null;
mPendingWriteFile = null;
mWriteLock.lock();
}
final long startTime = SystemClock.uptimeMillis();
FileOutputStream stream = null;
try {
stream = file.startWrite();
stream.write(data.marshall());
stream.flush();
file.finishWrite(stream);
com.android.internal.logging.EventLogTags.writeCommitSysConfigFile(
"procstats", SystemClock.uptimeMillis() - startTime + initialTime);
if (DEBUG) Slog.d(TAG, "Write completed successfully!");
} catch (IOException e) {
Slog.w(TAG, "Error writing process statistics", e);
file.failWrite(stream);
} finally {
data.recycle();
trimHistoricStatesWriteLocked();
mWriteLock.unlock();
}
}
public void pause() throws RemoteException {
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
try {
_data.writeInterfaceToken(Stub.DESCRIPTOR);
this.mRemote.transact(18, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
/** Strips the custom subclass out of the ResultReceiver so you can ship between packages. */
private static ResultReceiver marshableReceiver(ResultReceiver r) {
if (r.getClass().equals(ResultReceiver.class)) {
return r;
}
Parcel p = Parcel.obtain();
try {
r.writeToParcel(p, 0);
p.setDataPosition(0);
return ResultReceiver.CREATOR.createFromParcel(p);
} finally {
p.recycle();
}
}
public void corruptParcel(int testWith){
Parcel p = Parcel.obtain();
p.writeInt(4);
p.writeLong(System.currentTimeMillis());
p.writeParcelable(new ComponentName(mContext, "test"), 0);
p.writeParcelable(new Intent(), 0);
p.setDataPosition(0);
SdlRouterService.LocalRouterService local = getLocalRouterService(testWith, p);
assertNotNull(local);
assertNull(local.launchIntent);
assertNull(local.name);
p.recycle();
//---------------------------------------------------------------------------
p = Parcel.obtain();
p.writeInt(4);
p.writeLong(System.currentTimeMillis());
p.writeParcelable(null,0);
p.writeParcelable(null,0);
p.setDataPosition(0);
local = getLocalRouterService(testWith, p);
assertNotNull(local);
assertNull(local.launchIntent);
assertNull(local.name);
p.recycle();
//---------------------------------------------------------------------------
p = Parcel.obtain();
p.writeInt(4);
p.writeLong(System.currentTimeMillis());
p.setDataPosition(0);
local = getLocalRouterService(testWith, p);
assertNotNull(local);
assertNull(local.launchIntent);
assertNull(local.name);
p.recycle();
//---------------------------------------------------------------------------
local = null;
p = null;
p = Parcel.obtain();
p.writeInt(4);
p.writeLong(System.currentTimeMillis());
int space = p.dataSize();
p.writeParcelable(new Intent(), 0);
p.writeParcelable(new ComponentName(mContext, "test"), 0);
p.setDataPosition(0);
byte[] raw = p.marshall();
for(;space<raw.length;space++){
raw[space] = 0x00;
}
p.recycle();
p = Parcel.obtain();
p.unmarshall(raw, 0, raw.length);
p.setDataPosition(0);
local = getLocalRouterService(testWith, p);
assertNotNull(local);
assertNull(local.launchIntent);
assertNull(local.name);
p.recycle();
}