Move appIdToApp to WebSocketService

The PicassoHandler doesn't really need access to the full list, as it
only requires the application image.
This commit is contained in:
Jannis Mattheis
2023-02-21 13:31:48 +01:00
parent 1182f358cb
commit cfc473cce0
2 changed files with 18 additions and 35 deletions

View File

@@ -15,7 +15,6 @@ import okhttp3.Cache
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.util.concurrent.ConcurrentHashMap
internal class PicassoHandler(private val context: Context, private val settings: Settings) { internal class PicassoHandler(private val context: Context, private val settings: Settings) {
companion object { companion object {
@@ -29,7 +28,6 @@ internal class PicassoHandler(private val context: Context, private val settings
) )
private val picasso = makePicasso() private val picasso = makePicasso()
private val appIdToApp = ConcurrentHashMap<Long, Application>()
private fun makePicasso(): Picasso { private fun makePicasso(): Picasso {
val builder = OkHttpClient.Builder() val builder = OkHttpClient.Builder()
@@ -45,13 +43,13 @@ internal class PicassoHandler(private val context: Context, private val settings
@Throws(IOException::class) @Throws(IOException::class)
fun getImageFromUrl(url: String?): Bitmap = picasso.load(url).get() fun getImageFromUrl(url: String?): Bitmap = picasso.load(url).get()
fun getIcon(appId: Long): Bitmap { fun getIcon(app: Application?): Bitmap {
if (appId == -1L) { if (app == null) {
return BitmapFactory.decodeResource(context.resources, R.drawable.gotify) return BitmapFactory.decodeResource(context.resources, R.drawable.gotify)
} }
try { try {
return getImageFromUrl( return getImageFromUrl(
Utils.resolveAbsoluteUrl("${settings.url}/", appIdToApp[appId]?.image) Utils.resolveAbsoluteUrl("${settings.url}/", app.image)
) )
} catch (e: IOException) { } catch (e: IOException) {
Log.e("Could not load image for notification", e) 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) return BitmapFactory.decodeResource(context.resources, R.drawable.gotify)
} }
fun updateApps(apps: List<Application>) {
appIdToApp.clear()
appIdToApp.putAll(apps.associateBy { it.id })
}
fun get() = picasso fun get() = picasso
@Throws(IOException::class) @Throws(IOException::class)

View File

@@ -33,6 +33,7 @@ import com.github.gotify.messages.Extras
import com.github.gotify.messages.MessagesActivity import com.github.gotify.messages.MessagesActivity
import com.github.gotify.picasso.PicassoHandler import com.github.gotify.picasso.PicassoHandler
import io.noties.markwon.Markwon import io.noties.markwon.Markwon
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicLong import java.util.concurrent.atomic.AtomicLong
internal class WebSocketService : Service() { internal class WebSocketService : Service() {
@@ -43,6 +44,7 @@ internal class WebSocketService : Service() {
private lateinit var settings: Settings private lateinit var settings: Settings
private var connection: WebSocketConnection? = null private var connection: WebSocketConnection? = null
private val appIdToApp = ConcurrentHashMap<Long, Application>()
private val lastReceivedMessage = AtomicLong(NOT_LOADED) private val lastReceivedMessage = AtomicLong(NOT_LOADED)
private lateinit var missingMessageUtil: MissedMessageUtil private lateinit var missingMessageUtil: MissedMessageUtil
@@ -113,43 +115,31 @@ internal class WebSocketService : Service() {
val intentFilter = IntentFilter() val intentFilter = IntentFilter()
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION) intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
fetchAppIds( fetchApps()
onSuccess = { apps ->
picassoHandler.updateApps(apps)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannels(apps)
}
},
onError = { picassoHandler.updateApps(listOf()) }
)
} }
private fun fetchAppIds( private fun fetchApps() {
onSuccess: (apps: List<Application>) -> Unit,
onError: () -> Unit
) {
ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token) ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token)
.createService(ApplicationApi::class.java) .createService(ApplicationApi::class.java)
.apps .apps
.enqueue( .enqueue(
Callback.call( Callback.call(
onSuccess = Callback.SuccessBody { apps -> 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<Application>) {
NotificationSupport.createChannels(
this,
(this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager),
apps
)
}
private fun onClose() { private fun onClose() {
showForegroundNotification( showForegroundNotification(
getString(R.string.websocket_closed), getString(R.string.websocket_closed),
@@ -372,7 +362,7 @@ internal class WebSocketService : Service() {
.setDefaults(Notification.DEFAULT_ALL) .setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis()) .setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_gotify) .setSmallIcon(R.drawable.ic_gotify)
.setLargeIcon(picassoHandler.getIcon(appId)) .setLargeIcon(picassoHandler.getIcon(appIdToApp[appId]))
.setTicker("${getString(R.string.app_name)} - $title") .setTicker("${getString(R.string.app_name)} - $title")
.setGroup(NotificationSupport.Group.MESSAGES) .setGroup(NotificationSupport.Group.MESSAGES)
.setContentTitle(title) .setContentTitle(title)