From b7260f0d4b479f6a36962476ef6c93ba1102467d Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 12 Mar 2018 14:39:14 +0300 Subject: implement notification channels for sdk 26 --- .../fox/ttrss/offline/OfflineDownloadService.java | 26 +++++++++++++++++++++ .../fox/ttrss/offline/OfflineUploadService.java | 19 +++++++++++++-- .../java/org/fox/ttrss/util/ImageCacheService.java | 27 ++++++++++++++++++++-- 3 files changed, 68 insertions(+), 4 deletions(-) mode change 100644 => 100755 org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java mode change 100644 => 100755 org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineUploadService.java mode change 100644 => 100755 org.fox.ttrss/src/main/java/org/fox/ttrss/util/ImageCacheService.java (limited to 'org.fox.ttrss/src/main/java') diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java old mode 100644 new mode 100755 index 6ab16bda..9c4eb9ac --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java @@ -3,6 +3,7 @@ package org.fox.ttrss.offline; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -45,6 +46,8 @@ import java.util.List; public class OfflineDownloadService extends Service { private final String TAG = this.getClass().getSimpleName(); + private final String NOTIFICATION_CHANNEL_NORMAL = TAG + ":Normal"; + private final String NOTIFICATION_CHANNEL_PRIORITY = TAG + ":Priority"; // enable downloading read articles in debug configuration for testing private static boolean OFFLINE_DEBUG_READ = false; @@ -92,6 +95,21 @@ public class OfflineDownloadService extends Service { public void onCreate() { super.onCreate(); m_nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_PRIORITY, NOTIFICATION_CHANNEL_PRIORITY, + NotificationManager.IMPORTANCE_HIGH); + channel.setShowBadge(false); + channel.setSound(null, null); + m_nmgr.createNotificationChannel(channel); + + channel = new NotificationChannel(NOTIFICATION_CHANNEL_NORMAL, NOTIFICATION_CHANNEL_NORMAL, + NotificationManager.IMPORTANCE_DEFAULT); + channel.setShowBadge(false); + channel.setSound(null, null); + m_nmgr.createNotificationChannel(channel); + } + m_prefs = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); @@ -136,6 +154,10 @@ public class OfflineDownloadService extends Service { .addAction(R.drawable.ic_launcher, getString(R.string.cancel), cancelIntent); } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + builder.setChannelId(NOTIFICATION_CHANNEL_NORMAL); + } + m_nmgr.notify(NOTIFY_DOWNLOADING, builder.build()); } @@ -180,6 +202,10 @@ public class OfflineDownloadService extends Service { .setGroup("org.fox.ttrss"); } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + builder.setChannelId(NOTIFICATION_CHANNEL_PRIORITY); + } + m_nmgr.notify(NOTIFY_DOWNLOAD_SUCCESS, builder.build()); } diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineUploadService.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineUploadService.java old mode 100644 new mode 100755 index cba0914c..55df9063 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineUploadService.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineUploadService.java @@ -2,6 +2,7 @@ package org.fox.ttrss.offline; import android.app.IntentService; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; @@ -27,10 +28,11 @@ import java.util.List; public class OfflineUploadService extends IntentService { private final String TAG = this.getClass().getSimpleName(); + private final String NOTIFICATION_CHANNEL_ID = TAG; public static final int NOTIFY_UPLOADING = 2; public static final String INTENT_ACTION_SUCCESS = "org.fox.ttrss.intent.action.UploadComplete"; - + private String m_sessionId; private NotificationManager m_nmgr; private boolean m_uploadInProgress = false; @@ -45,6 +47,15 @@ public class OfflineUploadService extends IntentService { public void onCreate() { super.onCreate(); m_nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, TAG, + NotificationManager.IMPORTANCE_DEFAULT); + channel.setShowBadge(false); + channel.setSound(null, null); + m_nmgr.createNotificationChannel(channel); + } + initDatabase(); } @@ -82,7 +93,11 @@ public class OfflineUploadService extends IntentService { .addAction(R.drawable.ic_launcher, getString(R.string.offline_sync_try_again), contentIntent); } - m_nmgr.notify(NOTIFY_UPLOADING, builder.build()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + builder.setChannelId(NOTIFICATION_CHANNEL_ID); + } + + m_nmgr.notify(NOTIFY_UPLOADING, builder.build()); } private void updateNotification(int msgResId, int progress, int max, boolean showProgress, boolean isError) { diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/util/ImageCacheService.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/util/ImageCacheService.java old mode 100644 new mode 100755 index 3b2f4302..955df21e --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/util/ImageCacheService.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/util/ImageCacheService.java @@ -4,17 +4,16 @@ import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.app.IntentService; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.SharedPreferences; import android.graphics.BitmapFactory; import android.os.Build; import android.os.Environment; -import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.util.Log; @@ -36,6 +35,8 @@ public class ImageCacheService extends IntentService { @SuppressWarnings("unused") private final String TAG = this.getClass().getSimpleName(); + private final String NOTIFICATION_CHANNEL_NORMAL = TAG + ":Normal"; + private final String NOTIFICATION_CHANNEL_PRIORITY = TAG + ":Priority"; public static final int NOTIFY_DOWNLOADING = 1; public static final int NOTIFY_DOWNLOAD_SUCCESS = 2; @@ -71,6 +72,20 @@ public class ImageCacheService extends IntentService { super.onCreate(); m_nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_PRIORITY, NOTIFICATION_CHANNEL_PRIORITY, + NotificationManager.IMPORTANCE_HIGH); + channel.setShowBadge(false); + channel.setSound(null, null); + m_nmgr.createNotificationChannel(channel); + + channel = new NotificationChannel(NOTIFICATION_CHANNEL_NORMAL, NOTIFICATION_CHANNEL_NORMAL, + NotificationManager.IMPORTANCE_DEFAULT); + channel.setShowBadge(false); + channel.setSound(null, null); + m_nmgr.createNotificationChannel(channel); + } + m_receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -186,6 +201,10 @@ public class ImageCacheService extends IntentService { .setGroup("org.fox.ttrss"); } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + builder.setChannelId(NOTIFICATION_CHANNEL_PRIORITY); + } + m_nmgr.notify(NOTIFY_DOWNLOAD_SUCCESS, builder.build()); } @@ -224,6 +243,10 @@ public class ImageCacheService extends IntentService { .addAction(R.drawable.ic_launcher, getString(R.string.cancel), cancelIntent); } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + builder.setChannelId(NOTIFICATION_CHANNEL_NORMAL); + } + m_nmgr.notify(NOTIFY_DOWNLOADING, builder.build()); } -- cgit v1.2.3