diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e595eef8d..d7654c883 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -371,6 +371,11 @@ + + { if (newValue != null) { - boolean enabled = (boolean) newValue; - badger.setEnabled(enabled); - if (enabled) { + if ((boolean) newValue) { showRestartDialog(); + } else { + ShortcutBadger.removeCount(this); } return true; } diff --git a/app/src/main/java/org/tasks/Tasks.java b/app/src/main/java/org/tasks/Tasks.java index f6609573c..617fdace0 100644 --- a/app/src/main/java/org/tasks/Tasks.java +++ b/app/src/main/java/org/tasks/Tasks.java @@ -1,6 +1,9 @@ package org.tasks; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.Intent; +import androidx.core.app.JobIntentService; import com.jakewharton.processphoenix.ProcessPhoenix; import com.jakewharton.threetenabp.AndroidThreeTen; import com.todoroo.astrid.service.Upgrader; @@ -12,10 +15,11 @@ import org.tasks.files.FileHelper; import org.tasks.injection.ApplicationComponent; import org.tasks.injection.ForApplication; import org.tasks.injection.InjectingApplication; +import org.tasks.injection.InjectingJobIntentService; import org.tasks.jobs.WorkManager; import org.tasks.location.GeofenceService; import org.tasks.preferences.Preferences; -import org.tasks.receivers.Badger; +import org.tasks.receivers.RefreshReceiver; import org.tasks.scheduling.CalendarNotificationIntentService; import org.tasks.scheduling.NotificationSchedulerIntentService; import org.tasks.scheduling.RefreshScheduler; @@ -30,10 +34,10 @@ public class Tasks extends InjectingApplication { @Inject FlavorSetup flavorSetup; @Inject BuildSetup buildSetup; @Inject ThemeCache themeCache; - @Inject Badger badger; @Inject WorkManager workManager; @Inject RefreshScheduler refreshScheduler; @Inject GeofenceService geofenceService; + @Inject LocalBroadcastManager localBroadcastManager; @Override public void onCreate() { @@ -53,10 +57,10 @@ public class Tasks extends InjectingApplication { flavorSetup.setup(); - badger.setEnabled(preferences.getBoolean(R.string.p_badges_enabled, true)); - themeCache.getThemeBase(preferences.getInt(R.string.p_theme, 0)).setDefaultNightMode(); + localBroadcastManager.registerRefreshReceiver(new RefreshBroadcastReceiver()); + Completable.fromAction(this::doInBackground).subscribeOn(Schedulers.io()).subscribe(); } @@ -88,4 +92,15 @@ public class Tasks extends InjectingApplication { protected void inject(ApplicationComponent component) { 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); + } + } } diff --git a/app/src/main/java/org/tasks/data/DeletionDao.java b/app/src/main/java/org/tasks/data/DeletionDao.java index 8ff48fed2..cf583b243 100644 --- a/app/src/main/java/org/tasks/data/DeletionDao.java +++ b/app/src/main/java/org/tasks/data/DeletionDao.java @@ -7,19 +7,12 @@ import androidx.room.Dao; import androidx.room.Delete; import androidx.room.Query; import androidx.room.Transaction; -import com.todoroo.astrid.dao.Database; import java.util.ArrayList; import java.util.List; @Dao public abstract class DeletionDao { - private final Database database; - - public DeletionDao(Database database) { - this.database = database; - } - @Query("SELECT _id FROM tasks WHERE deleted > 0") public abstract List getDeleted(); @@ -51,7 +44,6 @@ public abstract class DeletionDao { deleteCaldavTasks(partition); deleteTasks(partition); } - database.onDatabaseUpdated(); } @Query("UPDATE tasks SET modified = :timestamp, deleted = :timestamp WHERE _id IN(:ids)") @@ -62,7 +54,6 @@ public abstract class DeletionDao { for (List partition : partition(ids, 997)) { markDeleted(now, partition); } - database.onDatabaseUpdated(); } @Query("SELECT task FROM google_tasks WHERE deleted = 0 AND list_id = :listId") diff --git a/app/src/main/java/org/tasks/injection/ApplicationModule.java b/app/src/main/java/org/tasks/injection/ApplicationModule.java index eecb22eeb..6f5233bb1 100644 --- a/app/src/main/java/org/tasks/injection/ApplicationModule.java +++ b/app/src/main/java/org/tasks/injection/ApplicationModule.java @@ -6,7 +6,6 @@ import android.content.Context; import androidx.room.Room; import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.TaskDao; -import com.todoroo.astrid.provider.Astrid2TaskProvider; import dagger.Module; import dagger.Provides; import org.tasks.data.AlarmDao; @@ -55,8 +54,7 @@ public class ApplicationModule { return Room.databaseBuilder(context, Database.class, Database.NAME) .allowMainThreadQueries() // TODO: remove me .addMigrations(Migrations.MIGRATIONS) - .build() - .init(() -> Astrid2TaskProvider.notifyDatabaseModification(context)); + .build(); } @Provides diff --git a/app/src/main/java/org/tasks/injection/InjectingJobIntentService.java b/app/src/main/java/org/tasks/injection/InjectingJobIntentService.java index b44c40851..24c62d91c 100644 --- a/app/src/main/java/org/tasks/injection/InjectingJobIntentService.java +++ b/app/src/main/java/org/tasks/injection/InjectingJobIntentService.java @@ -9,7 +9,7 @@ import timber.log.Timber; public abstract class InjectingJobIntentService extends JobIntentService { 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_CALENDAR_NOTIFICATION = 1085; public static final int JOB_ID_TASKER = 1086; diff --git a/app/src/main/java/org/tasks/injection/ServiceComponent.java b/app/src/main/java/org/tasks/injection/ServiceComponent.java index 335349f2a..44923106f 100644 --- a/app/src/main/java/org/tasks/injection/ServiceComponent.java +++ b/app/src/main/java/org/tasks/injection/ServiceComponent.java @@ -4,6 +4,7 @@ import dagger.Subcomponent; import org.tasks.jobs.NotificationService; import org.tasks.locale.receiver.TaskerIntentService; import org.tasks.location.GeofenceTransitionsIntentService; +import org.tasks.receivers.RefreshReceiver; import org.tasks.scheduling.CalendarNotificationIntentService; import org.tasks.scheduling.NotificationSchedulerIntentService; @@ -19,4 +20,6 @@ public interface ServiceComponent { void inject(TaskerIntentService taskerIntentService); void inject(NotificationService notificationService); + + void inject(RefreshReceiver refreshReceiver); } diff --git a/app/src/main/java/org/tasks/receivers/Badger.java b/app/src/main/java/org/tasks/receivers/Badger.java deleted file mode 100644 index 4809da3a6..000000000 --- a/app/src/main/java/org/tasks/receivers/Badger.java +++ /dev/null @@ -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); - } -} diff --git a/app/src/main/java/org/tasks/receivers/RefreshReceiver.java b/app/src/main/java/org/tasks/receivers/RefreshReceiver.java new file mode 100644 index 000000000..2105a65b1 --- /dev/null +++ b/app/src/main/java/org/tasks/receivers/RefreshReceiver.java @@ -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); + } +}