List of usage examples for android.text.format Time setToNow
public void setToNow()
From source file:com.example.mihai.inforoute.app.ForecastFragment.java
public static String getDayName(Context context, long dateInMillis) { // If the date is today, return the localized version of "Today" instead of the actual // day name.// ww w.j av a 2 s .co m Time t = new Time(); t.setToNow(); int julianDay = Time.getJulianDay(dateInMillis, t.gmtoff); int currentJulianDay = Time.getJulianDay(System.currentTimeMillis(), t.gmtoff); if (julianDay == currentJulianDay) { return context.getString(R.string.today); } else if (julianDay == currentJulianDay + 1) { return context.getString(R.string.tomorrow); } else { Time time = new Time(); time.setToNow(); // Otherwise, the format is just the day of the week (e.g "Wednesday". SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE"); return dayFormat.format(dateInMillis); } }
From source file:net.networksaremadeofstring.rhybudd.Notifications.java
public static void SendGCMNotification(ZenossEvent Event, Context context) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); Time now = new Time(); now.setToNow(); //We don't need to overwhelm the user with their notification sound / vibrator if ((now.toMillis(true) - PreferenceManager.getDefaultSharedPreferences(context).getLong("lastCheck", now.toMillis(true))) < 3000) { //Log.e("SendGCMNotification", "Not publishing a notification due to stampede control"); return;//from w w w . j av a2s .c om } Intent notificationIntent = new Intent(context, ViewZenossEventsListActivity.class); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); notificationIntent.putExtra("forceRefresh", true); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); Uri soundURI = null; try { if (settings.getBoolean("notificationSound", true)) { if (settings.getString("notificationSoundChoice", "").equals("")) { soundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); } else { try { soundURI = Uri.parse(settings.getString("notificationSoundChoice", "")); } catch (Exception e) { soundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); } } } else { soundURI = null; } } catch (Exception e) { } String notifTitle = "New Events Received"; int notifPriority = Notification.PRIORITY_DEFAULT; //int AlertType = NOTIFICATION_GCM_GENERIC; try { if (Event.getSeverity().equals("5")) { notifTitle = context.getString(R.string.CriticalNotificationTitle); notifPriority = Notification.PRIORITY_MAX; //AlertType = NOTIFICATION_GCM_CRITICAL; } else if (Event.getSeverity().equals("4")) { notifTitle = context.getString(R.string.ErrorNotificationTitle); notifPriority = Notification.PRIORITY_HIGH; //AlertType = NOTIFICATION_GCM_ERROR; } else if (Event.getSeverity().equals("3")) { notifTitle = context.getString(R.string.WarnNotificationTitle); notifPriority = Notification.PRIORITY_DEFAULT; //AlertType = NOTIFICATION_GCM_WARNING; } else if (Event.getSeverity().equals("2")) { notifTitle = context.getString(R.string.InfoNotificationTitle); notifPriority = Notification.PRIORITY_LOW; //AlertType = NOTIFICATION_GCM_INFO; } else if (Event.getSeverity().equals("1")) { notifTitle = context.getString(R.string.DebugNotificationTitle); notifPriority = Notification.PRIORITY_MIN; //AlertType = NOTIFICATION_GCM_DEBUG; } } catch (Exception e) { } long[] vibrate = { 0, 100, 200, 300 }; try { AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); //Log.e("audio.getRingerMode()",Integer.toString(audio.getRingerMode())); switch (audio.getRingerMode()) { case AudioManager.RINGER_MODE_SILENT: //Do nothing to fix GitHub issue #13 //Log.e("AudioManager","Doing nothing because we are silent"); vibrate = new long[] { 0, 0 }; break; } } catch (Exception e) { e.printStackTrace(); } Intent broadcastMassAck = new Intent(); broadcastMassAck.setAction(MassAcknowledgeReceiver.BROADCAST_ACTION); PendingIntent pBroadcastMassAck = PendingIntent.getBroadcast(context, 0, broadcastMassAck, 0); if (Build.VERSION.SDK_INT >= 16) { Notification noti = new Notification.BigTextStyle(new Notification.Builder(context) .setContentTitle(notifTitle).setPriority(notifPriority).setAutoCancel(true).setSound(soundURI) .setVibrate(vibrate).setContentText(Event.getDevice()).setContentIntent(contentIntent) .addAction(R.drawable.ic_action_resolve_all, "Acknowledge all Events", pBroadcastMassAck) .setSmallIcon(R.drawable.ic_stat_alert)).bigText( Event.getSummary() + "\r\n" + Event.getComponentText() + "\r\n" + Event.geteventClass()) .build(); if (settings.getBoolean("notificationSoundInsistent", false)) noti.flags |= Notification.FLAG_INSISTENT; noti.tickerText = notifTitle; NotificationManager mNM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mNM.notify(NOTIFICATION_GCM_GENERIC, noti); } else { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_stat_alert).setContentTitle(notifTitle) .setContentText(Event.getDevice() + ": " + Event.getSummary()).setContentIntent(contentIntent) .setSound(soundURI).setVibrate(vibrate) .addAction(R.drawable.ic_action_resolve_all, "Acknowledge all Events", pBroadcastMassAck) .setAutoCancel(true).setPriority(notifPriority); NotificationManager mNM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mNM.notify(NOTIFICATION_GCM_GENERIC, mBuilder.build()); } }
From source file:me.zhang.bingo.Utility.java
/** * Helper method to convert the database representation of the date into something to display * to users. As classy and polished a user experience as "20170701" is, we can do better. * * @param context Context to use for resource localization * @param rawStartDate The date string: "20170701" * @return a user-friendly representation of the date. *///w ww . j a v a 2s . co m public static String getFriendlyDayString(Context context, String rawStartDate) { // The day string for forecast uses the following logic: // For today: "Today, July 1" // For yesterday: "Yesterday" // For all days before that: "Wed Jun 28" Time time = new Time(); time.setToNow(); long currentTime = System.currentTimeMillis(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, getChoosedLocale(context)); long dateInMillis = 0; try { dateInMillis = sdf.parse(rawStartDate).getTime(); } catch (ParseException e) { e.printStackTrace(); } int julianDay = Time.getJulianDay(dateInMillis, time.gmtoff); int currentJulianDay = Time.getJulianDay(currentTime, time.gmtoff); if (julianDay == currentJulianDay) { // Today String today = context.getString(R.string.today); return context.getString(R.string.format_full_friendly_date, today, getFormattedMonthDay(context, dateInMillis)); } else if (julianDay == currentJulianDay - 1) { // Yesterday return context.getString(R.string.yesterday); } else { // Otherwise, use the form "Jun 28" SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("MMM d", getChoosedLocale(context)); return shortenedDateFormat.format(dateInMillis); } }
From source file:org.dmfs.tasks.notification.NotificationActionUtils.java
/** * Returns a string representation for the time, with a relative date and an absolute time *//*w w w. j a v a 2 s.c om*/ 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:com.granita.tasks.notification.NotificationActionUtils.java
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 {// w w w . java2 s .c o m startString = context.getString(R.string.notification_task_start_date, new DateFormatter(context) .format(makeTime(startDate, startAllDay), DateFormatContext.NOTIFICATION_VIEW)); } 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); // 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); } // 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( NotificationActionIntentService.ACTION_COMPLETE, R.string.notification_action_completed, notificationId, taskUri, startDate); mBuilder.addAction(NotificationActionIntentService.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: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 {/* w w w . j ava 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:be.ac.ucl.lfsab1509.llncampus.UCLouvain.java
/** * Launch the download of the courses list and store them in the database. * // w ww . j av a 2 s . c om * @param context * Application context. * @param username * UCL global user identifier. * @param password * UCL password. * @param end * Runnable to be executed at the end of the download. * @param mHandler * Handler to manage messages and threads. * */ public static void downloadCoursesFromUCLouvain(final LLNCampusActivity context, final String username, final String password, final Runnable end, final Handler mHandler) { Time t = new Time(); t.setToNow(); int year = t.year; // A new academic year begin in September (8th month on 0-based count). if (t.month < 8) { year--; } final int academicYear = year; mHandler.post(new Runnable() { public void run() { final ProgressDialog mProgress = new ProgressDialog(context); mProgress.setCancelable(false); mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgress.setMax(100); mProgress.setMessage(context.getString(R.string.connection)); mProgress.show(); new Thread(new Runnable() { /** * Set the progress to the value n and show the message nextStep * @param n * The progress value. * @param nextStep * The message to show (indicate the next step to be processed). */ public void progress(final int n, final String nextStep) { mHandler.post(new Runnable() { public void run() { mProgress.setProgress(n); mProgress.setMessage(nextStep); } }); Log.d("UCLouvain", nextStep); } /** * Notify to the user an error. * * @param msg * The message to show to the user. */ public void sendError(String msg) { notify(context.getString(R.string.error) + " :" + msg); mProgress.cancel(); } /** * Notify to the user a message. * * @param msg * The message to show to the user. */ public void notify(final String msg) { mHandler.post(new Runnable() { public void run() { context.notify(msg); } }); } public void run() { progress(0, context.getString(R.string.connection_ucl)); UCLouvain uclouvain = new UCLouvain(username, password); progress(20, context.getString(R.string.fetch_info)); ArrayList<Offer> offers = uclouvain.getOffers(academicYear); if (offers == null || offers.isEmpty()) { sendError(context.getString(R.string.error_academic_year) + academicYear); return; } int i = 40; ArrayList<Course> courses = new ArrayList<Course>(); for (Offer o : offers) { progress(i, context.getString(R.string.get_courses) + o.getOfferName()); ArrayList<Course> offerCourses = uclouvain.getCourses(o); if (offerCourses != null && !offerCourses.isEmpty()) { courses.addAll(offerCourses); } else { Log.e("CoursListEditActivity", "Error : No course for offer [" + o.getOfferCode() + "] " + o.getOfferName()); } i += (int) (30. / offers.size()); } if (courses.isEmpty()) { sendError(context.getString(R.string.courses_empty)); return; } // Remove old courses. progress(70, context.getString(R.string.cleaning_db)); LLNCampus.getDatabase().delete("Courses", "", null); LLNCampus.getDatabase().delete("Horaire", "", null); // Add new data. i = 80; for (Course c : courses) { progress(i, context.getString(R.string.add_courses_db)); ContentValues cv = new ContentValues(); cv.put("CODE", c.getCourseCode()); cv.put("NAME", c.getCoursName()); LLNCampus.getDatabase().insert("Courses", cv); i += (int) (20. / courses.size()); } progress(100, context.getString(R.string.end)); mProgress.cancel(); mHandler.post(end); } }).start(); } }); }
From source file:eu.inmite.apps.smsjizdenka.adapter.TicketsAdapter.java
/** * Gets validity of ticket with time {@code t} in minutes. *//*ww w . ja v a2 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
@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 {/*from w ww.j a v a2s.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 v a 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()); }