From 08e3bea0d500c17ac30f7b01d4a441347f4ef5fa Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Mon, 3 Dec 2018 11:19:41 +0100 Subject: [PATCH] Adds notification channels (for Android 8+). Fixes #468. --- .../transdroid/core/service/AppUpdateJob.java | 1 + .../core/service/AppUpdateJobRunner.java | 17 ++++--- .../core/service/NotificationChannels.java | 45 +++++++++++++++++++ .../core/service/RssCheckerJob.java | 1 + .../core/service/RssCheckerJobRunner.java | 17 ++++--- .../core/service/ServerCheckerJob.java | 1 + .../core/service/ServerCheckerJobRunner.java | 21 ++++----- 7 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/org/transdroid/core/service/NotificationChannels.java diff --git a/app/src/main/java/org/transdroid/core/service/AppUpdateJob.java b/app/src/main/java/org/transdroid/core/service/AppUpdateJob.java index 4ab8a00a..62197eec 100644 --- a/app/src/main/java/org/transdroid/core/service/AppUpdateJob.java +++ b/app/src/main/java/org/transdroid/core/service/AppUpdateJob.java @@ -39,6 +39,7 @@ public class AppUpdateJob extends Job { NavigationHelper_ navigationHelper = NavigationHelper_.getInstance_(context); if (systemSettings.checkForUpdates() && navigationHelper.enableUpdateChecker()) { Log_.getInstance_(context).d(TAG, "Schedule app update checker job"); + NotificationChannels.ensureAppUpdateChannel(context); scheduledJobId = new JobRequest.Builder(AppUpdateJob.TAG) .setPeriodic(TimeUnit.DAYS.toMillis(1)) .setRequiredNetworkType(JobRequest.NetworkType.CONNECTED) diff --git a/app/src/main/java/org/transdroid/core/service/AppUpdateJobRunner.java b/app/src/main/java/org/transdroid/core/service/AppUpdateJobRunner.java index a14758e4..ce41ddb9 100644 --- a/app/src/main/java/org/transdroid/core/service/AppUpdateJobRunner.java +++ b/app/src/main/java/org/transdroid/core/service/AppUpdateJobRunner.java @@ -16,7 +16,6 @@ */ package org.transdroid.core.service; -import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; @@ -24,7 +23,10 @@ import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.Uri; +import android.support.v4.app.NotificationCompat; + import com.evernote.android.job.Job; + import org.androidannotations.annotations.Bean; import org.androidannotations.annotations.EBean; import org.androidannotations.annotations.RootContext; @@ -170,11 +172,16 @@ public class AppUpdateJobRunner { private void newNotification(String ticker, String title, String text, String downloadUrl, int notifyID) { PendingIntent pi = PendingIntent.getActivity(context, notifyID, new Intent(Intent.ACTION_VIEW, Uri.parse(downloadUrl)), PendingIntent.FLAG_UPDATE_CURRENT); - Notification.Builder builder = new Notification.Builder(context).setSmallIcon(R.drawable.ic_stat_notification) - .setTicker(ticker).setContentTitle(title).setContentText(text) + final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannels.CHANNEL_APP_UPDATE) + .setSmallIcon(R.drawable.ic_stat_notification) + .setTicker(ticker) + .setContentTitle(title) + .setContentText(text) .setLights(notificationSettings.getDesiredLedColour(), 600, 1000) - .setSound(notificationSettings.getSound()).setAutoCancel(true).setContentIntent(pi); - notificationManager.notify(notifyID, builder.getNotification()); + .setSound(notificationSettings.getSound()) + .setAutoCancel(true) + .setContentIntent(pi); + notificationManager.notify(notifyID, builder.build()); } } diff --git a/app/src/main/java/org/transdroid/core/service/NotificationChannels.java b/app/src/main/java/org/transdroid/core/service/NotificationChannels.java new file mode 100644 index 00000000..5f19d810 --- /dev/null +++ b/app/src/main/java/org/transdroid/core/service/NotificationChannels.java @@ -0,0 +1,45 @@ +package org.transdroid.core.service; + +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.content.Context; +import android.os.Build; + +import org.transdroid.R; + +class NotificationChannels { + + public static final String CHANNEL_APP_UPDATE = "channel_app_update"; + public static final String CHANNEL_RSS_CHECKER = "channel_rss_checker"; + public static final String CHANNEL_SERVER_CHECKER = "channel_server_checker"; + + static void ensureAppUpdateChannel(final Context context) { + createChannel(context, CHANNEL_APP_UPDATE, R.string.pref_checkupdates, R.string.pref_checkupdates_info); + } + + static void ensureRssCheckerChannel(final Context context) { + createChannel(context, CHANNEL_RSS_CHECKER, R.string.pref_notifications_rss, null); + } + + static void ensureServerCheckerChannel(final Context context) { + createChannel(context, CHANNEL_SERVER_CHECKER, R.string.pref_notifications_torrent, null); + } + + private static void createChannel( + final Context context, + final String channelId, + final int name, + final Integer description) { + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + int importance = NotificationManager.IMPORTANCE_DEFAULT; + final NotificationChannel channel = new NotificationChannel(channelId, context.getString(name), importance); + if (description != null) { + channel.setDescription(context.getString(description)); + } + final NotificationManager notificationManager = context.getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(channel); + } + } + +} diff --git a/app/src/main/java/org/transdroid/core/service/RssCheckerJob.java b/app/src/main/java/org/transdroid/core/service/RssCheckerJob.java index 823b291a..85bf6479 100644 --- a/app/src/main/java/org/transdroid/core/service/RssCheckerJob.java +++ b/app/src/main/java/org/transdroid/core/service/RssCheckerJob.java @@ -40,6 +40,7 @@ public class RssCheckerJob extends Job { NotificationSettings notificationSettings = NotificationSettings_.getInstance_(context); if (notificationSettings.isEnabledForRss()) { Log_.getInstance_(context).d(TAG, "Schedule rss checker job"); + NotificationChannels.ensureRssCheckerChannel(context); scheduledJobId = new JobRequest.Builder(RssCheckerJob.TAG) .setPeriodic(notificationSettings.getInvervalInMilliseconds()) .setRequiredNetworkType(JobRequest.NetworkType.CONNECTED) diff --git a/app/src/main/java/org/transdroid/core/service/RssCheckerJobRunner.java b/app/src/main/java/org/transdroid/core/service/RssCheckerJobRunner.java index 28cfd82d..1ecbea54 100644 --- a/app/src/main/java/org/transdroid/core/service/RssCheckerJobRunner.java +++ b/app/src/main/java/org/transdroid/core/service/RssCheckerJobRunner.java @@ -16,12 +16,14 @@ */ package org.transdroid.core.service; -import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.support.v4.app.NotificationCompat; + import com.evernote.android.job.Job; + import org.androidannotations.annotations.Bean; import org.androidannotations.annotations.EBean; import org.androidannotations.annotations.RootContext; @@ -122,14 +124,19 @@ public class RssCheckerJobRunner { .getActivity(context, 80000, new Intent(context, RssfeedsActivity_.class), PendingIntent.FLAG_UPDATE_CURRENT); String title = context.getResources().getQuantityString(R.plurals.rss_service_new, unread, Integer.toString(unread)); String forString = Collections2.joinString(hasUnread, ", "); - Notification.Builder builder = new Notification.Builder(context).setSmallIcon(R.drawable.ic_stat_notification).setTicker(title) - .setContentTitle(title).setContentText(context.getString(R.string.rss_service_newfor, forString)) + final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannels.CHANNEL_RSS_CHECKER) + .setSmallIcon(R.drawable.ic_stat_notification) + .setTicker(title) + .setContentTitle(title) + .setContentText(context.getString(R.string.rss_service_newfor, forString)) .setNumber(unread).setLights(notificationSettings.getDesiredLedColour(), 600, 1000) - .setSound(notificationSettings.getSound()).setAutoCancel(true).setContentIntent(pi); + .setSound(notificationSettings.getSound()) + .setAutoCancel(true) + .setContentIntent(pi); if (notificationSettings.shouldVibrate()) { builder.setVibrate(notificationSettings.getDefaultVibratePattern()); } - notificationManager.notify(80001, builder.getNotification()); + notificationManager.notify(80001, builder.build()); return Job.Result.SUCCESS; } diff --git a/app/src/main/java/org/transdroid/core/service/ServerCheckerJob.java b/app/src/main/java/org/transdroid/core/service/ServerCheckerJob.java index 746a3bd7..a0f05bba 100644 --- a/app/src/main/java/org/transdroid/core/service/ServerCheckerJob.java +++ b/app/src/main/java/org/transdroid/core/service/ServerCheckerJob.java @@ -35,6 +35,7 @@ public class ServerCheckerJob extends Job { NotificationSettings notificationSettings = NotificationSettings_.getInstance_(context); if (notificationSettings.isEnabledForTorrents()) { Log_.getInstance_(context).d(TAG, "Schedule server checker job"); + NotificationChannels.ensureServerCheckerChannel(context); scheduledJobId = new JobRequest.Builder(ServerCheckerJob.TAG) .setPeriodic(notificationSettings.getInvervalInMilliseconds()) .setRequiredNetworkType(JobRequest.NetworkType.CONNECTED) diff --git a/app/src/main/java/org/transdroid/core/service/ServerCheckerJobRunner.java b/app/src/main/java/org/transdroid/core/service/ServerCheckerJobRunner.java index 9109740d..8573dfae 100644 --- a/app/src/main/java/org/transdroid/core/service/ServerCheckerJobRunner.java +++ b/app/src/main/java/org/transdroid/core/service/ServerCheckerJobRunner.java @@ -16,11 +16,11 @@ */ package org.transdroid.core.service; -import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.support.v4.app.NotificationCompat; import android.text.TextUtils; import com.evernote.android.job.Job; import org.androidannotations.annotations.Bean; @@ -175,18 +175,21 @@ public class ServerCheckerJobRunner { forString = forString.substring(0, forString.length() - 2); // Build the basic notification - Notification.Builder builder = new Notification.Builder(context).setSmallIcon(R.drawable.ic_stat_notification) - .setTicker(title).setContentTitle(title).setContentText(forString) + final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannels.CHANNEL_SERVER_CHECKER) + .setSmallIcon(R.drawable.ic_stat_notification) + .setTicker(title) + .setContentTitle(title).setContentText(forString) .setNumber(affectedTorrents.size()) .setLights(notificationSettings.getDesiredLedColour(), 600, 1000) - .setSound(notificationSettings.getSound()).setAutoCancel(true).setContentIntent(pi); + .setSound(notificationSettings.getSound()) + .setAutoCancel(true) + .setContentIntent(pi); if (notificationSettings.shouldVibrate()) builder.setVibrate(notificationSettings.getDefaultVibratePattern()); // Add at most 5 lines with the affected torrents - Notification notification; if (android.os.Build.VERSION.SDK_INT >= 16) { - Notification.InboxStyle inbox = new Notification.InboxStyle(builder); + final NotificationCompat.InboxStyle inbox = new NotificationCompat.InboxStyle(builder); if (affectedTorrents.size() < 6) { for (Torrent affectedTorrent : affectedTorrents) { inbox.addLine(affectedTorrent.getName()); @@ -197,11 +200,9 @@ public class ServerCheckerJobRunner { } inbox.addLine(context.getString(R.string.status_service_andothers, affectedTorrents.get(5).getName())); } - notification = inbox.build(); - } else { - notification = builder.getNotification(); + builder.setStyle(inbox); } - notificationManager.notify(notifyBase + server.getOrder(), notification); + notificationManager.notify(notifyBase + server.getOrder(), builder.build()); }