diff --git a/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt b/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt index fd502cbd..c8e43a0e 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/device/DeviceManager.kt @@ -12,11 +12,19 @@ import com.audiobookshelf.app.managers.DbManager import com.audiobookshelf.app.player.PlayerNotificationService import com.audiobookshelf.app.updateAppWidget +/** Interface for widget event handling. */ interface WidgetEventEmitter { - fun onPlayerChanged(pns:PlayerNotificationService) + /** + * Called when the player state changes. + * @param pns The PlayerNotificationService instance. + */ + fun onPlayerChanged(pns: PlayerNotificationService) + + /** Called when the player is closed. */ fun onPlayerClosed() } +/** Singleton object for managing device-related operations. */ object DeviceManager { const val tag = "DeviceManager" @@ -24,19 +32,26 @@ object DeviceManager { var deviceData: DeviceData = dbManager.getDeviceData() var serverConnectionConfig: ServerConnectionConfig? = null - val serverConnectionConfigId get() = serverConnectionConfig?.id ?: "" - val serverAddress get() = serverConnectionConfig?.address ?: "" - val serverUserId get() = serverConnectionConfig?.userId ?: "" - val token get() = serverConnectionConfig?.token ?: "" - val isConnectedToServer get() = serverConnectionConfig != null + val serverConnectionConfigId + get() = serverConnectionConfig?.id ?: "" + val serverAddress + get() = serverConnectionConfig?.address ?: "" + val serverUserId + get() = serverConnectionConfig?.userId ?: "" + val token + get() = serverConnectionConfig?.token ?: "" + val isConnectedToServer + get() = serverConnectionConfig != null - var widgetUpdater:WidgetEventEmitter? = null + var widgetUpdater: WidgetEventEmitter? = null init { Log.d(tag, "Device Manager Singleton invoked") // 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 = "06:00" deviceData.deviceSettings?.sleepTimerLength = 900000L @@ -65,21 +80,41 @@ object DeviceManager { deviceData.deviceSettings?.androidAutoBrowseLimitForGrouping = 100 } if (deviceData.deviceSettings?.androidAutoBrowseSeriesSequenceOrder == null) { - deviceData.deviceSettings?.androidAutoBrowseSeriesSequenceOrder = AndroidAutoBrowseSeriesSequenceOrderSetting.ASC + deviceData.deviceSettings?.androidAutoBrowseSeriesSequenceOrder = + AndroidAutoBrowseSeriesSequenceOrderSetting.ASC } } - fun getBase64Id(id:String):String { - return android.util.Base64.encodeToString(id.toByteArray(), android.util.Base64.URL_SAFE or android.util.Base64.NO_WRAP) + /** + * Encodes the given ID to a Base64 string. + * @param id The ID to encode. + * @return The Base64 encoded string. + */ + fun getBase64Id(id: String): String { + return android.util.Base64.encodeToString( + id.toByteArray(), + android.util.Base64.URL_SAFE or android.util.Base64.NO_WRAP + ) } - fun getServerConnectionConfig(id:String?):ServerConnectionConfig? { + /** + * 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? { if (id == null) return null return deviceData.serverConnectionConfigs.find { it.id == id } } - fun checkConnectivity(ctx:Context): Boolean { - val connectivityManager = ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + /** + * 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 { + val connectivityManager = + ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) if (capabilities != null) { if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { @@ -96,35 +131,58 @@ object DeviceManager { return false } - fun setLastPlaybackSession(playbackSession:PlaybackSession) { + /** + * Sets the last playback session. + * @param playbackSession The playback session to set. + */ + fun setLastPlaybackSession(playbackSession: PlaybackSession) { deviceData.lastPlaybackSession = playbackSession dbManager.saveDeviceData(deviceData) } - fun initializeWidgetUpdater(context:Context) { + /** + * Initializes the widget updater. + * @param context The context to use for initializing the widget updater. + */ + fun initializeWidgetUpdater(context: Context) { Log.d(tag, "Initializing widget updater") - widgetUpdater = (object : WidgetEventEmitter { - override fun onPlayerChanged(pns: PlayerNotificationService) { + widgetUpdater = + (object : WidgetEventEmitter { + override fun onPlayerChanged(pns: PlayerNotificationService) { + val isPlaying = pns.currentPlayer.isPlaying - val isPlaying = pns.currentPlayer.isPlaying + val appWidgetManager = AppWidgetManager.getInstance(context) + val componentName = ComponentName(context, MediaPlayerWidget::class.java) + val ids = appWidgetManager.getAppWidgetIds(componentName) + val playbackSession = pns.getCurrentPlaybackSessionCopy() - val appWidgetManager = AppWidgetManager.getInstance(context) - val componentName = ComponentName(context, MediaPlayerWidget::class.java) - val ids = appWidgetManager.getAppWidgetIds(componentName) - val playbackSession = pns.getCurrentPlaybackSessionCopy() + for (widgetId in ids) { + updateAppWidget( + context, + appWidgetManager, + widgetId, + playbackSession, + isPlaying, + PlayerNotificationService.isClosed + ) + } + } - for (widgetId in ids) { - updateAppWidget(context, appWidgetManager, widgetId, playbackSession, isPlaying, PlayerNotificationService.isClosed) - } - } - override fun onPlayerClosed() { - val appWidgetManager = AppWidgetManager.getInstance(context) - val componentName = ComponentName(context, MediaPlayerWidget::class.java) - val ids = appWidgetManager.getAppWidgetIds(componentName) - for (widgetId in ids) { - updateAppWidget(context, appWidgetManager, widgetId, deviceData.lastPlaybackSession, false, PlayerNotificationService.isClosed) - } - } - }) + override fun onPlayerClosed() { + val appWidgetManager = AppWidgetManager.getInstance(context) + val componentName = ComponentName(context, MediaPlayerWidget::class.java) + val ids = appWidgetManager.getAppWidgetIds(componentName) + for (widgetId in ids) { + updateAppWidget( + context, + appWidgetManager, + widgetId, + deviceData.lastPlaybackSession, + false, + PlayerNotificationService.isClosed + ) + } + } + }) } }