Inject AlarmService

pull/189/head
Alex Baker 11 years ago
parent 0fc86a66fb
commit 96f6b92755

@ -23,10 +23,13 @@ import com.todoroo.astrid.ui.DateAndTimeDialog.DateAndTimeDialogListener;
import com.todoroo.astrid.ui.DateAndTimePicker;
import org.tasks.R;
import org.tasks.injection.Injector;
import java.util.Date;
import java.util.LinkedHashSet;
import javax.inject.Inject;
import static org.tasks.date.DateTimeUtils.newDate;
/**
@ -37,19 +40,21 @@ import static org.tasks.date.DateTimeUtils.newDate;
*/
public final class AlarmControlSet extends TaskEditControlSet {
// --- instance variables
@Inject AlarmService alarmService;
private LinearLayout alertsContainer;
private DateAndTimeDialog pickerDialog;
public AlarmControlSet(Activity activity, int layout) {
super(activity, layout);
((Injector) activity.getApplication()).inject(this);
}
@Override
protected void readFromTaskOnInitialize() {
alertsContainer.removeAllViews();
TodorooCursor<Metadata> cursor = AlarmService.getInstance().getAlarms(model.getId());
TodorooCursor<Metadata> cursor = alarmService.getAlarms(model.getId());
try {
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
addAlarm(newDate(cursor.get(AlarmFields.TIME)));
@ -92,7 +97,7 @@ public final class AlarmControlSet extends TaskEditControlSet {
alarms.add(dateValue);
}
if(AlarmService.getInstance().synchronizeAlarms(task.getId(), alarms)) {
if(alarmService.synchronizeAlarms(task.getId(), alarms)) {
task.setModificationDate(DateUtilities.now());
}
}

@ -5,7 +5,6 @@
*/
package com.todoroo.astrid.alarms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.format.DateUtils;
@ -16,8 +15,12 @@ import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.data.Metadata;
import org.tasks.injection.InjectingBroadcastReceiver;
import java.util.Date;
import javax.inject.Inject;
import static org.tasks.date.DateTimeUtils.newDate;
/**
@ -26,10 +29,14 @@ import static org.tasks.date.DateTimeUtils.newDate;
* @author Tim Su <tim@todoroo.com>
*
*/
public class AlarmDetailExposer extends BroadcastReceiver {
public class AlarmDetailExposer extends InjectingBroadcastReceiver {
@Inject AlarmService alarmService;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
ContextManager.setContext(context);
// get tags associated with this task
long taskId = intent.getLongExtra(AstridApiConstants.EXTRAS_TASK_ID, -1);
@ -51,7 +58,7 @@ public class AlarmDetailExposer extends BroadcastReceiver {
}
public String getTaskDetails(Context context, long id) {
TodorooCursor<Metadata> cursor = AlarmService.getInstance().getAlarms(id);
TodorooCursor<Metadata> cursor = alarmService.getAlarms(id);
long nextTime = -1;
try {
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {

@ -18,7 +18,6 @@ import com.todoroo.andlib.sql.Join;
import com.todoroo.andlib.sql.Order;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Metadata;
@ -31,36 +30,35 @@ import com.todoroo.astrid.service.MetadataService.SynchronizeMetadataCallback;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* Provides operations for working with alerts
*
* @author Tim Su <tim@todoroo.com>
*
*/
@Singleton
public class AlarmService {
// --- singleton
private static AlarmService instance = null;
public static synchronized AlarmService getInstance() {
if(instance == null) {
instance = new AlarmService();
}
return instance;
}
// --- data retrieval
public static final String IDENTIFIER = "alarms"; //$NON-NLS-1$
private MetadataService metadataService;
@Inject
public AlarmService(MetadataService metadataService) {
this.metadataService = metadataService;
}
/**
* Return alarms for the given task. PLEASE CLOSE THE CURSOR!
*/
public TodorooCursor<Metadata> getAlarms(long taskId) {
return PluginServices.getMetadataService().query(Query.select(
return metadataService.query(Query.select(
Metadata.PROPERTIES).where(MetadataCriteria.byTaskAndwithKey(
taskId, AlarmFields.METADATA_KEY)).orderBy(Order.asc(AlarmFields.TIME)));
taskId, AlarmFields.METADATA_KEY)).orderBy(Order.asc(AlarmFields.TIME)));
}
/**
@ -68,8 +66,6 @@ public class AlarmService {
* @return true if data was changed
*/
public boolean synchronizeAlarms(final long taskId, LinkedHashSet<Long> alarms) {
MetadataService service = PluginServices.getMetadataService();
ArrayList<Metadata> metadata = new ArrayList<>();
for(Long alarm : alarms) {
Metadata item = new Metadata();
@ -82,7 +78,7 @@ public class AlarmService {
final Context context = ContextManager.getContext();
final AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
boolean changed = service.synchronizeMetadata(taskId, metadata, Metadata.KEY.eq(AlarmFields.METADATA_KEY), new SynchronizeMetadataCallback() {
boolean changed = metadataService.synchronizeMetadata(taskId, metadata, Metadata.KEY.eq(AlarmFields.METADATA_KEY), new SynchronizeMetadataCallback() {
@Override
public void beforeDeleteMetadata(Metadata m) {
// Cancel the alarm before the metadata is deleted
@ -104,7 +100,7 @@ public class AlarmService {
* @return todoroo cursor. PLEASE CLOSE THIS CURSOR!
*/
private TodorooCursor<Metadata> getActiveAlarms() {
return PluginServices.getMetadataService().query(Query.select(Metadata.ID, Metadata.TASK, AlarmFields.TIME).
return metadataService.query(Query.select(Metadata.ID, Metadata.TASK, AlarmFields.TIME).
join(Join.inner(Task.TABLE, Metadata.TASK.eq(Task.ID))).
where(Criterion.and(TaskCriteria.isActive(), MetadataCriteria.withKey(AlarmFields.METADATA_KEY))));
}
@ -114,7 +110,7 @@ public class AlarmService {
* @return todoroo cursor. PLEASE CLOSE THIS CURSOR!
*/
private TodorooCursor<Metadata> getActiveAlarmsForTask(long taskId) {
return PluginServices.getMetadataService().query(Query.select(Metadata.ID, Metadata.TASK, AlarmFields.TIME).
return metadataService.query(Query.select(Metadata.ID, Metadata.TASK, AlarmFields.TIME).
join(Join.inner(Task.TABLE, Metadata.TASK.eq(Task.ID))).
where(Criterion.and(TaskCriteria.isActive(),
MetadataCriteria.byTaskAndwithKey(taskId, AlarmFields.METADATA_KEY))));

@ -5,7 +5,6 @@
*/
package com.todoroo.astrid.alarms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@ -15,12 +14,20 @@ import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.data.Metadata;
import org.tasks.injection.InjectingBroadcastReceiver;
import java.util.LinkedHashSet;
public class AlarmTaskRepeatListener extends BroadcastReceiver {
import javax.inject.Inject;
public class AlarmTaskRepeatListener extends InjectingBroadcastReceiver {
@Inject AlarmService alarmService;
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
ContextManager.setContext(context);
long taskId = intent.getLongExtra(AstridApiConstants.EXTRAS_TASK_ID, -1);
if(taskId == -1) {
@ -36,7 +43,7 @@ public class AlarmTaskRepeatListener extends BroadcastReceiver {
return;
}
TodorooCursor<Metadata> cursor = AlarmService.getInstance().getAlarms(taskId);
TodorooCursor<Metadata> cursor = alarmService.getAlarms(taskId);
try {
if(cursor.getCount() == 0) {
return;
@ -48,7 +55,7 @@ public class AlarmTaskRepeatListener extends BroadcastReceiver {
metadata.readFromCursor(cursor);
alarms.add(metadata.getValue(AlarmFields.TIME) + (newDueDate - oldDueDate));
}
AlarmService.getInstance().synchronizeAlarms(taskId, alarms);
alarmService.synchronizeAlarms(taskId, alarms);
} finally {
cursor.close();

@ -27,6 +27,7 @@ import javax.inject.Inject;
public class ReminderSchedulingService extends InjectingService {
@Inject RefreshScheduler refreshScheduler;
@Inject AlarmService alarmService;
/** Receive the alarm - start the synchronize service! */
@Override
@ -48,7 +49,7 @@ public class ReminderSchedulingService extends InjectingService {
private void scheduleReminders() {
try {
ReminderService.getInstance().scheduleAllAlarms();
AlarmService.getInstance().scheduleAllAlarms();
alarmService.scheduleAllAlarms();
refreshScheduler.scheduleAllAlarms();
} catch (Exception e) {

@ -1,6 +1,7 @@
package org.tasks;
import com.todoroo.astrid.adapter.FilterAdapter;
import com.todoroo.astrid.alarms.AlarmControlSet;
import com.todoroo.astrid.files.FilesControlSet;
import com.todoroo.astrid.service.AstridDependencyInjector;
import com.todoroo.astrid.tags.TagsControlSet;
@ -19,6 +20,7 @@ import dagger.Module;
EditTitleControlSet.class,
FilesControlSet.class,
TagsControlSet.class,
AlarmControlSet.class,
FilterAdapter.class
}
)

Loading…
Cancel
Save