private void setWhenDefaults() {
    // it's possible to have:
    // 1) no times set
    // 2) due time set, but not start time
    // 3) start and due time set

    mShowStart = !Time.isEpoch(mStartTime);
    mShowDue = !Time.isEpoch(mDueTime);

    if (!mShowStart && !mShowDue) {
      mStartTime.setToNow();

      // Round the time to the nearest half hour.
      mStartTime.second = 0;
      int minute = mStartTime.minute;
      if (minute > 0 && minute <= 30) {
        mStartTime.minute = 30;
      } else {
        mStartTime.minute = 0;
        mStartTime.hour += 1;
      }

      long startMillis = mStartTime.normalize(true /* ignore isDst */);
      mDueTime.set(startMillis + DateUtils.HOUR_IN_MILLIS);
    } else if (!mShowStart) {
      // default start to same as due
      mStartTime.set(mDueTime);
    }
  }
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
      // Cache the member variables locally to avoid inner class overhead.
      Time startTime = mStartTime;
      Time dueTime = mDueTime;

      // Cache the start and due millis so that we limit the number
      // of calls to normalize() and toMillis(), which are fairly
      // expensive.
      long startMillis;
      long dueMillis;
      if (mView == mStartTimeButton) {
        // The start time was changed.
        int hourDuration = dueTime.hour - startTime.hour;
        int minuteDuration = dueTime.minute - startTime.minute;

        startTime.hour = hourOfDay;
        startTime.minute = minute;
        startMillis = startTime.normalize(true);
        mShowStart = true;

        // Also update the due time to keep the duration constant.
        dueTime.hour = hourOfDay + hourDuration;
        dueTime.minute = minute + minuteDuration;
        dueMillis = dueTime.normalize(true);
        mShowDue = true;
      } else {
        // The due time was changed.
        startMillis = startTime.toMillis(true);
        dueTime.hour = hourOfDay;
        dueTime.minute = minute;
        dueMillis = dueTime.normalize(true);
        mShowDue = true;

        if (mShowStart) {
          // Do not allow an event to have a due time before the start time.
          if (dueTime.before(startTime)) {
            dueTime.set(startTime);
            dueMillis = startMillis;
          }
        } else {
          // if start time is not shown, default it to be the same as due time
          startTime.set(dueTime);
          mShowStart = true;
        }
      }

      // update all 4 buttons in case visibility has changed
      setDate(mStartDateButton, startMillis, mShowStart);
      setTime(mStartTimeButton, startMillis, mShowStart);
      setDate(mDueDateButton, dueMillis, mShowDue);
      setTime(mDueTimeButton, dueMillis, mShowDue);
      updateCalendarPanel();
    }
 public static String prettyTimestamp(Timestamp t, boolean isChat) {
   // Set time in UTC time
   Time time = new Time("UTC");
   Calendar cal = Calendar.getInstance();
   cal.setTime(t);
   time.set(
       cal.get(Calendar.SECOND),
       cal.get(Calendar.MINUTE),
       cal.get(Calendar.HOUR_OF_DAY),
       cal.get(Calendar.DAY_OF_MONTH),
       cal.get(Calendar.MONTH),
       cal.get(Calendar.YEAR));
   // Switch to device timezone
   time.switchTimezone(Time.getCurrentTimezone());
   Time startOfDay = new Time(Time.getCurrentTimezone());
   startOfDay.setToNow();
   startOfDay.hour = 0;
   startOfDay.minute = 0;
   startOfDay.second = 0;
   Time startOfYear = new Time(Time.getCurrentTimezone());
   startOfYear.setToNow();
   startOfYear.hour = 0;
   startOfYear.minute = 0;
   startOfYear.second = 0;
   startOfYear.monthDay = 0;
   startOfYear.month = 0;
   Time startOfTime = new Time(Time.getCurrentTimezone());
   startOfTime.set(0);
   if (t.equals(new Timestamp(0, 0, 0, 0, 0, 0, 0))) {
     return "";
   } else if (isChat) {
     if (time.after(startOfDay)) {
       return time.format("%H:%M");
     } else if (time.after(startOfYear)) {
       return time.format("%b %d, %H:%M");
     } else {
       return time.format("%b %d %Y, %H:%M");
     }
   } else {
     if (time.after(startOfDay)) {
       return time.format("%H:%M");
     } else if (time.after(startOfYear)) {
       return time.format("%b %d");
     } else {
       return time.format("%b %d %Y");
     }
   }
 }
    /** Parses an SC timestamp and returns a currentTimeMillis()-style timestamp */
    long getSCTimestampMillis() {
      // TP-Service-Centre-Time-Stamp
      int year = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
      int month = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
      int day = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
      int hour = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
      int minute = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
      int second = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);

      // For the timezone, the most significant bit of the
      // least significant nibble is the sign byte
      // (meaning the max range of this field is 79 quarter-hours,
      // which is more than enough)

      byte tzByte = mPdu[mCur++];

      // Mask out sign bit.
      int timezoneOffset = IccUtils.gsmBcdByteToInt((byte) (tzByte & (~0x08)));

      timezoneOffset = ((tzByte & 0x08) == 0) ? timezoneOffset : -timezoneOffset;

      Time time = new Time(Time.TIMEZONE_UTC);

      // It's 2006.  Should I really support years < 2000?
      time.year = year >= 90 ? year + 1900 : year + 2000;
      time.month = month - 1;
      time.monthDay = day;
      time.hour = hour;
      time.minute = minute;
      time.second = second;

      // Timezone offset is in quarter hours.
      return time.toMillis(true) - (timezoneOffset * 15 * 60 * 1000);
    }
Example #5
0
 /*
  * Chargement des informations du reveil.
  * Ici pour la sauvegarde on a simplement déserialiser l'objet Alarm.
  */
 public void charger() {
   alarm = null;
   try {
     ObjectInputStream alarmOIS = new ObjectInputStream(context.openFileInput("alarm.serial"));
     try {
       alarm = (Alarm) alarmOIS.readObject();
     } finally {
       try {
         alarmOIS.close();
       } finally {;
       }
     }
   } catch (FileNotFoundException fnfe) {
     alarm = new Alarm();
     alarm.setActive(true);
     Time t = new Time();
     t.hour = 7;
     t.minute = 30;
     alarm.setHeure(t);
   } catch (IOException ioe) {
     ioe.printStackTrace();
   } catch (ClassNotFoundException cnfe) {
     cnfe.printStackTrace();
   }
 }
  private long getEndTime() {
    final Time time = new Time();

    time.hour = 23;
    time.minute = 59;
    time.second = 59;
    time.normalize(false);

    return TimeUtils.toDateTime(getView().getEndDate(), time);
  }
  private long getStartTime() {
    final Time time = new Time();

    time.hour = 0;
    time.minute = 0;
    time.second = 0;
    time.normalize(false);

    return TimeUtils.toDateTime(getView().getStartDate(), time);
  }
Example #8
0
  public static void registerAlarm(Context context) {
    Time fire = new Time();
    fire.setToNow();
    fire.hour = HOUR;
    fire.minute = MINUTE;
    fire.second = SECOND;

    if (fire.toMillis(true) <= System.currentTimeMillis()) fire.monthDay++;

    long time = fire.toMillis(true);

    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    // One day is not always 24 hours (consider DST), so we use set instead of setRepeating,
    // and set the next alarm in onReceive.
    alarm.set(AlarmManager.RTC_WAKEUP, time, getSender(context));

    Log.d(PurgeActivity.TAG, String.format("registered alarm with time %d", time));
  }
  private void scheduleNextUpdate() {
    Intent intent = new Intent(this, this.getClass());
    PendingIntent pendingIntent =
        PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    long currentTimeMillis = System.currentTimeMillis();
    long nextUpdateTimeMillis = currentTimeMillis + 3 * DateUtils.MINUTE_IN_MILLIS;
    Time nextUpdateTime = new Time();
    nextUpdateTime.set(nextUpdateTimeMillis);

    if (nextUpdateTime.hour < 8 || nextUpdateTime.hour >= 18) {
      nextUpdateTime.hour = 8;
      nextUpdateTime.minute = 0;
      nextUpdateTime.second = 0;
      nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
    }
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
  }
 /** Announce the currently-selected time when launched. */
 @Override
 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
   if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
     // Clear the event's current text so that only the current time will be spoken.
     event.getText().clear();
     Time time = new Time();
     time.hour = getHours();
     time.minute = getMinutes();
     long millis = time.normalize(true);
     int flags = DateUtils.FORMAT_SHOW_TIME;
     if (mIs24HourMode) {
       flags |= DateUtils.FORMAT_24HOUR;
     }
     String timeString = DateUtils.formatDateTime(getContext(), millis, flags);
     event.getText().add(timeString);
     return true;
   }
   return super.dispatchPopulateAccessibilityEvent(event);
 }
Example #11
0
  @Override
  public Time getDefault(ContentSet values) {
    // create a new Time for the given time zone, falling back to the default time zone if none is
    // given
    String timezone = mTzField == null ? Time.TIMEZONE_UTC : values.getAsString(mTzField);
    Time value = new Time(timezone == null ? TimeZone.getDefault().getID() : timezone);

    value.setToNow();

    Integer allDayInt = mAllDayField == null ? null : values.getAsInteger(mAllDayField);
    if ((allDayInt != null && allDayInt != 0) || (mAllDayField == null && mAllDayDefault)) {
      // make it an allday value
      value.set(value.monthDay, value.month, value.year);
    } else {
      value.second = 0;
      // round up to next quarter-hour
      value.minute = ((value.minute + 14) / 15) * 15;
      value.normalize(false);
    }

    return value;
  }
  private AgendaItem buildAgendaItemFromCursor(
      final Cursor cursor, int cursorPosition, boolean isDayHeader) {
    if (cursorPosition == -1) {
      cursor.moveToFirst();
    } else {
      cursor.moveToPosition(cursorPosition);
    }
    AgendaItem agendaItem = new AgendaItem();
    agendaItem.begin = cursor.getLong(AgendaWindowAdapter.INDEX_BEGIN);
    agendaItem.end = cursor.getLong(AgendaWindowAdapter.INDEX_END);
    agendaItem.startDay = cursor.getInt(AgendaWindowAdapter.INDEX_START_DAY);
    agendaItem.allDay = cursor.getInt(AgendaWindowAdapter.INDEX_ALL_DAY) != 0;
    if (agendaItem.allDay) { // UTC to Local time conversion
      Time time = new Time(mTimeZone);
      time.setJulianDay(Time.getJulianDay(agendaItem.begin, 0));
      agendaItem.begin = time.toMillis(false /* use isDst */);
    } else if (isDayHeader) { // Trim to midnight.
      Time time = new Time(mTimeZone);
      time.set(agendaItem.begin);
      time.hour = 0;
      time.minute = 0;
      time.second = 0;
      agendaItem.begin = time.toMillis(false /* use isDst */);
    }

    // If this is not a day header, then it's an event.
    if (!isDayHeader) {
      agendaItem.id = cursor.getLong(AgendaWindowAdapter.INDEX_EVENT_ID);
      if (agendaItem.allDay) {
        Time time = new Time(mTimeZone);
        time.setJulianDay(Time.getJulianDay(agendaItem.end, 0));
        agendaItem.end = time.toMillis(false /* use isDst */);
      }
    }
    return agendaItem;
  }
  @Override
  protected Task createItemFromUI() {
    Builder builder = Task.newBuilder();
    if (mOriginalItem != null) {
      builder.mergeFrom(mOriginalItem);
    }

    final String description = mDescriptionWidget.getText().toString();
    final long modified = System.currentTimeMillis();
    final String details = mDetailsWidget.getText().toString();
    final Id contextId = getSpinnerSelectedId(mContextSpinner, mContextIds);
    final Id projectId = getSpinnerSelectedId(mProjectSpinner, mProjectIds);
    final boolean allDay = mAllDayCheckBox.isChecked();
    final boolean complete = mCompletedCheckBox.isChecked();
    final boolean hasAlarms = !mReminderItems.isEmpty();
    final int order = calculateTaskOrder(projectId);
    final boolean deleted = mDeletedCheckBox.isChecked();
    final boolean active = true;

    builder
        .setDescription(description)
        .setModifiedDate(modified)
        .setDetails(details)
        .setContextId(contextId)
        .setProjectId(projectId)
        .setAllDay(allDay)
        .setComplete(complete)
        .setDeleted(deleted)
        .setActive(active)
        .setHasAlarm(hasAlarms)
        .setOrder(order);

    // If we are creating a new task, set the creation date
    if (mState == State.STATE_INSERT) {
      builder.setCreatedDate(modified);
    }

    String timezone;
    long startMillis = 0L;
    long dueMillis = 0L;

    if (allDay) {
      // Reset start and end time, increment the monthDay by 1, and set
      // the timezone to UTC, as required for all-day events.
      timezone = Time.TIMEZONE_UTC;
      mStartTime.hour = 0;
      mStartTime.minute = 0;
      mStartTime.second = 0;
      mStartTime.timezone = timezone;
      startMillis = mStartTime.normalize(true);

      mDueTime.hour = 0;
      mDueTime.minute = 0;
      mDueTime.second = 0;
      mDueTime.monthDay++;
      mDueTime.timezone = timezone;
      dueMillis = mDueTime.normalize(true);
    } else {
      if (mShowStart && !Time.isEpoch(mStartTime)) {
        startMillis = mStartTime.toMillis(true);
      }

      if (mShowDue && !Time.isEpoch(mDueTime)) {
        dueMillis = mDueTime.toMillis(true);
      }

      if (mState == State.STATE_INSERT) {
        // The timezone for a new task is the currently displayed timezone
        timezone = TimeZone.getDefault().getID();
      } else {
        timezone = mOriginalItem.getTimezone();

        // The timezone might be null if we are changing an existing
        // all-day task to a non-all-day event.  We need to assign
        // a timezone to the non-all-day task.
        if (TextUtils.isEmpty(timezone)) {
          timezone = TimeZone.getDefault().getID();
        }
      }
    }

    builder.setTimezone(timezone).setStartDate(startMillis).setDueDate(dueMillis);

    Id eventId = mOriginalItem == null ? Id.NONE : mOriginalItem.getCalendarEventId();
    final boolean updateCalendar = mUpdateCalendarCheckBox.isChecked();

    if (updateCalendar) {
      Uri calEntryUri =
          addOrUpdateCalendarEvent(
              eventId,
              description,
              details,
              projectId,
              contextId,
              timezone,
              startMillis,
              dueMillis,
              allDay);
      if (calEntryUri != null) {
        eventId = Id.create(ContentUris.parseId(calEntryUri));
        mNextIntent = new Intent(Intent.ACTION_EDIT, calEntryUri);
        mNextIntent.putExtra("beginTime", startMillis);
        mNextIntent.putExtra("endTime", dueMillis);
      }
      Log.i(cTag, "Updated calendar event " + eventId);
    }
    builder.setCalendarEventId(eventId);

    return builder.build();
  }
Example #14
0
 /**
  * Returns the current reminder time.
  *
  * @return the current reminder time.
  */
 public Time getReminderTime() {
   Time result = new Time();
   result.hour = getPreferences().getInt(HOURS, 6);
   result.minute = getPreferences().getInt(MINUTES, 0);
   return result;
 }