diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-01-29 21:26:05 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-01-29 21:26:05 +0100 |
commit | ce73f31bb50261e2b2289271291bedad6c7fc70a (patch) | |
tree | 7a6b466603fe27e35eaeaa39fe0a6ce7cd71f890 /src/main | |
parent | bed3b18312174edd1f8972d34bcb1e2bc22418c0 (diff) |
add snooze button to notification
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/de/pixart/messenger/services/NotificationService.java | 108 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/services/XmppConnectionService.java | 12 | ||||
-rw-r--r-- | src/main/res/drawable-hdpi/ic_notifications_paused_white_24dp.png | bin | 0 -> 309 bytes | |||
-rw-r--r-- | src/main/res/drawable-mdpi/ic_notifications_paused_white_24dp.png | bin | 0 -> 235 bytes | |||
-rw-r--r-- | src/main/res/drawable-xhdpi/ic_notifications_paused_white_24dp.png | bin | 0 -> 351 bytes | |||
-rw-r--r-- | src/main/res/drawable-xxhdpi/ic_notifications_paused_white_24dp.png | bin | 0 -> 482 bytes | |||
-rw-r--r-- | src/main/res/drawable-xxxhdpi/ic_notifications_paused_white_24dp.png | bin | 0 -> 616 bytes | |||
-rw-r--r-- | src/main/res/values/strings.xml | 1 |
8 files changed, 98 insertions, 23 deletions
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<Message> 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<Message> messages) { try { @@ -571,7 +617,7 @@ public class NotificationService { Iterable<Intent> 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 Binary files differnew file mode 100644 index 000000000..9210e39ab --- /dev/null +++ b/src/main/res/drawable-hdpi/ic_notifications_paused_white_24dp.png 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 Binary files differnew file mode 100644 index 000000000..8a48f2f72 --- /dev/null +++ b/src/main/res/drawable-mdpi/ic_notifications_paused_white_24dp.png 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 Binary files differnew file mode 100644 index 000000000..cf566ec2a --- /dev/null +++ b/src/main/res/drawable-xhdpi/ic_notifications_paused_white_24dp.png 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 Binary files differnew file mode 100644 index 000000000..e31b0d36f --- /dev/null +++ b/src/main/res/drawable-xxhdpi/ic_notifications_paused_white_24dp.png 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 Binary files differnew file mode 100644 index 000000000..5c65e5bc2 --- /dev/null +++ b/src/main/res/drawable-xxxhdpi/ic_notifications_paused_white_24dp.png 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 @@ <string name="destroy_muc_succeed">Group chat successfully destroyed</string> <string name="destroy_muc_failed">Group chat could not be destroyed</string> <string name="no_write_access_in_public_muc">You are not participating this group chat and have no write access at the moment.\nPlease go to the group chat details by pressing <b>OK</b> 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.</string> + <string name="snooze">Snooze</string> </resources> |