From 1fab3871c957ca62d72c87960b132265b3ae2ebb Mon Sep 17 00:00:00 2001 From: Andrewerr Date: Fri, 5 Jan 2024 21:28:43 +0100 Subject: [PATCH] Basic implementation of telling that quick reply done(#75) --- .../spreed/NextcloudTalkProcessor.java | 110 ++++++++++-------- .../spreed/chat/ChatController.java | 6 +- 2 files changed, 66 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/com/polar/nextcloudservices/Notification/Processors/spreed/NextcloudTalkProcessor.java b/app/src/main/java/com/polar/nextcloudservices/Notification/Processors/spreed/NextcloudTalkProcessor.java index f0b2221..74276fc 100644 --- a/app/src/main/java/com/polar/nextcloudservices/Notification/Processors/spreed/NextcloudTalkProcessor.java +++ b/app/src/main/java/com/polar/nextcloudservices/Notification/Processors/spreed/NextcloudTalkProcessor.java @@ -64,7 +64,8 @@ static private PendingIntent getReplyIntent(Context context, intent.putExtra("notification_id", rawNotification.getInt("notification_id")); intent.putExtra("notification_event", NOTIFICATION_EVENT_FASTREPLY); String[] link = rawNotification.getString("link").split("/"); // use provided link to extract talk chatroom id - intent.putExtra("talk_chatroom", link[link.length-1]); + intent.putExtra("talk_chatroom", cleanUpChatroom(link[link.length-1])); + intent.putExtra("talk_link", cleanUpChatroom(rawNotification.getString("link"))); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { return PendingIntent.getBroadcast( @@ -102,8 +103,8 @@ private Person getPersonFromNotification(@NonNull NotificationController control builder.setKey(key); final String name = rawNotification.getJSONObject("subjectRichParameters") .getJSONObject("call").getString("name"); - //NOTE:Nextcloud Talk does not seem to provide ability for setting avatar for calls - // so it is not fetched here + //NOTE: Nextcloud Talk does not seem to provide ability for setting avatar for calls + // so it is not fetched here return builder.setName(name).build(); } } @@ -254,62 +255,73 @@ private static String cleanUpChatroom(@NonNull String chatroom){ } } - @Override - public void onNotificationEvent(NotificationEvent event, Intent intent, - NotificationController controller) { - if (event == NOTIFICATION_EVENT_FASTREPLY) { - final String chatroom = - cleanUpChatroom( - Objects.requireNonNull(intent.getStringExtra("talk_chatroom"))); // the string send by spreed is chatroomid - final int notification_id = intent.getIntExtra("notification_id", -1); - if (notification_id < 0) { - Log.wtf(TAG, "Bad notification id: " + notification_id); - return; - } - Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); - if (remoteInput == null) { - Log.e(TAG, "Reply event has null reply text"); - return; + private void onFastReply(Intent intent, NotificationController controller){ + final String chatroom = + cleanUpChatroom( + Objects.requireNonNull(intent.getStringExtra("talk_chatroom"))); // the string send by spreed is chatroomid + final String chatroom_link = cleanUpChatroom( + Objects.requireNonNull(intent.getStringExtra("talk_link"))); + final int notification_id = intent.getIntExtra("notification_id", -1); + if (notification_id < 0) { + Log.wtf(TAG, "Bad notification id: " + notification_id); + return; + } + Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); + if (remoteInput == null) { + Log.e(TAG, "Reply event has null reply text"); + return; + } + final String reply = + Objects.requireNonNull(remoteInput.getCharSequence(KEY_TEXT_REPLY)).toString(); + INextcloudAbstractAPI api = controller.getAPI(); + Thread thread = new Thread(() -> { + try { + api.sendTalkReply(chatroom, reply); + appendQuickReply(controller, + mChatController.getNotificationIdByRoom(chatroom_link), reply); + } catch (Exception e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + ///controller.tellActionRequestFailed(); } - final String reply = - Objects.requireNonNull(remoteInput.getCharSequence(KEY_TEXT_REPLY)).toString(); - INextcloudAbstractAPI api = controller.getAPI(); + }); + thread.start(); + } + + private void onDeleteNotification(Intent intent, NotificationController controller){ + //NOTE: we actually can not get here if remove on dismiss disabled + // so we may safely ignore checking settings + final int notification_id = intent.getIntExtra("notification_id", -1); + if(notification_id == -1){ + Log.e(TAG, "Invalid notification id, can not properly handle notification deletion"); + return; + } + Chat chat = mChatController.getChatByNotificationId(notification_id); + if(chat == null){ + Log.wtf(TAG, "Can not find chat by notification id " + notification_id); + return; + } + INextcloudAbstractAPI api = controller.getAPI(); + for(ChatMessage message : chat.messages){ Thread thread = new Thread(() -> { try { - api.sendTalkReply(chatroom, reply); - appendQuickReply(controller, notification_id, reply); + api.removeNotification(message.notification_id); } catch (Exception e) { Log.e(TAG, e.toString()); - controller.tellActionRequestFailed(); } }); thread.start(); + } + mChatController.removeChat(chat); + } + @Override + public void onNotificationEvent(NotificationEvent event, Intent intent, + NotificationController controller) { + if (event == NOTIFICATION_EVENT_FASTREPLY) { + onFastReply(intent, controller); } else if(event == NOTIFICATION_EVENT_DELETE){ - //NOTE: we actually can not get here if remove on dismiss disabled - // so we may safely ignore checking settings - final int notification_id = intent.getIntExtra("notification_id", -1); - if(notification_id == -1){ - Log.e(TAG, "Invalid notification id, can not properly handle notification deletion"); - return; - } - Chat chat = mChatController.getChatByNotificationId(notification_id); - if(chat == null){ - Log.wtf(TAG, "Can not find chat by notification id " + notification_id); - return; - } - INextcloudAbstractAPI api = controller.getAPI(); - for(ChatMessage message : chat.messages){ - Thread thread = new Thread(() -> { - try { - api.removeNotification(message.notification_id); - } catch (Exception e) { - Log.e(TAG, e.toString()); - } - }); - thread.start(); - } - mChatController.removeChat(chat); + onDeleteNotification(intent, controller); } } diff --git a/app/src/main/java/com/polar/nextcloudservices/Notification/Processors/spreed/chat/ChatController.java b/app/src/main/java/com/polar/nextcloudservices/Notification/Processors/spreed/chat/ChatController.java index c965d4d..8824e56 100644 --- a/app/src/main/java/com/polar/nextcloudservices/Notification/Processors/spreed/chat/ChatController.java +++ b/app/src/main/java/com/polar/nextcloudservices/Notification/Processors/spreed/chat/ChatController.java @@ -9,6 +9,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.NoSuchElementException; /** * A generic controller of a chat logic. @@ -38,7 +39,10 @@ private Chat getChat(String room, int nc_notification_id){ return chat_by_room.get(room); } - public Integer getNotificationIdByRoom(String room){ + public @NonNull Integer getNotificationIdByRoom(String room) throws NoSuchElementException{ + if(!notification_id_by_room.containsKey(room)){ + throw new NoSuchElementException("Can not find room: " + room); + } return notification_id_by_room.get(room); }