mirror of https://github.com/tasks/tasks
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.6 KiB
Java
83 lines
2.6 KiB
Java
![]()
7 years ago
|
package org.tasks.jobs;
|
||
|
|
||
![]()
7 years ago
|
import android.app.PendingIntent;
|
||
![]()
7 years ago
|
import android.content.Context;
|
||
![]()
7 years ago
|
import android.content.Intent;
|
||
![]()
7 years ago
|
|
||
|
import org.tasks.injection.ApplicationScope;
|
||
|
import org.tasks.injection.ForApplication;
|
||
![]()
7 years ago
|
import org.tasks.scheduling.AlarmManager;
|
||
![]()
7 years ago
|
|
||
|
import javax.inject.Inject;
|
||
|
|
||
![]()
7 years ago
|
import timber.log.Timber;
|
||
|
|
||
![]()
7 years ago
|
import static org.tasks.time.DateTimeUtils.currentTimeMillis;
|
||
|
import static org.tasks.time.DateTimeUtils.nextMidnight;
|
||
![]()
7 years ago
|
import static org.tasks.time.DateTimeUtils.printTimestamp;
|
||
![]()
7 years ago
|
|
||
|
@ApplicationScope
|
||
|
public class JobManager {
|
||
|
|
||
![]()
7 years ago
|
private Context context;
|
||
|
private AlarmManager alarmManager;
|
||
![]()
7 years ago
|
|
||
|
@Inject
|
||
![]()
7 years ago
|
public JobManager(@ForApplication Context context, AlarmManager alarmManager) {
|
||
|
this.context = context;
|
||
|
this.alarmManager = alarmManager;
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
public void schedule(String tag, long time) {
|
||
|
Timber.d("%s: %s", tag, printTimestamp(time));
|
||
|
alarmManager.wakeup(adjust(time), getPendingIntent(tag));
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
public void scheduleRefresh(long time) {
|
||
|
Timber.d("%s: %s", RefreshJob.TAG, printTimestamp(time));
|
||
![]()
7 years ago
|
alarmManager.noWakeup(adjust(time), getPendingService(RefreshJob.class));
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
public void scheduleMidnightRefresh() {
|
||
|
long time = nextMidnight();
|
||
|
Timber.d("%s: %s", MidnightRefreshJob.TAG, printTimestamp(time));
|
||
![]()
7 years ago
|
alarmManager.noWakeup(adjust(time), getPendingService(MidnightRefreshJob.class));
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
public void scheduleMidnightBackup() {
|
||
|
long time = nextMidnight();
|
||
|
Timber.d("%s: %s", BackupJob.TAG, printTimestamp(time));
|
||
![]()
7 years ago
|
alarmManager.noWakeup(adjust(time), getPendingService(BackupJob.class));
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
public void cancel(String tag) {
|
||
|
Timber.d("CXL %s", tag);
|
||
|
alarmManager.cancel(getPendingIntent(tag));
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
private long adjust(long time) {
|
||
|
return Math.max(time, currentTimeMillis() + 5000);
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
private PendingIntent getPendingIntent(String tag) {
|
||
|
switch (tag) {
|
||
|
case ReminderJob.TAG:
|
||
![]()
7 years ago
|
return getPendingBroadcast(ReminderJobBroadcast.class);
|
||
![]()
7 years ago
|
case AlarmJob.TAG:
|
||
![]()
7 years ago
|
return getPendingBroadcast(AlarmJobBroadcast.class);
|
||
![]()
7 years ago
|
case RefreshJob.TAG:
|
||
![]()
7 years ago
|
return getPendingService(RefreshJob.class);
|
||
![]()
7 years ago
|
default:
|
||
|
throw new RuntimeException("Unexpected tag: " + tag);
|
||
|
}
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
private <T> PendingIntent getPendingBroadcast(Class<T> c) {
|
||
|
return PendingIntent.getBroadcast(context, 0, new Intent(context, c), 0);
|
||
|
}
|
||
|
|
||
|
private <T> PendingIntent getPendingService(Class<T> c) {
|
||
![]()
7 years ago
|
return PendingIntent.getService(context, 0, new Intent(context, c), 0);
|
||
![]()
7 years ago
|
}
|
||
|
}
|