File indexing completed on 2025-02-02 04:47:49
0001 /* 0002 * SPDX-FileCopyrightText: 2023 Albert Vaca Cintora <albertvaka@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.Helpers; 0008 0009 import android.app.Notification; 0010 import android.app.NotificationManager; 0011 import android.content.Context; 0012 import android.content.SharedPreferences; 0013 import android.os.Build; 0014 import android.preference.PreferenceManager; 0015 0016 import androidx.core.app.NotificationChannelCompat; 0017 import androidx.core.app.NotificationManagerCompat; 0018 0019 import org.kde.kdeconnect_tp.R; 0020 0021 import java.util.Arrays; 0022 import java.util.List; 0023 import java.util.stream.Collectors; 0024 0025 public class NotificationHelper { 0026 0027 public static class Channels { 0028 public final static String PERSISTENT = "persistent"; 0029 public final static String DEFAULT = "default"; 0030 public final static String MEDIA_CONTROL = "media_control"; 0031 0032 public final static String FILETRANSFER_DOWNLOAD = "filetransfer"; 0033 public final static String FILETRANSFER_UPLOAD = "filetransfer_upload"; 0034 public final static String FILETRANSFER_ERROR = "filetransfer_error"; 0035 0036 public final static String RECEIVENOTIFICATION = "receive"; 0037 public final static String HIGHPRIORITY = "highpriority"; 0038 } 0039 0040 public static void notifyCompat(NotificationManager notificationManager, int notificationId, Notification notification) { 0041 try { 0042 notificationManager.notify(notificationId, notification); 0043 } catch (Exception e) { 0044 //4.1 will throw an exception about not having the VIBRATE permission, ignore it. 0045 //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ 0046 } 0047 } 0048 0049 public static void notifyCompat(NotificationManager notificationManager, String tag, int notificationId, Notification notification) { 0050 try { 0051 notificationManager.notify(tag, notificationId, notification); 0052 } catch (Exception e) { 0053 //4.1 will throw an exception about not having the VIBRATE permission, ignore it. 0054 //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ 0055 } 0056 } 0057 0058 public static void initializeChannels(Context context) { 0059 final NotificationChannelCompat persistentChannel = new NotificationChannelCompat 0060 .Builder(Channels.PERSISTENT, NotificationManagerCompat.IMPORTANCE_MIN) 0061 .setName(context.getString(R.string.notification_channel_persistent)) 0062 .build(); 0063 final NotificationChannelCompat defaultChannel = new NotificationChannelCompat 0064 .Builder(Channels.DEFAULT, NotificationManagerCompat.IMPORTANCE_DEFAULT) 0065 .setName(context.getString(R.string.notification_channel_default)) 0066 .build(); 0067 final NotificationChannelCompat mediaChannel = new NotificationChannelCompat 0068 .Builder(Channels.MEDIA_CONTROL, NotificationManagerCompat.IMPORTANCE_LOW) 0069 .setName(context.getString(R.string.notification_channel_media_control)) 0070 .build(); 0071 final NotificationChannelCompat fileTransferDownloadChannel = new NotificationChannelCompat 0072 .Builder(Channels.FILETRANSFER_DOWNLOAD, NotificationManagerCompat.IMPORTANCE_LOW) 0073 .setName(context.getString(R.string.notification_channel_filetransfer)) 0074 .setVibrationEnabled(false) 0075 .build(); 0076 final NotificationChannelCompat fileTransferUploadChannel = new NotificationChannelCompat 0077 .Builder(Channels.FILETRANSFER_UPLOAD, NotificationManagerCompat.IMPORTANCE_LOW) 0078 .setName(context.getString(R.string.notification_channel_filetransfer_upload)) 0079 .setVibrationEnabled(false) 0080 .build(); 0081 final NotificationChannelCompat fileTransferErrorChannel = new NotificationChannelCompat 0082 .Builder(Channels.FILETRANSFER_ERROR, NotificationManagerCompat.IMPORTANCE_HIGH) 0083 .setName(context.getString(R.string.notification_channel_filetransfer_error)) 0084 .setVibrationEnabled(false) 0085 .build(); 0086 final NotificationChannelCompat receiveNotificationChannel = new NotificationChannelCompat 0087 .Builder(Channels.RECEIVENOTIFICATION, NotificationManagerCompat.IMPORTANCE_DEFAULT) 0088 .setName(context.getString(R.string.notification_channel_receivenotification)) 0089 .build(); 0090 final NotificationChannelCompat highPriorityChannel = new NotificationChannelCompat 0091 .Builder(Channels.HIGHPRIORITY, NotificationManagerCompat.IMPORTANCE_HIGH) 0092 .setName(context.getString(R.string.notification_channel_high_priority)) 0093 .build(); 0094 0095 final List<NotificationChannelCompat> channels = Arrays.asList(persistentChannel, 0096 defaultChannel, mediaChannel, fileTransferDownloadChannel, fileTransferUploadChannel, 0097 fileTransferErrorChannel, receiveNotificationChannel, highPriorityChannel); 0098 0099 NotificationManagerCompat.from(context).createNotificationChannelsCompat(channels); 0100 0101 // Delete any notification channels which weren't added. 0102 // Use this to deprecate old channels. 0103 NotificationManagerCompat.from(context).deleteUnlistedNotificationChannels( 0104 channels.stream() 0105 .map(notificationChannelCompat -> notificationChannelCompat.getId()) 0106 .collect(Collectors.toList())); 0107 } 0108 0109 public static void setPersistentNotificationEnabled(Context context, boolean enabled) { 0110 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 0111 prefs.edit().putBoolean("persistentNotification", enabled).apply(); 0112 } 0113 0114 public static boolean isPersistentNotificationEnabled(Context context) { 0115 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 0116 return true; 0117 } 0118 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 0119 return prefs.getBoolean("persistentNotification", false); 0120 } 0121 0122 }