Example usage for android.os Parcel marshall

List of usage examples for android.os Parcel marshall

Introduction

In this page you can find the example usage for android.os Parcel marshall.

Prototype

public final byte[] marshall() 

Source Link

Document

Returns the raw bytes of the parcel.

Usage

From source file:Main.java

public static boolean writeParcelable(Context context, String fileName, Parcelable parcelObject) {
    boolean success = false;
    FileOutputStream fos = null;/*from w ww  .j  a  va2  s.c o m*/
    try {
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        Parcel parcel = Parcel.obtain();
        parcel.writeParcelable(parcelObject, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
        byte[] data = parcel.marshall();
        fos.write(data);

        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

    return success;
}

From source file:Main.java

public static byte[] getBytes(@NonNull Parcelable parcelable) {
    if (parcelable == null) {
        return new byte[0];
    }/*from w  ww  . ja va 2s.co m*/
    Parcel parcel = Parcel.obtain();
    parcelable.writeToParcel(parcel, 0);
    byte[] bytes = parcel.marshall();
    parcel.recycle();
    return bytes;
}

From source file:edu.umich.flowfence.common.ParceledPayload.java

public static ParceledPayload create(Object object) {
    Parcel p = Parcel.obtain();
    boolean oldFds = p.pushAllowFds(false);
    try {//from w w  w .j  a v a2  s. c o m
        p.writeValue(object);
        return new ParceledPayload(p.marshall());
    } finally {
        p.restoreAllowFds(oldFds);
        p.recycle();
    }
}

From source file:com.facebook.TestUtils.java

public static <E extends Parcelable> E parcelAndUnparcel(final E object) {
    final Parcel writeParcel = Parcel.obtain();
    final Parcel readParcel = Parcel.obtain();
    try {// ww w.j  a va2 s.com
        writeParcel.writeParcelable(object, 0);
        final byte[] bytes = writeParcel.marshall();
        readParcel.unmarshall(bytes, 0, bytes.length);
        readParcel.setDataPosition(0);
        return readParcel.readParcelable(object.getClass().getClassLoader());
    } finally {
        writeParcel.recycle();
        readParcel.recycle();
    }
}

From source file:com.granita.tasks.notification.NotificationActionUtils.java

/**
 * <p>//from  w w w  . j av a  2 s  .  c  om
 * This is a slight hack to avoid an exception in the remote AlarmManagerService process. The AlarmManager adds extra data to this Intent which causes it to
 * inflate. Since the remote process does not know about the NotificationAction class, it throws a ClassNotFoundException.
 * </p>
 * <p>
 * To avoid this, we marshall the data ourselves and then parcel a plain byte[] array. The NotificationActionIntentService class knows to build the
 * NotificationAction object from the byte[] array.
 * </p>
 */
private static void putNotificationActionExtra(final Intent intent,
        final NotificationAction notificationAction) {
    final Parcel out = Parcel.obtain();
    notificationAction.writeToParcel(out, 0);
    out.setDataPosition(0);
    intent.putExtra(EXTRA_NOTIFICATION_ACTION, out.marshall());
}

From source file:com.smarthome.deskclock.Alarms.java

/**
 * Sets alert in AlarmManger and StatusBar.  This is what will
 * actually launch the alert when the alarm triggers.
 *
 * @param alarm Alarm.//  w ww  .j a v  a 2 s  . co  m
 * @param atTimeInMillis milliseconds since epoch
 */
private static void enableAlert(Context context, final Alarm alarm, final long atTimeInMillis) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    if (Log.LOGV) {
        Log.v("** setAlert id " + alarm.id + " atTime " + atTimeInMillis);
    }

    Intent intent = new Intent(ALARM_ALERT_ACTION);

    // XXX: This is a slight hack to avoid an exception in the remote
    // AlarmManagerService process. The AlarmManager adds extra data to
    // this Intent which causes it to inflate. Since the remote process
    // does not know about the Alarm class, it throws a
    // ClassNotFoundException.
    //
    // To avoid this, we marshall the data ourselves and then parcel a plain
    // byte[] array. The AlarmReceiver class knows to build the Alarm
    // object from the byte[] array.
    Parcel out = Parcel.obtain();
    alarm.writeToParcel(out, 0);
    out.setDataPosition(0);
    intent.putExtra(ALARM_RAW_DATA, out.marshall());

    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);

    setStatusBarIcon(context, true);

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(atTimeInMillis);
    String timeString = formatDayAndTime(context, c);
    saveNextAlarm(context, timeString);
}

From source file:com.ksk.droidbatterybooster.provider.TimeSchedule.java

/**
 * Sets action in AlarmManger.  This is what will
 * actually launch the action when the schedule triggers.
 *
 * @param schedule TimeSchedule.//from   ww w .j  av  a  2  s  . c o m
 * @param atTimeInMillis milliseconds since epoch
 */
@SuppressLint("NewApi")
private static void enableAction(Context context, final TimeSchedule schedule, final long atTimeInMillis) {

    if (Log.LOGV) {
        Log.v("** setSchedule id " + schedule.id + " atTime " + atTimeInMillis);
    }

    Intent intent = new Intent(EXECUTE_SCHEDULE_ACTION);

    // XXX: This is a slight hack to avoid an exception in the remote
    // AlarmManagerService process. The AlarmManager adds extra data to
    // this Intent which causes it to inflate. Since the remote process
    // does not know about the TimeSchedule class, it throws a
    // ClassNotFoundException.
    //
    // To avoid this, we marshall the data ourselves and then parcel a plain
    // byte[] array. The ScheduleReceiver class knows to build the TimeSchedule
    // object from the byte[] array.
    Parcel out = Parcel.obtain();
    schedule.writeToParcel(out, 0);
    out.setDataPosition(0);
    intent.putExtra(INTENT_RAW_DATA, out.marshall());

    PendingIntent sender = PendingIntent.getBroadcast(context, schedule.hashCode(), intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Utils.isKitKatOrLater()) {
        am.setExact(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
    }

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(atTimeInMillis);
    String timeString = formatDayAndTime(context, c);
    saveNextAlarm(context, timeString);
}

From source file:com.jungle.base.utils.MiscUtils.java

public static byte[] bundleToBytes(Bundle bundle) {
    Parcel accountInfoParcel = Parcel.obtain();
    bundle.writeToParcel(accountInfoParcel, 0);
    byte[] bytes = accountInfoParcel.marshall();

    accountInfoParcel.recycle();/*w w  w  .  j  a v  a 2s  .  c o  m*/
    return bytes;
}

From source file:com.android.mail.utils.NotificationActionUtils.java

/**
 * <p>/*from  ww w.  j a  v  a  2  s . c  o  m*/
 * This is a slight hack to avoid an exception in the remote AlarmManagerService process. The
 * AlarmManager adds extra data to this Intent which causes it to inflate. Since the remote
 * process does not know about the NotificationAction class, it throws a ClassNotFoundException.
 * </p>
 * <p>
 * To avoid this, we marshall the data ourselves and then parcel a plain byte[] array. The
 * NotificationActionIntentService class knows to build the NotificationAction object from the
 * byte[] array.
 * </p>
 */
private static void putNotificationActionExtra(final Intent intent,
        final NotificationAction notificationAction) {
    final Parcel out = Parcel.obtain();
    notificationAction.writeToParcel(out, 0);
    out.setDataPosition(0);
    intent.putExtra(NotificationActionIntentService.EXTRA_NOTIFICATION_ACTION, out.marshall());
}

From source file:at.bitfire.davdroid.resource.LocalGroup.java

@Override
protected ContentValues contentValues() {
    ContentValues values = super.contentValues();

    @Cleanup("recycle")
    Parcel members = Parcel.obtain();/*from  w w w.j  a va 2s  .com*/
    members.writeStringList(contact.members);
    values.put(COLUMN_PENDING_MEMBERS, members.marshall());

    return values;
}