Merge branch 'master' into devel
commit
68e8ccf6bd
@ -1,25 +0,0 @@
|
||||
package org.koitharu.kotatsu.download.domain
|
||||
|
||||
import android.os.PowerManager
|
||||
import kotlin.coroutines.AbstractCoroutineContextElement
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
class WakeLockNode(
|
||||
private val wakeLock: PowerManager.WakeLock,
|
||||
private val timeout: Long,
|
||||
) : AbstractCoroutineContextElement(Key) {
|
||||
|
||||
init {
|
||||
wakeLock.setReferenceCounted(true)
|
||||
}
|
||||
|
||||
fun acquire() {
|
||||
wakeLock.acquire(timeout)
|
||||
}
|
||||
|
||||
fun release() {
|
||||
wakeLock.release()
|
||||
}
|
||||
|
||||
companion object Key : CoroutineContext.Key<WakeLockNode>
|
||||
}
|
||||
@ -1,62 +0,0 @@
|
||||
package org.koitharu.kotatsu.download.ui.service
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationManager
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.SparseArray
|
||||
import androidx.core.app.ServiceCompat
|
||||
import androidx.core.util.isEmpty
|
||||
import androidx.core.util.size
|
||||
|
||||
private const val DEFAULT_DELAY = 500L
|
||||
|
||||
class ForegroundNotificationSwitcher(
|
||||
private val service: Service,
|
||||
) {
|
||||
|
||||
private val notificationManager = service.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
private val notifications = SparseArray<Notification>()
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
@Synchronized
|
||||
fun notify(startId: Int, notification: Notification) {
|
||||
if (notifications.isEmpty()) {
|
||||
service.startForeground(startId, notification)
|
||||
} else {
|
||||
notificationManager.notify(startId, notification)
|
||||
}
|
||||
notifications[startId] = notification
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun detach(startId: Int, notification: Notification?) {
|
||||
notifications.remove(startId)
|
||||
if (notifications.isEmpty()) {
|
||||
ServiceCompat.stopForeground(service, ServiceCompat.STOP_FOREGROUND_DETACH)
|
||||
}
|
||||
val nextIndex = notifications.size - 1
|
||||
if (nextIndex >= 0) {
|
||||
val nextStartId = notifications.keyAt(nextIndex)
|
||||
val nextNotification = notifications.valueAt(nextIndex)
|
||||
service.startForeground(nextStartId, nextNotification)
|
||||
}
|
||||
handler.postDelayed(NotifyRunnable(startId, notification), DEFAULT_DELAY)
|
||||
}
|
||||
|
||||
private inner class NotifyRunnable(
|
||||
private val startId: Int,
|
||||
private val notification: Notification?,
|
||||
) : Runnable {
|
||||
|
||||
override fun run() {
|
||||
if (notification != null) {
|
||||
notificationManager.notify(startId, notification)
|
||||
} else {
|
||||
notificationManager.cancel(startId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
package org.koitharu.kotatsu.utils
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ComponentName
|
||||
import android.content.Intent
|
||||
import android.content.ServiceConnection
|
||||
import android.os.IBinder
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
|
||||
class LifecycleAwareServiceConnection(
|
||||
private val host: Activity,
|
||||
) : ServiceConnection, DefaultLifecycleObserver {
|
||||
|
||||
private val serviceStateFlow = MutableStateFlow<IBinder?>(null)
|
||||
|
||||
val service: StateFlow<IBinder?>
|
||||
get() = serviceStateFlow
|
||||
|
||||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
||||
serviceStateFlow.value = service
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(name: ComponentName?) {
|
||||
serviceStateFlow.value = null
|
||||
}
|
||||
|
||||
override fun onDestroy(owner: LifecycleOwner) {
|
||||
super.onDestroy(owner)
|
||||
host.unbindService(this)
|
||||
}
|
||||
}
|
||||
|
||||
fun Activity.bindServiceWithLifecycle(
|
||||
owner: LifecycleOwner,
|
||||
service: Intent,
|
||||
flags: Int
|
||||
): LifecycleAwareServiceConnection {
|
||||
val connection = LifecycleAwareServiceConnection(this)
|
||||
bindService(service, connection, flags)
|
||||
owner.lifecycle.addObserver(connection)
|
||||
return connection
|
||||
}
|
||||
Loading…
Reference in New Issue