private static Parcel toParcel(Bundle data) { Parcel serialized = Parcel.obtain(); data.writeToParcel(serialized, 0); serialized.setDataPosition(0); return serialized; }
/** @see #MAX_EXTRAS_SIZE_BYTES */ private static int measureBundleSize(Bundle extras) { Parcel p = Parcel.obtain(); extras.writeToParcel(p, 0); int sizeInBytes = p.dataSize(); p.recycle(); return sizeInBytes; }
@Override public void windowAttached2(Bundle bundle, ILauncherOverlayCallback iLauncherOverlayCallback) throws RemoteException { IBinder iBinder = null; Parcel obtain = Parcel.obtain(); try { obtain.writeInterfaceToken("com.google.android.libraries.launcherclient.ILauncherOverlay"); if (bundle == null) { obtain.writeInt(0); } else { obtain.writeInt(1); bundle.writeToParcel(obtain, 0); } if (iLauncherOverlayCallback != null) { iBinder = iLauncherOverlayCallback.asBinder(); } obtain.writeStrongBinder(iBinder); this.mRemote.transact(14, obtain, null, FLAG_ONEWAY); } finally { obtain.recycle(); } }
//Create parcelable object and put to Bundle Question q = new Question(questionId, surveyServerId, title, type, answers); Bundle b = new Bundle(); b.putParcelable("someTag", q); //Save bundle to parcel Parcel parcel = Parcel.obtain(); b.writeToParcel(parcel, 0); //Extract bundle from parcel parcel.setDataPosition(0); Bundle b2 = parcel.readBundle(); b2.setClassLoader(Question.class.getClassLoader()); Question q2 = b2.getParcelable("someTag"); //Check that objects are not same and test that objects are equal assertFalse("Bundle is the same", b2 == b); assertFalse("Question is the same", q2 == q); assertTrue("Questions aren't equal", q2.equals(q));
Bundle in = ...; OutputStream os = ... Parcel p = Parcel.obtain(); //create empty parcel object in.writeToParcel(p, 0); //saving bundle as parcel os.write(p.marshall()); //write parcel to stream
Bundle in=yourBundle; FileOutputStream fos = context.openFileOutput(localFilename, Context.MODE_PRIVATE); Parcel p = Parcel.obtain(); //creating empty parcel object in.writeToParcel(p, 0); //saving bundle as parcel fos.write(p.marshall()); //writing parcel to file fos.flush(); fos.close();
/***************************************************** * * Returns a parcel containing the contents of a bundle. * *****************************************************/ static public Parcel parcelFromBundle( Bundle bundle ) { Parcel parcel = Parcel.obtain(); bundle.writeToParcel( parcel, 0 ); return ( parcel ); }
private void put(SQLiteDatabase db, String name, Bundle bundle) { ByteArrayOutputStream valueStream = new ByteArrayOutputStream(); try { ContentValues rows = new ContentValues(); rows.put("name", name); Parcel p = Parcel.obtain(); bundle.writeToParcel(p, 0); valueStream.write(p.marshall()); rows.put("value", valueStream.toByteArray()); db.insert("bundles", null, rows); valueStream.close(); } catch (IOException e) { Log.e("error writing object", e.toString()); } }
private static Parcel toParcel(Bundle data) { Parcel serialized = Parcel.obtain(); data.writeToParcel(serialized, 0); serialized.setDataPosition(0); return serialized; }
Bundle bundle = shortcutIntent.getExtras(); Parcel parcel = Parcel.obtain(); bundle.writeToParcel(parcel, 0); byte[] byt = parcel.marshall(); String s = Base64.encodeToString(byt, 0, byt.length, 0); //store this string to sqlite byte[] newByt = Base64.decode(s, 0); Bundle newBundle = new Bundle(); Parcel newParcel = Parcel.obtain(); newParcel.unmarshall(newByt, 0, newByt.length); newParcel.setDataPosition(0); newBundle.readFromParcel(newParcel); Intent intent = new Intent(); intent.putExtras(newBundle); MainActivity.getContext().startActivity((Intent)intent.getExtras().get(Intent.EXTRA_SHORTCUT_INTENT));
public void saveToPreferences(Bundle in) { Parcel parcel = Parcel.obtain(); String serialized = null; try { in.writeToParcel(parcel, 0); ByteArrayOutputStream bos = new ByteArrayOutputStream(); //IOUtils.write(parcel.marshall(), bos); bos.write(parcel.marshall(),0,parcel.marshall().length); serialized = Base64.encodeToString(bos.toByteArray(), 0); } catch (Exception e) { Log.e(getClass().getSimpleName(), e.toString(), e); } finally { parcel.recycle(); } if (serialized != null) { SharedPreferences settings = activity.getSharedPreferences("instance", 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("parcel", serialized); editor.commit(); } }
private String serialize(Bundle in) { Parcel parcel = Parcel.obtain(); try { in.writeToParcel(parcel, 0); return Base64.encodeToString(parcel.marshall(), 0); } finally { parcel.recycle(); } }
@Override public final boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case FIRST_CODE: { data.enforceInterface(DESCRIPTOR); Bundle param; if ((0 != data.readInt())) { param = Bundle.CREATOR.createFromParcel(data); } else { param = null; } Bundle result = MethodRouter.routerToInstance(param); reply.writeNoException(); if ((result != null)) { reply.writeInt(1); result.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE); } else { reply.writeInt(0); } return true; } } return super.onTransact(code, data, reply, flags); }
private Bundle createDeepCopy(Bundle original) { Parcel temp = Parcel.obtain(); original.writeToParcel(temp, 0); temp.setDataPosition(0); Bundle copy = temp.readBundle(); temp.recycle(); return copy; } }
@Test public void shouldBeParceled() throws Exception { JWT jwtOrigin = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.K17vlwhE8FCMShdl1_65jEYqsQqBOVMPUU9IgG-QlTM"); assertThat(jwtOrigin, is(notNullValue())); Bundle bundleOrigin = new Bundle(); bundleOrigin.putParcelable("jwt", jwtOrigin); Parcel parcel = Parcel.obtain(); bundleOrigin.writeToParcel(parcel, 0); //Extract bundle from parcel parcel.setDataPosition(0); Bundle bundleDest = parcel.readBundle(JWT.class.getClassLoader()); JWT jwtDest = bundleDest.getParcelable("jwt"); assertThat(jwtDest, is(notNullValue())); assertThat(bundleOrigin, is(not(bundleDest))); assertThat(jwtOrigin, is(not(jwtDest))); assertThat(jwtOrigin.toString(), is(jwtDest.toString())); }
private static Bundle transact(String descriptor, IBinder remote, Bundle param) throws RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); Bundle _result; try { _data.writeInterfaceToken(descriptor); if ((param != null)) { _data.writeInt(1); param.writeToParcel(_data, 0); } else { _data.writeInt(0); } remote.transact(ProcessBinder.FIRST_CODE, _data, _reply, 0); _reply.readException(); if ((0 != _reply.readInt())) { _result = Bundle.CREATOR.createFromParcel(_reply); } else { _result = null; } } finally { _reply.recycle(); _data.recycle(); } return _result; } }
public boolean config(Bundle params) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); int result = 0; try { data.writeInterfaceToken(DESCRIPTION); params.writeToParcel(data, 0); remote.transact(CONFIG_TRANSACTION, data, reply, 0); reply.readException(); result = reply.readInt(); } finally { reply.recycle(); data.recycle(); } return result != 0; }
public void testCanParcelJson(){ JsonObject o = new JsonObject(); o.put("Key1", "val1"); o.put("Key2", new JsonObject().put("a", "a").put("b", new JsonArray().add(new JsonObject().put("x", "x")))); Bundle b = new Bundle(); b.putParcelable("SAVE",o); Parcel p = Parcel.obtain(); b.writeToParcel(p,0); p.setDataPosition(0); Bundle readBack = p.readBundle(); readBack.setClassLoader(BaasDocument.class.getClassLoader()); JsonObject backObj = readBack.getParcelable("SAVE"); assertEquals(o.toString(),backObj.toString()); }
public void testCanParcelDocuments(){ BaasDocument doc = new BaasDocument("fake"); doc.put("Key", "Val1"); doc.put("Key2", "Val2"); doc.put("Key3", new JsonArray().add("ciao")); Bundle b = new Bundle(); b.putParcelable("SAVE",doc); Parcel p = Parcel.obtain(); b.writeToParcel(p,0); p.setDataPosition(0); Bundle readBack = p.readBundle(); readBack.setClassLoader(BaasDocument.class.getClassLoader()); BaasDocument newDoc = readBack.getParcelable("SAVE"); assertEquals(newDoc.getString("Key"),doc.getString("Key")); assertEquals(newDoc.getString("Key2"),doc.getString("Key2")); assertEquals(newDoc.getArray("Key3").getString(0),doc.getArray("Key3").getString(0)); }
b.writeToParcel(p,0); p.setDataPosition(0); Bundle readBack = p.readBundle();