Convert ShowNotificationReceiver to intent service

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

@ -9,11 +9,11 @@ import org.tasks.preferences.Preferences;
import java.util.concurrent.TimeUnit; 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.freezeAt;
import static org.tasks.Freeze.thaw; import static org.tasks.Freeze.thaw;
public class NotificationsTest extends AndroidTestCase { public class NotificationReceiverTest extends AndroidTestCase {
@SuppressLint("NewApi") @SuppressLint("NewApi")
private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1); private static final int MILLIS_PER_HOUR = (int) TimeUnit.HOURS.toMillis(1);

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

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

@ -233,16 +233,10 @@
<!-- ======================================================= Receivers = --> <!-- ======================================================= 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=".receivers.RefreshReceiver" />
<receiver android:name="com.todoroo.astrid.reminders.ShowNotificationReceiver">
<intent-filter>
<action android:name="org.tasks.IN_APP_NOTIFY" />
</intent-filter>
</receiver>
<!-- widgets --> <!-- widgets -->
<receiver <receiver
android:name="com.todoroo.astrid.widget.TasksWidget" android:name="com.todoroo.astrid.widget.TasksWidget"
@ -549,6 +543,9 @@
<service <service
android:name=".scheduling.ReminderSchedulerIntentService" android:name=".scheduling.ReminderSchedulerIntentService"
android:exported="false" /> android:exported="false" />
<service
android:name="com.todoroo.astrid.reminders.TaskNotificationIntentService"
android:exported="false" />
<!-- Uses Library --> <!-- Uses Library -->
<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.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task; 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.reminders.ReminderService;
import com.todoroo.astrid.service.SynchronizeMetadataCallback; import com.todoroo.astrid.service.SynchronizeMetadataCallback;
@ -130,10 +130,10 @@ public class AlarmService {
} }
private PendingIntent pendingIntentForAlarm(Metadata alarm, long taskId) { 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.setAction("ALARM" + alarm.getId()); //$NON-NLS-1$
intent.putExtra(Notifications.ID_KEY, taskId); intent.putExtra(NotificationReceiver.ID_KEY, taskId);
intent.putExtra(Notifications.EXTRAS_TYPE, ReminderService.TYPE_ALARM); intent.putExtra(NotificationReceiver.EXTRAS_TYPE, ReminderService.TYPE_ALARM);
return PendingIntent.getBroadcast(context, (int)alarm.getId(), return PendingIntent.getBroadcast(context, (int)alarm.getId(),
intent, PendingIntent.FLAG_UPDATE_CURRENT); 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) { if(task.getId() == Task.NO_ID) {
return; return;
} }
Intent intent = new Intent(context, Notifications.class); Intent intent = new Intent(context, NotificationReceiver.class);
intent.setType(Long.toString(task.getId())); intent.setType(Long.toString(task.getId()));
intent.setAction(Integer.toString(type)); intent.setAction(Integer.toString(type));
intent.putExtra(Notifications.ID_KEY, task.getId()); intent.putExtra(NotificationReceiver.ID_KEY, task.getId());
intent.putExtra(Notifications.EXTRAS_TYPE, type); intent.putExtra(NotificationReceiver.EXTRAS_TYPE, type);
// calculate the unique requestCode as a combination of the task-id and alarm-type: // calculate the unique requestCode as a combination of the task-id and alarm-type:
// concatenate id+type to keep the combo unique // concatenate id+type to keep the combo unique

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

@ -1,18 +1,13 @@
package org.tasks; package org.tasks;
import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import com.todoroo.astrid.api.AstridApiConstants; 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.injection.ForApplication;
import org.tasks.receivers.CompleteTaskReceiver; import org.tasks.receivers.CompleteTaskReceiver;
import org.tasks.receivers.FirstLaunchReceiver; import org.tasks.receivers.FirstLaunchReceiver;
import org.tasks.reminders.NotificationActivity;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -20,8 +15,6 @@ import javax.inject.Singleton;
@Singleton @Singleton
public class Broadcaster { public class Broadcaster {
public static final String BROADCAST_IN_APP_NOTIFY = Constants.PACKAGE + ".IN_APP_NOTIFY"; //$NON-NLS-1$
private final Context context; private final Context context;
@Inject @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) { public void taskCompleted(final long id) {
sendOrderedBroadcast(new Intent(AstridApiConstants.BROADCAST_EVENT_TASK_COMPLETED) {{ sendOrderedBroadcast(new Intent(AstridApiConstants.BROADCAST_EVENT_TASK_COMPLETED) {{
putExtra(AstridApiConstants.EXTRAS_TASK_ID, id); putExtra(AstridApiConstants.EXTRAS_TASK_ID, id);

@ -1,9 +1,4 @@
/** package org.tasks;
* Copyright (c) 2012 Todoroo Inc
*
* See the file "LICENSE" for the full license governing this code.
*/
package com.todoroo.astrid.reminders;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
@ -13,66 +8,39 @@ import android.text.TextUtils;
import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.tasks.Broadcaster;
import org.tasks.R;
import org.tasks.injection.ForApplication; import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingBroadcastReceiver;
import org.tasks.notifications.NotificationManager; import org.tasks.notifications.NotificationManager;
import org.tasks.reminders.NotificationActivity; import org.tasks.reminders.NotificationActivity;
import javax.inject.Inject; 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);
/** private Context context;
* task id extra private TaskDao taskDao;
*/ private NotificationManager notificationManager;
public static final String ID_KEY = "id"; //$NON-NLS-1$
/** @Inject
* Action name for broadcast intent notifying that task was created from repeating template public Notifier(@ForApplication Context context, TaskDao taskDao, NotificationManager notificationManager) {
*/ this.context = context;
public static final String EXTRAS_CUSTOM_INTENT = "intent"; //$NON-NLS-1$ this.taskDao = taskDao;
public static final String EXTRAS_NOTIF_ID = "notifId"; //$NON-NLS-1$ this.notificationManager = notificationManager;
/**
* 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);
} }
void handle(Intent intent) { public void triggerTaskNotification(long id, int type) {
long id = intent.getLongExtra(ID_KEY, 0);
int type = intent.getIntExtra(EXTRAS_TYPE, (byte) 0);
if (!showTaskNotification(id, type)) { if (!showTaskNotification(id, type)) {
notificationManager.cancel((int) id); notificationManager.cancel(id);
} }
} }
/** private boolean showTaskNotification(final long id, final int type) {
* 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) {
Task task; Task task;
try { try {
task = taskDao.fetch(id, Task.ID, Task.TITLE, Task.HIDE_UNTIL, Task.COMPLETION_DATE, 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 // read properties
String taskTitle = task.getTitle(); final String taskTitle = task.getTitle();
boolean nonstopMode = task.isNotifyModeNonstop(); boolean nonstopMode = task.isNotifyModeNonstop();
boolean ringFiveMode = task.isNotifyModeFive(); boolean ringFiveMode = task.isNotifyModeFive();
int ringTimes = nonstopMode ? -1 : (ringFiveMode ? 5 : 1); final int ringTimes = nonstopMode ? -1 : (ringFiveMode ? 5 : 1);
// update last reminder time // update last reminder time
task.setReminderLast(DateUtilities.now()); task.setReminderLast(DateUtilities.now());
taskDao.saveExisting(task); taskDao.saveExisting(task);
String text = context.getString(R.string.app_name); final String text = context.getString(R.string.app_name);
PendingIntent intent = createNotificationIntent(id, taskTitle);
broadcaster.requestNotification((int) id, intent, type, taskTitle, text, ringTimes); final Intent intent = new Intent(context, NotificationActivity.class) {{
return true;
}
private PendingIntent createNotificationIntent(final long id, final String taskTitle) {
Intent intent = new Intent(context, NotificationActivity.class) {{
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
setAction("NOTIFY" + id); //$NON-NLS-1$ setAction("NOTIFY" + id); //$NON-NLS-1$
putExtra(NotificationActivity.EXTRA_TASK_ID, id); putExtra(NotificationActivity.EXTRA_TASK_ID, id);
putExtra(NotificationActivity.EXTRA_TITLE, taskTitle); 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.CalendarStartupReceiver;
import com.todoroo.astrid.gcal.GCalTaskCompleteListener; import com.todoroo.astrid.gcal.GCalTaskCompleteListener;
import com.todoroo.astrid.gtasks.GtasksCustomFilterCriteriaExposer; import com.todoroo.astrid.gtasks.GtasksCustomFilterCriteriaExposer;
import com.todoroo.astrid.reminders.Notifications; import com.todoroo.astrid.reminders.NotificationReceiver;
import com.todoroo.astrid.reminders.ShowNotificationReceiver;
import com.todoroo.astrid.repeats.RepeatTaskCompleteListener; import com.todoroo.astrid.repeats.RepeatTaskCompleteListener;
import com.todoroo.astrid.tags.TagCustomFilterCriteriaExposer; import com.todoroo.astrid.tags.TagCustomFilterCriteriaExposer;
import com.todoroo.astrid.timers.TimerTaskCompleteListener; import com.todoroo.astrid.timers.TimerTaskCompleteListener;
@ -26,7 +25,7 @@ import dagger.Module;
@Module(addsTo = TasksModule.class, @Module(addsTo = TasksModule.class,
injects = { injects = {
TasksWidget.class, TasksWidget.class,
Notifications.class, NotificationReceiver.class,
GtasksCustomFilterCriteriaExposer.class, GtasksCustomFilterCriteriaExposer.class,
TagCustomFilterCriteriaExposer.class, TagCustomFilterCriteriaExposer.class,
GCalTaskCompleteListener.class, GCalTaskCompleteListener.class,
@ -34,7 +33,6 @@ import dagger.Module;
RepeatTaskCompleteListener.class, RepeatTaskCompleteListener.class,
AlarmTaskRepeatListener.class, AlarmTaskRepeatListener.class,
PhoneStateChangedReceiver.class, PhoneStateChangedReceiver.class,
ShowNotificationReceiver.class,
CalendarAlarmReceiver.class, CalendarAlarmReceiver.class,
CalendarStartupReceiver.class, CalendarStartupReceiver.class,
BootCompletedReceiver.class, BootCompletedReceiver.class,

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

Loading…
Cancel
Save