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 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<Long, Application>()
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<Application>) {
appIdToApp.clear()
appIdToApp.putAll(apps.associateBy { it.id })
}
fun get() = picasso
@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.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<Long, Application>()
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<Application>) -> 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<Application>) {
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)