change notifications led color

This commit is contained in:
kosyak 2024-01-10 00:17:46 +01:00
parent 6872b36f74
commit 9b748a7b4f

View file

@ -13,7 +13,6 @@ import android.content.SharedPreferences;
import android.content.pm.ShortcutManager; import android.content.pm.ShortcutManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.media.AudioAttributes; import android.media.AudioAttributes;
import android.media.Ringtone; import android.media.Ringtone;
@ -29,7 +28,6 @@ import android.text.style.StyleSpan;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import androidx.annotation.ColorInt;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationCompat.BigPictureStyle; import androidx.core.app.NotificationCompat.BigPictureStyle;
@ -95,6 +93,8 @@ public class NotificationService {
public static final Object CATCHUP_LOCK = new Object(); public static final Object CATCHUP_LOCK = new Object();
private static final int LED_COLOR = 0xffffffff;
private static final long[] CALL_PATTERN = {0, 500, 300, 600}; private static final long[] CALL_PATTERN = {0, 500, 300, 600};
private static final String MESSAGES_GROUP = "eu.siacs.conversations.messages"; private static final String MESSAGES_GROUP = "eu.siacs.conversations.messages";
@ -216,7 +216,7 @@ public class NotificationService {
NotificationManager.IMPORTANCE_HIGH); NotificationManager.IMPORTANCE_HIGH);
incomingCallsChannel.setSound(null, null); incomingCallsChannel.setSound(null, null);
incomingCallsChannel.setShowBadge(false); incomingCallsChannel.setShowBadge(false);
incomingCallsChannel.setLightColor(getPrimaryColor()); incomingCallsChannel.setLightColor(LED_COLOR);
incomingCallsChannel.enableLights(true); incomingCallsChannel.enableLights(true);
incomingCallsChannel.setGroup("calls"); incomingCallsChannel.setGroup("calls");
incomingCallsChannel.setBypassDnd(true); incomingCallsChannel.setBypassDnd(true);
@ -239,7 +239,7 @@ public class NotificationService {
NotificationManager.IMPORTANCE_HIGH); NotificationManager.IMPORTANCE_HIGH);
missedCallsChannel.setShowBadge(true); missedCallsChannel.setShowBadge(true);
missedCallsChannel.setSound(null, null); missedCallsChannel.setSound(null, null);
missedCallsChannel.setLightColor(getPrimaryColor()); missedCallsChannel.setLightColor(LED_COLOR);
missedCallsChannel.enableLights(true); missedCallsChannel.enableLights(true);
missedCallsChannel.setGroup("calls"); missedCallsChannel.setGroup("calls");
notificationManager.createNotificationChannel(missedCallsChannel); notificationManager.createNotificationChannel(missedCallsChannel);
@ -256,7 +256,7 @@ public class NotificationService {
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT) .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.build()); .build());
messagesChannel.setLightColor(getPrimaryColor()); messagesChannel.setLightColor(LED_COLOR);
final int dat = 70; final int dat = 70;
final long[] pattern = {0, 3 * dat, dat, dat}; final long[] pattern = {0, 3 * dat, dat, dat};
messagesChannel.setVibrationPattern(pattern); messagesChannel.setVibrationPattern(pattern);
@ -272,7 +272,7 @@ public class NotificationService {
silentMessagesChannel.setDescription( silentMessagesChannel.setDescription(
c.getString(R.string.silent_messages_channel_description)); c.getString(R.string.silent_messages_channel_description));
silentMessagesChannel.setShowBadge(true); silentMessagesChannel.setShowBadge(true);
silentMessagesChannel.setLightColor(getPrimaryColor()); silentMessagesChannel.setLightColor(LED_COLOR);
silentMessagesChannel.enableLights(true); silentMessagesChannel.enableLights(true);
silentMessagesChannel.setGroup("chats"); silentMessagesChannel.setGroup("chats");
notificationManager.createNotificationChannel(silentMessagesChannel); notificationManager.createNotificationChannel(silentMessagesChannel);
@ -283,7 +283,7 @@ public class NotificationService {
c.getString(R.string.title_pref_quiet_hours), c.getString(R.string.title_pref_quiet_hours),
NotificationManager.IMPORTANCE_LOW); NotificationManager.IMPORTANCE_LOW);
quietHoursChannel.setShowBadge(true); quietHoursChannel.setShowBadge(true);
quietHoursChannel.setLightColor(getPrimaryColor()); quietHoursChannel.setLightColor(LED_COLOR);
quietHoursChannel.enableLights(true); quietHoursChannel.enableLights(true);
quietHoursChannel.setGroup("chats"); quietHoursChannel.setGroup("chats");
quietHoursChannel.enableVibration(false); quietHoursChannel.enableVibration(false);
@ -411,8 +411,8 @@ public class NotificationService {
private int getBacklogMessageCount(Account account) { private int getBacklogMessageCount(Account account) {
int count = 0; int count = 0;
for (Iterator<Map.Entry<Conversation, AtomicInteger>> it = for (Iterator<Map.Entry<Conversation, AtomicInteger>> it =
mBacklogMessageCounter.entrySet().iterator(); mBacklogMessageCounter.entrySet().iterator();
it.hasNext(); ) { it.hasNext(); ) {
Map.Entry<Conversation, AtomicInteger> entry = it.next(); Map.Entry<Conversation, AtomicInteger> entry = it.next();
if (entry.getKey().getAccount() == account) { if (entry.getKey().getAccount() == account) {
count += entry.getValue().get(); count += entry.getValue().get();
@ -590,19 +590,19 @@ public class NotificationService {
builder.setOngoing(true); builder.setOngoing(true);
builder.addAction( builder.addAction(
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_call_end_white_48dp, R.drawable.ic_call_end_white_48dp,
mXmppConnectionService.getString(R.string.dismiss_call), mXmppConnectionService.getString(R.string.dismiss_call),
createCallAction( createCallAction(
id.sessionId, id.sessionId,
XmppConnectionService.ACTION_DISMISS_CALL, XmppConnectionService.ACTION_DISMISS_CALL,
102)) 102))
.build()); .build());
builder.addAction( builder.addAction(
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_call_white_24dp, R.drawable.ic_call_white_24dp,
mXmppConnectionService.getString(R.string.answer_call), mXmppConnectionService.getString(R.string.answer_call),
createPendingRtpSession( createPendingRtpSession(
id, RtpSessionActivity.ACTION_ACCEPT_CALL, 103)) id, RtpSessionActivity.ACTION_ACCEPT_CALL, 103))
.build()); .build());
modifyIncomingCall(builder); modifyIncomingCall(builder);
final Notification notification = builder.build(); final Notification notification = builder.build();
@ -641,10 +641,10 @@ public class NotificationService {
builder.setOngoing(true); builder.setOngoing(true);
builder.addAction( builder.addAction(
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_call_end_white_48dp, R.drawable.ic_call_end_white_48dp,
mXmppConnectionService.getString(R.string.hang_up), mXmppConnectionService.getString(R.string.hang_up),
createCallAction( createCallAction(
id.sessionId, XmppConnectionService.ACTION_END_CALL, 104)) id.sessionId, XmppConnectionService.ACTION_END_CALL, 104))
.build()); .build());
builder.setLocalOnly(true); builder.setLocalOnly(true);
return builder.build(); return builder.build();
@ -831,13 +831,8 @@ public class NotificationService {
} }
private void setNotificationColor(final Builder mBuilder) { private void setNotificationColor(final Builder mBuilder) {
mBuilder.setColor(getPrimaryColor());
}
@ColorInt
private int getPrimaryColor() {
Integer primaryColorOverride = ThemeHelper.getOverriddenPrimaryColor(mXmppConnectionService); Integer primaryColorOverride = ThemeHelper.getOverriddenPrimaryColor(mXmppConnectionService);
return primaryColorOverride == null ? ContextCompat.getColor(mXmppConnectionService, R.color.green600) : primaryColorOverride; mBuilder.setColor(primaryColorOverride == null ? ContextCompat.getColor(mXmppConnectionService, R.color.green600) : primaryColorOverride);
} }
public void updateNotification() { public void updateNotification() {
@ -865,7 +860,7 @@ public class NotificationService {
notify notify
&& conversations != null && conversations != null
&& conversations.size() && conversations.size()
== 1; // if this check is changed to > 0 catchup messages will == 1; // if this check is changed to > 0 catchup messages will
// create one notification per conversation // create one notification per conversation
if (notifications.size() == 0) { if (notifications.size() == 0) {
@ -971,20 +966,20 @@ public class NotificationService {
mBuilder.setPriority( mBuilder.setPriority(
notify notify
? (headsup ? (headsup
? NotificationCompat.PRIORITY_HIGH ? NotificationCompat.PRIORITY_HIGH
: NotificationCompat.PRIORITY_DEFAULT) : NotificationCompat.PRIORITY_DEFAULT)
: NotificationCompat.PRIORITY_LOW); : NotificationCompat.PRIORITY_LOW);
setNotificationColor(mBuilder); setNotificationColor(mBuilder);
mBuilder.setDefaults(0); mBuilder.setDefaults(0);
if (led) { if (led) {
mBuilder.setLights(getPrimaryColor(), 2000, 3000); mBuilder.setLights(LED_COLOR, 2000, 3000);
} }
} }
private void modifyIncomingCall(final Builder mBuilder) { private void modifyIncomingCall(final Builder mBuilder) {
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH); mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
setNotificationColor(mBuilder); setNotificationColor(mBuilder);
mBuilder.setLights(getPrimaryColor(), 2000, 3000); mBuilder.setLights(LED_COLOR, 2000, 3000);
} }
private Uri fixRingtoneUri(Uri uri) { private Uri fixRingtoneUri(Uri uri) {
@ -1019,17 +1014,17 @@ public class NotificationService {
(totalCalls == 1) (totalCalls == 1)
? mXmppConnectionService.getString(R.string.missed_call) ? mXmppConnectionService.getString(R.string.missed_call)
: (mMissedCalls.size() == 1) : (mMissedCalls.size() == 1)
? mXmppConnectionService ? mXmppConnectionService
.getResources() .getResources()
.getQuantityString( .getQuantityString(
R.plurals.n_missed_calls, totalCalls, totalCalls) R.plurals.n_missed_calls, totalCalls, totalCalls)
: mXmppConnectionService : mXmppConnectionService
.getResources() .getResources()
.getQuantityString( .getQuantityString(
R.plurals.n_missed_calls_from_m_contacts, R.plurals.n_missed_calls_from_m_contacts,
mMissedCalls.size(), mMissedCalls.size(),
totalCalls, totalCalls,
mMissedCalls.size()); mMissedCalls.size());
builder.setContentTitle(title); builder.setContentTitle(title);
builder.setTicker(title); builder.setTicker(title);
if (!publicVersion) { if (!publicVersion) {
@ -1065,11 +1060,11 @@ public class NotificationService {
(info.getNumberOfCalls() == 1) (info.getNumberOfCalls() == 1)
? mXmppConnectionService.getString(R.string.missed_call) ? mXmppConnectionService.getString(R.string.missed_call)
: mXmppConnectionService : mXmppConnectionService
.getResources() .getResources()
.getQuantityString( .getQuantityString(
R.plurals.n_missed_calls, R.plurals.n_missed_calls,
info.getNumberOfCalls(), info.getNumberOfCalls(),
info.getNumberOfCalls()); info.getNumberOfCalls());
builder.setContentTitle(title); builder.setContentTitle(title);
final String name = conversation.getContact().getDisplayName(); final String name = conversation.getContact().getDisplayName();
if (publicVersion) { if (publicVersion) {
@ -1109,7 +1104,7 @@ public class NotificationService {
final Resources resources = mXmppConnectionService.getResources(); final Resources resources = mXmppConnectionService.getResources();
final boolean led = preferences.getBoolean("led", resources.getBoolean(R.bool.led)); final boolean led = preferences.getBoolean("led", resources.getBoolean(R.bool.led));
if (led) { if (led) {
builder.setLights(getPrimaryColor(), 2000, 3000); builder.setLights(LED_COLOR, 2000, 3000);
} }
builder.setPriority(NotificationCompat.PRIORITY_HIGH); builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setSound(null); builder.setSound(null);
@ -1145,9 +1140,9 @@ public class NotificationService {
name name
+ ": " + ": "
+ mXmppConnectionService + mXmppConnectionService
.getResources() .getResources()
.getQuantityString( .getQuantityString(
R.plurals.x_messages, count, count)); R.plurals.x_messages, count, count));
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0); styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
style.addLine(styledString); style.addLine(styledString);
} else { } else {
@ -1156,8 +1151,8 @@ public class NotificationService {
name name
+ ": " + ": "
+ UIHelper.getMessagePreview( + UIHelper.getMessagePreview(
mXmppConnectionService, messages.get(0)) mXmppConnectionService, messages.get(0))
.first); .first);
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0); styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
style.addLine(styledString); style.addLine(styledString);
} }
@ -1223,9 +1218,9 @@ public class NotificationService {
PendingIntent markAsReadPendingIntent = createReadPendingIntent(conversation); PendingIntent markAsReadPendingIntent = createReadPendingIntent(conversation);
NotificationCompat.Action markReadAction = NotificationCompat.Action markReadAction =
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_drafts_white_24dp, R.drawable.ic_drafts_white_24dp,
mXmppConnectionService.getString(R.string.mark_as_read), mXmppConnectionService.getString(R.string.mark_as_read),
markAsReadPendingIntent) markAsReadPendingIntent)
.setSemanticAction( .setSemanticAction(
NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ) NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
.setShowsUserInterface(false) .setShowsUserInterface(false)
@ -1234,18 +1229,18 @@ public class NotificationService {
final String lastMessageUuid = Iterables.getLast(messages).getUuid(); final String lastMessageUuid = Iterables.getLast(messages).getUuid();
final NotificationCompat.Action replyAction = final NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_send_text_offline, R.drawable.ic_send_text_offline,
replyLabel, replyLabel,
createReplyIntent(conversation, lastMessageUuid, false)) createReplyIntent(conversation, lastMessageUuid, false))
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY) .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
.setShowsUserInterface(false) .setShowsUserInterface(false)
.addRemoteInput(remoteInput) .addRemoteInput(remoteInput)
.build(); .build();
final NotificationCompat.Action wearReplyAction = final NotificationCompat.Action wearReplyAction =
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_wear_reply, R.drawable.ic_wear_reply,
replyLabel, replyLabel,
createReplyIntent(conversation, lastMessageUuid, true)) createReplyIntent(conversation, lastMessageUuid, true))
.addRemoteInput(remoteInput) .addRemoteInput(remoteInput)
.build(); .build();
mBuilder.extend( mBuilder.extend(
@ -1262,9 +1257,9 @@ public class NotificationService {
PendingIntent pendingSnoozeIntent = createSnoozeIntent(conversation); PendingIntent pendingSnoozeIntent = createSnoozeIntent(conversation);
NotificationCompat.Action snoozeAction = NotificationCompat.Action snoozeAction =
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_notifications_paused_white_24dp, R.drawable.ic_notifications_paused_white_24dp,
label, label,
pendingSnoozeIntent) pendingSnoozeIntent)
.build(); .build();
mBuilder.addAction(snoozeAction); mBuilder.addAction(snoozeAction);
++addedActionsCount; ++addedActionsCount;
@ -1281,9 +1276,9 @@ public class NotificationService {
.getString(R.string.show_location); .getString(R.string.show_location);
NotificationCompat.Action locationAction = NotificationCompat.Action locationAction =
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_room_white_24dp, R.drawable.ic_room_white_24dp,
label, label,
pendingShowLocationIntent) pendingShowLocationIntent)
.build(); .build();
mBuilder.addAction(locationAction); mBuilder.addAction(locationAction);
++addedActionsCount; ++addedActionsCount;
@ -1305,9 +1300,9 @@ public class NotificationService {
createDownloadIntent(firstDownloadableMessage); createDownloadIntent(firstDownloadableMessage);
NotificationCompat.Action downloadAction = NotificationCompat.Action downloadAction =
new NotificationCompat.Action.Builder( new NotificationCompat.Action.Builder(
R.drawable.ic_file_download_white_24dp, R.drawable.ic_file_download_white_24dp,
label, label,
pendingDownloadIntent) pendingDownloadIntent)
.build(); .build();
mBuilder.addAction(downloadAction); mBuilder.addAction(downloadAction);
++addedActionsCount; ++addedActionsCount;
@ -1455,7 +1450,7 @@ public class NotificationService {
new NotificationCompat.BigTextStyle().bigText(getMergedBodies(messages))); new NotificationCompat.BigTextStyle().bigText(getMergedBodies(messages)));
final CharSequence preview = final CharSequence preview =
UIHelper.getMessagePreview( UIHelper.getMessagePreview(
mXmppConnectionService, messages.get(messages.size() - 1)) mXmppConnectionService, messages.get(messages.size() - 1))
.first; .first;
builder.setContentText(preview); builder.setContentText(preview);
builder.setTicker(preview); builder.setTicker(preview);
@ -1823,7 +1818,7 @@ public class NotificationService {
if (account.hasErrorStatus() if (account.hasErrorStatus()
&& account.showErrorNotification() && account.showErrorNotification()
&& (showAllErrors && (showAllErrors
|| account.getLastErrorStatus() == Account.State.UNAUTHORIZED)) { || account.getLastErrorStatus() == Account.State.UNAUTHORIZED)) {
errors.add(account); errors.add(account);
torNotAvailable |= account.getStatus() == Account.State.TOR_NOT_AVAILABLE; torNotAvailable |= account.getStatus() == Account.State.TOR_NOT_AVAILABLE;
} }
@ -1859,7 +1854,7 @@ public class NotificationService {
TorServiceUtils.LAUNCH_INTENT, TorServiceUtils.LAUNCH_INTENT,
s() s()
? PendingIntent.FLAG_IMMUTABLE ? PendingIntent.FLAG_IMMUTABLE
| PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT
: PendingIntent.FLAG_UPDATE_CURRENT)); : PendingIntent.FLAG_UPDATE_CURRENT));
} else { } else {
mBuilder.addAction( mBuilder.addAction(
@ -1871,7 +1866,7 @@ public class NotificationService {
TorServiceUtils.INSTALL_INTENT, TorServiceUtils.INSTALL_INTENT,
s() s()
? PendingIntent.FLAG_IMMUTABLE ? PendingIntent.FLAG_IMMUTABLE
| PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT
: PendingIntent.FLAG_UPDATE_CURRENT)); : PendingIntent.FLAG_UPDATE_CURRENT));
} }
} }