Move channel creation to WebSocketService

This commit is contained in:
Niko Diamadis
2023-02-18 12:39:55 +01:00
parent 5399d003e4
commit 1182f358cb
4 changed files with 44 additions and 31 deletions

View File

@@ -9,7 +9,6 @@ import android.graphics.Canvas
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
@@ -29,7 +28,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.gotify.BuildConfig
import com.github.gotify.MissedMessageUtil
import com.github.gotify.NotificationSupport
import com.github.gotify.R
import com.github.gotify.Utils
import com.github.gotify.Utils.launchCoroutine
@@ -206,14 +204,6 @@ internal class MessagesActivity :
.into(t)
}
selectAppInMenu(selectedItem)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationSupport.createChannels(
this,
(this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager),
applications
)
}
}
private fun initDrawer() {

View File

@@ -9,7 +9,7 @@ internal object MessageImageCombiner {
return messages.map { MessageWithImage(message = it, image = appIdToImage[it.appid]) }
}
fun appIdToImage(applications: List<Application>): Map<Long, String> {
private fun appIdToImage(applications: List<Application>): Map<Long, String> {
val map = mutableMapOf<Long, String>()
applications.forEach {
map[it.id] = it.image

View File

@@ -6,12 +6,9 @@ import android.graphics.BitmapFactory
import com.github.gotify.R
import com.github.gotify.Settings
import com.github.gotify.Utils
import com.github.gotify.api.Callback
import com.github.gotify.api.CertUtils
import com.github.gotify.api.ClientFactory
import com.github.gotify.client.api.ApplicationApi
import com.github.gotify.client.model.Application
import com.github.gotify.log.Log
import com.github.gotify.messages.provider.MessageImageCombiner
import com.squareup.picasso.OkHttp3Downloader
import com.squareup.picasso.Picasso
import okhttp3.Cache
@@ -32,7 +29,7 @@ internal class PicassoHandler(private val context: Context, private val settings
)
private val picasso = makePicasso()
private val appIdToAppImage = ConcurrentHashMap<Long, String>()
private val appIdToApp = ConcurrentHashMap<Long, Application>()
private fun makePicasso(): Picasso {
val builder = OkHttpClient.Builder()
@@ -54,7 +51,7 @@ internal class PicassoHandler(private val context: Context, private val settings
}
try {
return getImageFromUrl(
Utils.resolveAbsoluteUrl("${settings.url}/", appIdToAppImage[appId])
Utils.resolveAbsoluteUrl("${settings.url}/", appIdToApp[appId]?.image)
)
} catch (e: IOException) {
Log.e("Could not load image for notification", e)
@@ -62,19 +59,9 @@ internal class PicassoHandler(private val context: Context, private val settings
return BitmapFactory.decodeResource(context.resources, R.drawable.gotify)
}
fun updateAppIds() {
ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token)
.createService(ApplicationApi::class.java)
.apps
.enqueue(
Callback.call(
onSuccess = Callback.SuccessBody { apps ->
appIdToAppImage.clear()
appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps))
},
onError = { appIdToAppImage.clear() }
)
)
fun updateApps(apps: List<Application>) {
appIdToApp.clear()
appIdToApp.putAll(apps.associateBy { it.id })
}
fun get() = picasso

View File

@@ -23,7 +23,9 @@ import com.github.gotify.Settings
import com.github.gotify.Utils
import com.github.gotify.api.Callback
import com.github.gotify.api.ClientFactory
import com.github.gotify.client.api.ApplicationApi
import com.github.gotify.client.api.MessageApi
import com.github.gotify.client.model.Application
import com.github.gotify.client.model.Message
import com.github.gotify.log.Log
import com.github.gotify.log.UncaughtExceptionHandler
@@ -111,7 +113,41 @@ internal class WebSocketService : Service() {
val intentFilter = IntentFilter()
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
picassoHandler.updateAppIds()
fetchAppIds(
onSuccess = { apps ->
picassoHandler.updateApps(apps)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannels(apps)
}
},
onError = { picassoHandler.updateApps(listOf()) }
)
}
private fun fetchAppIds(
onSuccess: (apps: List<Application>) -> Unit,
onError: () -> Unit
) {
ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token)
.createService(ApplicationApi::class.java)
.apps
.enqueue(
Callback.call(
onSuccess = Callback.SuccessBody { apps ->
onSuccess(apps)
},
onError = { onError() }
)
)
}
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannels(apps: List<Application>) {
NotificationSupport.createChannels(
this,
(this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager),
apps
)
}
private fun onClose() {