From cfc473cce0ec257004d3543d00d73b8104d30016 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Tue, 21 Feb 2023 13:31:48 +0100 Subject: [PATCH] Move appIdToApp to WebSocketService The PicassoHandler doesn't really need access to the full list, as it only requires the application image. --- .../github/gotify/picasso/PicassoHandler.kt | 13 ++---- .../github/gotify/service/WebSocketService.kt | 40 +++++++------------ 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/app/src/main/kotlin/com/github/gotify/picasso/PicassoHandler.kt b/app/src/main/kotlin/com/github/gotify/picasso/PicassoHandler.kt index 0cfa587..bddc3a4 100644 --- a/app/src/main/kotlin/com/github/gotify/picasso/PicassoHandler.kt +++ b/app/src/main/kotlin/com/github/gotify/picasso/PicassoHandler.kt @@ -15,7 +15,6 @@ import okhttp3.Cache import okhttp3.OkHttpClient import java.io.File import java.io.IOException -import java.util.concurrent.ConcurrentHashMap internal class PicassoHandler(private val context: Context, private val settings: Settings) { companion object { @@ -29,7 +28,6 @@ internal class PicassoHandler(private val context: Context, private val settings ) private val picasso = makePicasso() - private val appIdToApp = ConcurrentHashMap() private fun makePicasso(): Picasso { val builder = OkHttpClient.Builder() @@ -45,13 +43,13 @@ internal class PicassoHandler(private val context: Context, private val settings @Throws(IOException::class) fun getImageFromUrl(url: String?): Bitmap = picasso.load(url).get() - fun getIcon(appId: Long): Bitmap { - if (appId == -1L) { + fun getIcon(app: Application?): Bitmap { + if (app == null) { return BitmapFactory.decodeResource(context.resources, R.drawable.gotify) } try { return getImageFromUrl( - Utils.resolveAbsoluteUrl("${settings.url}/", appIdToApp[appId]?.image) + Utils.resolveAbsoluteUrl("${settings.url}/", app.image) ) } catch (e: IOException) { Log.e("Could not load image for notification", e) @@ -59,11 +57,6 @@ internal class PicassoHandler(private val context: Context, private val settings return BitmapFactory.decodeResource(context.resources, R.drawable.gotify) } - fun updateApps(apps: List) { - appIdToApp.clear() - appIdToApp.putAll(apps.associateBy { it.id }) - } - fun get() = picasso @Throws(IOException::class) diff --git a/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt b/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt index 4893f78..2a33235 100644 --- a/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt +++ b/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt @@ -33,6 +33,7 @@ import com.github.gotify.messages.Extras import com.github.gotify.messages.MessagesActivity import com.github.gotify.picasso.PicassoHandler import io.noties.markwon.Markwon +import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicLong internal class WebSocketService : Service() { @@ -43,6 +44,7 @@ internal class WebSocketService : Service() { private lateinit var settings: Settings private var connection: WebSocketConnection? = null + private val appIdToApp = ConcurrentHashMap() private val lastReceivedMessage = AtomicLong(NOT_LOADED) private lateinit var missingMessageUtil: MissedMessageUtil @@ -113,43 +115,31 @@ internal class WebSocketService : Service() { val intentFilter = IntentFilter() intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION) - fetchAppIds( - onSuccess = { apps -> - picassoHandler.updateApps(apps) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - createNotificationChannels(apps) - } - }, - onError = { picassoHandler.updateApps(listOf()) } - ) + fetchApps() } - private fun fetchAppIds( - onSuccess: (apps: List) -> Unit, - onError: () -> Unit - ) { + private fun fetchApps() { ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token) .createService(ApplicationApi::class.java) .apps .enqueue( Callback.call( onSuccess = Callback.SuccessBody { apps -> - onSuccess(apps) + appIdToApp.clear() + appIdToApp.putAll(apps.associateBy { it.id }) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationSupport.createChannels( + this, + (this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager), + apps + ) + } }, - onError = { onError() } + onError = { appIdToApp.clear() } ) ) } - @RequiresApi(Build.VERSION_CODES.O) - private fun createNotificationChannels(apps: List) { - NotificationSupport.createChannels( - this, - (this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager), - apps - ) - } - private fun onClose() { showForegroundNotification( getString(R.string.websocket_closed), @@ -372,7 +362,7 @@ internal class WebSocketService : Service() { .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_gotify) - .setLargeIcon(picassoHandler.getIcon(appId)) + .setLargeIcon(picassoHandler.getIcon(appIdToApp[appId])) .setTicker("${getString(R.string.app_name)} - $title") .setGroup(NotificationSupport.Group.MESSAGES) .setContentTitle(title)