List of usage examples for org.joda.time MonthDay toDateTime
public DateTime toDateTime(ReadableInstant baseInstant)
From source file:fr.nicopico.dashclock.birthday.BirthdayService.java
License:Apache License
@Override protected void onUpdateData(int reason) { if (reason == UPDATE_REASON_SETTINGS_CHANGED) { updatePreferences();/*from ww w.j a v a 2 s . c o m*/ } final Resources res = getResources(); final List<Birthday> birthdays = birthdayRetriever.getContactWithBirthdays(getApplicationContext(), contactGroupId); Configuration config = new Configuration(); config.setToDefaults(); // Disable/enable Android localization if (needToRefreshLocalization || (disableLocalization && !DEFAULT_LANG.equals(Locale.getDefault().getLanguage()))) { if (disableLocalization) { config.locale = new Locale(DEFAULT_LANG); } else { // Restore Android localization //noinspection ConstantConditions config.locale = Resources.getSystem().getConfiguration().locale; } Locale.setDefault(config.locale); getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); } DateTime today = new DateTime(); int upcomingBirthdays = 0; String collapsedTitle = null; String expandedTitle = null; StringBuilder body = new StringBuilder(); for (Birthday birthday : birthdays) { DateTime birthdayEvent; MonthDay birthdayDate = birthday.birthdayDate; try { birthdayEvent = birthdayDate.toDateTime(today); } catch (IllegalFieldValueException e) { if (birthdayDate.getDayOfMonth() == 29 && birthdayDate.getMonthOfYear() == 2) { // Birthday on February 29th (leap year) -> March 1st birthdayEvent = birthdayDate.dayOfMonth().addToCopy(1).toDateTime(today); } else { Log.e(TAG, "Invalid date", e); continue; } } // How many days before the birthday ? int days; if (birthdayEvent.isAfter(today) || birthdayEvent.isEqual(today)) { days = Days.daysBetween(today, birthdayEvent).getDays(); } else { // Next birthday event is next year days = Days.daysBetween(today, birthdayEvent.plusYears(1)).getDays(); } // Should the birthday be displayed ? if (days <= daysLimit) { upcomingBirthdays++; if (upcomingBirthdays == 1) { // A single birthday will be displayed collapsedTitle = birthday.displayName; expandedTitle = res.getString(R.string.single_birthday_title_format, birthday.displayName); } // More than 1 upcoming birthday: display contact name if (upcomingBirthdays > 1) { body.append("\n").append(birthday.displayName).append(", "); } // Age if (!birthday.unknownYear) { int age = today.get(DateTimeFieldType.year()) - birthday.year; body.append(res.getQuantityString(R.plurals.age_format, age, age)); body.append(' '); } // When int daysFormatResId; switch (days) { case 0: daysFormatResId = R.string.when_today_format; break; case 1: daysFormatResId = R.string.when_tomorrow_format; break; default: daysFormatResId = R.string.when_days_format; } body.append(res.getString(daysFormatResId, days)); } else { // All visible birthdays have been processed break; } } if (upcomingBirthdays > 0) { Intent clickIntent = buildClickIntent(birthdays.subList(0, upcomingBirthdays)); if (upcomingBirthdays > 1) { collapsedTitle += " + " + (upcomingBirthdays - 1); } // Display message publishUpdate( new ExtensionData().visible(true).icon(R.drawable.ic_extension_white).status(collapsedTitle) .expandedTitle(expandedTitle).expandedBody(body.toString()).clickIntent(clickIntent)); } else { // Nothing to show publishUpdate(new ExtensionData().visible(false)); } }