Example usage for android.os Parcel obtain

List of usage examples for android.os Parcel obtain

Introduction

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

Prototype

public static Parcel obtain() 

Source Link

Document

Retrieve a new Parcel object from the pool.

Usage

From source file:org.droid2droid.internal.AbstractRemoteAndroidImpl.java

@Override
public void finalizeOID(int connid, int oid, long timeout) {
    if (oid == 0) {
        if (E)/*from   ww w .ja v a 2s. c om*/
            Log.e(TAG_CLIENT_BIND, PREFIX_LOG + "FinalizeOID with oid=0!");
        return;
    }
    if (!isBinderAlive())
        return;
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    try {
        data.writeInt(oid);
        transactRemoteAndroid(getConnectionId(), FINALIZE_OID, data, reply, 0, timeout);
        return;
    } catch (RemoteException e) {
        if (V)
            Log.v(TAG_CLIENT_BIND, PREFIX_LOG + "FinalizeOID", e);
    } finally {
        if (data != null)
            data.recycle();
        if (reply != null)
            reply.recycle();
    }

}

From source file:org.droid2droid.internal.AbstractRemoteAndroidImpl.java

@Override
public boolean isBinderAlive(int connid, int oid, long timeout) throws RemoteException {
    if (D)/*from   w  w  w  .  j a  v a 2 s . com*/
        Log.d(TAG_CLIENT_BIND, PREFIX_LOG + "isBinderAlive...");
    if (!isBinderAlive()) {
        if (E)
            Log.e(TAG_CLIENT_BIND, PREFIX_LOG + "Binder is not alive");
        throw new RemoteException();
    }
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    try {
        data.writeInt(oid);
        transactRemoteAndroid(getConnectionId(), IS_BINDER_ALIVE, data, reply, 0, timeout);
        return reply.readByte() == 1;
    } finally {
        if (data != null)
            data.recycle();
        if (reply != null)
            reply.recycle();
    }
}

From source file:org.chromium.chrome.browser.util.IntentUtils.java

/**
 * Returns how large the Intent will be in Parcel form, which is helpful for gauging whether
 * Android will deliver the Intent instead of throwing a TransactionTooLargeException.
 *
 * @param intent Intent to get the size of.
 * @return Number of bytes required to parcel the Intent.
 *///from   w w w  .j a  va 2s.  c o m
public static int getParceledIntentSize(Intent intent) {
    Parcel parcel = Parcel.obtain();
    intent.writeToParcel(parcel, 0);
    return parcel.dataSize();
}

From source file:com.marianhello.bgloc.Config.java

public static Config fromByteArray(byte[] byteArray) {
    Parcel parcel = Parcel.obtain();
    parcel.unmarshall(byteArray, 0, byteArray.length);
    parcel.setDataPosition(0);/*from   w w  w . ja va 2  s.  c om*/
    return Config.CREATOR.createFromParcel(parcel);
}

From source file:org.droid2droid.internal.AbstractRemoteAndroidImpl.java

@Override
public boolean pingBinder(int connid, int oid, long timeout) throws RemoteException {
    if (D)/*ww  w  . j  av  a2s  . c  om*/
        Log.d(TAG_CLIENT_BIND, PREFIX_LOG + "pingBinder...");
    if (!isBinderAlive())
        return false;
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    try {
        data.writeInt(oid);
        transactRemoteAndroid(getConnectionId(), PING_BINDER, data, reply, 0, timeout);
        return reply.readByte() == 1;
    } finally {
        if (data != null)
            data.recycle();
        if (reply != null)
            reply.recycle();
    }
}

From source file:com.marianhello.bgloc.Config.java

public Parcel toParcel() {
    Parcel parcel = Parcel.obtain();
    this.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    return parcel;
}

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.//www .j a  v  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.tct.mail.NotificationActionIntentService.java

@Override
protected void onHandleIntent(final Intent intent) {
    final Context context = this;
    final String action = intent.getAction();
    // TS: chao.zhang 2015-09-21 EMAIL FEATURE-585337 ADD_S
    //NOTE: handle the refresh intent.
    if (ACTION_REFRESH.equals(action)) {
        boolean cleanStatus = intent.getBooleanExtra(NotificationUtils.EXTRA_NEED_CLEAN_STATUS, false);
        long boxId = intent.getLongExtra(NotificationUtils.EXTRA_OUTBOX_ID, -1);
        //after click the action,cancel the notification.
        int notificationId = intent.getIntExtra(NotificationUtils.EXTRA_FAIL_NOTIFICATION_ID, 0);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(notificationId);
        if (boxId == -1) {
            LogUtils.e(LOG_TAG,//from   ww  w. ja  v  a2s .co m
                    "can't find the oubox during handle Intent ACTION_REFRESH in NotificationActionIntentService#onHandleIntent");
            return;
        }
        Uri refreshUri = intent.getData();
        if (cleanStatus && isOutboxNotEmpty(context, boxId)) {
            // 1.clean failed status
            cleanFaildMailStatus(context, boxId);
            // 2.start refresh(sync) the outbox
            context.getContentResolver().query(refreshUri, null, null, null, null);
            // 3. show the sending toast
            // Why add toast to Handler? cause the notificationActionIntentService is
            // asynchronous,so want show toast,
            // only must add toast to Main thread.
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), R.string.sending, Toast.LENGTH_SHORT).show();
                }
            });
        }
        return;
    }
    // TS: chao.zhang 2015-09-21 EMAIL FEATURE-585337 ADD_E
    else if (ACTION_CALENDAR_NEVER_ASK_AGAIN.equals(action)) {
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (mNotificationManager != null) {
            mNotificationManager.cancel(NotificationController.EXCHANGE_NEWCALENDAR_NOTIFICATION_ID);
        }
        MailPrefs.get(context).setIgnoreExchangeCalendarPermission(true); //TS: zheng.zou 2016-1-22 EMAIL BUGFIX-1431088 ADD
        return;
    } else if (ACTION_CONTACTS_NEVER_ASK_AGAIN.equals(action)) {
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (mNotificationManager != null) {
            mNotificationManager.cancel(NotificationController.EXCHANGE_NEWCONTACTS_NOTIFICATION_ID);
        }
        MailPrefs.get(context).setIgnoreExchangeContactPermission(true); //TS: zheng.zou 2016-1-22 EMAIL BUGFIX-1431088 ADD
        return;
    } else if (ACTION_STORAGE_NEVER_ASK_AGAIN.equals(action)) { //TS: zheng.zou 2016-1-22 EMAIL BUGFIX-1431088 ADD_S
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (mNotificationManager != null) {
            mNotificationManager.cancel(NotificationController.EXCHANGE_NEWSTORAGE_NOTIFICATION_ID);
        }
        MailPrefs.get(context).setIgnoreExchangeStoragePermission(true);
        //TS: zheng.zou 2016-1-22 EMAIL BUGFIX-1431088 ADD_E
    }
    /*
     * Grab the alarm from the intent. Since the remote AlarmManagerService fills in the Intent
     * to add some extra data, it must unparcel the NotificationAction object. It throws a
     * ClassNotFoundException when unparcelling.
     * To avoid this, do the marshalling ourselves.
     */
    final NotificationAction notificationAction;
    final byte[] data = intent.getByteArrayExtra(EXTRA_NOTIFICATION_ACTION);
    if (data != null) {
        final Parcel in = Parcel.obtain();
        in.unmarshall(data, 0, data.length);
        in.setDataPosition(0);
        notificationAction = NotificationAction.CREATOR.createFromParcel(in,
                NotificationAction.class.getClassLoader());
    } else {
        LogUtils.wtf(LOG_TAG, "data was null trying to unparcel the NotificationAction");
        return;
    }

    final Message message = notificationAction.getMessage();

    final ContentResolver contentResolver = getContentResolver();

    LogUtils.i(LOG_TAG, "Handling %s", action);

    logNotificationAction(action, notificationAction);

    if (notificationAction.getSource() == NotificationAction.SOURCE_REMOTE) {
        // Skip undo if the action is bridged from remote node.  This should be similar to the
        // logic after the Undo notification expires in a regular flow.
        LogUtils.d(LOG_TAG, "Canceling %s", notificationAction.getNotificationId());
        NotificationManagerCompat.from(context).cancel(notificationAction.getNotificationId());
        NotificationActionUtils.processDestructiveAction(this, notificationAction);
        NotificationActionUtils.resendNotifications(context, notificationAction.getAccount(),
                notificationAction.getFolder());
        return;
    }

    if (ACTION_UNDO.equals(action)) {
        NotificationActionUtils.cancelUndoTimeout(context, notificationAction);
        NotificationActionUtils.cancelUndoNotification(context, notificationAction);
    } else if (ACTION_ARCHIVE_REMOVE_LABEL.equals(action) || ACTION_DELETE.equals(action)) {
        // All we need to do is switch to an Undo notification
        NotificationActionUtils.createUndoNotification(context, notificationAction);

        NotificationActionUtils.registerUndoTimeout(context, notificationAction);
    } else {
        if (ACTION_UNDO_TIMEOUT.equals(action) || ACTION_DESTRUCT.equals(action)) {
            // Process the action
            NotificationActionUtils.cancelUndoTimeout(this, notificationAction);
            NotificationActionUtils.processUndoNotification(this, notificationAction);
        } else if (ACTION_MARK_READ.equals(action)) {
            final Uri uri = message.uri;

            final ContentValues values = new ContentValues(1);
            values.put(UIProvider.MessageColumns.READ, 1);

            contentResolver.update(uri, values, null, null);
        }

        NotificationActionUtils.resendNotifications(context, notificationAction.getAccount(),
                notificationAction.getFolder());
    }
}

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./*from  w w w .  j av a 2s  . c  o  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.granita.tasks.notification.NotificationActionUtils.java

/**
 * <p>/* www.  ja v  a 2s. 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(EXTRA_NOTIFICATION_ACTION, out.marshall());
}