Skip to content

Commit

Permalink
WIP: 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 41f86f5 commit 6be09c0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import android.widget.Toast;

Expand All @@ -26,6 +27,7 @@
import org.json.JSONObject;

import java.util.HashSet;
import java.util.NoSuchElementException;

public class NotificationController implements NotificationEventReceiver, StatusCheckable {
private final HashSet<Integer> active_notifications = new HashSet<>();
Expand Down Expand Up @@ -159,6 +161,19 @@ public void tellActionRequestFailed(){
Toast.makeText(mContext, R.string.quick_action_failed, Toast.LENGTH_LONG).show();
}

public Notification getNotificationById(int id) throws NoSuchElementException {
for(StatusBarNotification notification: mNotificationManager.getActiveNotifications()){
if(notification.getId() == id){
return notification.getNotification();
}
}
throw new NoSuchElementException("Can not find notification with specified id: " + id);
}

public void postNotification(int id, Notification notification){
mNotificationManager.notify(id, notification);
}

public INextcloudAbstractAPI getAPI(){
return mServiceSettings.getAPIFromSettings();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.polar.nextcloudservices.Notification.NotificationEvent.NOTIFICATION_EVENT_FASTREPLY;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
Expand Down Expand Up @@ -276,6 +277,7 @@ public void onNotificationEvent(NotificationEvent event, Intent intent,
Thread thread = new Thread(() -> {
try {
api.sendTalkReply(chatroom, reply);
appendQuickReply(controller, notification_id, reply);
} catch (Exception e) {
Log.e(TAG, e.toString());
controller.tellActionRequestFailed();
Expand Down Expand Up @@ -311,6 +313,18 @@ public void onNotificationEvent(NotificationEvent event, Intent intent,
}
}

private void appendQuickReply(NotificationController controller,
int notification_id, String text){
Notification notification = controller.getNotificationById(notification_id);
Context context = controller.getContext();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, notification);
NotificationCompat.MessagingStyle style = NotificationCompat
.MessagingStyle.extractMessagingStyleFromNotification(notification);
style.addMessage(text, CommonUtil.getTimestamp(), "You");
notification = builder.setStyle(style).build();
controller.postNotification(notification_id, notification);
}

@Override
public int getPriority() {
return priority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ public static void safeSleep(long millis){
Log.e(TAG, "Interrupted while sleeping " + millis + "ms");
}
}

public static long getTimestamp(){
return System.currentTimeMillis();
}
}

0 comments on commit 6be09c0

Please sign in to comment.