improve sending error notifications (#2671)

* improve sending error notifications

* rename draftNotification -> buildDraftNotification
This commit is contained in:
Konrad Pozniak 2022-08-16 20:08:03 +02:00 committed by GitHub
parent edbc624625
commit 7a53bce439
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 16 deletions

View File

@ -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"
}
}

View File

@ -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;

View File

@ -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