2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-22 18:07:55 +00:00

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
This commit is contained in:
Nicolas Fella 2018-03-24 01:02:25 +01:00 committed by Albert Vaca
parent 050081e4c8
commit 73bf3e6fa1
8 changed files with 42 additions and 17 deletions

View File

@ -12,9 +12,9 @@ apply plugin: 'com.android.application'
android { android {
buildToolsVersion '27.0.3' buildToolsVersion '27.0.3'
compileSdkVersion 25 compileSdkVersion 27
defaultConfig { defaultConfig {
minSdkVersion 9 minSdkVersion 16
targetSdkVersion 25 targetSdkVersion 25
//multiDexEnabled true //multiDexEnabled true
//testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner" //testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
@ -71,9 +71,9 @@ dependencies {
google() google()
} }
implementation 'com.android.support:support-v4:25.4.0' implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:appcompat-v7:25.4.0' implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:25.4.0' implementation 'com.android.support:design:27.1.1'
implementation 'com.jakewharton:disklrucache:2.0.2' //For caching album art bitmaps 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 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

View File

@ -404,7 +404,9 @@ public class Device implements BaseLink.PacketReceiver {
Resources res = getContext().getResources(); 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())) .setContentTitle(res.getString(R.string.pairing_request_from, getName()))
.setContentText(res.getString(R.string.tap_to_answer)) .setContentText(res.getString(R.string.tap_to_answer))
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
@ -416,7 +418,6 @@ public class Device implements BaseLink.PacketReceiver {
.setDefaults(Notification.DEFAULT_ALL) .setDefaults(Notification.DEFAULT_ALL)
.build(); .build();
final NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationHelper.notifyCompat(notificationManager, notificationId, noti); NotificationHelper.notifyCompat(notificationManager, notificationId, noti);
BackgroundService.addGuiInUseCounter(context); BackgroundService.addGuiInUseCounter(context);

View File

@ -1,10 +1,13 @@
package org.kde.kdeconnect.Helpers; package org.kde.kdeconnect.Helpers;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
public class NotificationHelper { public class NotificationHelper {
private static NotificationChannel defaultChannel;
public static void notifyCompat(NotificationManager notificationManager, int notificationId, Notification notification) { public static void notifyCompat(NotificationManager notificationManager, int notificationId, Notification notification) {
try { try {
notificationManager.notify(notificationId, notification); 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!/ //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;
}
} }

View File

@ -79,7 +79,9 @@ public class PingPlugin extends Plugin {
id = 42; //A unique id to create only one notification 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()) .setContentTitle(device.getName())
.setContentText(message) .setContentText(message)
.setContentIntent(resultPendingIntent) .setContentIntent(resultPendingIntent)
@ -89,7 +91,6 @@ public class PingPlugin extends Plugin {
.setDefaults(Notification.DEFAULT_ALL) .setDefaults(Notification.DEFAULT_ALL)
.build(); .build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationHelper.notifyCompat(notificationManager, id, noti); NotificationHelper.notifyCompat(notificationManager, id, noti);
return true; return true;

View File

@ -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")) .setContentTitle(np.getString("appName"))
.setContentText(np.getString("ticker")) .setContentText(np.getString("ticker"))
.setContentIntent(resultPendingIntent) .setContentIntent(resultPendingIntent)
@ -117,7 +120,6 @@ public class ReceiveNotificationsPlugin extends Plugin {
.setDefaults(Notification.DEFAULT_ALL) .setDefaults(Notification.DEFAULT_ALL)
.build(); .build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationHelper.notifyCompat(notificationManager, "kdeconnectId:" + np.getString("id", "0"), np.getInt("id", 0), noti); NotificationHelper.notifyCompat(notificationManager, "kdeconnectId:" + np.getString("id", "0"), np.getInt("id", 0), noti);
} }

View File

@ -39,8 +39,9 @@ class NotificationUpdateCallback extends Device.SendPacketStatusCallback {
} else { } else {
title = res.getString(R.string.outgoing_file_title, device.getName()); 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) .setSmallIcon(android.R.drawable.stat_sys_upload)
.setAutoCancel(true) .setAutoCancel(true)
.setProgress(100, 0, false) .setProgress(100, 0, false)

View File

@ -55,7 +55,7 @@ public class ShareNotification {
this.filename = filename; this.filename = filename;
notificationId = (int) System.currentTimeMillis(); notificationId = (int) System.currentTimeMillis();
notificationManager = (NotificationManager) device.getContext().getSystemService(Context.NOTIFICATION_SERVICE); 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())) .setContentTitle(device.getContext().getResources().getString(R.string.incoming_file_title, device.getName()))
.setContentText(device.getContext().getResources().getString(R.string.incoming_file_text, filename)) .setContentText(device.getContext().getResources().getString(R.string.incoming_file_text, filename))
.setTicker(device.getContext().getResources().getString(R.string.incoming_file_title, device.getName())) .setTicker(device.getContext().getResources().getString(R.string.incoming_file_title, device.getName()))
@ -80,7 +80,7 @@ public class ShareNotification {
public void setFinished(boolean success) { 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()); 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) builder.setContentTitle(message)
.setTicker(message) .setTicker(message)
.setSmallIcon(android.R.drawable.stat_sys_download_done) .setSmallIcon(android.R.drawable.stat_sys_download_done)

View File

@ -159,7 +159,9 @@ public class SharePlugin extends Plugin {
PendingIntent.FLAG_UPDATE_CURRENT 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())) .setContentTitle(res.getString(R.string.received_url_title, device.getName()))
.setContentText(res.getString(R.string.received_url_text, url)) .setContentText(res.getString(R.string.received_url_text, url))
.setContentIntent(resultPendingIntent) .setContentIntent(resultPendingIntent)
@ -169,7 +171,6 @@ public class SharePlugin extends Plugin {
.setDefaults(Notification.DEFAULT_ALL) .setDefaults(Notification.DEFAULT_ALL)
.build(); .build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationHelper.notifyCompat(notificationManager, (int) System.currentTimeMillis(), noti); NotificationHelper.notifyCompat(notificationManager, (int) System.currentTimeMillis(), noti);
} }
} }