mirror of https://github.com/tasks/tasks
Use JobIntentService to update badges
parent
9345322168
commit
64999b0fb2
@ -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…
Reference in New Issue