|
|
|
@ -6,9 +6,7 @@ import android.app.Service |
|
|
|
|
import android.content.Context |
|
|
|
|
import android.content.Intent |
|
|
|
|
import android.os.Build |
|
|
|
|
import android.os.Handler |
|
|
|
|
import android.os.IBinder |
|
|
|
|
import android.os.Message |
|
|
|
|
import android.util.Log |
|
|
|
|
import androidx.core.app.NotificationCompat |
|
|
|
|
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.AccountManager |
|
|
|
|
import com.keylesspalace.tusky.di.Injectable |
|
|
|
|
import com.keylesspalace.tusky.entity.Account |
|
|
|
|
import com.keylesspalace.tusky.entity.Notification |
|
|
|
|
import com.keylesspalace.tusky.entity.StreamEvent |
|
|
|
|
import com.keylesspalace.tusky.network.MastodonApi |
|
|
|
|
import com.keylesspalace.tusky.util.isLessThan |
|
|
|
|
import dagger.android.AndroidInjection |
|
|
|
|
import okhttp3.* |
|
|
|
|
import javax.inject.Inject |
|
|
|
@ -41,6 +39,9 @@ class StreamingService: Service(), Injectable { |
|
|
|
|
@Inject |
|
|
|
|
lateinit var gson: Gson |
|
|
|
|
|
|
|
|
|
@Inject |
|
|
|
|
lateinit var client: OkHttpClient |
|
|
|
|
|
|
|
|
|
private val sockets: MutableMap<Long, WebSocket> = mutableMapOf() |
|
|
|
|
|
|
|
|
|
private val notificationManager by lazy { getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager } |
|
|
|
@ -75,10 +76,17 @@ class StreamingService: Service(), Injectable { |
|
|
|
|
return START_NOT_STICKY |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onDestroy() { |
|
|
|
|
stopStreaming() |
|
|
|
|
super.onDestroy() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { |
|
|
|
|
if(intent.hasExtra(KEY_STOP_STREAMING)) { |
|
|
|
|
Log.d(TAG, "Stopping stream") |
|
|
|
|
return stopStreaming() |
|
|
|
|
Log.d(TAG, "Stream goes suya..") |
|
|
|
|
stopStreaming() |
|
|
|
|
stopSelfResult(startId) |
|
|
|
|
return START_NOT_STICKY |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var description = getString(R.string.streaming_notification_description) |
|
|
|
@ -91,13 +99,21 @@ class StreamingService: Service(), Injectable { |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
val endpoint = "wss://${account.domain}/api/v1/streaming/?access_token=${account.accessToken}&stream=user:notification" |
|
|
|
|
|
|
|
|
|
val request = Request.Builder().url(endpoint).build() |
|
|
|
|
val client = OkHttpClient.Builder().build() |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
count++ |
|
|
|
|
} |
|
|
|
@ -117,6 +133,8 @@ class StreamingService: Service(), Injectable { |
|
|
|
|
.setContentTitle(getString(R.string.streaming_notification_name)) |
|
|
|
|
.setContentText(description) |
|
|
|
|
.setOngoing(true) |
|
|
|
|
.setNotificationSilent() |
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_MIN) |
|
|
|
|
.setColor(ContextCompat.getColor(this, R.color.tusky_blue)) |
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
|
|
@ -155,7 +173,7 @@ class StreamingService: Service(), Injectable { |
|
|
|
|
@JvmStatic |
|
|
|
|
fun stopStreaming(context: Context) { |
|
|
|
|
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...") |
|
|
|
|
|
|
|
|
@ -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) { |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
@ -179,16 +206,16 @@ class StreamingService: Service(), Injectable { |
|
|
|
|
StreamEvent.EventType.NOTIFICATION -> { |
|
|
|
|
val notification = gson.fromJson(event.payload, Notification::class.java) |
|
|
|
|
NotificationHelper.make(context, notification, account, true) |
|
|
|
|
|
|
|
|
|
if(account.lastNotificationId.isLessThan(notification.id)) { |
|
|
|
|
account.lastNotificationId = notification.id |
|
|
|
|
accountManager.saveAccount(account) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else -> { |
|
|
|
|
Log.d(TAG, "Unknown event type: ${event.event.toString()}") |
|
|
|
|
Log.d(TAG, "Unknown event type: ${event.event}") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
super.onMessage(webSocket, text) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |