add snooze button to notification

This commit is contained in:
Daniel Gultsch 2018-01-28 13:14:30 +01:00
parent c7bdc343ec
commit 3e9224bde9
3 changed files with 101 additions and 23 deletions

View File

@ -395,30 +395,65 @@ public class NotificationService {
modifyForTextOnly(mBuilder, mUnreadBuilder, messages); modifyForTextOnly(mBuilder, mUnreadBuilder, messages);
} }
RemoteInput remoteInput = new RemoteInput.Builder("text_reply").setLabel(UIHelper.getMessageHint(mXmppConnectionService, conversation)).build(); 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, "Mark As Read", createReadPendingIntent(conversation)).build(); PendingIntent markAsReadPendingIntent = createReadPendingIntent(conversation);
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(R.drawable.ic_send_text_offline, "Reply", createReplyIntent(conversation, false)).addRemoteInput(remoteInput).build(); NotificationCompat.Action markReadAction = new NotificationCompat.Action.Builder(
NotificationCompat.Action wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_wear_reply, "Reply", createReplyIntent(conversation, true)).addRemoteInput(remoteInput).build(); 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)); mBuilder.extend(new NotificationCompat.WearableExtender().addAction(wearReplyAction));
mUnreadBuilder.setReplyAction(createReplyIntent(conversation, true), remoteInput); mUnreadBuilder.setReplyAction(createReplyIntent(conversation, true), remoteInput);
mUnreadBuilder.setReadPendingIntent(createReadPendingIntent(conversation)); mUnreadBuilder.setReadPendingIntent(markAsReadPendingIntent);
mBuilder.extend(new NotificationCompat.CarExtender().setUnreadConversation(mUnreadBuilder.build())); mBuilder.extend(new NotificationCompat.CarExtender().setUnreadConversation(mUnreadBuilder.build()));
int addedActionsCount = 1;
mBuilder.addAction(markReadAction);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mBuilder.addAction(markReadAction);
mBuilder.addAction(replyAction); mBuilder.addAction(replyAction);
++addedActionsCount;
} }
if ((message = getFirstDownloadableMessage(messages)) != null) {
mBuilder.addAction( if (displaySnoozeAction(messages)) {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? String label = mXmppConnectionService.getString(R.string.snooze);
R.drawable.ic_file_download_white_24dp : R.drawable.ic_action_download, PendingIntent pendingSnoozeIntent = createSnoozeIntent(conversation);
mXmppConnectionService.getResources().getString(R.string.download_x_file, NotificationCompat.Action snoozeAction = new NotificationCompat.Action.Builder(
UIHelper.getFileDescriptionString(mXmppConnectionService, message)), R.drawable.ic_notifications_paused_white_24dp,
createDownloadIntent(message) label,
); pendingSnoozeIntent).build();
mBuilder.addAction(snoozeAction);
++addedActionsCount;
} }
if ((message = getFirstLocationMessage(messages)) != null) { if (addedActionsCount < 3) {
mBuilder.addAction(R.drawable.ic_room_white_24dp, final Message firstLocationMessage = getFirstLocationMessage(messages);
mXmppConnectionService.getString(R.string.show_location), if (firstLocationMessage != null) {
createShowLocationIntent(message)); 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) { if (conversation.getMode() == Conversation.MODE_SINGLE) {
@ -436,6 +471,18 @@ public class NotificationService {
return mBuilder; 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, private void modifyForImage(final Builder builder, final UnreadConversation.Builder uBuilder,
final Message message, final ArrayList<Message> messages) { final Message message, final ArrayList<Message> messages) {
try { try {
@ -562,7 +609,7 @@ public class NotificationService {
Iterable<Intent> intents = GeoHelper.createGeoIntentsFromMessage(message); Iterable<Intent> intents = GeoHelper.createGeoIntentsFromMessage(message);
for (Intent intent : intents) { for (Intent intent : intents) {
if (intent.resolveActivity(mXmppConnectionService.getPackageManager()) != null) { 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(); return createOpenConversationsIntent();
@ -575,17 +622,25 @@ public class NotificationService {
if (downloadMessageUuid != null) { if (downloadMessageUuid != null) {
viewConversationIntent.putExtra(ConversationActivity.EXTRA_DOWNLOAD_UUID, downloadMessageUuid); viewConversationIntent.putExtra(ConversationActivity.EXTRA_DOWNLOAD_UUID, downloadMessageUuid);
return PendingIntent.getActivity(mXmppConnectionService, return PendingIntent.getActivity(mXmppConnectionService,
(conversationUuid.hashCode() % NOTIFICATION_ID_MULTIPLIER) + 8 * NOTIFICATION_ID_MULTIPLIER, generateRequestCode(conversationUuid, 8),
viewConversationIntent, viewConversationIntent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
} else { } else {
return PendingIntent.getActivity(mXmppConnectionService, return PendingIntent.getActivity(mXmppConnectionService,
(conversationUuid.hashCode() % NOTIFICATION_ID_MULTIPLIER) + 10 * NOTIFICATION_ID_MULTIPLIER, generateRequestCode(conversationUuid, 10),
viewConversationIntent, viewConversationIntent,
PendingIntent.FLAG_UPDATE_CURRENT); 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) { private PendingIntent createDownloadIntent(final Message message) {
return createContentIntent(message.getConversationUuid(), message.getUuid()); return createContentIntent(message.getConversationUuid(), message.getUuid());
} }
@ -599,7 +654,7 @@ public class NotificationService {
intent.setAction(XmppConnectionService.ACTION_CLEAR_NOTIFICATION); intent.setAction(XmppConnectionService.ACTION_CLEAR_NOTIFICATION);
if (conversation != null) { if (conversation != null) {
intent.putExtra("uuid", conversation.getUuid()); 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); return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
} }
@ -609,7 +664,7 @@ public class NotificationService {
intent.setAction(XmppConnectionService.ACTION_REPLY_TO_CONVERSATION); intent.setAction(XmppConnectionService.ACTION_REPLY_TO_CONVERSATION);
intent.putExtra("uuid",conversation.getUuid()); intent.putExtra("uuid",conversation.getUuid());
intent.putExtra("dismiss_notification",dismissAfterReply); 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); return PendingIntent.getService(mXmppConnectionService, id, intent, 0);
} }
@ -618,7 +673,15 @@ public class NotificationService {
intent.setAction(XmppConnectionService.ACTION_MARK_AS_READ); intent.setAction(XmppConnectionService.ACTION_MARK_AS_READ);
intent.putExtra("uuid", conversation.getUuid()); intent.putExtra("uuid", conversation.getUuid());
intent.setPackage(mXmppConnectionService.getPackageName()); 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() { private PendingIntent createTryAgainIntent() {

View File

@ -149,6 +149,7 @@ public class XmppConnectionService extends Service {
public static final String ACTION_REPLY_TO_CONVERSATION = "reply_to_conversations"; 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_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_CLEAR_NOTIFICATION = "clear_notification";
public static final String ACTION_DISMISS_ERROR_NOTIFICATIONS = "dismiss_error"; public static final String ACTION_DISMISS_ERROR_NOTIFICATIONS = "dismiss_error";
public static final String ACTION_TRY_AGAIN = "try_again"; public static final String ACTION_TRY_AGAIN = "try_again";
@ -646,6 +647,17 @@ public class XmppConnectionService extends Service {
}); });
break; 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: case AudioManager.RINGER_MODE_CHANGED_ACTION:
if (dndOnSilentMode()) { if (dndOnSilentMode()) {
refreshAllPresences(); refreshAllPresences();

View File

@ -452,6 +452,9 @@
<string name="two_hours">2 hours</string> <string name="two_hours">2 hours</string>
<string name="eight_hours">8 hours</string> <string name="eight_hours">8 hours</string>
<string name="until_further_notice">Until further notice</string> <string name="until_further_notice">Until further notice</string>
<string name="snooze">Snooze</string>
<string name="reply">Reply</string>
<string name="mark_as_read">Mark as read</string>
<string name="pref_input_options">Input</string> <string name="pref_input_options">Input</string>
<string name="pref_enter_is_send">Enter is send</string> <string name="pref_enter_is_send">Enter is send</string>
<string name="pref_enter_is_send_summary">Use enter key to send message</string> <string name="pref_enter_is_send_summary">Use enter key to send message</string>