From 7a53bce439fc16ca5089dd689782a829940a9a83 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Tue, 16 Aug 2022 20:08:03 +0200 Subject: [PATCH] improve sending error notifications (#2671) * improve sending error notifications * rename draftNotification -> buildDraftNotification --- .../com/keylesspalace/tusky/MainActivity.kt | 7 ++ .../notifications/NotificationHelper.java | 2 - .../tusky/service/SendStatusService.kt | 68 +++++++++++++++---- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt b/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt index 4e1603fe0..8958feb01 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt @@ -175,6 +175,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje if (accountRequested && accountId != activeAccount.id) { accountManager.setActiveAccount(accountId) } + + val openDrafts = intent.getBooleanExtra(OPEN_DRAFTS, false) + if (canHandleMimeType(intent.type)) { // Sharing to Tusky from an external app if (accountRequested) { @@ -199,6 +202,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje } ) } + } else if (openDrafts) { + val intent = DraftsActivity.newIntent(this) + startActivity(intent) } else if (accountRequested && savedInstanceState == null) { // user clicked a notification, show notification tab showNotificationTab = true @@ -839,6 +845,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje private const val DRAWER_ITEM_ADD_ACCOUNT: Long = -13 private const val DRAWER_ITEM_ANNOUNCEMENTS: Long = 14 const val REDIRECT_URL = "redirectUrl" + const val OPEN_DRAFTS = "draft" } } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java index bb1599fb6..620d49a9a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java +++ b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java @@ -38,7 +38,6 @@ import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.core.app.RemoteInput; import androidx.core.app.TaskStackBuilder; -import androidx.core.content.ContextCompat; import androidx.work.Constraints; import androidx.work.NetworkType; import androidx.work.PeriodicWorkRequest; @@ -57,7 +56,6 @@ import com.keylesspalace.tusky.entity.Notification; import com.keylesspalace.tusky.entity.Poll; import com.keylesspalace.tusky.entity.PollOption; import com.keylesspalace.tusky.entity.Status; -import com.keylesspalace.tusky.network.MastodonApi; import com.keylesspalace.tusky.receiver.NotificationClearBroadcastReceiver; import com.keylesspalace.tusky.receiver.SendStatusBroadcastReceiver; import com.keylesspalace.tusky.util.StringUtils; diff --git a/app/src/main/java/com/keylesspalace/tusky/service/SendStatusService.kt b/app/src/main/java/com/keylesspalace/tusky/service/SendStatusService.kt index 13af53ec6..9471ee2fa 100644 --- a/app/src/main/java/com/keylesspalace/tusky/service/SendStatusService.kt +++ b/app/src/main/java/com/keylesspalace/tusky/service/SendStatusService.kt @@ -1,5 +1,6 @@ package com.keylesspalace.tusky.service +import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent @@ -12,9 +13,11 @@ import android.os.Build import android.os.IBinder import android.os.Parcelable import android.util.Log +import androidx.annotation.StringRes import androidx.core.app.NotificationCompat import androidx.core.app.ServiceCompat import at.connyduck.calladapter.networkresult.fold +import com.keylesspalace.tusky.MainActivity import com.keylesspalace.tusky.R import com.keylesspalace.tusky.appstore.EventHub import com.keylesspalace.tusky.appstore.StatusComposedEvent @@ -184,14 +187,15 @@ class SendStatusService : Service(), Injectable { statusesToSend.remove(statusId) saveStatusToDrafts(statusToSend) - val builder = NotificationCompat.Builder(this@SendStatusService, CHANNEL_ID) - .setSmallIcon(R.drawable.ic_notify) - .setContentTitle(getString(R.string.send_post_notification_error_title)) - .setContentText(getString(R.string.send_post_notification_saved_content)) - .setColor(getColor(R.color.notification_color)) + val notification = buildDraftNotification( + R.string.send_post_notification_error_title, + R.string.send_post_notification_saved_content, + statusToSend.accountId, + statusId + ) notificationManager.cancel(statusId) - notificationManager.notify(errorNotificationId--, builder.build()) + notificationManager.notify(errorNotificationId--, notification) } else { // a network problem occurred, let's retry sending the status retrySending(statusId) @@ -227,15 +231,18 @@ class SendStatusService : Service(), Injectable { saveStatusToDrafts(statusToCancel) - val builder = NotificationCompat.Builder(this@SendStatusService, CHANNEL_ID) - .setSmallIcon(R.drawable.ic_notify) - .setContentTitle(getString(R.string.send_post_notification_cancel_title)) - .setContentText(getString(R.string.send_post_notification_saved_content)) - .setColor(getColor(R.color.notification_color)) + val notification = buildDraftNotification( + R.string.send_post_notification_cancel_title, + R.string.send_post_notification_saved_content, + statusToCancel.accountId, + statusId + ) - notificationManager.notify(statusId, builder.build()) + notificationManager.notify(statusId, notification) delay(5000) + + stopSelfWhenDone() } } @@ -259,7 +266,41 @@ class SendStatusService : Service(), Injectable { private fun cancelSendingIntent(statusId: Int): PendingIntent { val intent = Intent(this, SendStatusService::class.java) intent.putExtra(KEY_CANCEL, statusId) - return PendingIntent.getService(this, statusId, intent, NotificationHelper.pendingIntentFlags(false)) + return PendingIntent.getService( + this, + statusId, + intent, + NotificationHelper.pendingIntentFlags(false) + ) + } + + private fun buildDraftNotification( + @StringRes title: Int, + @StringRes content: Int, + accountId: Long, + statusId: Int + ): Notification { + + val intent = Intent(this, MainActivity::class.java) + intent.putExtra(NotificationHelper.ACCOUNT_ID, accountId) + intent.putExtra(MainActivity.OPEN_DRAFTS, true) + + val pendingIntent = PendingIntent.getActivity( + this, + statusId, + intent, + NotificationHelper.pendingIntentFlags(false) + ) + + return NotificationCompat.Builder(this@SendStatusService, CHANNEL_ID) + .setSmallIcon(R.drawable.ic_notify) + .setContentTitle(getString(title)) + .setContentText(getString(content)) + .setColor(getColor(R.color.notification_color)) + .setAutoCancel(true) + .setOngoing(false) + .setContentIntent(pendingIntent) + .build() } override fun onDestroy() { @@ -279,7 +320,6 @@ class SendStatusService : Service(), Injectable { private var sendingNotificationId = -1 // use negative ids to not clash with other notis private var errorNotificationId = Int.MIN_VALUE // use even more negative ids to not clash with other notis - @JvmStatic fun sendStatusIntent( context: Context, statusToSend: StatusToSend