diff --git a/src/org/thoughtcrime/securesms/connect/KeepAliveService.java b/src/org/thoughtcrime/securesms/connect/KeepAliveService.java index 4a672dd6e..f04d194a2 100644 --- a/src/org/thoughtcrime/securesms/connect/KeepAliveService.java +++ b/src/org/thoughtcrime/securesms/connect/KeepAliveService.java @@ -93,7 +93,7 @@ public class KeepAliveService extends Service { * Delta Chat won't get new messages reliable **********************************************************************************************/ - public static final int FG_NOTIFICATION_ID = 4142; + public static final int FG_NOTIFICATION_ID = 1; // given to NotificationManagerCompat.notify(); for normal notifications, we use msgId>=10 private Notification createNotification() { Intent intent = new Intent(this, ConversationListActivity.class); diff --git a/src/org/thoughtcrime/securesms/notifications/MsgNotificationManager.java b/src/org/thoughtcrime/securesms/notifications/MsgNotificationManager.java index 8a5f9e84c..2d10bc8a3 100644 --- a/src/org/thoughtcrime/securesms/notifications/MsgNotificationManager.java +++ b/src/org/thoughtcrime/securesms/notifications/MsgNotificationManager.java @@ -1,22 +1,142 @@ package org.thoughtcrime.securesms.notifications; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.content.Context; +import android.graphics.Color; +import android.media.AudioAttributes; +import android.net.Uri; +import android.os.Build; +import android.text.TextUtils; import android.util.Log; import androidx.annotation.NonNull; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; +import com.b44t.messenger.DcChat; +import com.b44t.messenger.DcMsg; + +import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.connect.ApplicationDcContext; import org.thoughtcrime.securesms.util.Prefs; import org.thoughtcrime.securesms.util.Util; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.util.List; + public class MsgNotificationManager { private static final String TAG = InChatSounds.class.getSimpleName(); @NonNull private ApplicationDcContext dcContext; + @NonNull private Context context; private volatile int visibleChatId = 0; public MsgNotificationManager(ApplicationDcContext dcContext) { this.dcContext = dcContext; + this.context = dcContext.context.getApplicationContext(); } + + // handle notification channels + // -------------------------------------------------------------------------------------------- + + // - since oreo, a NotificationChannel is a MUST + // - NotificationChannels have default values that have a higher precedence as the Notification.Builder setting + // - once created, NotificationChannels cannot be modified programmatically + // - NotificationChannels can be deleted, however, on re-creation it becomes un-deleted with the old settings + // - the idea is that sound and vibrate are handled outside of the scope of the notification channel + + private boolean notificationChannelsSupported() { + return Build.VERSION.SDK_INT >= 26; + } + + private String getNotificationChannel(NotificationManagerCompat notificationManager) { + final String chBase = "ch_msg4_"; + String chId = chBase + "unsupported"; + + if(notificationChannelsSupported()) { + try { + // get all values we'll use as settings for the NotificationChannel + String ledColor = Prefs.getNotificationLedColor(context); + boolean defaultVibrate = Prefs.isNotificationVibrateEnabled(context); + Uri ringtone = Prefs.getNotificationRingtone(context); + + // compute hash from these settings + String hash = ""; + MessageDigest md = MessageDigest.getInstance("SHA-256"); + md.update(ledColor.getBytes()); + md.update(defaultVibrate ? (byte) 1 : (byte) 0); + md.update(ringtone.toString().getBytes()); + hash = String.format("%X", new BigInteger(1, md.digest())).substring(0, 16); + + // get channel name + chId = chBase + hash; + String oldChId = Prefs.getStringPreference(context, "ch_curr_" + chBase, ""); + if (!oldChId.equals(chId)) { + try { + notificationManager.deleteNotificationChannel(oldChId); + } + catch (Exception e) { + // channel not created before + } + Prefs.setStringPreference(context, "ch_curr_" + chBase, chId); + } + + // check if there is already a channel with the given name + List channels = notificationManager.getNotificationChannels(); + boolean channelExists = false; + for (int i = 0; i < channels.size(); i++) { + if (chId.equals(channels.get(i).getId())) { + channelExists = true; + } + } + + // create a channel with the given settings; + // we cannot change the settings, however, this is handled by using different values for chId + if(!channelExists) { + NotificationChannel channel = new NotificationChannel(chId, + "New messages", NotificationManager.IMPORTANCE_HIGH); + channel.setDescription("Informs about new messages."); + + if (!ledColor.equals("none")) { + channel.enableLights(true); + int argb; + try { + argb = Color.parseColor(ledColor); + } + catch (Exception e) { + argb = Color.rgb(0xFF, 0xFF, 0xFF); + } + channel.setLightColor(argb); + } else { + channel.enableLights(false); + } + + channel.enableVibration(defaultVibrate); + + if (!TextUtils.isEmpty(ringtone.toString())) { + channel.setSound(ringtone, + new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) + .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT) + .build()); + } + + notificationManager.createNotificationChannel(channel); + } + } + catch(Exception e) { + e.printStackTrace(); + } + } + + return chId; + } + + + // add notifications & co. + // -------------------------------------------------------------------------------------------- + public void addNotification(int chatId, int msgId) { Util.runOnAnyBackgroundThread(() -> { @@ -29,6 +149,17 @@ public class MsgNotificationManager { return; } + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); + + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, getNotificationChannel(notificationManager)) + .setSmallIcon(R.drawable.icon_notification) + .setColor(context.getResources().getColor(R.color.delta_primary)) + .setPriority(Prefs.getNotificationPriority(context)) + .setCategory(NotificationCompat.CATEGORY_MESSAGE) + .setContentTitle(dcContext.getChat(chatId).getName()) + .setContentText(dcContext.getMsg(msgId).getSummarytext(100)); + + notificationManager.notify(msgId, builder.build()); }); }