Snooze from Android Wear notification

pull/384/head
Alex Baker 9 years ago
parent 4e6d7989d7
commit a50c2ef05c

@ -1,14 +0,0 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.reminders;
public interface SnoozeCallback {
void snoozeForTime(long time);
void pickDateTime();
}

@ -33,6 +33,8 @@ import org.tasks.receivers.CompleteTaskReceiver;
import org.tasks.reminders.MissedCallActivity;
import org.tasks.reminders.NotificationActivity;
import org.tasks.reminders.SnoozeActivity;
import org.tasks.reminders.SnoozeDialog;
import org.tasks.reminders.SnoozeOption;
import java.io.InputStream;
@ -259,13 +261,32 @@ public class Notifier {
putExtra(CompleteTaskReceiver.TASK_ID, id);
}}, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action completeAction = new NotificationCompat.Action.Builder(
R.drawable.ic_check_white_24dp, context.getResources().getString(R.string.rmd_NoA_done), completeIntent).build();
PendingIntent snoozePendingIntent = PendingIntent.getActivity(context, (int) id, new Intent(context, SnoozeActivity.class) {{
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
putExtra(SnoozeActivity.EXTRA_TASK_ID, id);
}}, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.ic_check_white_24dp, context.getResources().getString(R.string.rmd_NoA_done), completeIntent)
.addAction(R.drawable.ic_snooze_white_24dp, context.getResources().getString(R.string.rmd_NoA_snooze), snoozePendingIntent);
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
wearableExtender.addAction(completeAction);
for (final SnoozeOption snoozeOption : SnoozeDialog.getSnoozeOptions(preferences)) {
final long timestamp = snoozeOption.getDateTime().getMillis();
PendingIntent snoozeIntent = PendingIntent.getActivity(context, (int) id, new Intent(context, SnoozeActivity.class) {{
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
setAction(String.format("snooze-%s-%s", id, timestamp));
putExtra(SnoozeActivity.EXTRA_TASK_ID, id);
putExtra(SnoozeActivity.EXTRA_SNOOZE_TIME, timestamp);
}}, PendingIntent.FLAG_UPDATE_CURRENT);
wearableExtender.addAction(new NotificationCompat.Action.Builder(
R.drawable.ic_snooze_white_24dp, context.getString(snoozeOption.getResId()), snoozeIntent)
.build());
}
builder.addAction(completeAction)
.addAction(R.drawable.ic_snooze_white_24dp, context.getResources().getString(R.string.rmd_NoA_snooze), snoozePendingIntent)
.extend(wearableExtender);
}
activateNotification(ringTimes, (int) id, builder.build(), taskTitle);

@ -6,7 +6,6 @@ import android.content.Intent;
import android.os.Bundle;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.reminders.SnoozeCallback;
import com.todoroo.astrid.service.StartupService;
import com.todoroo.astrid.service.TaskService;
@ -26,6 +25,7 @@ public class SnoozeActivity extends InjectingAppCompatActivity implements Snooze
private static final int REQUEST_DATE_TIME = 10101;
public static final String EXTRA_TASK_ID = "id";
public static final String EXTRA_SNOOZE_TIME = "snooze_time";
@Inject StartupService startupService;
@Inject TaskService taskService;
@ -65,21 +65,25 @@ public class SnoozeActivity extends InjectingAppCompatActivity implements Snooze
startupService.onStartupApplication(this);
FragmentManager fragmentManager = getFragmentManager();
SnoozeDialog fragmentByTag = (SnoozeDialog) fragmentManager.findFragmentByTag(FRAG_TAG_SNOOZE_DIALOG);
if (fragmentByTag == null) {
fragmentByTag = new SnoozeDialog();
fragmentByTag.show(fragmentManager, FRAG_TAG_SNOOZE_DIALOG);
if (intent.hasExtra(EXTRA_SNOOZE_TIME)) {
snoozeForTime(new DateTime(intent.getLongExtra(EXTRA_SNOOZE_TIME, 0L)));
} else {
FragmentManager fragmentManager = getFragmentManager();
SnoozeDialog fragmentByTag = (SnoozeDialog) fragmentManager.findFragmentByTag(FRAG_TAG_SNOOZE_DIALOG);
if (fragmentByTag == null) {
fragmentByTag = new SnoozeDialog();
fragmentByTag.show(fragmentManager, FRAG_TAG_SNOOZE_DIALOG);
}
fragmentByTag.setOnCancelListener(this);
fragmentByTag.setSnoozeCallback(this);
}
fragmentByTag.setOnCancelListener(this);
fragmentByTag.setSnoozeCallback(this);
}
@Override
public void snoozeForTime(long time) {
public void snoozeForTime(DateTime time) {
Task task = new Task();
task.setId(taskId);
task.setReminderSnooze(time);
task.setReminderSnooze(time.getMillis());
taskService.save(task);
notificationManager.cancel(taskId);
setResult(RESULT_OK);
@ -110,7 +114,8 @@ public class SnoozeActivity extends InjectingAppCompatActivity implements Snooze
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_DATE_TIME) {
if (resultCode == RESULT_OK && data != null) {
snoozeForTime(data.getLongExtra(TimePickerActivity.EXTRA_TIMESTAMP, 0L));
long timestamp = data.getLongExtra(TimePickerActivity.EXTRA_TIMESTAMP, 0L);
snoozeForTime(new DateTime(timestamp));
} else {
finish();
}

@ -0,0 +1,11 @@
package org.tasks.reminders;
import org.tasks.time.DateTime;
public interface SnoozeCallback {
void snoozeForTime(DateTime time);
void pickDateTime();
}

@ -6,8 +6,6 @@ import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import com.todoroo.astrid.reminders.SnoozeCallback;
import org.tasks.injection.DialogFragmentComponent;
import org.tasks.time.DateTime;
import org.tasks.R;
@ -29,48 +27,21 @@ public class SnoozeDialog extends InjectingDialogFragment {
@Inject @ForApplication Context context;
@Inject DialogBuilder dialogBuilder;
private DateTime now = new DateTime();
private SnoozeCallback snoozeCallback;
private DialogInterface.OnCancelListener onCancelListener;
private List<Long> snoozeTimes = new ArrayList<>();
private List<String> items = new ArrayList<>();
private void add(int resId, DateTime dateTime) {
items.add(String.format("%s (%s)", getString(resId), getTimeString(context, dateTime)));
snoozeTimes.add(dateTime.getMillis());
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DateTime morning = now.withMillisOfDay(preferences.getDateShortcutMorning());
DateTime afternoon = now.withMillisOfDay(preferences.getDateShortcutAfternoon());
DateTime evening = now.withMillisOfDay(preferences.getDateShortcutEvening());
DateTime night = now.withMillisOfDay(preferences.getDateShortcutNight());
DateTime tomorrowMorning = morning.plusDays(1);
DateTime tomorrowAfternoon = afternoon.plusDays(1);
final List<SnoozeOption> snoozeOptions = getSnoozeOptions(preferences);
items.add(getString(R.string.date_shortcut_hour));
snoozeTimes.add(0L);
DateTime hourCutoff = new DateTime().plusMinutes(75);
if (morning.isAfter(hourCutoff)) {
add(R.string.date_shortcut_morning, morning);
add(R.string.date_shortcut_afternoon, afternoon);
} else if (afternoon.isAfter(hourCutoff)) {
add(R.string.date_shortcut_afternoon, afternoon);
add(R.string.date_shortcut_evening, evening);
} else if (evening.isAfter(hourCutoff)) {
add(R.string.date_shortcut_evening, evening);
add(R.string.date_shortcut_night, night);
} else if (night.isAfter(hourCutoff)) {
add(R.string.date_shortcut_night, night);
add(R.string.date_shortcut_tomorrow_morning, tomorrowMorning);
} else {
add(R.string.date_shortcut_tomorrow_morning, tomorrowMorning);
add(R.string.date_shortcut_tomorrow_afternoon, tomorrowAfternoon);
for (SnoozeOption snoozeOption : snoozeOptions) {
items.add(String.format("%s (%s)",
getString(snoozeOption.getResId()),
getTimeString(context, snoozeOption.getDateTime())));
}
items.add(getString(R.string.pick_a_date_and_time));
return dialogBuilder.newDialog()
@ -80,11 +51,9 @@ public class SnoozeDialog extends InjectingDialogFragment {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
snoozeCallback.snoozeForTime(new DateTime().plusHours(1).getMillis());
break;
case 1:
case 2:
snoozeCallback.snoozeForTime(snoozeTimes.get(which));
snoozeCallback.snoozeForTime(snoozeOptions.get(which).getDateTime());
break;
case 3:
dialog.dismiss();
@ -117,4 +86,39 @@ public class SnoozeDialog extends InjectingDialogFragment {
protected void inject(DialogFragmentComponent component) {
component.inject(this);
}
public static List<SnoozeOption> getSnoozeOptions(Preferences preferences) {
DateTime now = new DateTime();
DateTime morning = now.withMillisOfDay(preferences.getDateShortcutMorning());
DateTime afternoon = now.withMillisOfDay(preferences.getDateShortcutAfternoon());
DateTime evening = now.withMillisOfDay(preferences.getDateShortcutEvening());
DateTime night = now.withMillisOfDay(preferences.getDateShortcutNight());
DateTime tomorrowMorning = morning.plusDays(1);
DateTime tomorrowAfternoon = afternoon.plusDays(1);
DateTime hourCutoff = new DateTime().plusMinutes(75);
List<SnoozeOption> snoozeOptions = new ArrayList<>();
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_hour, now.plusHours(1)));
if (morning.isAfter(hourCutoff)) {
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_morning, morning));
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_afternoon, afternoon));
} else if (afternoon.isAfter(hourCutoff)) {
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_afternoon, afternoon));
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_evening, evening));
} else if (evening.isAfter(hourCutoff)) {
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_evening, evening));
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_night, night));
} else if (night.isAfter(hourCutoff)) {
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_night, night));
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_tomorrow_morning, tomorrowMorning));
} else {
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_tomorrow_morning, tomorrowMorning));
snoozeOptions.add(new SnoozeOption(R.string.date_shortcut_tomorrow_afternoon, tomorrowAfternoon));
}
return snoozeOptions;
}
}

@ -0,0 +1,29 @@
package org.tasks.reminders;
import org.tasks.time.DateTime;
public class SnoozeOption {
private final int resId;
private final DateTime dateTime;
public SnoozeOption(int resId, DateTime dateTime) {
this.resId = resId;
this.dateTime = dateTime;
}
public int getResId() {
return resId;
}
public DateTime getDateTime() {
return dateTime;
}
@Override
public String toString() {
return "SnoozeOption{" +
"resId=" + resId +
", dateTime=" + dateTime +
'}';
}
}
Loading…
Cancel
Save