Use JobIntentService to update badges

pull/795/head
Alex Baker 6 years ago
parent 9345322168
commit 64999b0fb2

@ -371,6 +371,11 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<service
android:exported="false"
android:name=".receivers.RefreshReceiver"
android:permission="android.permission.BIND_JOB_SERVICE"/>
<service <service
android:exported="false" android:exported="false"
android:name=".scheduling.NotificationSchedulerIntentService" android:name=".scheduling.NotificationSchedulerIntentService"

@ -60,7 +60,6 @@ import org.tasks.notifications.NotificationDao;
public abstract class Database extends RoomDatabase { public abstract class Database extends RoomDatabase {
public static final String NAME = "database"; public static final String NAME = "database";
private Runnable onDatabaseUpdated;
public abstract NotificationDao notificationDao(); public abstract NotificationDao notificationDao();
@ -90,23 +89,10 @@ public abstract class Database extends RoomDatabase {
public abstract DeletionDao getDeletionDao(); public abstract DeletionDao getDeletionDao();
// --- implementation
public String getName() { public String getName() {
return NAME; return NAME;
} }
public Database init(Runnable onDatabaseUpdated) {
this.onDatabaseUpdated = onDatabaseUpdated;
return this;
}
public void onDatabaseUpdated() {
if (onDatabaseUpdated != null) {
onDatabaseUpdated.run();
}
}
/** @return human-readable database name for debugging */ /** @return human-readable database name for debugging */
@Override @Override
public String toString() { public String toString() {

@ -163,12 +163,7 @@ public abstract class TaskDao {
if (!item.insignificantChange(original)) { if (!item.insignificantChange(original)) {
item.setModificationDate(now()); item.setModificationDate(now());
} }
int updated = update(item); return update(item) == 1;
if (updated == 1) {
database.onDatabaseUpdated();
return true;
}
return false;
} }
@Query( @Query(

@ -29,7 +29,7 @@ import org.tasks.activities.TimePickerActivity;
import org.tasks.injection.ActivityComponent; import org.tasks.injection.ActivityComponent;
import org.tasks.injection.InjectingPreferenceActivity; import org.tasks.injection.InjectingPreferenceActivity;
import org.tasks.preferences.DefaultFilterProvider; import org.tasks.preferences.DefaultFilterProvider;
import org.tasks.receivers.Badger; import org.tasks.receivers.ShortcutBadger;
import org.tasks.scheduling.NotificationSchedulerIntentService; import org.tasks.scheduling.NotificationSchedulerIntentService;
import org.tasks.time.DateTime; import org.tasks.time.DateTime;
import org.tasks.ui.TimePreference; import org.tasks.ui.TimePreference;
@ -41,7 +41,6 @@ public class ReminderPreferences extends InjectingPreferenceActivity {
private static final int REQUEST_DEFAULT_REMIND = 10003; private static final int REQUEST_DEFAULT_REMIND = 10003;
private static final int REQUEST_BADGE_LIST = 10004; private static final int REQUEST_BADGE_LIST = 10004;
@Inject Badger badger;
@Inject DefaultFilterProvider defaultFilterProvider; @Inject DefaultFilterProvider defaultFilterProvider;
@Inject LocalBroadcastManager localBroadcastManager; @Inject LocalBroadcastManager localBroadcastManager;
@ -79,10 +78,10 @@ public class ReminderPreferences extends InjectingPreferenceActivity {
.setOnPreferenceChangeListener( .setOnPreferenceChangeListener(
(preference, newValue) -> { (preference, newValue) -> {
if (newValue != null) { if (newValue != null) {
boolean enabled = (boolean) newValue; if ((boolean) newValue) {
badger.setEnabled(enabled);
if (enabled) {
showRestartDialog(); showRestartDialog();
} else {
ShortcutBadger.removeCount(this);
} }
return true; return true;
} }

@ -1,6 +1,9 @@
package org.tasks; package org.tasks;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import androidx.core.app.JobIntentService;
import com.jakewharton.processphoenix.ProcessPhoenix; import com.jakewharton.processphoenix.ProcessPhoenix;
import com.jakewharton.threetenabp.AndroidThreeTen; import com.jakewharton.threetenabp.AndroidThreeTen;
import com.todoroo.astrid.service.Upgrader; import com.todoroo.astrid.service.Upgrader;
@ -12,10 +15,11 @@ import org.tasks.files.FileHelper;
import org.tasks.injection.ApplicationComponent; import org.tasks.injection.ApplicationComponent;
import org.tasks.injection.ForApplication; import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingApplication; import org.tasks.injection.InjectingApplication;
import org.tasks.injection.InjectingJobIntentService;
import org.tasks.jobs.WorkManager; import org.tasks.jobs.WorkManager;
import org.tasks.location.GeofenceService; import org.tasks.location.GeofenceService;
import org.tasks.preferences.Preferences; import org.tasks.preferences.Preferences;
import org.tasks.receivers.Badger; import org.tasks.receivers.RefreshReceiver;
import org.tasks.scheduling.CalendarNotificationIntentService; import org.tasks.scheduling.CalendarNotificationIntentService;
import org.tasks.scheduling.NotificationSchedulerIntentService; import org.tasks.scheduling.NotificationSchedulerIntentService;
import org.tasks.scheduling.RefreshScheduler; import org.tasks.scheduling.RefreshScheduler;
@ -30,10 +34,10 @@ public class Tasks extends InjectingApplication {
@Inject FlavorSetup flavorSetup; @Inject FlavorSetup flavorSetup;
@Inject BuildSetup buildSetup; @Inject BuildSetup buildSetup;
@Inject ThemeCache themeCache; @Inject ThemeCache themeCache;
@Inject Badger badger;
@Inject WorkManager workManager; @Inject WorkManager workManager;
@Inject RefreshScheduler refreshScheduler; @Inject RefreshScheduler refreshScheduler;
@Inject GeofenceService geofenceService; @Inject GeofenceService geofenceService;
@Inject LocalBroadcastManager localBroadcastManager;
@Override @Override
public void onCreate() { public void onCreate() {
@ -53,10 +57,10 @@ public class Tasks extends InjectingApplication {
flavorSetup.setup(); flavorSetup.setup();
badger.setEnabled(preferences.getBoolean(R.string.p_badges_enabled, true));
themeCache.getThemeBase(preferences.getInt(R.string.p_theme, 0)).setDefaultNightMode(); themeCache.getThemeBase(preferences.getInt(R.string.p_theme, 0)).setDefaultNightMode();
localBroadcastManager.registerRefreshReceiver(new RefreshBroadcastReceiver());
Completable.fromAction(this::doInBackground).subscribeOn(Schedulers.io()).subscribe(); Completable.fromAction(this::doInBackground).subscribeOn(Schedulers.io()).subscribe();
} }
@ -88,4 +92,15 @@ public class Tasks extends InjectingApplication {
protected void inject(ApplicationComponent component) { protected void inject(ApplicationComponent component) {
component.inject(this); component.inject(this);
} }
private class RefreshBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
JobIntentService.enqueueWork(
context,
RefreshReceiver.class,
InjectingJobIntentService.JOB_ID_REFRESH_RECEIVER,
intent);
}
}
} }

@ -7,19 +7,12 @@ import androidx.room.Dao;
import androidx.room.Delete; import androidx.room.Delete;
import androidx.room.Query; import androidx.room.Query;
import androidx.room.Transaction; import androidx.room.Transaction;
import com.todoroo.astrid.dao.Database;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Dao @Dao
public abstract class DeletionDao { public abstract class DeletionDao {
private final Database database;
public DeletionDao(Database database) {
this.database = database;
}
@Query("SELECT _id FROM tasks WHERE deleted > 0") @Query("SELECT _id FROM tasks WHERE deleted > 0")
public abstract List<Long> getDeleted(); public abstract List<Long> getDeleted();
@ -51,7 +44,6 @@ public abstract class DeletionDao {
deleteCaldavTasks(partition); deleteCaldavTasks(partition);
deleteTasks(partition); deleteTasks(partition);
} }
database.onDatabaseUpdated();
} }
@Query("UPDATE tasks SET modified = :timestamp, deleted = :timestamp WHERE _id IN(:ids)") @Query("UPDATE tasks SET modified = :timestamp, deleted = :timestamp WHERE _id IN(:ids)")
@ -62,7 +54,6 @@ public abstract class DeletionDao {
for (List<Long> partition : partition(ids, 997)) { for (List<Long> partition : partition(ids, 997)) {
markDeleted(now, partition); markDeleted(now, partition);
} }
database.onDatabaseUpdated();
} }
@Query("SELECT task FROM google_tasks WHERE deleted = 0 AND list_id = :listId") @Query("SELECT task FROM google_tasks WHERE deleted = 0 AND list_id = :listId")

@ -6,7 +6,6 @@ import android.content.Context;
import androidx.room.Room; import androidx.room.Room;
import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.provider.Astrid2TaskProvider;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import org.tasks.data.AlarmDao; import org.tasks.data.AlarmDao;
@ -55,8 +54,7 @@ public class ApplicationModule {
return Room.databaseBuilder(context, Database.class, Database.NAME) return Room.databaseBuilder(context, Database.class, Database.NAME)
.allowMainThreadQueries() // TODO: remove me .allowMainThreadQueries() // TODO: remove me
.addMigrations(Migrations.MIGRATIONS) .addMigrations(Migrations.MIGRATIONS)
.build() .build();
.init(() -> Astrid2TaskProvider.notifyDatabaseModification(context));
} }
@Provides @Provides

@ -9,7 +9,7 @@ import timber.log.Timber;
public abstract class InjectingJobIntentService extends JobIntentService { public abstract class InjectingJobIntentService extends JobIntentService {
public static final int JOB_ID_GEOFENCE_TRANSITION = 1081; public static final int JOB_ID_GEOFENCE_TRANSITION = 1081;
public static final int JOB_ID_GEOFENCE_SCHEDULING = 1082; public static final int JOB_ID_REFRESH_RECEIVER = 1082;
public static final int JOB_ID_NOTIFICATION_SCHEDULER = 1084; public static final int JOB_ID_NOTIFICATION_SCHEDULER = 1084;
public static final int JOB_ID_CALENDAR_NOTIFICATION = 1085; public static final int JOB_ID_CALENDAR_NOTIFICATION = 1085;
public static final int JOB_ID_TASKER = 1086; public static final int JOB_ID_TASKER = 1086;

@ -4,6 +4,7 @@ import dagger.Subcomponent;
import org.tasks.jobs.NotificationService; import org.tasks.jobs.NotificationService;
import org.tasks.locale.receiver.TaskerIntentService; import org.tasks.locale.receiver.TaskerIntentService;
import org.tasks.location.GeofenceTransitionsIntentService; import org.tasks.location.GeofenceTransitionsIntentService;
import org.tasks.receivers.RefreshReceiver;
import org.tasks.scheduling.CalendarNotificationIntentService; import org.tasks.scheduling.CalendarNotificationIntentService;
import org.tasks.scheduling.NotificationSchedulerIntentService; import org.tasks.scheduling.NotificationSchedulerIntentService;
@ -19,4 +20,6 @@ public interface ServiceComponent {
void inject(TaskerIntentService taskerIntentService); void inject(TaskerIntentService taskerIntentService);
void inject(NotificationService notificationService); void inject(NotificationService notificationService);
void inject(RefreshReceiver refreshReceiver);
} }

@ -1,63 +0,0 @@
package org.tasks.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.dao.TaskDao;
import javax.inject.Inject;
import org.tasks.LocalBroadcastManager;
import org.tasks.injection.ApplicationScope;
import org.tasks.injection.ForApplication;
import org.tasks.preferences.DefaultFilterProvider;
import timber.log.Timber;
@ApplicationScope
public class Badger {
private final Context context;
private final DefaultFilterProvider defaultFilterProvider;
private final TaskDao taskDao;
private final LocalBroadcastManager localBroadcastManager;
private final BroadcastReceiver receiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
publishCount();
}
};
private boolean enabled;
@Inject
public Badger(
@ForApplication Context context,
DefaultFilterProvider defaultFilterProvider,
TaskDao taskDao,
LocalBroadcastManager localBroadcastManager) {
this.context = context;
this.defaultFilterProvider = defaultFilterProvider;
this.taskDao = taskDao;
this.localBroadcastManager = localBroadcastManager;
}
public void setEnabled(boolean newValue) {
try {
if (newValue) {
localBroadcastManager.registerRefreshReceiver(receiver);
publishCount();
} else if (enabled) {
localBroadcastManager.unregisterReceiver(receiver);
ShortcutBadger.removeCount(context);
}
enabled = newValue;
} catch (Exception e) {
Timber.e(e);
}
}
private void publishCount() {
Filter badgeFilter = defaultFilterProvider.getBadgeFilter();
int count = taskDao.count(badgeFilter);
ShortcutBadger.applyCount(context, count);
}
}

@ -0,0 +1,38 @@
package org.tasks.receivers;
import android.content.Context;
import android.content.Intent;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.provider.Astrid2TaskProvider;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingJobIntentService;
import org.tasks.injection.ServiceComponent;
import org.tasks.preferences.DefaultFilterProvider;
import org.tasks.preferences.Preferences;
public class RefreshReceiver extends InjectingJobIntentService {
@Inject @ForApplication Context context;
@Inject DefaultFilterProvider defaultFilterProvider;
@Inject TaskDao taskDao;
@Inject Preferences preferences;
@Override
protected void doWork(@Nonnull Intent intent) {
if (preferences.getBoolean(R.string.p_badges_enabled, true)) {
Filter badgeFilter = defaultFilterProvider.getBadgeFilter();
ShortcutBadger.applyCount(context, taskDao.count(badgeFilter));
}
Astrid2TaskProvider.notifyDatabaseModification(context);
}
@Override
protected void inject(ServiceComponent component) {
component.inject(this);
}
}
Loading…
Cancel
Save