From 2521bc648a46c2a07d640424a3c798b0301299d0 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 19 Feb 2019 16:43:29 -0600 Subject: [PATCH] Create notification channels on every launch App crashes if user upgrades from Android 7 to 8 --- .../com/todoroo/astrid/service/Upgrader.java | 48 ------------------- .../NotificationSchedulerIntentService.java | 48 +++++++++++++++++++ 2 files changed, 48 insertions(+), 48 deletions(-) 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 10dba6feb..13a844cec 100644 --- a/app/src/main/java/com/todoroo/astrid/service/Upgrader.java +++ b/app/src/main/java/com/todoroo/astrid/service/Upgrader.java @@ -1,16 +1,7 @@ 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; @@ -38,7 +29,6 @@ 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; @@ -60,11 +50,9 @@ 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, @@ -75,7 +63,6 @@ public class Upgrader { GoogleTaskListDao googleTaskListDao, UserActivityDao userActivityDao, TaskAttachmentDao taskAttachmentDao) { - this.context = context; this.preferences = preferences; this.tracker = tracker; this.tagDataDao = tagDataDao; @@ -110,44 +97,9 @@ public class Upgrader { } tracker.reportEvent(Tracking.Events.UPGRADE, Integer.toString(from)); } - createNotificationChannels(); preferences.setCurrentVersion(to); } - 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/scheduling/NotificationSchedulerIntentService.java b/app/src/main/java/org/tasks/scheduling/NotificationSchedulerIntentService.java index c5ee9b5bd..7e26e2d12 100644 --- a/app/src/main/java/org/tasks/scheduling/NotificationSchedulerIntentService.java +++ b/app/src/main/java/org/tasks/scheduling/NotificationSchedulerIntentService.java @@ -1,11 +1,22 @@ package org.tasks.scheduling; +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.content.Intent; +import android.os.Build; import androidx.core.app.JobIntentService; import com.todoroo.astrid.alarms.AlarmService; import com.todoroo.astrid.reminders.ReminderService; 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.jobs.NotificationQueue; @@ -16,6 +27,7 @@ public class NotificationSchedulerIntentService extends InjectingJobIntentServic private static final String EXTRA_CANCEL_EXISTING_NOTIFICATIONS = "extra_cancel_existing_notifications"; + @Inject @ForApplication Context context; @Inject AlarmService alarmService; @Inject ReminderService reminderService; @Inject NotificationQueue notificationQueue; @@ -35,6 +47,8 @@ public class NotificationSchedulerIntentService extends InjectingJobIntentServic protected void doWork(Intent intent) { Timber.d("onHandleWork(%s)", intent); + createNotificationChannels(); + notificationQueue.clear(); boolean cancelExistingNotifications = @@ -45,6 +59,40 @@ public class NotificationSchedulerIntentService extends InjectingJobIntentServic alarmService.scheduleAllAlarms(); } + 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; + } + @Override protected void inject(ServiceComponent component) { component.inject(this);