Kaynağa Gözat

Implement mark as read via notification

Resolves #2164

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 3 yıl önce
ebeveyn
işleme
f977b566a5

+ 1 - 0
app/src/main/AndroidManifest.xml

@@ -199,6 +199,7 @@
         </receiver>
 
         <receiver android:name=".receivers.DirectReplyReceiver" />
+        <receiver android:name=".receivers.MarkAsReadReceiver" />
 
         <service
             android:name=".utils.SyncService"

+ 35 - 0
app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java

@@ -52,6 +52,7 @@ import com.nextcloud.talk.models.json.notifications.NotificationOverall;
 import com.nextcloud.talk.models.json.push.DecryptedPushMessage;
 import com.nextcloud.talk.models.json.push.NotificationUser;
 import com.nextcloud.talk.receivers.DirectReplyReceiver;
+import com.nextcloud.talk.receivers.MarkAsReadReceiver;
 import com.nextcloud.talk.utils.ApiUtils;
 import com.nextcloud.talk.utils.DoNotDisturbUtils;
 import com.nextcloud.talk.utils.NotificationUtils;
@@ -401,6 +402,7 @@ public class NotificationWorker extends Worker {
 
         notificationBuilder.setOnlyAlertOnce(true);
         addReplyAction(notificationBuilder, systemNotificationId);
+        addMarkAsReadAction(notificationBuilder, systemNotificationId);
 
         if ("user".equals(userType) || "guest".equals(userType)) {
             String baseUrl = signatureVerification.getUserEntity().getBaseUrl();
@@ -413,6 +415,39 @@ public class NotificationWorker extends Worker {
         notificationBuilder.setStyle(getStyle(person.build(), style));
     }
 
+    private void addMarkAsReadAction(NotificationCompat.Builder notificationBuilder, int systemNotificationId) {
+        String label = context.getResources().getString(R.string.nc_mark_as_read);
+
+        // Build a PendingIntent for the reply action
+        Intent actualIntent = new Intent(context, MarkAsReadReceiver.class);
+
+        // NOTE - systemNotificationId is an internal ID used on the device only.
+        // It is NOT the same as the notification ID used in communication with the server.
+        actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_INTERNAL_USER_ID(), Objects.requireNonNull(signatureVerification.getUserEntity()).getId());
+        actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_SYSTEM_NOTIFICATION_ID(), systemNotificationId);
+        actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), decryptedPushMessage.getId());
+        if (decryptedPushMessage.getNotificationId() != null) {
+            actualIntent.putExtra(BundleKeys.KEY_MESSAGE_ID, decryptedPushMessage.getNotificationId().intValue());
+        }
+
+        int intentFlag;
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+            intentFlag = PendingIntent.FLAG_MUTABLE|PendingIntent.FLAG_UPDATE_CURRENT;
+        } else {
+            intentFlag = PendingIntent.FLAG_UPDATE_CURRENT;
+        }
+        PendingIntent pendingIntent =
+            PendingIntent.getBroadcast(context, systemNotificationId, actualIntent, intentFlag);
+
+        NotificationCompat.Action action =
+            new NotificationCompat.Action.Builder(R.drawable.ic_eye, label, pendingIntent)
+                .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
+                .setShowsUserInterface(false)
+                .build();
+
+        notificationBuilder.addAction(action);
+    }
+
     @RequiresApi(api = Build.VERSION_CODES.N)
     private void addReplyAction(NotificationCompat.Builder notificationBuilder, int systemNotificationId) {
         String replyLabel = context.getResources().getString(R.string.nc_reply);

+ 125 - 0
app/src/main/java/com/nextcloud/talk/receivers/MarkAsReadReceiver.kt

@@ -0,0 +1,125 @@
+/*
+ * Nextcloud Talk application
+ *
+ * @author Andy Scherzinger
+ * @author Dariusz Olszewski
+ * Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
+ * Copyright (C) 2022 Dariusz Olszewski <starypatyk@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.talk.receivers
+
+import android.app.NotificationManager
+import android.content.BroadcastReceiver
+import android.content.Context
+import android.content.Intent
+import android.os.Build
+import android.util.Log
+import androidx.annotation.RequiresApi
+import autodagger.AutoInjector
+import com.nextcloud.talk.api.NcApi
+import com.nextcloud.talk.application.NextcloudTalkApplication
+import com.nextcloud.talk.models.database.UserEntity
+import com.nextcloud.talk.models.json.generic.GenericOverall
+import com.nextcloud.talk.utils.ApiUtils
+import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_INTERNAL_USER_ID
+import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_MESSAGE_ID
+import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN
+import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_SYSTEM_NOTIFICATION_ID
+import com.nextcloud.talk.utils.database.user.UserUtils
+import io.reactivex.Observer
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.disposables.Disposable
+import io.reactivex.schedulers.Schedulers
+import javax.inject.Inject
+
+@AutoInjector(NextcloudTalkApplication::class)
+class MarkAsReadReceiver : BroadcastReceiver() {
+
+    @Inject
+    lateinit var userUtils: UserUtils
+
+    @Inject
+    lateinit var ncApi: NcApi
+
+    lateinit var context: Context
+    lateinit var currentUser: UserEntity
+    private var systemNotificationId: Int? = null
+    private var roomToken: String? = null
+    private var messageId: Int = 0
+
+    init {
+        NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
+    }
+
+    override fun onReceive(receiveContext: Context, intent: Intent?) {
+        context = receiveContext
+
+        // NOTE - systemNotificationId is an internal ID used on the device only.
+        // It is NOT the same as the notification ID used in communication with the server.
+        systemNotificationId = intent!!.getIntExtra(KEY_SYSTEM_NOTIFICATION_ID, 0)
+        roomToken = intent.getStringExtra(KEY_ROOM_TOKEN)
+        messageId = intent.getIntExtra(KEY_MESSAGE_ID, 0)
+
+        val id = intent.getLongExtra(KEY_INTERNAL_USER_ID, userUtils.currentUser!!.id)
+        currentUser = userUtils.getUserWithId(id)
+
+        markAsRead()
+    }
+
+    private fun markAsRead() {
+        val credentials = ApiUtils.getCredentials(currentUser.username, currentUser.token)
+        val apiVersion = ApiUtils.getChatApiVersion(currentUser, intArrayOf(1))
+        val url = ApiUtils.getUrlForSetChatReadMarker(
+            apiVersion,
+            currentUser.baseUrl,
+            roomToken
+        )
+
+        ncApi.setChatReadMarker(credentials, url, messageId)
+            ?.subscribeOn(Schedulers.io())
+            ?.observeOn(AndroidSchedulers.mainThread())
+            ?.subscribe(object : Observer<GenericOverall> {
+                override fun onSubscribe(d: Disposable) {
+                    // unused atm
+                }
+
+                @RequiresApi(Build.VERSION_CODES.N)
+                override fun onNext(genericOverall: GenericOverall) {
+                    cancelNotification(systemNotificationId!!)
+                }
+
+                @RequiresApi(Build.VERSION_CODES.N)
+                override fun onError(e: Throwable) {
+                    Log.e(TAG, "Failed to send reply", e)
+                }
+
+                override fun onComplete() {
+                    // unused atm
+                }
+            })
+    }
+
+    @RequiresApi(Build.VERSION_CODES.N)
+    private fun cancelNotification(notificationId: Int) {
+        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
+        notificationManager.cancel(notificationId)
+    }
+
+    companion object {
+        const val TAG = "MarkAsReadReceiver"
+    }
+}