Convert ShowNotificationReceiver to intent service

pull/281/head
Alex Baker 9 years ago
parent c03b8882fa
commit 1cd5a34d68

@ -9,11 +9,11 @@ import org.tasks.preferences.Preferences;
import java.util.concurrent.TimeUnit;
import static com.todoroo.astrid.reminders.ShowNotificationReceiver.isQuietHours;
import static com.todoroo.astrid.reminders.TaskNotificationIntentService.isQuietHours;
import static org.tasks.Freeze.freezeAt;
import static org.tasks.Freeze.thaw;
public class NotificationsTest extends AndroidTestCase {
public class NotificationReceiverTest extends AndroidTestCase {
@SuppressLint("NewApi")
private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1);

@ -5,7 +5,6 @@
*/
package com.todoroo.astrid.reminders;
import android.app.PendingIntent;
import android.content.Intent;
import com.todoroo.andlib.utility.DateUtilities;
@ -14,6 +13,7 @@ import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.test.DatabaseTestCase;
import org.tasks.Broadcaster;
import org.tasks.Notifier;
import org.tasks.injection.TestModule;
import org.tasks.notifications.NotificationManager;
@ -23,8 +23,6 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@ -44,12 +42,20 @@ public class NotificationTests extends DatabaseTestCase {
public Broadcaster getBroadcaster() {
return mock(Broadcaster.class);
}
@Singleton
@Provides
public Notifier getNotifier() {
return mock(Notifier.class);
}
}
@Inject TaskDao taskDao;
@Inject Notifications notifications;
@Inject
NotificationReceiver notificationReceiver;
@Inject NotificationManager notificationManager;
@Inject Broadcaster broadcaster;
@Inject Notifier notifier;
@Override
protected void tearDown() {
@ -57,6 +63,7 @@ public class NotificationTests extends DatabaseTestCase {
verifyNoMoreInteractions(notificationManager);
verifyNoMoreInteractions(broadcaster);
verifyNoMoreInteractions(notifier);
}
public void testAlarmToNotification() {
@ -66,18 +73,12 @@ public class NotificationTests extends DatabaseTestCase {
}};
taskDao.persist(task);
notifications.handle(new Intent() {{
putExtra(Notifications.ID_KEY, task.getId());
putExtra(Notifications.EXTRAS_TYPE, ReminderService.TYPE_DUE);
notificationReceiver.handle(new Intent() {{
putExtra(NotificationReceiver.ID_KEY, task.getId());
putExtra(NotificationReceiver.EXTRAS_TYPE, ReminderService.TYPE_DUE);
}});
verify(broadcaster).requestNotification(
eq(task.getId()),
any(PendingIntent.class),
eq(ReminderService.TYPE_DUE),
eq("rubberduck"),
eq("Tasks"),
eq(1));
verify(notifier).triggerTaskNotification(task.getId(), ReminderService.TYPE_DUE);
}
public void testDeletedTaskDoesntTriggerNotification() {
@ -87,9 +88,9 @@ public class NotificationTests extends DatabaseTestCase {
}};
taskDao.persist(task);
notifications.handle(new Intent() {{
putExtra(Notifications.ID_KEY, task.getId());
putExtra(Notifications.EXTRAS_TYPE, ReminderService.TYPE_DUE);
notificationReceiver.handle(new Intent() {{
putExtra(NotificationReceiver.ID_KEY, task.getId());
putExtra(NotificationReceiver.EXTRAS_TYPE, ReminderService.TYPE_DUE);
}});
verify(notificationManager).cancel((int) task.getId());
@ -102,9 +103,9 @@ public class NotificationTests extends DatabaseTestCase {
}};
taskDao.persist(task);
notifications.handle(new Intent() {{
putExtra(Notifications.ID_KEY, task.getId());
putExtra(Notifications.EXTRAS_TYPE, ReminderService.TYPE_DUE);
notificationReceiver.handle(new Intent() {{
putExtra(NotificationReceiver.ID_KEY, task.getId());
putExtra(NotificationReceiver.EXTRAS_TYPE, ReminderService.TYPE_DUE);
}});
verify(notificationManager).cancel((int) task.getId());
@ -115,14 +116,14 @@ public class NotificationTests extends DatabaseTestCase {
// task.setTitle("rubberduck");
// taskDao.persist(task);
// Intent intent = new Intent();
// intent.putExtra(Notifications.ID_KEY, task.getId());
// intent.putExtra(NotificationReceiver.ID_KEY, task.getId());
//
// int hour = newDate().getHours();
// Preferences.setStringFromInteger(R.string.p_rmd_quietStart, hour - 1);
// Preferences.setStringFromInteger(R.string.p_rmd_quietEnd, hour + 1);
//
// // due date notification has vibrate
// Notifications.setNotificationManager(new TestNotificationManager() {
// NotificationReceiver.setNotificationManager(new TestNotificationManager() {
// public void notify(int id, Notification notification) {
// assertNull(notification.sound);
// assertTrue((notification.defaults & Notification.DEFAULT_SOUND) == 0);
@ -130,11 +131,11 @@ public class NotificationTests extends DatabaseTestCase {
// assertTrue(notification.vibrate.length > 0);
// }
// });
// intent.putExtra(Notifications.EXTRAS_TYPE, ReminderService.TYPE_DUE);
// notifications.onReceive(getContext(), intent);
// intent.putExtra(NotificationReceiver.EXTRAS_TYPE, ReminderService.TYPE_DUE);
// notificationReceiver.onReceive(getContext(), intent);
//
// // random notification does not
// Notifications.setNotificationManager(new TestNotificationManager() {
// NotificationReceiver.setNotificationManager(new TestNotificationManager() {
// public void notify(int id, Notification notification) {
// assertNull(notification.sound);
// assertTrue((notification.defaults & Notification.DEFAULT_SOUND) == 0);
@ -142,34 +143,34 @@ public class NotificationTests extends DatabaseTestCase {
// notification.vibrate.length == 0);
// }
// });
// intent.removeExtra(Notifications.EXTRAS_TYPE);
// intent.putExtra(Notifications.EXTRAS_TYPE, ReminderService.TYPE_RANDOM);
// notifications.onReceive(getContext(), intent);
// intent.removeExtra(NotificationReceiver.EXTRAS_TYPE);
// intent.putExtra(NotificationReceiver.EXTRAS_TYPE, ReminderService.TYPE_RANDOM);
// notificationReceiver.onReceive(getContext(), intent);
//
// // wrapping works
// Preferences.setStringFromInteger(R.string.p_rmd_quietStart, hour + 2);
// Preferences.setStringFromInteger(R.string.p_rmd_quietEnd, hour + 1);
//
// Notifications.setNotificationManager(new TestNotificationManager() {
// NotificationReceiver.setNotificationManager(new TestNotificationManager() {
// public void notify(int id, Notification notification) {
// assertNull(notification.sound);
// assertTrue((notification.defaults & Notification.DEFAULT_SOUND) == 0);
// }
// });
// intent.removeExtra(Notifications.EXTRAS_TYPE);
// intent.putExtra(Notifications.EXTRAS_TYPE, ReminderService.TYPE_DUE);
// notifications.onReceive(getContext(), intent);
// intent.removeExtra(NotificationReceiver.EXTRAS_TYPE);
// intent.putExtra(NotificationReceiver.EXTRAS_TYPE, ReminderService.TYPE_DUE);
// notificationReceiver.onReceive(getContext(), intent);
//
// // nonstop notification still sounds
// task.setReminderFlags(Task.NOTIFY_MODE_NONSTOP);
// taskDao.persist(task);
// Notifications.setNotificationManager(new TestNotificationManager() {
// NotificationReceiver.setNotificationManager(new TestNotificationManager() {
// public void notify(int id, Notification notification) {
// assertTrue(notification.sound != null ||
// (notification.defaults & Notification.DEFAULT_SOUND) > 0);
// }
// });
// notifications.onReceive(getContext(), intent);
// notificationReceiver.onReceive(getContext(), intent);
// }
@Override

@ -5,10 +5,12 @@ import android.content.Intent;
import com.google.android.gms.location.GeofencingEvent;
import com.todoroo.astrid.dao.MetadataDao;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.reminders.ReminderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tasks.Broadcaster;
import org.tasks.Notifier;
import org.tasks.injection.InjectingIntentService;
import java.util.List;
@ -19,8 +21,8 @@ public class GeofenceTransitionsIntentService extends InjectingIntentService {
private static final Logger log = LoggerFactory.getLogger(GeofenceTransitionsIntentService.class);
@Inject Broadcaster broadcaster;
@Inject MetadataDao metadataDao;
@Inject Notifier notifier;
public GeofenceTransitionsIntentService() {
super(GeofenceTransitionsIntentService.class.getSimpleName());
@ -53,7 +55,7 @@ public class GeofenceTransitionsIntentService extends InjectingIntentService {
try {
Metadata fetch = metadataDao.fetch(Long.parseLong(requestId), Metadata.TASK, GeofenceFields.PLACE, GeofenceFields.LATITUDE, GeofenceFields.LONGITUDE, GeofenceFields.RADIUS);
Geofence geofence = new Geofence(fetch);
broadcaster.requestNotification(geofence.getMetadataId(), geofence.getTaskId());
notifier.triggerTaskNotification(geofence.getTaskId(), ReminderService.TYPE_ALARM);
} catch(Exception e) {
log.error(String.format("Error triggering geofence %s: %s", requestId, e.getMessage()), e);
}

@ -233,16 +233,10 @@
<!-- ======================================================= Receivers = -->
<receiver android:name="com.todoroo.astrid.reminders.Notifications" />
<receiver android:name="com.todoroo.astrid.reminders.NotificationReceiver" />
<receiver android:name=".receivers.RefreshReceiver" />
<receiver android:name="com.todoroo.astrid.reminders.ShowNotificationReceiver">
<intent-filter>
<action android:name="org.tasks.IN_APP_NOTIFY" />
</intent-filter>
</receiver>
<!-- widgets -->
<receiver
android:name="com.todoroo.astrid.widget.TasksWidget"
@ -549,6 +543,9 @@
<service
android:name=".scheduling.ReminderSchedulerIntentService"
android:exported="false" />
<service
android:name="com.todoroo.astrid.reminders.TaskNotificationIntentService"
android:exported="false" />
<!-- Uses Library -->
<uses-library

@ -22,7 +22,7 @@ import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.reminders.Notifications;
import com.todoroo.astrid.reminders.NotificationReceiver;
import com.todoroo.astrid.reminders.ReminderService;
import com.todoroo.astrid.service.SynchronizeMetadataCallback;
@ -130,10 +130,10 @@ public class AlarmService {
}
private PendingIntent pendingIntentForAlarm(Metadata alarm, long taskId) {
Intent intent = new Intent(context, Notifications.class);
Intent intent = new Intent(context, NotificationReceiver.class);
intent.setAction("ALARM" + alarm.getId()); //$NON-NLS-1$
intent.putExtra(Notifications.ID_KEY, taskId);
intent.putExtra(Notifications.EXTRAS_TYPE, ReminderService.TYPE_ALARM);
intent.putExtra(NotificationReceiver.ID_KEY, taskId);
intent.putExtra(NotificationReceiver.EXTRAS_TYPE, ReminderService.TYPE_ALARM);
return PendingIntent.getBroadcast(context, (int)alarm.getId(),
intent, PendingIntent.FLAG_UPDATE_CURRENT);

@ -0,0 +1,26 @@
package com.todoroo.astrid.reminders;
import android.content.Context;
import android.content.Intent;
import org.tasks.Notifier;
import org.tasks.injection.InjectingBroadcastReceiver;
import javax.inject.Inject;
public class NotificationReceiver extends InjectingBroadcastReceiver {
public static final String ID_KEY = "id"; //$NON-NLS-1$
public static final String EXTRAS_TYPE = "type"; //$NON-NLS-1$
@Inject Notifier notifier;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
notifier.triggerTaskNotification(
intent.getLongExtra(ID_KEY, 0),
intent.getIntExtra(EXTRAS_TYPE, (byte) 0));
}
}

@ -404,11 +404,11 @@ public final class ReminderService {
if(task.getId() == Task.NO_ID) {
return;
}
Intent intent = new Intent(context, Notifications.class);
Intent intent = new Intent(context, NotificationReceiver.class);
intent.setType(Long.toString(task.getId()));
intent.setAction(Integer.toString(type));
intent.putExtra(Notifications.ID_KEY, task.getId());
intent.putExtra(Notifications.EXTRAS_TYPE, type);
intent.putExtra(NotificationReceiver.ID_KEY, task.getId());
intent.putExtra(NotificationReceiver.EXTRAS_TYPE, type);
// calculate the unique requestCode as a combination of the task-id and alarm-type:
// concatenate id+type to keep the combo unique

@ -17,18 +17,15 @@ import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tasks.R;
import org.tasks.injection.InjectingBroadcastReceiver;
import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingIntentService;
import org.tasks.notifications.AudioManager;
import org.tasks.notifications.NotificationManager;
import org.tasks.notifications.TelephonyManager;
import org.tasks.preferences.Preferences;
import org.tasks.receivers.CompleteTaskReceiver;
import org.tasks.reminders.NotificationActivity;
import org.tasks.reminders.SnoozeActivity;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.inject.Inject;
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
@ -39,32 +36,51 @@ import static org.tasks.date.DateTimeUtils.currentTimeMillis;
*
* @author Sam
*/
public class ShowNotificationReceiver extends InjectingBroadcastReceiver {
public class TaskNotificationIntentService extends InjectingIntentService {
private static final Logger log = LoggerFactory.getLogger(ShowNotificationReceiver.class);
private static final Logger log = LoggerFactory.getLogger(TaskNotificationIntentService.class);
private static ExecutorService singleThreadVoicePool = Executors.newSingleThreadExecutor();
private static long lastNotificationSound = 0L;
/**
* Action name for broadcast intent notifying that task was created from repeating template
*/
public static final String EXTRAS_CUSTOM_INTENT = "intent"; //$NON-NLS-1$
public static final String EXTRAS_NOTIF_ID = "notifId"; //$NON-NLS-1$
/**
* notification type extra
*/
public static final String EXTRAS_TITLE = "title"; //$NON-NLS-1$
public static final String EXTRAS_TEXT = "text"; //$NON-NLS-1$
public static final String EXTRAS_RING_TIMES = "ringTimes"; //$NON-NLS-1$
public static final String EXTRA_TASK_ID = "extra_task_id";
public static final String EXTRAS_TYPE = "type"; //$NON-NLS-1$
@Inject NotificationManager notificationManager;
@Inject TelephonyManager telephonyManager;
@Inject Preferences preferences;
@Inject VoiceOutputAssistant voiceOutputAssistant;
@Inject AudioManager audioManager;
@Inject @ForApplication Context context;
public TaskNotificationIntentService() {
super(TaskNotificationIntentService.class.getSimpleName());
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
protected void onHandleIntent(Intent intent) {
super.onHandleIntent(intent);
showNotification(
context,
intent.getIntExtra(Notifications.EXTRAS_NOTIF_ID, 0),
intent.getLongExtra(NotificationActivity.EXTRA_TASK_ID, 0L),
intent.<PendingIntent>getParcelableExtra(Notifications.EXTRAS_CUSTOM_INTENT),
intent.getIntExtra(Notifications.EXTRAS_TYPE, 0),
intent.getStringExtra(Notifications.EXTRAS_TITLE),
intent.getStringExtra(Notifications.EXTRAS_TEXT),
intent.getIntExtra(Notifications.EXTRAS_RING_TIMES, 1));
intent.getIntExtra(EXTRAS_NOTIF_ID, 0),
intent.getLongExtra(EXTRA_TASK_ID, 0L),
intent.<PendingIntent>getParcelableExtra(EXTRAS_CUSTOM_INTENT),
intent.getIntExtra(EXTRAS_TYPE, 0),
intent.getStringExtra(EXTRAS_TITLE),
intent.getStringExtra(EXTRAS_TEXT),
intent.getIntExtra(EXTRAS_RING_TIMES, 1));
}
/**
@ -156,7 +172,7 @@ public class ShowNotificationReceiver extends InjectingBroadcastReceiver {
boolean maxOutVolumeForMultipleRingReminders = preferences.getBoolean(R.string.p_rmd_maxvolume, true);
// remember it to set it to the old value after the alarm
int previousAlarmVolume = audioManager.getAlarmVolume();
if (ringTimes != 1 && (type != ReminderService.TYPE_RANDOM)) {
if (ringTimes != 1) {
notification.audioStreamType = android.media.AudioManager.STREAM_ALARM;
if (maxOutVolumeForMultipleRingReminders) {
audioManager.setMaxAlarmVolume();
@ -202,67 +218,30 @@ public class ShowNotificationReceiver extends InjectingBroadcastReceiver {
voiceReminder = false;
}
singleThreadVoicePool.submit(new NotificationRunnable(ringTimes, notificationId, notification, voiceReminder,
maxOutVolumeForMultipleRingReminders, audioManager, previousAlarmVolume, title,
notificationManager, voiceOutputAssistant));
}
private static class NotificationRunnable implements Runnable {
private final int ringTimes;
private final int notificationId;
private final Notification notification;
private final boolean voiceReminder;
private final boolean maxOutVolumeForMultipleRingReminders;
private final AudioManager audioManager;
private final int previousAlarmVolume;
private final String text;
private NotificationManager notificationManager;
private final VoiceOutputAssistant voiceOutputAssistant;
public NotificationRunnable(int ringTimes, int notificationId, Notification notification,
boolean voiceReminder, boolean maxOutVolume,
AudioManager audioManager, int previousAlarmVolume,
String text, NotificationManager notificationManager,
VoiceOutputAssistant voiceOutputAssistant) {
this.ringTimes = ringTimes;
this.notificationId = notificationId;
this.notification = notification;
this.voiceReminder = voiceReminder;
this.maxOutVolumeForMultipleRingReminders = maxOutVolume;
this.audioManager = audioManager;
this.previousAlarmVolume = previousAlarmVolume;
this.text = text;
this.notificationManager = notificationManager;
this.voiceOutputAssistant = voiceOutputAssistant;
for (int i = 0; i < Math.max(ringTimes, 1); i++) {
notificationManager.notify(notificationId, notification);
AndroidUtilities.sleepDeep(500);
}
@Override
public void run() {
for (int i = 0; i < Math.max(ringTimes, 1); i++) {
notificationManager.notify(notificationId, notification);
Flags.set(Flags.REFRESH); // Forces a reload when app launches
if (voiceReminder || maxOutVolumeForMultipleRingReminders) {
AndroidUtilities.sleepDeep(2000);
for (int i = 0; i < 50; i++) {
AndroidUtilities.sleepDeep(500);
if (!audioManager.isRingtoneMode()) {
break;
}
}
Flags.set(Flags.REFRESH); // Forces a reload when app launches
if (voiceReminder || maxOutVolumeForMultipleRingReminders) {
AndroidUtilities.sleepDeep(2000);
for (int i = 0; i < 50; i++) {
AndroidUtilities.sleepDeep(500);
if (!audioManager.isRingtoneMode()) {
break;
}
try {
// first reset the Alarm-volume to the value before it was eventually maxed out
if (maxOutVolumeForMultipleRingReminders) {
audioManager.setAlarmVolume(previousAlarmVolume);
}
try {
// first reset the Alarm-volume to the value before it was eventually maxed out
if (maxOutVolumeForMultipleRingReminders) {
audioManager.setAlarmVolume(previousAlarmVolume);
}
if (voiceReminder) {
voiceOutputAssistant.speak(text);
}
} catch (VerifyError e) {
// unavailable
log.error(e.getMessage(), e);
if (voiceReminder) {
voiceOutputAssistant.speak(text);
}
} catch (VerifyError e) {
// unavailable
log.error(e.getMessage(), e);
}
}
}

@ -1,18 +1,13 @@
package org.tasks;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.reminders.Notifications;
import com.todoroo.astrid.reminders.ReminderService;
import com.todoroo.astrid.utility.Constants;
import org.tasks.injection.ForApplication;
import org.tasks.receivers.CompleteTaskReceiver;
import org.tasks.receivers.FirstLaunchReceiver;
import org.tasks.reminders.NotificationActivity;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -20,8 +15,6 @@ import javax.inject.Singleton;
@Singleton
public class Broadcaster {
public static final String BROADCAST_IN_APP_NOTIFY = Constants.PACKAGE + ".IN_APP_NOTIFY"; //$NON-NLS-1$
private final Context context;
@Inject
@ -44,27 +37,6 @@ public class Broadcaster {
}});
}
public void requestNotification(final long taskId, final PendingIntent intent, final int type,
final String title, final String text, final int ringTimes) {
sendOrderedBroadcast(new Intent(BROADCAST_IN_APP_NOTIFY) {{
putExtra(Notifications.EXTRAS_NOTIF_ID, (int) taskId);
putExtra(NotificationActivity.EXTRA_TASK_ID, taskId);
putExtra(Notifications.EXTRAS_CUSTOM_INTENT, intent);
putExtra(Notifications.EXTRAS_TYPE, type);
putExtra(Notifications.EXTRAS_TITLE, title);
putExtra(Notifications.EXTRAS_TEXT, text);
putExtra(Notifications.EXTRAS_RING_TIMES, ringTimes);
}}, AstridApiConstants.PERMISSION_READ);
}
public void requestNotification(final long alarmId, final long taskId) {
sendOrderedBroadcast(new Intent(context, Notifications.class) {{
setAction("ALARM" + alarmId); //$NON-NLS-1$
putExtra(Notifications.ID_KEY, taskId);
putExtra(Notifications.EXTRAS_TYPE, ReminderService.TYPE_ALARM);
}});
}
public void taskCompleted(final long id) {
sendOrderedBroadcast(new Intent(AstridApiConstants.BROADCAST_EVENT_TASK_COMPLETED) {{
putExtra(AstridApiConstants.EXTRAS_TASK_ID, id);

@ -1,9 +1,4 @@
/**
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.reminders;
package org.tasks;
import android.app.PendingIntent;
import android.content.Context;
@ -13,66 +8,39 @@ import android.text.TextUtils;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.reminders.ReminderService;
import com.todoroo.astrid.reminders.TaskNotificationIntentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tasks.Broadcaster;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingBroadcastReceiver;
import org.tasks.notifications.NotificationManager;
import org.tasks.reminders.NotificationActivity;
import javax.inject.Inject;
public class Notifications extends InjectingBroadcastReceiver {
public class Notifier {
private static final Logger log = LoggerFactory.getLogger(Notifications.class);
private static final Logger log = LoggerFactory.getLogger(Notifier.class);
/**
* task id extra
*/
public static final String ID_KEY = "id"; //$NON-NLS-1$
private Context context;
private TaskDao taskDao;
private NotificationManager notificationManager;
/**
* Action name for broadcast intent notifying that task was created from repeating template
*/
public static final String EXTRAS_CUSTOM_INTENT = "intent"; //$NON-NLS-1$
public static final String EXTRAS_NOTIF_ID = "notifId"; //$NON-NLS-1$
/**
* notification type extra
*/
public static final String EXTRAS_TYPE = "type"; //$NON-NLS-1$
public static final String EXTRAS_TITLE = "title"; //$NON-NLS-1$
public static final String EXTRAS_TEXT = "text"; //$NON-NLS-1$
public static final String EXTRAS_RING_TIMES = "ringTimes"; //$NON-NLS-1$
@Inject TaskDao taskDao;
@Inject @ForApplication Context context;
@Inject NotificationManager notificationManager;
@Inject Broadcaster broadcaster;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
handle(intent);
@Inject
public Notifier(@ForApplication Context context, TaskDao taskDao, NotificationManager notificationManager) {
this.context = context;
this.taskDao = taskDao;
this.notificationManager = notificationManager;
}
void handle(Intent intent) {
long id = intent.getLongExtra(ID_KEY, 0);
int type = intent.getIntExtra(EXTRAS_TYPE, (byte) 0);
public void triggerTaskNotification(long id, int type) {
if (!showTaskNotification(id, type)) {
notificationManager.cancel((int) id);
notificationManager.cancel(id);
}
}
/**
* Show a new notification about the given task. Returns false if there was
* some sort of error or the alarm should be disabled.
*/
private boolean showTaskNotification(long id, int type) {
private boolean showTaskNotification(final long id, final int type) {
Task task;
try {
task = taskDao.fetch(id, Task.ID, Task.TITLE, Task.HIDE_UNTIL, Task.COMPLETION_DATE,
@ -110,30 +78,34 @@ public class Notifications extends InjectingBroadcastReceiver {
}
// read properties
String taskTitle = task.getTitle();
final String taskTitle = task.getTitle();
boolean nonstopMode = task.isNotifyModeNonstop();
boolean ringFiveMode = task.isNotifyModeFive();
int ringTimes = nonstopMode ? -1 : (ringFiveMode ? 5 : 1);
final int ringTimes = nonstopMode ? -1 : (ringFiveMode ? 5 : 1);
// update last reminder time
task.setReminderLast(DateUtilities.now());
taskDao.saveExisting(task);
String text = context.getString(R.string.app_name);
PendingIntent intent = createNotificationIntent(id, taskTitle);
final String text = context.getString(R.string.app_name);
broadcaster.requestNotification((int) id, intent, type, taskTitle, text, ringTimes);
return true;
}
private PendingIntent createNotificationIntent(final long id, final String taskTitle) {
Intent intent = new Intent(context, NotificationActivity.class) {{
final Intent intent = new Intent(context, NotificationActivity.class) {{
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
setAction("NOTIFY" + id); //$NON-NLS-1$
putExtra(NotificationActivity.EXTRA_TASK_ID, id);
putExtra(NotificationActivity.EXTRA_TITLE, taskTitle);
}};
return PendingIntent.getActivity(context, (int) id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
context.startService(new Intent(context, TaskNotificationIntentService.class) {{
putExtra(TaskNotificationIntentService.EXTRAS_NOTIF_ID, (int) id);
putExtra(TaskNotificationIntentService.EXTRA_TASK_ID, id);
putExtra(TaskNotificationIntentService.EXTRAS_CUSTOM_INTENT, PendingIntent.getActivity(context, (int) id, intent, PendingIntent.FLAG_UPDATE_CURRENT));
putExtra(TaskNotificationIntentService.EXTRAS_TYPE, type);
putExtra(TaskNotificationIntentService.EXTRAS_TITLE, taskTitle);
putExtra(TaskNotificationIntentService.EXTRAS_TEXT, text);
putExtra(TaskNotificationIntentService.EXTRAS_RING_TIMES, ringTimes);
}});
return true;
}
}

@ -6,8 +6,7 @@ import com.todoroo.astrid.gcal.CalendarAlarmReceiver;
import com.todoroo.astrid.gcal.CalendarStartupReceiver;
import com.todoroo.astrid.gcal.GCalTaskCompleteListener;
import com.todoroo.astrid.gtasks.GtasksCustomFilterCriteriaExposer;
import com.todoroo.astrid.reminders.Notifications;
import com.todoroo.astrid.reminders.ShowNotificationReceiver;
import com.todoroo.astrid.reminders.NotificationReceiver;
import com.todoroo.astrid.repeats.RepeatTaskCompleteListener;
import com.todoroo.astrid.tags.TagCustomFilterCriteriaExposer;
import com.todoroo.astrid.timers.TimerTaskCompleteListener;
@ -26,7 +25,7 @@ import dagger.Module;
@Module(addsTo = TasksModule.class,
injects = {
TasksWidget.class,
Notifications.class,
NotificationReceiver.class,
GtasksCustomFilterCriteriaExposer.class,
TagCustomFilterCriteriaExposer.class,
GCalTaskCompleteListener.class,
@ -34,7 +33,6 @@ import dagger.Module;
RepeatTaskCompleteListener.class,
AlarmTaskRepeatListener.class,
PhoneStateChangedReceiver.class,
ShowNotificationReceiver.class,
CalendarAlarmReceiver.class,
CalendarStartupReceiver.class,
BootCompletedReceiver.class,

@ -1,5 +1,7 @@
package org.tasks.injection;
import com.todoroo.astrid.reminders.TaskNotificationIntentService;
import org.tasks.location.GeofenceTransitionsIntentService;
import org.tasks.scheduling.*;
@ -13,7 +15,8 @@ import dagger.Module;
MidnightRefreshService.class,
RefreshSchedulerIntentService.class,
ReminderSchedulerIntentService.class,
GeofenceTransitionsIntentService.class
GeofenceTransitionsIntentService.class,
TaskNotificationIntentService.class
})
public class IntentServiceModule {
}

Loading…
Cancel
Save