2016-12-05 23:42:58 +01:00
|
|
|
package org.kde.kdeconnect.Helpers;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
2018-03-24 01:02:25 +01:00
|
|
|
import android.app.NotificationChannel;
|
2016-12-05 23:42:58 +01:00
|
|
|
import android.app.NotificationManager;
|
2018-05-15 00:37:24 +02:00
|
|
|
import android.content.Context;
|
2018-10-25 16:53:10 +02:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.preference.PreferenceManager;
|
2018-05-15 00:37:24 +02:00
|
|
|
|
|
|
|
import org.kde.kdeconnect_tp.R;
|
2016-12-05 23:42:58 +01:00
|
|
|
|
|
|
|
public class NotificationHelper {
|
|
|
|
|
2018-05-15 00:32:51 +02:00
|
|
|
public static class Channels {
|
|
|
|
public final static String PERSISTENT = "persistent";
|
|
|
|
public final static String DEFAULT = "default";
|
2018-05-15 01:10:39 +02:00
|
|
|
public final static String MEDIA_CONTROL = "media_control";
|
2018-06-03 15:18:29 +02:00
|
|
|
public final static String FILETRANSFER = "filetransfer";
|
2018-12-24 23:14:44 +01:00
|
|
|
public final static String RECEIVENOTIFICATION = "receive";
|
2018-05-15 00:32:51 +02:00
|
|
|
}
|
2018-03-24 01:02:25 +01:00
|
|
|
|
2016-12-05 23:42:58 +01:00
|
|
|
public static void notifyCompat(NotificationManager notificationManager, int notificationId, Notification notification) {
|
|
|
|
try {
|
|
|
|
notificationManager.notify(notificationId, notification);
|
2018-03-03 16:06:52 +01:00
|
|
|
} catch (Exception e) {
|
2016-12-05 23:42:58 +01:00
|
|
|
//4.1 will throw an exception about not having the VIBRATE permission, ignore it.
|
|
|
|
//https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void notifyCompat(NotificationManager notificationManager, String tag, int notificationId, Notification notification) {
|
|
|
|
try {
|
|
|
|
notificationManager.notify(tag, notificationId, notification);
|
2018-03-03 16:06:52 +01:00
|
|
|
} catch (Exception e) {
|
2016-12-05 23:42:58 +01:00
|
|
|
//4.1 will throw an exception about not having the VIBRATE permission, ignore it.
|
|
|
|
//https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/
|
|
|
|
}
|
|
|
|
}
|
2018-03-24 01:02:25 +01:00
|
|
|
|
2018-05-15 00:37:24 +02:00
|
|
|
public static void initializeChannels(Context context) {
|
2018-05-15 00:32:51 +02:00
|
|
|
|
|
|
|
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-15 00:37:24 +02:00
|
|
|
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2018-05-15 00:32:51 +02:00
|
|
|
|
2018-05-21 17:50:24 +02:00
|
|
|
NotificationChannel persistentChannel = new NotificationChannel(
|
2018-05-15 01:10:39 +02:00
|
|
|
Channels.PERSISTENT,
|
|
|
|
context.getString(R.string.notification_channel_persistent),
|
2018-05-21 17:50:24 +02:00
|
|
|
NotificationManager.IMPORTANCE_MIN);
|
|
|
|
|
|
|
|
manager.createNotificationChannel(persistentChannel);
|
2018-05-15 01:10:39 +02:00
|
|
|
|
2018-05-15 00:37:24 +02:00
|
|
|
manager.createNotificationChannel(new NotificationChannel(
|
|
|
|
Channels.DEFAULT,
|
|
|
|
context.getString(R.string.notification_channel_default),
|
|
|
|
NotificationManager.IMPORTANCE_DEFAULT)
|
|
|
|
);
|
2018-05-15 00:32:51 +02:00
|
|
|
|
2018-05-15 00:37:24 +02:00
|
|
|
manager.createNotificationChannel(new NotificationChannel(
|
2018-05-15 01:10:39 +02:00
|
|
|
Channels.MEDIA_CONTROL,
|
|
|
|
context.getString(R.string.notification_channel_media_control),
|
|
|
|
NotificationManager.IMPORTANCE_LOW)
|
2018-05-15 00:37:24 +02:00
|
|
|
);
|
2018-05-15 00:32:51 +02:00
|
|
|
|
2018-06-03 15:18:29 +02:00
|
|
|
NotificationChannel fileTransfer = new NotificationChannel(
|
|
|
|
Channels.FILETRANSFER,
|
|
|
|
context.getString(R.string.notification_channel_filetransfer),
|
|
|
|
NotificationManager.IMPORTANCE_LOW);
|
|
|
|
|
|
|
|
fileTransfer.enableVibration(false);
|
|
|
|
|
|
|
|
manager.createNotificationChannel(fileTransfer);
|
|
|
|
|
2018-12-24 23:14:44 +01:00
|
|
|
manager.createNotificationChannel(new NotificationChannel(
|
|
|
|
Channels.RECEIVENOTIFICATION,
|
|
|
|
context.getString(R.string.notification_channel_receivenotification),
|
|
|
|
NotificationManager.IMPORTANCE_DEFAULT)
|
|
|
|
);
|
|
|
|
|
2018-03-24 01:02:25 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 16:53:10 +02:00
|
|
|
|
|
|
|
public static void setPersistentNotificationEnabled(Context context, boolean enabled) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
prefs.edit().putBoolean("persistentNotification", enabled).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isPersistentNotificationEnabled(Context context) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
return prefs.getBoolean("persistentNotification", false);
|
|
|
|
}
|
|
|
|
|
2016-12-05 23:42:58 +01:00
|
|
|
}
|