- Fix some date issues that crept in.

This commit is contained in:
Timothy Allen 2014-04-14 16:47:07 +02:00
parent b98934c15d
commit 74994b99ee
3 changed files with 19 additions and 20 deletions

View File

@ -96,8 +96,10 @@ public class AlarmService extends Service {
// Reset for tomorrow; as of API 19, setRepeating() is inexact, so we use setExact()
Calendar cal = MainActivity.TimeStringToCalendar(alarmTimeStr);
// Advance the calendar to tomorrow
// Advance the calendar to tomorrow if it's in the past
if (cal.before(Calendar.getInstance())) {
cal.add(Calendar.DAY_OF_MONTH, 1);
}
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, MainActivity.ALARM_REQUEST, intent, 0);
alarmManager.cancel(alarmPendingIntent);
if (Build.VERSION.SDK_INT >= 19) {

View File

@ -149,6 +149,7 @@ public class MainActivity extends ActionBarActivity {
String alarmTimeStr = verifyTimeString(sharedPref.getString(getString(R.string.AlarmTimePref), defaultTimeStr));
alarmTimeButton = (Button) getActivity().findViewById(R.id.alarm_time);
// When debugging, set the alarm for one minute's time
if (HYPOALARM_DEBUG) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 1);
@ -164,6 +165,7 @@ public class MainActivity extends ActionBarActivity {
}
});
// When debugging, activate the time when starting the app.
if (HYPOALARM_DEBUG) {
DialogFragment alarmFragment = new TimePickerFragment();
alarmFragment.show(getActivity().getSupportFragmentManager(), "alarmTimePicker");
@ -184,10 +186,6 @@ public class MainActivity extends ActionBarActivity {
int minutes = GracePeriodToMinutes(value.toString());
editor.putInt(getString(R.string.GracePeriodPref), minutes);
editor.commit();
/*
CharSequence text = "Grace period is " + GracePeriodToMinutes(value.toString()) + " or " + MinutesToGracePeriodStr(GracePeriodToMinutes(value.toString()))+ ".";
Toast.makeText(getActivity().getApplicationContext(), text, Toast.LENGTH_SHORT).show();
*/
}
@Override
@ -317,6 +315,7 @@ public class MainActivity extends ActionBarActivity {
sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
// Use the current set time as the default value for the picker
String alarmTimeStr = alarmTimeButton.getText().toString();
// For selecting a time, the date doesn't matter, just the hour and minute
Calendar cal = TimeStringToCalendar(alarmTimeStr);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
@ -327,10 +326,19 @@ public class MainActivity extends ActionBarActivity {
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String alarmStr = HourMinuteToTimeString(hourOfDay, minute);
Boolean alarmActive = sharedPref.getBoolean(getString(R.string.AlarmActivePref), true);
// Set time preference
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
// Advance to tomorrow if setting a time earlier than now
if (cal.before(Calendar.getInstance())) {
cal.add(Calendar.DAY_OF_MONTH, 1);
}
String alarmStr = CalendarToTimeString(cal);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(getString(R.string.AlarmTimePref), alarmStr);
editor.commit();
@ -338,12 +346,6 @@ public class MainActivity extends ActionBarActivity {
Button alarm_time = (Button) getActivity().findViewById(R.id.alarm_time);
alarm_time.setText(alarmStr);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
if (alarmActive) {
// Initialise alarm, which displays a dialog and system alert, and
// calls AlarmManager with grace_period as the delay
@ -468,14 +470,10 @@ public class MainActivity extends ActionBarActivity {
return CalendarToTimeString(TimeStringToCalendar(timeStr));
}
public static String HourMinuteToTimeString(int hour, int minute) {
return CalendarToTimeString(TimeStringToCalendar(hour + ":" + minute));
}
public static Calendar TimeStringToCalendar(String dateString) {
Date date;
Calendar cal;
final String FORMAT = "HH:mm"; // z = time zone
final String FORMAT = "HH:mm";
SimpleDateFormat sdf = new SimpleDateFormat(FORMAT, Locale.getDefault());
sdf.format(new Date());
try {
@ -489,7 +487,7 @@ public class MainActivity extends ActionBarActivity {
// Create a new calendar with the correct day, month and year,
// and set the hour and minute by hand.
cal = Calendar.getInstance();
cal.set(Calendar.HOUR, dateCal.get(Calendar.HOUR));
cal.set(Calendar.HOUR_OF_DAY, dateCal.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, dateCal.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, 0);
return cal;

View File

@ -57,7 +57,6 @@
android:id="@+id/alarm_time_text"
android:layout_column="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"