From ce73f31bb50261e2b2289271291bedad6c7fc70a Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Mon, 29 Jan 2018 21:26:05 +0100 Subject: add snooze button to notification --- .../messenger/services/NotificationService.java | 108 ++++++++++++++++----- .../messenger/services/XmppConnectionService.java | 12 +++ .../ic_notifications_paused_white_24dp.png | Bin 0 -> 309 bytes .../ic_notifications_paused_white_24dp.png | Bin 0 -> 235 bytes .../ic_notifications_paused_white_24dp.png | Bin 0 -> 351 bytes .../ic_notifications_paused_white_24dp.png | Bin 0 -> 482 bytes .../ic_notifications_paused_white_24dp.png | Bin 0 -> 616 bytes src/main/res/values/strings.xml | 1 + 8 files changed, 98 insertions(+), 23 deletions(-) create mode 100644 src/main/res/drawable-hdpi/ic_notifications_paused_white_24dp.png create mode 100644 src/main/res/drawable-mdpi/ic_notifications_paused_white_24dp.png create mode 100644 src/main/res/drawable-xhdpi/ic_notifications_paused_white_24dp.png create mode 100644 src/main/res/drawable-xxhdpi/ic_notifications_paused_white_24dp.png create mode 100644 src/main/res/drawable-xxxhdpi/ic_notifications_paused_white_24dp.png (limited to 'src/main') diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index 985af31c2..646f257f9 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -404,30 +404,64 @@ public class NotificationService { modifyForTextOnly(mBuilder, mUnreadBuilder, messages); } RemoteInput remoteInput = new RemoteInput.Builder("text_reply").setLabel(UIHelper.getMessageHint(mXmppConnectionService, conversation)).build(); - NotificationCompat.Action markReadAction = new NotificationCompat.Action.Builder(R.drawable.ic_send_text_offline, mXmppConnectionService.getResources().getString(R.string.mark_as_read), createReadPendingIntent(conversation)).build(); - NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(R.drawable.ic_send_text_offline, mXmppConnectionService.getResources().getString(R.string.reply), createReplyIntent(conversation, false)).addRemoteInput(remoteInput).build(); - NotificationCompat.Action wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_wear_reply, "Reply", createReplyIntent(conversation, true)).addRemoteInput(remoteInput).build(); + PendingIntent markAsReadPendingIntent = createReadPendingIntent(conversation); + NotificationCompat.Action markReadAction = new NotificationCompat.Action.Builder( + R.drawable.ic_send_text_offline, + mXmppConnectionService.getString(R.string.mark_as_read), + markAsReadPendingIntent).build(); + String replyLabel = mXmppConnectionService.getString(R.string.reply); + NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder( + R.drawable.ic_send_text_offline, + replyLabel, + createReplyIntent(conversation, false)).addRemoteInput(remoteInput).build(); + NotificationCompat.Action wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_wear_reply, + replyLabel, + createReplyIntent(conversation, true)).addRemoteInput(remoteInput).build(); mBuilder.extend(new NotificationCompat.WearableExtender().addAction(wearReplyAction)); mUnreadBuilder.setReplyAction(createReplyIntent(conversation, true), remoteInput); - mUnreadBuilder.setReadPendingIntent(createReadPendingIntent(conversation)); + mUnreadBuilder.setReadPendingIntent(markAsReadPendingIntent); mBuilder.extend(new NotificationCompat.CarExtender().setUnreadConversation(mUnreadBuilder.build())); + int addedActionsCount = 1; + mBuilder.addAction(markReadAction); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - mBuilder.addAction(markReadAction); mBuilder.addAction(replyAction); + ++addedActionsCount; } - if ((message = getFirstDownloadableMessage(messages)) != null) { - mBuilder.addAction( - Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? - R.drawable.ic_file_download_white_24dp : R.drawable.ic_action_download, - mXmppConnectionService.getResources().getString(R.string.download_x_file, - UIHelper.getFileDescriptionString(mXmppConnectionService, message)), - createDownloadIntent(message) - ); + if (displaySnoozeAction(messages)) { + String label = mXmppConnectionService.getString(R.string.snooze); + PendingIntent pendingSnoozeIntent = createSnoozeIntent(conversation); + NotificationCompat.Action snoozeAction = new NotificationCompat.Action.Builder( + R.drawable.ic_notifications_paused_white_24dp, + label, + pendingSnoozeIntent).build(); + mBuilder.addAction(snoozeAction); + ++addedActionsCount; } - if ((message = getFirstLocationMessage(messages)) != null) { - mBuilder.addAction(R.drawable.ic_room_white_24dp, - mXmppConnectionService.getString(R.string.show_location), - createShowLocationIntent(message)); + if (addedActionsCount < 3) { + final Message firstLocationMessage = getFirstLocationMessage(messages); + if (firstLocationMessage != null) { + String label = mXmppConnectionService.getResources().getString(R.string.show_location); + PendingIntent pendingShowLocationIntent = createShowLocationIntent(firstLocationMessage); + NotificationCompat.Action locationAction = new NotificationCompat.Action.Builder( + R.drawable.ic_room_white_24dp, + label, + pendingShowLocationIntent).build(); + mBuilder.addAction(locationAction); + ++addedActionsCount; + } + } + if (addedActionsCount < 3) { + Message firstDownloadableMessage = getFirstDownloadableMessage(messages); + if (firstDownloadableMessage != null) { + String label = mXmppConnectionService.getResources().getString(R.string.download_x_file, UIHelper.getFileDescriptionString(mXmppConnectionService, message)); + PendingIntent pendingDownloadIntent = createDownloadIntent(message); + NotificationCompat.Action downloadAction = new NotificationCompat.Action.Builder( + R.drawable.ic_file_download_white_24dp, + label, + pendingDownloadIntent).build(); + mBuilder.addAction(downloadAction); + ++addedActionsCount; + } } } if (conversation.getMode() == Conversation.MODE_SINGLE) { @@ -445,6 +479,18 @@ public class NotificationService { return mBuilder; } + private static boolean displaySnoozeAction(List messages) { + int numberOfMessagesWithoutReply = 0; + for (Message message : messages) { + if (message.getStatus() == Message.STATUS_RECEIVED) { + ++numberOfMessagesWithoutReply; + } else { + return false; + } + } + return numberOfMessagesWithoutReply >= 3; + } + private void modifyForImage(final Builder builder, final UnreadConversation.Builder uBuilder, final Message message, final ArrayList messages) { try { @@ -571,7 +617,7 @@ public class NotificationService { Iterable intents = GeoHelper.createGeoIntentsFromMessage(message, this.mXmppConnectionService.getApplicationContext()); for (Intent intent : intents) { if (intent.resolveActivity(mXmppConnectionService.getPackageManager()) != null) { - return PendingIntent.getActivity(mXmppConnectionService, 18, intent, PendingIntent.FLAG_UPDATE_CURRENT); + return PendingIntent.getActivity(mXmppConnectionService, generateRequestCode(message.getConversation(), 18), intent, PendingIntent.FLAG_UPDATE_CURRENT); } } return createOpenConversationsIntent(); @@ -584,17 +630,25 @@ public class NotificationService { if (downloadMessageUuid != null) { viewConversationIntent.putExtra(ConversationActivity.EXTRA_DOWNLOAD_UUID, downloadMessageUuid); return PendingIntent.getActivity(mXmppConnectionService, - (conversationUuid.hashCode() % NOTIFICATION_ID_MULTIPLIER) + 8 * NOTIFICATION_ID_MULTIPLIER, + generateRequestCode(conversationUuid, 8), viewConversationIntent, PendingIntent.FLAG_UPDATE_CURRENT); } else { return PendingIntent.getActivity(mXmppConnectionService, - (conversationUuid.hashCode() % NOTIFICATION_ID_MULTIPLIER) + 10 * NOTIFICATION_ID_MULTIPLIER, + generateRequestCode(conversationUuid, 10), viewConversationIntent, PendingIntent.FLAG_UPDATE_CURRENT); } } + private int generateRequestCode(String uuid, int actionId) { + return (actionId * NOTIFICATION_ID_MULTIPLIER) + (uuid.hashCode() % NOTIFICATION_ID_MULTIPLIER); + } + + private int generateRequestCode(Conversation conversation, int actionId) { + return generateRequestCode(conversation.getUuid(), actionId); + } + private PendingIntent createDownloadIntent(final Message message) { return createContentIntent(message.getConversationUuid(), message.getUuid()); } @@ -608,7 +662,7 @@ public class NotificationService { intent.setAction(XmppConnectionService.ACTION_CLEAR_NOTIFICATION); if (conversation != null) { intent.putExtra("uuid", conversation.getUuid()); - return PendingIntent.getService(mXmppConnectionService, (conversation.getUuid().hashCode() % NOTIFICATION_ID_MULTIPLIER) + 12 * NOTIFICATION_ID_MULTIPLIER, intent, 0); + return PendingIntent.getService(mXmppConnectionService, generateRequestCode(conversation, 20), intent, 0); } return PendingIntent.getService(mXmppConnectionService, 0, intent, 0); } @@ -618,7 +672,7 @@ public class NotificationService { intent.setAction(XmppConnectionService.ACTION_REPLY_TO_CONVERSATION); intent.putExtra("uuid", conversation.getUuid()); intent.putExtra("dismiss_notification", dismissAfterReply); - int id = (conversation.getUuid().hashCode() % NOTIFICATION_ID_MULTIPLIER) + (dismissAfterReply ? 12 : 14) * NOTIFICATION_ID_MULTIPLIER; + final int id = generateRequestCode(conversation, dismissAfterReply ? 12 : 14); return PendingIntent.getService(mXmppConnectionService, id, intent, 0); } @@ -627,7 +681,15 @@ public class NotificationService { intent.setAction(XmppConnectionService.ACTION_MARK_AS_READ); intent.putExtra("uuid", conversation.getUuid()); intent.setPackage(mXmppConnectionService.getPackageName()); - return PendingIntent.getService(mXmppConnectionService, (conversation.getUuid().hashCode() % NOTIFICATION_ID_MULTIPLIER) + 16 * NOTIFICATION_ID_MULTIPLIER, intent, PendingIntent.FLAG_UPDATE_CURRENT); + return PendingIntent.getService(mXmppConnectionService, generateRequestCode(conversation, 16), intent, PendingIntent.FLAG_UPDATE_CURRENT); + } + + public PendingIntent createSnoozeIntent(Conversation conversation) { + final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class); + intent.setAction(XmppConnectionService.ACTION_SNOOZE); + intent.putExtra("uuid", conversation.getUuid()); + intent.setPackage(mXmppConnectionService.getPackageName()); + return PendingIntent.getService(mXmppConnectionService, generateRequestCode(conversation, 22), intent, PendingIntent.FLAG_UPDATE_CURRENT); } private PendingIntent createTryAgainIntent() { diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 6143ab02e..e3bbc2215 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -155,6 +155,7 @@ public class XmppConnectionService extends Service { public static final String ACTION_REPLY_TO_CONVERSATION = "reply_to_conversations"; public static final String ACTION_MARK_AS_READ = "mark_as_read"; + public static final String ACTION_SNOOZE = "snooze"; public static final String ACTION_CLEAR_NOTIFICATION = "clear_notification"; public static final String ACTION_TRY_AGAIN = "try_again"; public static final String ACTION_DISMISS_ERROR_NOTIFICATIONS = "dismiss_error"; @@ -652,6 +653,17 @@ public class XmppConnectionService extends Service { }); break; + case ACTION_SNOOZE: + mNotificationExecutor.execute(() -> { + final Conversation c = findConversationByUuid(uuid); + if (c == null) { + Log.d(Config.LOGTAG, "received snooze intent for unknown conversation (" + uuid + ")"); + return; + } + c.setMutedTill(System.currentTimeMillis() + 30 * 60 * 1000); + mNotificationService.clear(c); + updateConversation(c); + }); case AudioManager.RINGER_MODE_CHANGED_ACTION: if (dndOnSilentMode()) { refreshAllPresences(); diff --git a/src/main/res/drawable-hdpi/ic_notifications_paused_white_24dp.png b/src/main/res/drawable-hdpi/ic_notifications_paused_white_24dp.png new file mode 100644 index 000000000..9210e39ab Binary files /dev/null and b/src/main/res/drawable-hdpi/ic_notifications_paused_white_24dp.png differ diff --git a/src/main/res/drawable-mdpi/ic_notifications_paused_white_24dp.png b/src/main/res/drawable-mdpi/ic_notifications_paused_white_24dp.png new file mode 100644 index 000000000..8a48f2f72 Binary files /dev/null and b/src/main/res/drawable-mdpi/ic_notifications_paused_white_24dp.png differ diff --git a/src/main/res/drawable-xhdpi/ic_notifications_paused_white_24dp.png b/src/main/res/drawable-xhdpi/ic_notifications_paused_white_24dp.png new file mode 100644 index 000000000..cf566ec2a Binary files /dev/null and b/src/main/res/drawable-xhdpi/ic_notifications_paused_white_24dp.png differ diff --git a/src/main/res/drawable-xxhdpi/ic_notifications_paused_white_24dp.png b/src/main/res/drawable-xxhdpi/ic_notifications_paused_white_24dp.png new file mode 100644 index 000000000..e31b0d36f Binary files /dev/null and b/src/main/res/drawable-xxhdpi/ic_notifications_paused_white_24dp.png differ diff --git a/src/main/res/drawable-xxxhdpi/ic_notifications_paused_white_24dp.png b/src/main/res/drawable-xxxhdpi/ic_notifications_paused_white_24dp.png new file mode 100644 index 000000000..5c65e5bc2 Binary files /dev/null and b/src/main/res/drawable-xxxhdpi/ic_notifications_paused_white_24dp.png differ diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 7eb7f12a5..481a8b54b 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -758,4 +758,5 @@ Group chat successfully destroyed Group chat could not be destroyed You are not participating this group chat and have no write access at the moment.\nPlease go to the group chat details by pressing OK and send a private message to a moderator, administator or owner by long clicking one of the members name at the top of the list to ask for write access. + Snooze -- cgit v1.2.3