From 73bf3e6fa1dc7bbeae5a9e93762e3031544fcbb4 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Sat, 24 Mar 2018 01:02:25 +0100 Subject: [PATCH] Add channel to notifications. Summary: Oreo requires that each notification has a channel assigned. This patch uses a common channel for all. Individual channels for different notifications (Notifications plugin, pairing notifications, share plugin) could be done, but since in our case notifications are not a critical part of the UX it does not seem necessary to me. Setting the channel on NotificationCompat.Builder requires version 26 of the support library, the implications of this were discussed in D8966. Reviewers: #kde_connect Subscribers: mtijink, albertvaka, #kde_connect Tags: #kde_connect Differential Revision: https://phabricator.kde.org/D9514 --- build.gradle | 10 +++++----- src/org/kde/kdeconnect/Device.java | 5 +++-- .../Helpers/NotificationHelper.java | 19 +++++++++++++++++++ .../Plugins/PingPlugin/PingPlugin.java | 5 +++-- .../ReceiveNotificationsPlugin.java | 6 ++++-- .../NotificationUpdateCallback.java | 5 +++-- .../SharePlugin/ShareNotification.java | 4 ++-- .../Plugins/SharePlugin/SharePlugin.java | 5 +++-- 8 files changed, 42 insertions(+), 17 deletions(-) diff --git a/build.gradle b/build.gradle index 25c59e9e..f0fcfa63 100644 --- a/build.gradle +++ b/build.gradle @@ -12,9 +12,9 @@ apply plugin: 'com.android.application' android { buildToolsVersion '27.0.3' - compileSdkVersion 25 + compileSdkVersion 27 defaultConfig { - minSdkVersion 9 + minSdkVersion 16 targetSdkVersion 25 //multiDexEnabled true //testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner" @@ -71,9 +71,9 @@ dependencies { google() } - implementation 'com.android.support:support-v4:25.4.0' - implementation 'com.android.support:appcompat-v7:25.4.0' - implementation 'com.android.support:design:25.4.0' + implementation 'com.android.support:support-v4:27.1.1' + implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'com.android.support:design:27.1.1' implementation 'com.jakewharton:disklrucache:2.0.2' //For caching album art bitmaps implementation 'org.apache.sshd:sshd-core:0.8.0' //0.9 seems to fail on Android 6 and 1.+ requires java.nio.file, which doesn't exist in Android diff --git a/src/org/kde/kdeconnect/Device.java b/src/org/kde/kdeconnect/Device.java index d5ffc778..76d42777 100644 --- a/src/org/kde/kdeconnect/Device.java +++ b/src/org/kde/kdeconnect/Device.java @@ -404,7 +404,9 @@ public class Device implements BaseLink.PacketReceiver { Resources res = getContext().getResources(); - Notification noti = new NotificationCompat.Builder(getContext()) + final NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE); + + Notification noti = new NotificationCompat.Builder(getContext(), NotificationHelper.getDefaultChannelId(notificationManager)) .setContentTitle(res.getString(R.string.pairing_request_from, getName())) .setContentText(res.getString(R.string.tap_to_answer)) .setContentIntent(pendingIntent) @@ -416,7 +418,6 @@ public class Device implements BaseLink.PacketReceiver { .setDefaults(Notification.DEFAULT_ALL) .build(); - final NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE); NotificationHelper.notifyCompat(notificationManager, notificationId, noti); BackgroundService.addGuiInUseCounter(context); diff --git a/src/org/kde/kdeconnect/Helpers/NotificationHelper.java b/src/org/kde/kdeconnect/Helpers/NotificationHelper.java index 6702b6ac..2bf53ed7 100644 --- a/src/org/kde/kdeconnect/Helpers/NotificationHelper.java +++ b/src/org/kde/kdeconnect/Helpers/NotificationHelper.java @@ -1,10 +1,13 @@ package org.kde.kdeconnect.Helpers; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; public class NotificationHelper { + private static NotificationChannel defaultChannel; + public static void notifyCompat(NotificationManager notificationManager, int notificationId, Notification notification) { try { notificationManager.notify(notificationId, notification); @@ -22,4 +25,20 @@ public class NotificationHelper { //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ } } + + public static String getDefaultChannelId(NotificationManager manager) { + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + if (defaultChannel == null) { + String id = "default"; + CharSequence name = "KDE Connect"; + int importance = NotificationManager.IMPORTANCE_DEFAULT; + defaultChannel = new NotificationChannel(id, name, importance); + manager.createNotificationChannel(defaultChannel); + } + return defaultChannel.getId(); + } + return null; + } + } diff --git a/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java b/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java index b3ebc50c..16d74dce 100644 --- a/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java @@ -79,7 +79,9 @@ public class PingPlugin extends Plugin { id = 42; //A unique id to create only one notification } - Notification noti = new NotificationCompat.Builder(context) + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + Notification noti = new NotificationCompat.Builder(context, NotificationHelper.getDefaultChannelId(notificationManager)) .setContentTitle(device.getName()) .setContentText(message) .setContentIntent(resultPendingIntent) @@ -89,7 +91,6 @@ public class PingPlugin extends Plugin { .setDefaults(Notification.DEFAULT_ALL) .build(); - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationHelper.notifyCompat(notificationManager, id, noti); return true; diff --git a/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java b/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java index 83929e99..cb2ff707 100644 --- a/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java @@ -105,7 +105,10 @@ public class ReceiveNotificationsPlugin extends Plugin { } } } - Notification noti = new NotificationCompat.Builder(context) + + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + Notification noti = new NotificationCompat.Builder(context, NotificationHelper.getDefaultChannelId(notificationManager)) .setContentTitle(np.getString("appName")) .setContentText(np.getString("ticker")) .setContentIntent(resultPendingIntent) @@ -117,7 +120,6 @@ public class ReceiveNotificationsPlugin extends Plugin { .setDefaults(Notification.DEFAULT_ALL) .build(); - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationHelper.notifyCompat(notificationManager, "kdeconnectId:" + np.getString("id", "0"), np.getInt("id", 0), noti); } diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/NotificationUpdateCallback.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/NotificationUpdateCallback.java index a5746025..437b4b85 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/NotificationUpdateCallback.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/NotificationUpdateCallback.java @@ -39,8 +39,9 @@ class NotificationUpdateCallback extends Device.SendPacketStatusCallback { } else { title = res.getString(R.string.outgoing_file_title, device.getName()); } - notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - builder = new NotificationCompat.Builder(context) + + notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); + builder = new NotificationCompat.Builder(context, NotificationHelper.getDefaultChannelId(notificationManager)) .setSmallIcon(android.R.drawable.stat_sys_upload) .setAutoCancel(true) .setProgress(100, 0, false) diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java index 3208ea45..a68c2c5b 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/ShareNotification.java @@ -55,7 +55,7 @@ public class ShareNotification { this.filename = filename; notificationId = (int) System.currentTimeMillis(); notificationManager = (NotificationManager) device.getContext().getSystemService(Context.NOTIFICATION_SERVICE); - builder = new NotificationCompat.Builder(device.getContext()) + builder = new NotificationCompat.Builder(device.getContext(), NotificationHelper.getDefaultChannelId(notificationManager)) .setContentTitle(device.getContext().getResources().getString(R.string.incoming_file_title, device.getName())) .setContentText(device.getContext().getResources().getString(R.string.incoming_file_text, filename)) .setTicker(device.getContext().getResources().getString(R.string.incoming_file_title, device.getName())) @@ -80,7 +80,7 @@ public class ShareNotification { public void setFinished(boolean success) { String message = success ? device.getContext().getResources().getString(R.string.received_file_title, device.getName()) : device.getContext().getResources().getString(R.string.received_file_fail_title, device.getName()); - builder = new NotificationCompat.Builder(device.getContext()); + builder = new NotificationCompat.Builder(device.getContext(), NotificationHelper.getDefaultChannelId(notificationManager)); builder.setContentTitle(message) .setTicker(message) .setSmallIcon(android.R.drawable.stat_sys_download_done) diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java index cb3b66dc..b30d9a2c 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java @@ -159,7 +159,9 @@ public class SharePlugin extends Plugin { PendingIntent.FLAG_UPDATE_CURRENT ); - Notification noti = new NotificationCompat.Builder(context) + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + Notification noti = new NotificationCompat.Builder(context, NotificationHelper.getDefaultChannelId(notificationManager)) .setContentTitle(res.getString(R.string.received_url_title, device.getName())) .setContentText(res.getString(R.string.received_url_text, url)) .setContentIntent(resultPendingIntent) @@ -169,7 +171,6 @@ public class SharePlugin extends Plugin { .setDefaults(Notification.DEFAULT_ALL) .build(); - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationHelper.notifyCompat(notificationManager, (int) System.currentTimeMillis(), noti); } }