Example usage for android.text.format Time toMillis

List of usage examples for android.text.format Time toMillis

Introduction

In this page you can find the example usage for android.text.format Time toMillis.

Prototype

public long toMillis(boolean ignoreDst) 

Source Link

Document

Converts this time to milliseconds.

Usage

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

public static void sendDueAlarmNotification(Context context, String title, Uri taskUri, int notificationId,
        long dueDate, boolean dueAllDay, String timezone, boolean silent) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    String dueString = "";
    if (dueAllDay) {
        dueString = context.getString(R.string.notification_task_due_today);
    } else {//from  www  .  j a v  a  2 s.c  om
        dueString = context.getString(R.string.notification_task_due_date, new DateFormatter(context)
                .format(makeTime(dueDate, dueAllDay), DateFormatContext.NOTIFICATION_VIEW));
    }

    // build notification
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(context.getString(R.string.notification_task_due_title, title))
            .setContentText(dueString);

    // dismisses the notification on click
    mBuilder.setAutoCancel(true);

    // set status bar test
    mBuilder.setTicker(title);

    // enable light, sound and vibration
    if (silent) {
        mBuilder.setDefaults(0);
    } else {
        mBuilder.setDefaults(Notification.DEFAULT_ALL);
    }

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(Intent.ACTION_VIEW);
    resultIntent.setData(taskUri);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // add actions
    if (android.os.Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
        // delay notification
        mBuilder.addAction(NotificationActionIntentService.getDelay1dAction(context, notificationId, taskUri,
                dueDate, timezone, dueAllDay));

        // complete action
        NotificationAction completeAction = new NotificationAction(
                NotificationActionIntentService.ACTION_COMPLETE, R.string.notification_action_completed,
                notificationId, taskUri, dueDate);
        mBuilder.addAction(NotificationActionIntentService.getCompleteAction(context,
                NotificationActionUtils.getNotificationActionPendingIntent(context, completeAction)));
    }

    // set displayed time
    if (dueAllDay) {
        Time now = new Time();
        now.setToNow();
        now.set(0, 0, 0, now.monthDay, now.month, now.year);
        mBuilder.setWhen(now.toMillis(true));
    } else {
        mBuilder.setWhen(dueDate);
    }

    mBuilder.setContentIntent(resultPendingIntent);
    notificationManager.notify(notificationId, mBuilder.build());
}

From source file:org.dmfs.tasks.notification.NotificationActionUtils.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void sendStartNotification(Context context, String title, Uri taskUri, int notificationId,
        long startDate, boolean startAllDay, boolean silent) {
    String startString = "";
    if (startAllDay) {
        startString = context.getString(R.string.notification_task_start_today);
    } else {/*ww  w. ja  va 2  s  .c o m*/
        startString = context.getString(R.string.notification_task_start_date,
                formatTime(context, makeTime(startDate, startAllDay)));
    }

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    // build notification
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(context.getString(R.string.notification_task_start_title, title))
            .setContentText(startString);

    // color
    mBuilder.setColor(context.getResources().getColor(R.color.colorPrimary));

    // dismisses the notification on click
    mBuilder.setAutoCancel(true);

    // set high priority to display heads-up notification
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
        mBuilder.setPriority(Notification.PRIORITY_HIGH);
    }

    // set status bar test
    mBuilder.setTicker(title);

    // enable light, sound and vibration
    if (silent) {
        mBuilder.setDefaults(0);
    } else {
        mBuilder.setDefaults(Notification.DEFAULT_ALL);
    }

    // set notification time
    // set displayed time
    if (startAllDay) {
        Time now = new Time();
        now.setToNow();
        now.set(0, 0, 0, now.monthDay, now.month, now.year);
        mBuilder.setWhen(now.toMillis(true));
    } else {
        mBuilder.setWhen(startDate);
    }

    // add actions
    if (android.os.Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
        NotificationAction completeAction = new NotificationAction(NotificationUpdaterService.ACTION_COMPLETE,
                R.string.notification_action_completed, notificationId, taskUri, startDate);
        mBuilder.addAction(NotificationUpdaterService.getCompleteAction(context,
                NotificationActionUtils.getNotificationActionPendingIntent(context, completeAction)));

    }

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(Intent.ACTION_VIEW);
    resultIntent.setData(taskUri);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);
    notificationManager.notify(notificationId, mBuilder.build());
}

From source file:org.dmfs.tasks.notification.NotificationActionUtils.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void sendDueAlarmNotification(Context context, String title, Uri taskUri, int notificationId,
        long dueDate, boolean dueAllDay, String timezone, boolean silent) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    String dueString = "";
    if (dueAllDay) {
        dueString = context.getString(R.string.notification_task_due_today);
    } else {//from  w  w  w. j  a  va 2s  .c  o m
        dueString = context.getString(R.string.notification_task_due_date,
                formatTime(context, makeTime(dueDate, dueAllDay)));
    }

    // build notification
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(context.getString(R.string.notification_task_due_title, title))
            .setContentText(dueString);

    // color
    mBuilder.setColor(context.getResources().getColor(R.color.colorPrimary));

    // dismisses the notification on click
    mBuilder.setAutoCancel(true);

    // set high priority to display heads-up notification
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
        mBuilder.setPriority(Notification.PRIORITY_HIGH);
    }

    // set status bar test
    mBuilder.setTicker(title);

    // enable light, sound and vibration
    if (silent) {
        mBuilder.setDefaults(0);
    } else {
        mBuilder.setDefaults(Notification.DEFAULT_ALL);
    }

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(Intent.ACTION_VIEW);
    resultIntent.setData(taskUri);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // add actions
    if (android.os.Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
        // delay action
        mBuilder.addAction(NotificationUpdaterService.getDelay1dAction(context, notificationId, taskUri,
                dueDate, timezone, dueAllDay));

        // complete action
        NotificationAction completeAction = new NotificationAction(NotificationUpdaterService.ACTION_COMPLETE,
                R.string.notification_action_completed, notificationId, taskUri, dueDate);
        mBuilder.addAction(NotificationUpdaterService.getCompleteAction(context,
                NotificationActionUtils.getNotificationActionPendingIntent(context, completeAction)));
    }

    // set displayed time
    if (dueAllDay) {
        Time now = new Time();
        now.setToNow();
        now.set(0, 0, 0, now.monthDay, now.month, now.year);
        mBuilder.setWhen(now.toMillis(true));
    } else {
        mBuilder.setWhen(dueDate);
    }

    mBuilder.setContentIntent(resultPendingIntent);
    notificationManager.notify(notificationId, mBuilder.build());
}

From source file:nerd.tuxmobil.fahrplan.congress.FahrplanMisc.java

public static long setUpdateAlarm(Context context, boolean initial) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent alarmintent = new Intent(context, AlarmReceiver.class);
    alarmintent.setAction(AlarmReceiver.ALARM_UPDATE);

    PendingIntent pendingintent = PendingIntent.getBroadcast(context, 0, alarmintent, 0);

    MyApp.LogDebug(LOG_TAG, "set update alarm");
    long next_fetch;
    long interval;
    Time t = new Time();
    t.setToNow();/*from  ww w  .j  a  v a2  s .  c om*/
    long now = t.toMillis(true);

    if ((now >= MyApp.first_day_start) && (now < MyApp.last_day_end)) {
        interval = 2 * AlarmManager.INTERVAL_HOUR;
        next_fetch = now + interval;
    } else if (now >= MyApp.last_day_end) {
        MyApp.LogDebug(LOG_TAG, "cancel alarm post congress");
        alarmManager.cancel(pendingintent);
        return 0;
    } else {
        interval = AlarmManager.INTERVAL_DAY;
        next_fetch = now + interval;
    }

    if ((now < MyApp.first_day_start) && ((now + AlarmManager.INTERVAL_DAY) >= MyApp.first_day_start)) {
        next_fetch = MyApp.first_day_start;
        interval = 2 * AlarmManager.INTERVAL_HOUR;
        if (!initial) {
            MyApp.LogDebug(LOG_TAG, "update alarm to interval " + interval + ", next in " + (next_fetch - now));
            alarmManager.cancel(pendingintent);
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, next_fetch, interval, pendingintent);
        }
    }

    if (initial) {
        MyApp.LogDebug(LOG_TAG,
                "set initial alarm to interval " + interval + ", next in " + (next_fetch - now));
        alarmManager.cancel(pendingintent);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, next_fetch, interval, pendingintent);
    }

    return interval;
}

From source file:eu.inmite.apps.smsjizdenka.adapter.TicketsAdapter.java

/**
 * Gets validity of ticket with time {@code t} in minutes.
 *///ww  w .  j  av  a  2  s.  c  o  m
public static int getValidityMinutes(Time t) {
    if (t == null) {
        return -1;
    }

    final Time now = new Time();
    now.setToNow();
    now.switchTimezone(Time.getCurrentTimezone());
    return (int) Math.ceil((t.toMillis(true) - now.toMillis(true)) / 1000d / 60d);
}

From source file:org.dmfs.tasks.notification.NotificationActionUtils.java

/**
 * Returns a string representation for the time, with a relative date and an absolute time
 *///from www  .  j  a va  2s .c  o  m
public static String formatTime(Context context, Time time) {
    Time now = new Time();
    now.setToNow();
    String dateString;
    if (time.allDay) {
        Time allDayNow = new Time("UTC");
        allDayNow.set(now.monthDay, now.month, now.year);
        dateString = DateUtils.getRelativeTimeSpanString(time.toMillis(false), allDayNow.toMillis(false),
                DateUtils.DAY_IN_MILLIS).toString();
    } else {
        dateString = DateUtils
                .getRelativeTimeSpanString(time.toMillis(false), now.toMillis(false), DateUtils.DAY_IN_MILLIS)
                .toString();
    }

    // return combined date and time
    String timeString = new DateFormatter(context).format(time, DateFormatContext.NOTIFICATION_VIEW_TIME);
    return new StringBuilder().append(dateString).append(", ").append(timeString).toString();
}

From source file:org.dmfs.tasks.notification.NotificationUpdaterService.java

private static String makePinNotificationContentText(Context context, ContentSet task) {
    boolean isAllDay = TaskFieldAdapters.ALLDAY.get(task);
    Time now = new Time();
    now.setToNow();//from   w  w  w .  j a  v  a 2  s. c om
    now.minute--;
    Time start = TaskFieldAdapters.DTSTART.get(task);
    Time due = TaskFieldAdapters.DUE.get(task);

    if (start != null && start.toMillis(true) > 0 && (now.before(start) || due == null)) {
        start.allDay = isAllDay;
        String startString = context.getString(R.string.notification_task_start_date,
                NotificationActionUtils.formatTime(context, start));
        return startString;
    }

    if (due != null && due.toMillis(true) > 0) {
        due.allDay = isAllDay;
        String dueString = context.getString(R.string.notification_task_due_date,
                NotificationActionUtils.formatTime(context, due));
        return dueString;
    }

    String description = TaskFieldAdapters.DESCRIPTION.get(task);
    if (description != null) {
        description = description.replaceAll("\\[\\s?\\]", "?").replaceAll("\\[[xX]\\]", "");

    }
    return description;
}

From source file:nerd.tuxmobil.fahrplan.congress.FahrplanMisc.java

public static void share(Context context, Lecture l) {
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    StringBuilder sb = new StringBuilder();
    Time time = l.getTime();
    sb.append(l.title).append("\n")
            .append(SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.SHORT)
                    .format(new Date(time.toMillis(true))));
    sb.append(", ").append(l.room).append("\n\n");
    final String eventUrl = getEventUrl(context, l.lecture_id);
    sb.append(eventUrl);//from   ww  w  .  jav  a 2 s .co  m
    sendIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());
    sendIntent.setType("text/plain");
    context.startActivity(sendIntent);
}

From source file:org.dmfs.tasks.notification.NotificationUpdaterService.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private static Notification makePinNotification(Context context, Builder builder, ContentSet task,
        boolean withSound, boolean withTickerText, boolean withHeadsUpNotification) {
    Resources resources = context.getResources();

    // reset actions
    builder.mActions = new ArrayList<Action>(2);

    // content/*from  ww w . ja va2 s  . co m*/
    builder.setSmallIcon(R.drawable.ic_pin_white_24dp).setContentTitle(TaskFieldAdapters.TITLE.get(task))
            .setOngoing(true).setShowWhen(false);

    // set priority for HeadsUpNotification
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
        if (withHeadsUpNotification) {
            builder.setPriority(Notification.PRIORITY_HIGH);
        } else {
            builder.setPriority(Notification.PRIORITY_DEFAULT);
        }
    }

    // color is based on the priority of the task. If the task has no priority we use the primary color.
    Integer priority = TaskFieldAdapters.PRIORITY.get(task);
    if (priority != null && priority > 0) {
        if (priority < 5) {
            builder.setColor(resources.getColor(R.color.priority_red));
        }
        if (priority == 5) {
            builder.setColor(resources.getColor(R.color.priority_yellow));
        }
        if (priority > 5 && priority <= 9) {
            builder.setColor(resources.getColor(R.color.priority_green));
        }
    } else {
        builder.setColor(resources.getColor(R.color.colorPrimary));
    }

    // description
    String contentText = makePinNotificationContentText(context, task);
    if (contentText != null) {
        builder.setContentText(contentText);
    }

    // ticker text
    if (withTickerText) {
        builder.setTicker(
                context.getString(R.string.notification_task_pin_ticker, (TaskFieldAdapters.TITLE.get(task))));
    }

    // click action
    Intent resultIntent = new Intent(Intent.ACTION_VIEW);
    resultIntent.setData(task.getUri());
    resultIntent.setPackage(context.getPackageName());

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);

    // complete action
    Boolean closed = TaskFieldAdapters.IS_CLOSED.get(task);
    if (closed == null || !closed) {
        Time dueTime = TaskFieldAdapters.DUE.get(task);
        long dueTimestamp = dueTime == null ? 0 : dueTime.toMillis(true);

        NotificationAction completeAction = new NotificationAction(NotificationUpdaterService.ACTION_COMPLETE,
                R.string.notification_action_completed, TaskFieldAdapters.TASK_ID.get(task), task.getUri(),
                dueTimestamp);
        builder.addAction(NotificationUpdaterService.getCompleteAction(context,
                NotificationActionUtils.getNotificationActionPendingIntent(context, completeAction)));
    }

    // unpin action
    builder.addAction(NotificationUpdaterService.getUnpinAction(context, TaskFieldAdapters.TASK_ID.get(task),
            task.getUri()));

    // sound
    if (withSound) {
        builder.setDefaults(Notification.DEFAULT_ALL);
    } else {
        builder.setDefaults(Notification.DEFAULT_LIGHTS);
    }

    return builder.build();
}

From source file:org.gnucash.android.ui.util.RecurrenceViewClickListener.java

@Override
public void onClick(View v) {
    FragmentManager fm = mActivity.getSupportFragmentManager();
    Bundle b = new Bundle();
    Time t = new Time();
    t.setToNow();// w  ww.  j a  v  a  2  s  .c  o m
    b.putLong(RecurrencePickerDialogFragment.BUNDLE_START_TIME_MILLIS, t.toMillis(false));
    b.putString(RecurrencePickerDialogFragment.BUNDLE_TIME_ZONE, t.timezone);

    // may be more efficient to serialize and pass in EventRecurrence
    b.putString(RecurrencePickerDialogFragment.BUNDLE_RRULE, mRecurrenceRule);

    RecurrencePickerDialogFragment rpd = (RecurrencePickerDialogFragment) fm
            .findFragmentByTag(FRAGMENT_TAG_RECURRENCE_PICKER);
    if (rpd != null) {
        rpd.dismiss();
    }
    rpd = new RecurrencePickerDialogFragment();
    rpd.setArguments(b);
    rpd.setOnRecurrenceSetListener(mRecurrenceSetListener);
    rpd.show(fm, FRAGMENT_TAG_RECURRENCE_PICKER);
}