diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ab744a396..9b0ab9083 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -107,6 +107,10 @@ android:name=".activities.TimePickerActivity" android:theme="@style/Tasks.Dialog" /> + + 0) { + hideUntilControls.setCustomDate(timestamp); + } else { + log.error("Invalid timestamp"); + } + return; + } else if (editNotes != null && editNotes.activityResult(requestCode, resultCode, data)) { return; } else if (requestCode == REQUEST_CODE_RECORD && resultCode == Activity.RESULT_OK) { String recordedAudioPath = data.getStringExtra(AACRecordingActivity.RESULT_OUTFILE); diff --git a/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java b/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java index 300bcfd25..403d97f52 100644 --- a/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java +++ b/src/main/java/com/todoroo/astrid/reminders/ReminderPreferences.java @@ -49,8 +49,7 @@ public class ReminderPreferences extends InjectingPreferenceActivity { public boolean onPreferenceClick(Preference ignored) { final DateTime current = new DateTime().withMillisOfDay(preference.getMillisOfDay()); startActivityForResult(new Intent(ReminderPreferences.this, TimePickerActivity.class) {{ - putExtra(TimePickerActivity.EXTRA_HOURS, current.getHourOfDay()); - putExtra(TimePickerActivity.EXTRA_MINUTES, current.getMinuteOfHour()); + putExtra(TimePickerActivity.EXTRA_TIMESTAMP, current.getMillis()); }}, requestCode); return true; } diff --git a/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.java b/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.java index 5a6501595..bafefe760 100644 --- a/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.java +++ b/src/main/java/com/todoroo/astrid/ui/HideUntilControlSet.java @@ -6,6 +6,7 @@ package com.todoroo.astrid.ui; import android.content.DialogInterface; +import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; @@ -21,6 +22,7 @@ import com.todoroo.astrid.helper.TaskEditControlSetBase; import org.joda.time.DateTime; import org.tasks.R; +import org.tasks.activities.DateAndTimePickerActivity; import org.tasks.dialogs.DateAndTimePickerDialog; import java.util.Date; @@ -40,6 +42,7 @@ public class HideUntilControlSet extends TaskEditControlSetBase implements OnIte private static final int SPECIFIC_DATE = -1; private static final int EXISTING_TIME_UNSET = -2; + public static final int REQUEST_HIDE_UNTIL = 11011; //private final CheckBox enabled; private Spinner spinner; @@ -125,20 +128,9 @@ public class HideUntilControlSet extends TaskEditControlSetBase implements OnIte customDate = newDate(existingDate == EXISTING_TIME_UNSET ? DateUtilities.now() : existingDate); customDate.setSeconds(0); - DateAndTimePickerDialog.dateAndTimePickerDialog(taskEditFragment.getFragmentManager(), taskEditFragment.getActivity(), newDateTime(customDate), new DateAndTimePickerDialog.OnDateTimePicked() { - @Override - public void onDateTimePicked(DateTime dateTime) { - customDate = dateTime.toDate(); - customDateFinished(); - } - }, new DialogInterface.OnDismissListener() { - @Override - public void onDismiss(DialogInterface dialog) { - spinner.setSelection(previousSetting); - refreshDisplayView(); - } - }); - + taskEditFragment.startActivityForResult(new Intent(taskEditFragment.getActivity(), DateAndTimePickerActivity.class) {{ + putExtra(DateAndTimePickerActivity.EXTRA_TIMESTAMP, customDate.getTime()); + }}, REQUEST_HIDE_UNTIL); spinner.setSelection(previousSetting); } else { previousSetting = position; @@ -147,6 +139,11 @@ public class HideUntilControlSet extends TaskEditControlSetBase implements OnIte refreshDisplayView(); } + public void setCustomDate(long timestamp) { + customDate = new DateTime(timestamp).toDate(); + customDateFinished(); + } + @Override public void onNothingSelected(AdapterView arg0) { // ignore diff --git a/src/main/java/org/tasks/activities/DateAndTimePickerActivity.java b/src/main/java/org/tasks/activities/DateAndTimePickerActivity.java new file mode 100644 index 000000000..39434a5c7 --- /dev/null +++ b/src/main/java/org/tasks/activities/DateAndTimePickerActivity.java @@ -0,0 +1,89 @@ +package org.tasks.activities; + +import android.app.Activity; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; + +import com.fourmob.datetimepicker.date.DatePickerDialog; + +import org.joda.time.DateTime; +import org.tasks.dialogs.MyDatePickerDialog; + +import static org.tasks.date.DateTimeUtils.currentTimeMillis; + +public class DateAndTimePickerActivity extends FragmentActivity implements DatePickerDialog.OnDateSetListener, DialogInterface.OnDismissListener { + + private static final String FRAG_TAG_DATE_PICKER = "frag_tag_date_picker"; + private static final int REQUEST_PICK_TIME = 12345; + + public static final String EXTRA_TIMESTAMP = "extra_timestamp"; + private static final String EXTRA_DATE_SELECTED = "extra_date_selected"; + + private DateTime initial; + private boolean dateSelected; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + initial = new DateTime(getIntent().getLongExtra(EXTRA_TIMESTAMP, currentTimeMillis())); + + if (savedInstanceState != null) { + dateSelected = savedInstanceState.getBoolean(EXTRA_DATE_SELECTED, false); + if (dateSelected) { + return; + } + } + + FragmentManager supportFragmentManager = getSupportFragmentManager(); + MyDatePickerDialog datePickerDialog = (MyDatePickerDialog) supportFragmentManager.findFragmentByTag(FRAG_TAG_DATE_PICKER); + if (datePickerDialog == null) { + datePickerDialog = new MyDatePickerDialog(); + datePickerDialog.initialize(null, initial.getYear(), initial.getMonthOfYear() - 1, initial.getDayOfMonth(), false); + datePickerDialog.show(supportFragmentManager, FRAG_TAG_DATE_PICKER); + } + datePickerDialog.setOnDismissListener(this); + datePickerDialog.setOnDateSetListener(this); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putBoolean(EXTRA_DATE_SELECTED, dateSelected); + } + + @Override + public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) { + dateSelected = true; + datePickerDialog.dismiss(); + final long timestamp = initial.withYear(year).withMonthOfYear(month + 1).withDayOfMonth(day).getMillis(); + startActivityForResult(new Intent(this, TimePickerActivity.class) {{ + putExtra(TimePickerActivity.EXTRA_TIMESTAMP, timestamp); + }}, REQUEST_PICK_TIME); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, final Intent data) { + if (requestCode == REQUEST_PICK_TIME) { + if (resultCode == Activity.RESULT_OK) { + setResult(RESULT_OK, new Intent() {{ + putExtra(EXTRA_TIMESTAMP, data.getLongExtra(TimePickerActivity.EXTRA_TIMESTAMP, 0L)); + }}); + } else { + setResult(RESULT_CANCELED); + } + finish(); + } else { + super.onActivityResult(requestCode, resultCode, data); + } + } + + @Override + public void onDismiss(DialogInterface dialog) { + setResult(RESULT_CANCELED); + finish(); + } +} diff --git a/src/main/java/org/tasks/activities/TimePickerActivity.java b/src/main/java/org/tasks/activities/TimePickerActivity.java index 6fe3bea72..7faae485a 100644 --- a/src/main/java/org/tasks/activities/TimePickerActivity.java +++ b/src/main/java/org/tasks/activities/TimePickerActivity.java @@ -1,5 +1,6 @@ package org.tasks.activities; +import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; @@ -9,36 +10,53 @@ import android.text.format.DateFormat; import com.sleepbot.datetimepicker.time.RadialPickerLayout; import com.sleepbot.datetimepicker.time.TimePickerDialog; +import org.joda.time.DateTime; +import org.tasks.dialogs.MyTimePickerDialog; -public class TimePickerActivity extends FragmentActivity implements TimePickerDialog.OnTimeSetListener { +import static org.tasks.date.DateTimeUtils.currentTimeMillis; + + +public class TimePickerActivity extends FragmentActivity implements TimePickerDialog.OnTimeSetListener, DialogInterface.OnDismissListener { - public static final String EXTRA_HOURS = "extra_hours"; - public static final String EXTRA_MINUTES = "extra_minutes"; private static final String FRAG_TAG_TIME_PICKER = "frag_tag_time_picker"; + public static final String EXTRA_TIMESTAMP = "extra_timestamp"; + + private DateTime initial; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + initial = new DateTime(getIntent().getLongExtra(EXTRA_TIMESTAMP, currentTimeMillis())); + FragmentManager supportFragmentManager = getSupportFragmentManager(); - TimePickerDialog dialog = (TimePickerDialog) supportFragmentManager.findFragmentByTag(FRAG_TAG_TIME_PICKER); + MyTimePickerDialog dialog = (MyTimePickerDialog) supportFragmentManager.findFragmentByTag(FRAG_TAG_TIME_PICKER); if (dialog == null) { - Intent intent = getIntent(); - int hours = intent.getIntExtra(EXTRA_HOURS, 0); - int minutes = intent.getIntExtra(EXTRA_MINUTES, 0); - dialog = new TimePickerDialog(); - dialog.initialize(null, hours, minutes, DateFormat.is24HourFormat(this), false); - dialog.show(getSupportFragmentManager(), FRAG_TAG_TIME_PICKER); + dialog = new MyTimePickerDialog(); + dialog.initialize(null, initial.getHourOfDay(), initial.getMinuteOfHour(), DateFormat.is24HourFormat(this), false); + dialog.show(supportFragmentManager, FRAG_TAG_TIME_PICKER); } + dialog.setOnDismissListener(this); dialog.setOnTimeSetListener(this); } @Override public void onTimeSet(RadialPickerLayout radialPickerLayout, final int hours, final int minutes) { setResult(RESULT_OK, new Intent() {{ - putExtra(EXTRA_HOURS, hours); - putExtra(EXTRA_MINUTES, minutes); + putExtra(EXTRA_TIMESTAMP, initial + .withMillisOfDay(0) + .withHourOfDay(hours) + .withMinuteOfHour(minutes) + .getMillis()); }}); + } + + @Override + public void onDismiss(DialogInterface dialog) { + if (isChangingConfigurations()) { + return; + } finish(); } } diff --git a/src/main/java/org/tasks/dialogs/MyDatePickerDialog.java b/src/main/java/org/tasks/dialogs/MyDatePickerDialog.java index fe74fb672..3198eedd8 100644 --- a/src/main/java/org/tasks/dialogs/MyDatePickerDialog.java +++ b/src/main/java/org/tasks/dialogs/MyDatePickerDialog.java @@ -14,6 +14,8 @@ public class MyDatePickerDialog extends DatePickerDialog { @Override public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + if (listener != null) { listener.onDismiss(dialog); } diff --git a/src/main/java/org/tasks/dialogs/MyTimePickerDialog.java b/src/main/java/org/tasks/dialogs/MyTimePickerDialog.java index 3bc3fb3a4..e31b612bb 100644 --- a/src/main/java/org/tasks/dialogs/MyTimePickerDialog.java +++ b/src/main/java/org/tasks/dialogs/MyTimePickerDialog.java @@ -12,8 +12,19 @@ public class MyTimePickerDialog extends TimePickerDialog { this.listener = listener; } + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + + if (listener != null) { + listener.onDismiss(dialog); + } + } + @Override public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + if (listener != null) { listener.onDismiss(dialog); } diff --git a/src/main/java/org/tasks/ui/TimePreference.java b/src/main/java/org/tasks/ui/TimePreference.java index e03252490..6fe10ac5d 100644 --- a/src/main/java/org/tasks/ui/TimePreference.java +++ b/src/main/java/org/tasks/ui/TimePreference.java @@ -12,8 +12,6 @@ import org.tasks.activities.TimePickerActivity; import java.text.DateFormat; -import static org.tasks.date.DateTimeUtils.newDateTime; - public class TimePreference extends Preference { private int millisOfDay; @@ -49,9 +47,8 @@ public class TimePreference extends Preference { } public void handleTimePickerActivityIntent(Intent data) { - int hour = data.getIntExtra(TimePickerActivity.EXTRA_HOURS, 0); - int minute = data.getIntExtra(TimePickerActivity.EXTRA_MINUTES, 0); - int millisOfDay = newDateTime().withMillisOfDay(0).withHourOfDay(hour).withMinuteOfHour(minute).getMillisOfDay(); + long timestamp = data.getLongExtra(TimePickerActivity.EXTRA_TIMESTAMP, 0L); + int millisOfDay = new DateTime(timestamp).getMillisOfDay(); if (callChangeListener(millisOfDay)) { persistInt(millisOfDay); setMillisOfDay(millisOfDay);