Skip to content

Commit

Permalink
Basic implementation of telling that quick reply done(#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xf104a committed Jan 5, 2024
1 parent 6be09c0 commit 1fab387
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 1fab387

Please sign in to comment.