mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-19 09:08:26 +02:00
Add: function comments, autoformatting applied
This commit is contained in:
parent
c7f51e815c
commit
973dca83a2
1 changed files with 94 additions and 36 deletions
|
@ -12,11 +12,19 @@ import com.audiobookshelf.app.managers.DbManager
|
||||||
import com.audiobookshelf.app.player.PlayerNotificationService
|
import com.audiobookshelf.app.player.PlayerNotificationService
|
||||||
import com.audiobookshelf.app.updateAppWidget
|
import com.audiobookshelf.app.updateAppWidget
|
||||||
|
|
||||||
|
/** Interface for widget event handling. */
|
||||||
interface WidgetEventEmitter {
|
interface WidgetEventEmitter {
|
||||||
|
/**
|
||||||
|
* Called when the player state changes.
|
||||||
|
* @param pns The PlayerNotificationService instance.
|
||||||
|
*/
|
||||||
fun onPlayerChanged(pns: PlayerNotificationService)
|
fun onPlayerChanged(pns: PlayerNotificationService)
|
||||||
|
|
||||||
|
/** Called when the player is closed. */
|
||||||
fun onPlayerClosed()
|
fun onPlayerClosed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Singleton object for managing device-related operations. */
|
||||||
object DeviceManager {
|
object DeviceManager {
|
||||||
const val tag = "DeviceManager"
|
const val tag = "DeviceManager"
|
||||||
|
|
||||||
|
@ -24,11 +32,16 @@ object DeviceManager {
|
||||||
var deviceData: DeviceData = dbManager.getDeviceData()
|
var deviceData: DeviceData = dbManager.getDeviceData()
|
||||||
var serverConnectionConfig: ServerConnectionConfig? = null
|
var serverConnectionConfig: ServerConnectionConfig? = null
|
||||||
|
|
||||||
val serverConnectionConfigId get() = serverConnectionConfig?.id ?: ""
|
val serverConnectionConfigId
|
||||||
val serverAddress get() = serverConnectionConfig?.address ?: ""
|
get() = serverConnectionConfig?.id ?: ""
|
||||||
val serverUserId get() = serverConnectionConfig?.userId ?: ""
|
val serverAddress
|
||||||
val token get() = serverConnectionConfig?.token ?: ""
|
get() = serverConnectionConfig?.address ?: ""
|
||||||
val isConnectedToServer get() = serverConnectionConfig != null
|
val serverUserId
|
||||||
|
get() = serverConnectionConfig?.userId ?: ""
|
||||||
|
val token
|
||||||
|
get() = serverConnectionConfig?.token ?: ""
|
||||||
|
val isConnectedToServer
|
||||||
|
get() = serverConnectionConfig != null
|
||||||
|
|
||||||
var widgetUpdater: WidgetEventEmitter? = null
|
var widgetUpdater: WidgetEventEmitter? = null
|
||||||
|
|
||||||
|
@ -36,7 +49,9 @@ object DeviceManager {
|
||||||
Log.d(tag, "Device Manager Singleton invoked")
|
Log.d(tag, "Device Manager Singleton invoked")
|
||||||
|
|
||||||
// Initialize new sleep timer settings and shake sensitivity added in v0.9.61
|
// Initialize new sleep timer settings and shake sensitivity added in v0.9.61
|
||||||
if (deviceData.deviceSettings?.autoSleepTimerStartTime == null || deviceData.deviceSettings?.autoSleepTimerEndTime == null) {
|
if (deviceData.deviceSettings?.autoSleepTimerStartTime == null ||
|
||||||
|
deviceData.deviceSettings?.autoSleepTimerEndTime == null
|
||||||
|
) {
|
||||||
deviceData.deviceSettings?.autoSleepTimerStartTime = "22:00"
|
deviceData.deviceSettings?.autoSleepTimerStartTime = "22:00"
|
||||||
deviceData.deviceSettings?.autoSleepTimerStartTime = "06:00"
|
deviceData.deviceSettings?.autoSleepTimerStartTime = "06:00"
|
||||||
deviceData.deviceSettings?.sleepTimerLength = 900000L
|
deviceData.deviceSettings?.sleepTimerLength = 900000L
|
||||||
|
@ -65,21 +80,41 @@ object DeviceManager {
|
||||||
deviceData.deviceSettings?.androidAutoBrowseLimitForGrouping = 100
|
deviceData.deviceSettings?.androidAutoBrowseLimitForGrouping = 100
|
||||||
}
|
}
|
||||||
if (deviceData.deviceSettings?.androidAutoBrowseSeriesSequenceOrder == null) {
|
if (deviceData.deviceSettings?.androidAutoBrowseSeriesSequenceOrder == null) {
|
||||||
deviceData.deviceSettings?.androidAutoBrowseSeriesSequenceOrder = AndroidAutoBrowseSeriesSequenceOrderSetting.ASC
|
deviceData.deviceSettings?.androidAutoBrowseSeriesSequenceOrder =
|
||||||
|
AndroidAutoBrowseSeriesSequenceOrderSetting.ASC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes the given ID to a Base64 string.
|
||||||
|
* @param id The ID to encode.
|
||||||
|
* @return The Base64 encoded string.
|
||||||
|
*/
|
||||||
fun getBase64Id(id: String): String {
|
fun getBase64Id(id: String): String {
|
||||||
return android.util.Base64.encodeToString(id.toByteArray(), android.util.Base64.URL_SAFE or android.util.Base64.NO_WRAP)
|
return android.util.Base64.encodeToString(
|
||||||
|
id.toByteArray(),
|
||||||
|
android.util.Base64.URL_SAFE or android.util.Base64.NO_WRAP
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the server connection configuration for the given ID.
|
||||||
|
* @param id The ID of the server connection configuration.
|
||||||
|
* @return The ServerConnectionConfig instance or null if not found.
|
||||||
|
*/
|
||||||
fun getServerConnectionConfig(id: String?): ServerConnectionConfig? {
|
fun getServerConnectionConfig(id: String?): ServerConnectionConfig? {
|
||||||
if (id == null) return null
|
if (id == null) return null
|
||||||
return deviceData.serverConnectionConfigs.find { it.id == id }
|
return deviceData.serverConnectionConfigs.find { it.id == id }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks the network connectivity status.
|
||||||
|
* @param ctx The context to use for checking connectivity.
|
||||||
|
* @return True if connected to the internet, false otherwise.
|
||||||
|
*/
|
||||||
fun checkConnectivity(ctx: Context): Boolean {
|
fun checkConnectivity(ctx: Context): Boolean {
|
||||||
val connectivityManager = ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
val connectivityManager =
|
||||||
|
ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
|
val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
|
||||||
if (capabilities != null) {
|
if (capabilities != null) {
|
||||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
||||||
|
@ -96,16 +131,24 @@ object DeviceManager {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the last playback session.
|
||||||
|
* @param playbackSession The playback session to set.
|
||||||
|
*/
|
||||||
fun setLastPlaybackSession(playbackSession: PlaybackSession) {
|
fun setLastPlaybackSession(playbackSession: PlaybackSession) {
|
||||||
deviceData.lastPlaybackSession = playbackSession
|
deviceData.lastPlaybackSession = playbackSession
|
||||||
dbManager.saveDeviceData(deviceData)
|
dbManager.saveDeviceData(deviceData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the widget updater.
|
||||||
|
* @param context The context to use for initializing the widget updater.
|
||||||
|
*/
|
||||||
fun initializeWidgetUpdater(context: Context) {
|
fun initializeWidgetUpdater(context: Context) {
|
||||||
Log.d(tag, "Initializing widget updater")
|
Log.d(tag, "Initializing widget updater")
|
||||||
widgetUpdater = (object : WidgetEventEmitter {
|
widgetUpdater =
|
||||||
|
(object : WidgetEventEmitter {
|
||||||
override fun onPlayerChanged(pns: PlayerNotificationService) {
|
override fun onPlayerChanged(pns: PlayerNotificationService) {
|
||||||
|
|
||||||
val isPlaying = pns.currentPlayer.isPlaying
|
val isPlaying = pns.currentPlayer.isPlaying
|
||||||
|
|
||||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||||
|
@ -114,15 +157,30 @@ object DeviceManager {
|
||||||
val playbackSession = pns.getCurrentPlaybackSessionCopy()
|
val playbackSession = pns.getCurrentPlaybackSessionCopy()
|
||||||
|
|
||||||
for (widgetId in ids) {
|
for (widgetId in ids) {
|
||||||
updateAppWidget(context, appWidgetManager, widgetId, playbackSession, isPlaying, PlayerNotificationService.isClosed)
|
updateAppWidget(
|
||||||
|
context,
|
||||||
|
appWidgetManager,
|
||||||
|
widgetId,
|
||||||
|
playbackSession,
|
||||||
|
isPlaying,
|
||||||
|
PlayerNotificationService.isClosed
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPlayerClosed() {
|
override fun onPlayerClosed() {
|
||||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||||
val componentName = ComponentName(context, MediaPlayerWidget::class.java)
|
val componentName = ComponentName(context, MediaPlayerWidget::class.java)
|
||||||
val ids = appWidgetManager.getAppWidgetIds(componentName)
|
val ids = appWidgetManager.getAppWidgetIds(componentName)
|
||||||
for (widgetId in ids) {
|
for (widgetId in ids) {
|
||||||
updateAppWidget(context, appWidgetManager, widgetId, deviceData.lastPlaybackSession, false, PlayerNotificationService.isClosed)
|
updateAppWidget(
|
||||||
|
context,
|
||||||
|
appWidgetManager,
|
||||||
|
widgetId,
|
||||||
|
deviceData.lastPlaybackSession,
|
||||||
|
false,
|
||||||
|
PlayerNotificationService.isClosed
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue