2018-04-14 06:37:21 +10:00
|
|
|
package com.keylesspalace.tusky.service
|
|
|
|
|
|
|
|
import android.app.NotificationChannel
|
|
|
|
import android.app.NotificationManager
|
|
|
|
import android.app.PendingIntent
|
|
|
|
import android.app.Service
|
2018-06-29 06:22:29 +10:00
|
|
|
import android.content.ClipData
|
|
|
|
import android.content.ClipDescription
|
2018-04-14 06:37:21 +10:00
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Build
|
|
|
|
import android.os.IBinder
|
|
|
|
import android.os.Parcelable
|
2018-12-18 01:25:35 +11:00
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
|
import androidx.core.app.ServiceCompat
|
|
|
|
import androidx.core.content.ContextCompat
|
2018-04-14 06:37:21 +10:00
|
|
|
import com.keylesspalace.tusky.R
|
2018-05-27 18:22:12 +10:00
|
|
|
import com.keylesspalace.tusky.appstore.EventHub
|
|
|
|
import com.keylesspalace.tusky.appstore.StatusComposedEvent
|
2019-10-03 05:28:12 +10:00
|
|
|
import com.keylesspalace.tusky.appstore.StatusScheduledEvent
|
2021-01-22 04:57:09 +11:00
|
|
|
import com.keylesspalace.tusky.components.drafts.DraftHelper
|
2022-03-10 06:50:23 +11:00
|
|
|
import com.keylesspalace.tusky.components.notifications.NotificationHelper
|
2018-04-14 06:37:21 +10:00
|
|
|
import com.keylesspalace.tusky.db.AccountManager
|
|
|
|
import com.keylesspalace.tusky.di.Injectable
|
2019-08-23 04:30:08 +10:00
|
|
|
import com.keylesspalace.tusky.entity.NewPoll
|
|
|
|
import com.keylesspalace.tusky.entity.NewStatus
|
2018-04-14 06:37:21 +10:00
|
|
|
import com.keylesspalace.tusky.entity.Status
|
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
|
|
|
import dagger.android.AndroidInjection
|
2021-06-25 05:23:29 +10:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.SupervisorJob
|
|
|
|
import kotlinx.coroutines.launch
|
2021-03-21 22:42:28 +11:00
|
|
|
import kotlinx.parcelize.Parcelize
|
2018-04-14 06:37:21 +10:00
|
|
|
import retrofit2.Call
|
|
|
|
import retrofit2.Callback
|
|
|
|
import retrofit2.Response
|
2021-06-29 05:13:24 +10:00
|
|
|
import java.util.Timer
|
|
|
|
import java.util.TimerTask
|
2018-04-14 06:37:21 +10:00
|
|
|
import java.util.concurrent.ConcurrentHashMap
|
2018-05-27 18:22:12 +10:00
|
|
|
import java.util.concurrent.TimeUnit
|
2018-04-14 06:37:21 +10:00
|
|
|
import javax.inject.Inject
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
class SendStatusService : Service(), Injectable {
|
2018-04-14 06:37:21 +10:00
|
|
|
|
|
|
|
@Inject
|
|
|
|
lateinit var mastodonApi: MastodonApi
|
|
|
|
@Inject
|
|
|
|
lateinit var accountManager: AccountManager
|
2018-05-27 18:22:12 +10:00
|
|
|
@Inject
|
|
|
|
lateinit var eventHub: EventHub
|
|
|
|
@Inject
|
2021-01-22 04:57:09 +11:00
|
|
|
lateinit var draftHelper: DraftHelper
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2021-06-25 05:23:29 +10:00
|
|
|
private val supervisorJob = SupervisorJob()
|
|
|
|
private val serviceScope = CoroutineScope(Dispatchers.Main + supervisorJob)
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
private val statusesToSend = ConcurrentHashMap<Int, StatusToSend>()
|
2018-04-14 06:37:21 +10:00
|
|
|
private val sendCalls = ConcurrentHashMap<Int, Call<Status>>()
|
|
|
|
|
|
|
|
private val timer = Timer()
|
|
|
|
|
2018-04-18 04:07:14 +10:00
|
|
|
private val notificationManager by lazy { getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager }
|
2018-04-14 06:37:21 +10:00
|
|
|
|
|
|
|
override fun onCreate() {
|
|
|
|
AndroidInjection.inject(this)
|
|
|
|
super.onCreate()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBind(intent: Intent): IBinder? {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
if (intent.hasExtra(KEY_STATUS)) {
|
|
|
|
val statusToSend = intent.getParcelableExtra<StatusToSend>(KEY_STATUS)
|
|
|
|
?: throw IllegalStateException("SendStatusService started without $KEY_STATUS extra")
|
2018-04-14 06:37:21 +10:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2022-03-21 06:21:42 +11:00
|
|
|
val channel = NotificationChannel(CHANNEL_ID, getString(R.string.send_post_notification_channel_name), NotificationManager.IMPORTANCE_LOW)
|
2018-04-14 06:37:21 +10:00
|
|
|
notificationManager.createNotificationChannel(channel)
|
|
|
|
}
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
var notificationText = statusToSend.warningText
|
2018-04-14 06:37:21 +10:00
|
|
|
if (notificationText.isBlank()) {
|
2022-03-21 06:21:42 +11:00
|
|
|
notificationText = statusToSend.text
|
2018-04-14 06:37:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
|
2021-06-29 05:13:24 +10:00
|
|
|
.setSmallIcon(R.drawable.ic_notify)
|
2022-03-21 06:21:42 +11:00
|
|
|
.setContentTitle(getString(R.string.send_post_notification_title))
|
2021-06-29 05:13:24 +10:00
|
|
|
.setContentText(notificationText)
|
|
|
|
.setProgress(1, 0, true)
|
|
|
|
.setOngoing(true)
|
2022-03-10 06:50:23 +11:00
|
|
|
.setColor(ContextCompat.getColor(this, R.color.notification_color))
|
2021-06-29 05:13:24 +10:00
|
|
|
.addAction(0, getString(android.R.string.cancel), cancelSendingIntent(sendingNotificationId))
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
if (statusesToSend.size == 0 || Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2018-04-14 06:37:21 +10:00
|
|
|
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_DETACH)
|
2018-04-22 18:37:09 +10:00
|
|
|
startForeground(sendingNotificationId, builder.build())
|
2018-04-14 06:37:21 +10:00
|
|
|
} else {
|
2018-04-22 18:37:09 +10:00
|
|
|
notificationManager.notify(sendingNotificationId, builder.build())
|
2018-04-14 06:37:21 +10:00
|
|
|
}
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
statusesToSend[sendingNotificationId] = statusToSend
|
|
|
|
sendStatus(sendingNotificationId--)
|
2018-04-14 06:37:21 +10:00
|
|
|
} else {
|
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
if (intent.hasExtra(KEY_CANCEL)) {
|
2018-04-14 06:37:21 +10:00
|
|
|
cancelSending(intent.getIntExtra(KEY_CANCEL, 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return START_NOT_STICKY
|
|
|
|
}
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
private fun sendStatus(statusId: Int) {
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
// when statusToSend == null, sending has been canceled
|
|
|
|
val statusToSend = statusesToSend[statusId] ?: return
|
2018-04-14 06:37:21 +10:00
|
|
|
|
|
|
|
// when account == null, user has logged out, cancel sending
|
2022-03-21 06:21:42 +11:00
|
|
|
val account = accountManager.getAccountById(statusToSend.accountId)
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
if (account == null) {
|
2022-03-21 06:21:42 +11:00
|
|
|
statusesToSend.remove(statusId)
|
|
|
|
notificationManager.cancel(statusId)
|
2018-04-18 04:07:14 +10:00
|
|
|
stopSelfWhenDone()
|
2018-04-14 06:37:21 +10:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
statusToSend.retries++
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2019-08-23 04:30:08 +10:00
|
|
|
val newStatus = NewStatus(
|
2022-03-21 06:21:42 +11:00
|
|
|
statusToSend.text,
|
|
|
|
statusToSend.warningText,
|
|
|
|
statusToSend.inReplyToId,
|
|
|
|
statusToSend.visibility,
|
|
|
|
statusToSend.sensitive,
|
|
|
|
statusToSend.mediaIds,
|
|
|
|
statusToSend.scheduledAt,
|
|
|
|
statusToSend.poll
|
2019-08-23 04:30:08 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
val sendCall = mastodonApi.createStatus(
|
2021-06-29 05:13:24 +10:00
|
|
|
"Bearer " + account.accessToken,
|
|
|
|
account.domain,
|
2022-03-21 06:21:42 +11:00
|
|
|
statusToSend.idempotencyKey,
|
2021-06-29 05:13:24 +10:00
|
|
|
newStatus
|
2018-04-14 06:37:21 +10:00
|
|
|
)
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
sendCalls[statusId] = sendCall
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
val callback = object : Callback<Status> {
|
2018-04-14 06:37:21 +10:00
|
|
|
override fun onResponse(call: Call<Status>, response: Response<Status>) {
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
val scheduled = !statusToSend.scheduledAt.isNullOrEmpty()
|
|
|
|
statusesToSend.remove(statusId)
|
2018-04-14 06:37:21 +10:00
|
|
|
|
|
|
|
if (response.isSuccessful) {
|
|
|
|
// If the status was loaded from a draft, delete the draft and associated media files.
|
2022-03-21 06:21:42 +11:00
|
|
|
if (statusToSend.draftId != 0) {
|
2021-06-25 05:23:29 +10:00
|
|
|
serviceScope.launch {
|
2022-03-21 06:21:42 +11:00
|
|
|
draftHelper.deleteDraftAndAttachments(statusToSend.draftId)
|
2021-06-25 05:23:29 +10:00
|
|
|
}
|
2021-01-22 04:57:09 +11:00
|
|
|
}
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2019-10-03 05:28:12 +10:00
|
|
|
if (scheduled) {
|
|
|
|
response.body()?.let(::StatusScheduledEvent)?.let(eventHub::dispatch)
|
|
|
|
} else {
|
|
|
|
response.body()?.let(::StatusComposedEvent)?.let(eventHub::dispatch)
|
|
|
|
}
|
2018-05-27 18:22:12 +10:00
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
notificationManager.cancel(statusId)
|
2018-04-14 06:37:21 +10:00
|
|
|
} else {
|
2022-03-21 06:21:42 +11:00
|
|
|
// the server refused to accept the status, save status & show error message
|
|
|
|
saveStatusToDrafts(statusToSend)
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
val builder = NotificationCompat.Builder(this@SendStatusService, CHANNEL_ID)
|
2021-06-29 05:13:24 +10:00
|
|
|
.setSmallIcon(R.drawable.ic_notify)
|
2022-03-21 06:21:42 +11:00
|
|
|
.setContentTitle(getString(R.string.send_post_notification_error_title))
|
|
|
|
.setContentText(getString(R.string.send_post_notification_saved_content))
|
|
|
|
.setColor(ContextCompat.getColor(this@SendStatusService, R.color.notification_color))
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
notificationManager.cancel(statusId)
|
2018-04-22 18:37:09 +10:00
|
|
|
notificationManager.notify(errorNotificationId--, builder.build())
|
2018-04-14 06:37:21 +10:00
|
|
|
}
|
2018-04-22 18:37:09 +10:00
|
|
|
|
|
|
|
stopSelfWhenDone()
|
2018-04-14 06:37:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call<Status>, t: Throwable) {
|
2022-03-21 06:21:42 +11:00
|
|
|
var backoff = TimeUnit.SECONDS.toMillis(statusToSend.retries.toLong())
|
2018-04-14 06:37:21 +10:00
|
|
|
if (backoff > MAX_RETRY_INTERVAL) {
|
|
|
|
backoff = MAX_RETRY_INTERVAL
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:13:24 +10:00
|
|
|
timer.schedule(
|
|
|
|
object : TimerTask() {
|
|
|
|
override fun run() {
|
2022-03-21 06:21:42 +11:00
|
|
|
sendStatus(statusId)
|
2021-06-29 05:13:24 +10:00
|
|
|
}
|
|
|
|
},
|
|
|
|
backoff
|
|
|
|
)
|
2018-04-14 06:37:21 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendCall.enqueue(callback)
|
|
|
|
}
|
|
|
|
|
2018-04-18 04:07:14 +10:00
|
|
|
private fun stopSelfWhenDone() {
|
2018-04-22 18:37:09 +10:00
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
if (statusesToSend.isEmpty()) {
|
|
|
|
ServiceCompat.stopForeground(this@SendStatusService, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
2018-04-18 04:07:14 +10:00
|
|
|
stopSelf()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
private fun cancelSending(statusId: Int) {
|
|
|
|
val statusToCancel = statusesToSend.remove(statusId)
|
|
|
|
if (statusToCancel != null) {
|
|
|
|
val sendCall = sendCalls.remove(statusId)
|
2018-04-14 06:37:21 +10:00
|
|
|
sendCall?.cancel()
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
saveStatusToDrafts(statusToCancel)
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
val builder = NotificationCompat.Builder(this@SendStatusService, CHANNEL_ID)
|
2021-06-29 05:13:24 +10:00
|
|
|
.setSmallIcon(R.drawable.ic_notify)
|
2022-03-21 06:21:42 +11:00
|
|
|
.setContentTitle(getString(R.string.send_post_notification_cancel_title))
|
|
|
|
.setContentText(getString(R.string.send_post_notification_saved_content))
|
2022-03-10 06:50:23 +11:00
|
|
|
.setColor(ContextCompat.getColor(this, R.color.notification_color))
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
notificationManager.notify(statusId, builder.build())
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2021-06-29 05:13:24 +10:00
|
|
|
timer.schedule(
|
|
|
|
object : TimerTask() {
|
|
|
|
override fun run() {
|
2022-03-21 06:21:42 +11:00
|
|
|
notificationManager.cancel(statusId)
|
2021-06-29 05:13:24 +10:00
|
|
|
stopSelfWhenDone()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
5000
|
|
|
|
)
|
2018-04-14 06:37:21 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
private fun saveStatusToDrafts(status: StatusToSend) {
|
2021-06-25 05:23:29 +10:00
|
|
|
serviceScope.launch {
|
|
|
|
draftHelper.saveDraft(
|
2022-03-21 06:21:42 +11:00
|
|
|
draftId = status.draftId,
|
|
|
|
accountId = status.accountId,
|
|
|
|
inReplyToId = status.inReplyToId,
|
|
|
|
content = status.text,
|
|
|
|
contentWarning = status.warningText,
|
|
|
|
sensitive = status.sensitive,
|
|
|
|
visibility = Status.Visibility.byString(status.visibility),
|
|
|
|
mediaUris = status.mediaUris,
|
|
|
|
mediaDescriptions = status.mediaDescriptions,
|
|
|
|
poll = status.poll,
|
2021-01-22 04:57:09 +11:00
|
|
|
failedToSend = true
|
2021-06-25 05:23:29 +10:00
|
|
|
)
|
|
|
|
}
|
2018-04-14 06:37:21 +10:00
|
|
|
}
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
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))
|
2018-04-14 06:37:21 +10:00
|
|
|
}
|
|
|
|
|
2021-06-25 05:23:29 +10:00
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
supervisorJob.cancel()
|
|
|
|
}
|
2018-04-14 06:37:21 +10:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
private const val KEY_STATUS = "status"
|
2018-04-14 06:37:21 +10:00
|
|
|
private const val KEY_CANCEL = "cancel_id"
|
|
|
|
private const val CHANNEL_ID = "send_toots"
|
|
|
|
|
2018-05-27 18:22:12 +10:00
|
|
|
private val MAX_RETRY_INTERVAL = TimeUnit.MINUTES.toMillis(1)
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2018-04-22 18:37:09 +10:00
|
|
|
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
|
2018-04-14 06:37:21 +10:00
|
|
|
|
|
|
|
@JvmStatic
|
2022-03-21 06:21:42 +11:00
|
|
|
fun sendStatusIntent(
|
2021-06-29 05:13:24 +10:00
|
|
|
context: Context,
|
2022-03-21 06:21:42 +11:00
|
|
|
statusToSend: StatusToSend
|
2018-04-14 06:37:21 +10:00
|
|
|
): Intent {
|
2022-03-21 06:21:42 +11:00
|
|
|
val intent = Intent(context, SendStatusService::class.java)
|
|
|
|
intent.putExtra(KEY_STATUS, statusToSend)
|
2018-04-14 06:37:21 +10:00
|
|
|
|
2022-03-21 06:21:42 +11:00
|
|
|
if (statusToSend.mediaUris.isNotEmpty()) {
|
2018-06-29 06:22:29 +10:00
|
|
|
// forward uri permissions
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
|
|
val uriClip = ClipData(
|
2022-03-21 06:21:42 +11:00
|
|
|
ClipDescription("Status Media", arrayOf("image/*", "video/*")),
|
|
|
|
ClipData.Item(statusToSend.mediaUris[0])
|
2018-06-29 06:22:29 +10:00
|
|
|
)
|
2022-03-21 06:21:42 +11:00
|
|
|
statusToSend.mediaUris
|
2021-06-29 05:13:24 +10:00
|
|
|
.drop(1)
|
|
|
|
.forEach { mediaUri ->
|
|
|
|
uriClip.addItem(ClipData.Item(mediaUri))
|
|
|
|
}
|
2018-06-29 06:22:29 +10:00
|
|
|
|
|
|
|
intent.clipData = uriClip
|
|
|
|
}
|
|
|
|
|
2018-04-14 06:37:21 +10:00
|
|
|
return intent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Parcelize
|
2022-03-21 06:21:42 +11:00
|
|
|
data class StatusToSend(
|
2021-06-29 05:13:24 +10:00
|
|
|
val text: String,
|
|
|
|
val warningText: String,
|
|
|
|
val visibility: String,
|
|
|
|
val sensitive: Boolean,
|
|
|
|
val mediaIds: List<String>,
|
|
|
|
val mediaUris: List<String>,
|
|
|
|
val mediaDescriptions: List<String>,
|
|
|
|
val scheduledAt: String?,
|
|
|
|
val inReplyToId: String?,
|
|
|
|
val poll: NewPoll?,
|
|
|
|
val replyingStatusContent: String?,
|
|
|
|
val replyingStatusAuthorUsername: String?,
|
|
|
|
val accountId: Long,
|
|
|
|
val draftId: Int,
|
|
|
|
val idempotencyKey: String,
|
|
|
|
var retries: Int
|
2019-12-20 05:09:40 +11:00
|
|
|
) : Parcelable
|