[Android] Remote access: remember the stopped state
Nicolas Pomepuy
git at videolan.org
Mon Nov 25 11:26:11 UTC 2024
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Nov 25 10:35:06 2024 +0100| [662494bf987c5e283cb02ee7c41f233f44766c6e] | committer: Duncan McNamara
Remote access: remember the stopped state
Fixes #3111
> https://code.videolan.org/videolan/vlc-android/commit/662494bf987c5e283cb02ee7c41f233f44766c6e
---
.../src/main/java/org/videolan/tools/Settings.kt | 1 +
.../videolan/vlc/webserver/RemoteAccessServer.kt | 3 +++
.../videolan/vlc/webserver/RemoteAccessService.kt | 26 ++++++++++++++--------
3 files changed, 21 insertions(+), 9 deletions(-)
diff --git a/application/tools/src/main/java/org/videolan/tools/Settings.kt b/application/tools/src/main/java/org/videolan/tools/Settings.kt
index 9e84ff5f89..6440622074 100644
--- a/application/tools/src/main/java/org/videolan/tools/Settings.kt
+++ b/application/tools/src/main/java/org/videolan/tools/Settings.kt
@@ -134,6 +134,7 @@ const val ML_SCAN_OFF = 1
//Remote access
const val KEY_ENABLE_REMOTE_ACCESS = "enable_remote_access"
+const val KEY_REMOTE_ACCESS_LAST_STATE_STOPPED = "remote_access_last_state_stopped"
const val KEY_REMOTE_ACCESS_ML_CONTENT = "remote_access_medialibrary_content"
const val REMOTE_ACCESS_FILE_BROWSER_CONTENT = "remote_access_file_browser_content"
const val REMOTE_ACCESS_NETWORK_BROWSER_CONTENT = "remote_access_network_browser_content"
diff --git a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessServer.kt b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessServer.kt
index 84840cbb60..93bb004dd0 100644
--- a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessServer.kt
+++ b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessServer.kt
@@ -89,6 +89,7 @@ import org.videolan.medialibrary.interfaces.media.MediaWrapper
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.tools.AppScope
import org.videolan.tools.KEYSTORE_PASSWORD
+import org.videolan.tools.KEY_REMOTE_ACCESS_LAST_STATE_STOPPED
import org.videolan.tools.NetworkMonitor
import org.videolan.tools.REMOTE_ACCESS_NETWORK_BROWSER_CONTENT
import org.videolan.tools.Settings
@@ -203,6 +204,8 @@ class RemoteAccessServer(private val context: Context) : PlaybackService.Callbac
* Also start monitoring the network shares for the web browser
*/
suspend fun start() {
+ Settings.getInstance(context).putSingle(
+ KEY_REMOTE_ACCESS_LAST_STATE_STOPPED, false)
clearFileDownloads()
Log.i(TAG, "Server connecting")
_serverStatus.postValue(ServerStatus.CONNECTING)
diff --git a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessService.kt b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessService.kt
index e237ee6af1..2020f5f4be 100644
--- a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessService.kt
+++ b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessService.kt
@@ -25,6 +25,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
+import android.content.pm.ServiceInfo
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
@@ -45,7 +46,9 @@ import org.videolan.resources.ACTION_START_SERVER
import org.videolan.resources.ACTION_STOP_SERVER
import org.videolan.resources.AppContextProvider
import org.videolan.resources.util.registerReceiverCompat
+import org.videolan.resources.util.startForegroundCompat
import org.videolan.tools.KEY_ENABLE_REMOTE_ACCESS
+import org.videolan.tools.KEY_REMOTE_ACCESS_LAST_STATE_STOPPED
import org.videolan.tools.Settings
import org.videolan.tools.getContextWithLocale
import org.videolan.tools.putSingle
@@ -87,6 +90,8 @@ class RemoteAccessService : LifecycleService(), CoroutineScope by MainScope() {
if (!::server.isInitialized) return
when (intent.action) {
ACTION_STOP_SERVER -> {
+ Settings.getInstance(this at RemoteAccessService)
+ .putSingle(KEY_REMOTE_ACCESS_LAST_STATE_STOPPED, true)
startServerActor.trySend(ACTION_STOP_SERVER)
}
ACTION_DISABLE_SERVER -> {
@@ -118,14 +123,17 @@ class RemoteAccessService : LifecycleService(), CoroutineScope by MainScope() {
override fun onCreate() {
super.onCreate()
if (AndroidUtil.isOOrLater) forceForeground()
- lifecycleScope.launch(Dispatchers.IO) {
- server = RemoteAccessServer.getInstance(applicationContext)
- server.start()
- withContext(Dispatchers.Main) {
- server.serverStatus.observe(this at RemoteAccessService) {
- forceForeground()
- }
- }
+ lifecycleScope.launch(Dispatchers.IO) {
+ server = RemoteAccessServer.getInstance(applicationContext)
+ if (!Settings.getInstance(this at RemoteAccessService)
+ .getBoolean(KEY_REMOTE_ACCESS_LAST_STATE_STOPPED, false)
+ )
+ server.start()
+ withContext(Dispatchers.Main) {
+ server.serverStatus.observe(this at RemoteAccessService) {
+ forceForeground()
+ }
+ }
}
val filter = IntentFilter()
filter.addAction(ACTION_STOP_SERVER)
@@ -149,7 +157,7 @@ class RemoteAccessService : LifecycleService(), CoroutineScope by MainScope() {
val started = ::server.isInitialized && server.serverStatus.value == ServerStatus.STARTED
val notification = NotificationHelper.createRemoteAccessNotification(applicationContext, contentString, started)
try {
- startForeground(44, notification)
+ startForegroundCompat(44, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
} catch (e: Exception) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
Log.w("RemoteAccessService", "ForegroundServiceStartNotAllowedException caught!")
More information about the Android
mailing list