From 05d31f25117bd940cbe032b6921cd4735a59384f Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 24 Jan 2019 23:31:55 -0600 Subject: [PATCH] Call startForeground in onCreate --- app/build.gradle | 4 +- .../com/todoroo/astrid/service/Upgrader.java | 49 ++++++++++++++++++- .../org/tasks/injection/InjectingService.java | 24 ++++++--- .../notifications/NotificationManager.java | 35 +------------ 4 files changed, 67 insertions(+), 45 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c672cb307..2d537b7e0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -19,8 +19,8 @@ android { defaultConfig { testApplicationId "org.tasks.test" applicationId "org.tasks" - versionCode 550 - versionName "6.4.3" + versionCode 552 + versionName "6.4.5" targetSdkVersion 28 minSdkVersion 16 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/java/com/todoroo/astrid/service/Upgrader.java b/app/src/main/java/com/todoroo/astrid/service/Upgrader.java index 3aaf638b4..276ac8351 100644 --- a/app/src/main/java/com/todoroo/astrid/service/Upgrader.java +++ b/app/src/main/java/com/todoroo/astrid/service/Upgrader.java @@ -1,7 +1,16 @@ package com.todoroo.astrid.service; import static com.google.common.base.Strings.isNullOrEmpty; +import static com.todoroo.andlib.utility.AndroidUtilities.atLeastOreo; +import static org.tasks.notifications.NotificationManager.NOTIFICATION_CHANNEL_DEFAULT; +import static org.tasks.notifications.NotificationManager.NOTIFICATION_CHANNEL_MISCELLANEOUS; +import static org.tasks.notifications.NotificationManager.NOTIFICATION_CHANNEL_TASKER; +import static org.tasks.notifications.NotificationManager.NOTIFICATION_CHANNEL_TIMERS; +import android.annotation.TargetApi; +import android.app.NotificationChannel; +import android.content.Context; +import android.os.Build; import android.os.Environment; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; @@ -30,6 +39,7 @@ import org.tasks.data.TaskAttachment; import org.tasks.data.TaskAttachmentDao; import org.tasks.data.UserActivity; import org.tasks.data.UserActivityDao; +import org.tasks.injection.ForApplication; import org.tasks.preferences.DefaultFilterProvider; import org.tasks.preferences.Preferences; @@ -41,7 +51,6 @@ public class Upgrader { private static final int V6_0_beta_1 = 522; private static final int V6_0_beta_2 = 523; private static final int V6_4 = 546; - private final Preferences preferences; private final Tracker tracker; private final TagDataDao tagDataDao; @@ -53,9 +62,11 @@ public class Upgrader { private final GoogleTaskListDao googleTaskListDao; private final UserActivityDao userActivityDao; private final TaskAttachmentDao taskAttachmentDao; + private Context context; @Inject public Upgrader( + @ForApplication Context context, Preferences preferences, Tracker tracker, TagDataDao tagDataDao, @@ -67,6 +78,7 @@ public class Upgrader { GoogleTaskListDao googleTaskListDao, UserActivityDao userActivityDao, TaskAttachmentDao taskAttachmentDao) { + this.context = context; this.preferences = preferences; this.tracker = tracker; this.tagDataDao = tagDataDao; @@ -103,12 +115,47 @@ public class Upgrader { } tracker.reportEvent(Tracking.Events.UPGRADE, Integer.toString(from)); } + createNotificationChannels(); preferences.setCurrentVersion(to); } finally { localBroadcastManager.broadcastRefresh(); } } + private void createNotificationChannels() { + if (atLeastOreo()) { + android.app.NotificationManager notificationManager = + (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.createNotificationChannel( + createNotificationChannel(NOTIFICATION_CHANNEL_DEFAULT, R.string.notifications, true)); + notificationManager.createNotificationChannel( + createNotificationChannel(NOTIFICATION_CHANNEL_TASKER, R.string.tasker_locale, true)); + notificationManager.createNotificationChannel( + createNotificationChannel( + NOTIFICATION_CHANNEL_TIMERS, R.string.TEA_timer_controls, true)); + notificationManager.createNotificationChannel( + createNotificationChannel( + NOTIFICATION_CHANNEL_MISCELLANEOUS, R.string.miscellaneous, false)); + } + } + + @TargetApi(Build.VERSION_CODES.O) + private NotificationChannel createNotificationChannel( + String channelId, int nameResId, boolean alert) { + String channelName = context.getString(nameResId); + int importance = + alert + ? android.app.NotificationManager.IMPORTANCE_HIGH + : android.app.NotificationManager.IMPORTANCE_LOW; + NotificationChannel notificationChannel = + new NotificationChannel(channelId, channelName, importance); + notificationChannel.enableLights(alert); + notificationChannel.enableVibration(alert); + notificationChannel.setBypassDnd(alert); + notificationChannel.setShowBadge(alert); + return notificationChannel; + } + private void performMarshmallowMigration() { try { // preserve pre-marshmallow default backup location diff --git a/app/src/main/java/org/tasks/injection/InjectingService.java b/app/src/main/java/org/tasks/injection/InjectingService.java index 282fc1916..24b612899 100644 --- a/app/src/main/java/org/tasks/injection/InjectingService.java +++ b/app/src/main/java/org/tasks/injection/InjectingService.java @@ -13,19 +13,27 @@ import org.tasks.notifications.NotificationManager; public abstract class InjectingService extends Service { - private CompositeDisposable disposables = new CompositeDisposable(); + private CompositeDisposable disposables; @Override - public int onStartCommand(Intent intent, int flags, int startId) { - inject(((InjectingApplication) getApplication()).getComponent().plus(new ServiceModule())); + public void onCreate() { + super.onCreate(); startForeground(getNotificationId(), buildNotification()); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + startForeground(getNotificationId(), buildNotification()); + + inject(((InjectingApplication) getApplication()).getComponent().plus(new ServiceModule())); - disposables.add( - Completable.fromAction(this::doWork) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(this::stopSelf)); + disposables = + new CompositeDisposable( + Completable.fromAction(this::doWork) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(this::stopSelf)); return Service.START_NOT_STICKY; } diff --git a/app/src/main/java/org/tasks/notifications/NotificationManager.java b/app/src/main/java/org/tasks/notifications/NotificationManager.java index a4355baa6..fea6bb05a 100644 --- a/app/src/main/java/org/tasks/notifications/NotificationManager.java +++ b/app/src/main/java/org/tasks/notifications/NotificationManager.java @@ -7,17 +7,13 @@ import static com.google.common.collect.Iterables.tryFind; import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Lists.transform; import static com.todoroo.andlib.utility.AndroidUtilities.atLeastNougat; -import static com.todoroo.andlib.utility.AndroidUtilities.atLeastOreo; import static com.todoroo.astrid.reminders.ReminderService.TYPE_GEOFENCE_ENTER; import static com.todoroo.astrid.reminders.ReminderService.TYPE_GEOFENCE_EXIT; -import android.annotation.TargetApi; import android.app.Notification; -import android.app.NotificationChannel; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; -import android.os.Build; import android.text.TextUtils; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; @@ -55,7 +51,7 @@ import timber.log.Timber; @ApplicationScope public class NotificationManager { - private static final String NOTIFICATION_CHANNEL_DEFAULT = "notifications"; + public static final String NOTIFICATION_CHANNEL_DEFAULT = "notifications"; public static final String NOTIFICATION_CHANNEL_TASKER = "notifications_tasker"; public static final String NOTIFICATION_CHANNEL_TIMERS = "notifications_timers"; public static final String NOTIFICATION_CHANNEL_MISCELLANEOUS = "notifications_miscellaneous"; @@ -89,35 +85,6 @@ public class NotificationManager { this.checkBoxes = checkBoxes; this.locationDao = locationDao; notificationManagerCompat = NotificationManagerCompat.from(context); - if (atLeastOreo()) { - android.app.NotificationManager notificationManager = - (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.createNotificationChannel( - createNotificationChannel(NOTIFICATION_CHANNEL_DEFAULT, R.string.notifications, true)); - notificationManager.createNotificationChannel( - createNotificationChannel(NOTIFICATION_CHANNEL_TASKER, R.string.tasker_locale, true)); - notificationManager.createNotificationChannel( - createNotificationChannel(NOTIFICATION_CHANNEL_TIMERS, R.string.TEA_timer_controls, true)); - notificationManager.createNotificationChannel( - createNotificationChannel(NOTIFICATION_CHANNEL_MISCELLANEOUS, R.string.miscellaneous, false)); - } - } - - @TargetApi(Build.VERSION_CODES.O) - private NotificationChannel createNotificationChannel( - String channelId, int nameResId, boolean alert) { - String channelName = context.getString(nameResId); - int importance = - alert - ? android.app.NotificationManager.IMPORTANCE_HIGH - : android.app.NotificationManager.IMPORTANCE_LOW; - NotificationChannel notificationChannel = - new NotificationChannel(channelId, channelName, importance); - notificationChannel.enableLights(alert); - notificationChannel.enableVibration(alert); - notificationChannel.setBypassDnd(alert); - notificationChannel.setShowBadge(alert); - return notificationChannel; } public void cancel(long id) {