StreamingService: cleanup, use shared OkHttpClient

main
Alibek Omarov 4 years ago
parent 55d3e9ed08
commit 866f7d3bce
  1. 65
      app/src/main/java/com/keylesspalace/tusky/service/StreamingService.kt

@ -6,9 +6,7 @@ import android.app.Service
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.os.Handler
import android.os.IBinder import android.os.IBinder
import android.os.Message
import android.util.Log import android.util.Log
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat import androidx.core.app.ServiceCompat
@ -20,10 +18,10 @@ import com.keylesspalace.tusky.components.notifications.NotificationHelper
import com.keylesspalace.tusky.db.AccountEntity import com.keylesspalace.tusky.db.AccountEntity
import com.keylesspalace.tusky.db.AccountManager import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.di.Injectable import com.keylesspalace.tusky.di.Injectable
import com.keylesspalace.tusky.entity.Account
import com.keylesspalace.tusky.entity.Notification import com.keylesspalace.tusky.entity.Notification
import com.keylesspalace.tusky.entity.StreamEvent import com.keylesspalace.tusky.entity.StreamEvent
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.util.isLessThan
import dagger.android.AndroidInjection import dagger.android.AndroidInjection
import okhttp3.* import okhttp3.*
import javax.inject.Inject import javax.inject.Inject
@ -41,6 +39,9 @@ class StreamingService: Service(), Injectable {
@Inject @Inject
lateinit var gson: Gson lateinit var gson: Gson
@Inject
lateinit var client: OkHttpClient
private val sockets: MutableMap<Long, WebSocket> = mutableMapOf() private val sockets: MutableMap<Long, WebSocket> = mutableMapOf()
private val notificationManager by lazy { getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager } private val notificationManager by lazy { getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager }
@ -75,10 +76,17 @@ class StreamingService: Service(), Injectable {
return START_NOT_STICKY return START_NOT_STICKY
} }
override fun onDestroy() {
stopStreaming()
super.onDestroy()
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
if(intent.hasExtra(KEY_STOP_STREAMING)) { if(intent.hasExtra(KEY_STOP_STREAMING)) {
Log.d(TAG, "Stopping stream") Log.d(TAG, "Stream goes suya..")
return stopStreaming() stopStreaming()
stopSelfResult(startId)
return START_NOT_STICKY
} }
var description = getString(R.string.streaming_notification_description) var description = getString(R.string.streaming_notification_description)
@ -91,13 +99,21 @@ class StreamingService: Service(), Injectable {
continue continue
val endpoint = "wss://${account.domain}/api/v1/streaming/?access_token=${account.accessToken}&stream=user:notification" val endpoint = "wss://${account.domain}/api/v1/streaming/?access_token=${account.accessToken}&stream=user:notification"
val request = Request.Builder().url(endpoint).build() val request = Request.Builder().url(endpoint).build()
val client = OkHttpClient.Builder().build()
Log.d(TAG, "Running stream for ${account.fullName}") Log.d(TAG, "Running stream for ${account.fullName}")
sockets[account.id] = client.newWebSocket(request, StreamingListener(this, gson, account)) sockets[account.id] = client.newWebSocket(
request,
StreamingListener(
"${account.fullName}/user:notification",
this,
gson,
accountManager,
account
)
)
description += "\n" + account.fullName description += "\n" + account.fullName
count++ count++
} }
@ -117,6 +133,8 @@ class StreamingService: Service(), Injectable {
.setContentTitle(getString(R.string.streaming_notification_name)) .setContentTitle(getString(R.string.streaming_notification_name))
.setContentText(description) .setContentText(description)
.setOngoing(true) .setOngoing(true)
.setNotificationSilent()
.setPriority(NotificationCompat.PRIORITY_MIN)
.setColor(ContextCompat.getColor(this, R.color.tusky_blue)) .setColor(ContextCompat.getColor(this, R.color.tusky_blue))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -155,7 +173,7 @@ class StreamingService: Service(), Injectable {
@JvmStatic @JvmStatic
fun stopStreaming(context: Context) { fun stopStreaming(context: Context) {
val intent = Intent(context, StreamingService::class.java) val intent = Intent(context, StreamingService::class.java)
intent.putExtra(KEY_STOP_STREAMING, 123) intent.putExtra(KEY_STOP_STREAMING, 0)
Log.d(TAG, "Stopping notifications streaming service...") Log.d(TAG, "Stopping notifications streaming service...")
@ -163,14 +181,23 @@ class StreamingService: Service(), Injectable {
} }
} }
class StreamingListener(val context: Context, val gson: Gson, val account: AccountEntity) : WebSocketListener() { class StreamingListener(
val tag: String,
val context: Context,
val gson: Gson,
val accountManager: AccountManager,
val account: AccountEntity
) : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: Response) { override fun onOpen(webSocket: WebSocket, response: Response) {
Log.d(TAG, "Stream connected to: ${account.fullName}/user:notification") Log.d(TAG, "Stream connected to: $tag")
} }
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
Log.d(TAG, "Stream closed for: ${account.fullName}/user:notification") Log.d(TAG, "Stream closed for: $tag")
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
Log.d(TAG, "Stream failed for $tag", t)
} }
override fun onMessage(webSocket: WebSocket, text: String) { override fun onMessage(webSocket: WebSocket, text: String) {
@ -179,16 +206,16 @@ class StreamingService: Service(), Injectable {
StreamEvent.EventType.NOTIFICATION -> { StreamEvent.EventType.NOTIFICATION -> {
val notification = gson.fromJson(event.payload, Notification::class.java) val notification = gson.fromJson(event.payload, Notification::class.java)
NotificationHelper.make(context, notification, account, true) NotificationHelper.make(context, notification, account, true)
if(account.lastNotificationId.isLessThan(notification.id)) {
account.lastNotificationId = notification.id
accountManager.saveAccount(account)
}
} }
else -> { else -> {
Log.d(TAG, "Unknown event type: ${event.event.toString()}") Log.d(TAG, "Unknown event type: ${event.event}")
} }
} }
super.onMessage(webSocket, text)
} }
} }
} }
Loading…
Cancel
Save