Move channel creation to WebSocketService
This commit is contained in:
@@ -9,7 +9,6 @@ import android.graphics.Canvas
|
|||||||
import android.graphics.drawable.ColorDrawable
|
import android.graphics.drawable.ColorDrawable
|
||||||
import android.graphics.drawable.Drawable
|
import android.graphics.drawable.Drawable
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
@@ -29,7 +28,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
|||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.github.gotify.BuildConfig
|
import com.github.gotify.BuildConfig
|
||||||
import com.github.gotify.MissedMessageUtil
|
import com.github.gotify.MissedMessageUtil
|
||||||
import com.github.gotify.NotificationSupport
|
|
||||||
import com.github.gotify.R
|
import com.github.gotify.R
|
||||||
import com.github.gotify.Utils
|
import com.github.gotify.Utils
|
||||||
import com.github.gotify.Utils.launchCoroutine
|
import com.github.gotify.Utils.launchCoroutine
|
||||||
@@ -206,14 +204,6 @@ internal class MessagesActivity :
|
|||||||
.into(t)
|
.into(t)
|
||||||
}
|
}
|
||||||
selectAppInMenu(selectedItem)
|
selectAppInMenu(selectedItem)
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
NotificationSupport.createChannels(
|
|
||||||
this,
|
|
||||||
(this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager),
|
|
||||||
applications
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initDrawer() {
|
private fun initDrawer() {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ internal object MessageImageCombiner {
|
|||||||
return messages.map { MessageWithImage(message = it, image = appIdToImage[it.appid]) }
|
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>()
|
val map = mutableMapOf<Long, String>()
|
||||||
applications.forEach {
|
applications.forEach {
|
||||||
map[it.id] = it.image
|
map[it.id] = it.image
|
||||||
|
|||||||
@@ -6,12 +6,9 @@ import android.graphics.BitmapFactory
|
|||||||
import com.github.gotify.R
|
import com.github.gotify.R
|
||||||
import com.github.gotify.Settings
|
import com.github.gotify.Settings
|
||||||
import com.github.gotify.Utils
|
import com.github.gotify.Utils
|
||||||
import com.github.gotify.api.Callback
|
|
||||||
import com.github.gotify.api.CertUtils
|
import com.github.gotify.api.CertUtils
|
||||||
import com.github.gotify.api.ClientFactory
|
import com.github.gotify.client.model.Application
|
||||||
import com.github.gotify.client.api.ApplicationApi
|
|
||||||
import com.github.gotify.log.Log
|
import com.github.gotify.log.Log
|
||||||
import com.github.gotify.messages.provider.MessageImageCombiner
|
|
||||||
import com.squareup.picasso.OkHttp3Downloader
|
import com.squareup.picasso.OkHttp3Downloader
|
||||||
import com.squareup.picasso.Picasso
|
import com.squareup.picasso.Picasso
|
||||||
import okhttp3.Cache
|
import okhttp3.Cache
|
||||||
@@ -32,7 +29,7 @@ internal class PicassoHandler(private val context: Context, private val settings
|
|||||||
)
|
)
|
||||||
|
|
||||||
private val picasso = makePicasso()
|
private val picasso = makePicasso()
|
||||||
private val appIdToAppImage = ConcurrentHashMap<Long, String>()
|
private val appIdToApp = ConcurrentHashMap<Long, Application>()
|
||||||
|
|
||||||
private fun makePicasso(): Picasso {
|
private fun makePicasso(): Picasso {
|
||||||
val builder = OkHttpClient.Builder()
|
val builder = OkHttpClient.Builder()
|
||||||
@@ -54,7 +51,7 @@ internal class PicassoHandler(private val context: Context, private val settings
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return getImageFromUrl(
|
return getImageFromUrl(
|
||||||
Utils.resolveAbsoluteUrl("${settings.url}/", appIdToAppImage[appId])
|
Utils.resolveAbsoluteUrl("${settings.url}/", appIdToApp[appId]?.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)
|
||||||
@@ -62,19 +59,9 @@ 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 updateAppIds() {
|
fun updateApps(apps: List<Application>) {
|
||||||
ClientFactory.clientToken(settings.url, settings.sslSettings(), settings.token)
|
appIdToApp.clear()
|
||||||
.createService(ApplicationApi::class.java)
|
appIdToApp.putAll(apps.associateBy { it.id })
|
||||||
.apps
|
|
||||||
.enqueue(
|
|
||||||
Callback.call(
|
|
||||||
onSuccess = Callback.SuccessBody { apps ->
|
|
||||||
appIdToAppImage.clear()
|
|
||||||
appIdToAppImage.putAll(MessageImageCombiner.appIdToImage(apps))
|
|
||||||
},
|
|
||||||
onError = { appIdToAppImage.clear() }
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun get() = picasso
|
fun get() = picasso
|
||||||
|
|||||||
@@ -23,7 +23,9 @@ import com.github.gotify.Settings
|
|||||||
import com.github.gotify.Utils
|
import com.github.gotify.Utils
|
||||||
import com.github.gotify.api.Callback
|
import com.github.gotify.api.Callback
|
||||||
import com.github.gotify.api.ClientFactory
|
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.api.MessageApi
|
||||||
|
import com.github.gotify.client.model.Application
|
||||||
import com.github.gotify.client.model.Message
|
import com.github.gotify.client.model.Message
|
||||||
import com.github.gotify.log.Log
|
import com.github.gotify.log.Log
|
||||||
import com.github.gotify.log.UncaughtExceptionHandler
|
import com.github.gotify.log.UncaughtExceptionHandler
|
||||||
@@ -111,7 +113,41 @@ internal class WebSocketService : Service() {
|
|||||||
val intentFilter = IntentFilter()
|
val intentFilter = IntentFilter()
|
||||||
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
|
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() {
|
private fun onClose() {
|
||||||
|
|||||||
Reference in New Issue
Block a user