Browse Source

Remote access: remember the stopped state

Fixes #3111
merge-requests/2174/head
Nicolas Pomepuy 1 year ago
committed by Duncan McNamara
parent
commit
662494bf98
  1. 1
      application/tools/src/main/java/org/videolan/tools/Settings.kt
  2. 3
      application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessServer.kt
  3. 26
      application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessService.kt

1
application/tools/src/main/java/org/videolan/tools/Settings.kt

@ -134,6 +134,7 @@ const val ML_SCAN_OFF = 1
//Remote access //Remote access
const val KEY_ENABLE_REMOTE_ACCESS = "enable_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 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_FILE_BROWSER_CONTENT = "remote_access_file_browser_content"
const val REMOTE_ACCESS_NETWORK_BROWSER_CONTENT = "remote_access_network_browser_content" const val REMOTE_ACCESS_NETWORK_BROWSER_CONTENT = "remote_access_network_browser_content"

3
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.medialibrary.media.MediaLibraryItem
import org.videolan.tools.AppScope import org.videolan.tools.AppScope
import org.videolan.tools.KEYSTORE_PASSWORD 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.NetworkMonitor
import org.videolan.tools.REMOTE_ACCESS_NETWORK_BROWSER_CONTENT import org.videolan.tools.REMOTE_ACCESS_NETWORK_BROWSER_CONTENT
import org.videolan.tools.Settings 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 * Also start monitoring the network shares for the web browser
*/ */
suspend fun start() { suspend fun start() {
Settings.getInstance(context).putSingle(
KEY_REMOTE_ACCESS_LAST_STATE_STOPPED, false)
clearFileDownloads() clearFileDownloads()
Log.i(TAG, "Server connecting") Log.i(TAG, "Server connecting")
_serverStatus.postValue(ServerStatus.CONNECTING) _serverStatus.postValue(ServerStatus.CONNECTING)

26
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.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.content.pm.ServiceInfo
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import androidx.annotation.RequiresApi 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.ACTION_STOP_SERVER
import org.videolan.resources.AppContextProvider import org.videolan.resources.AppContextProvider
import org.videolan.resources.util.registerReceiverCompat 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_ENABLE_REMOTE_ACCESS
import org.videolan.tools.KEY_REMOTE_ACCESS_LAST_STATE_STOPPED
import org.videolan.tools.Settings import org.videolan.tools.Settings
import org.videolan.tools.getContextWithLocale import org.videolan.tools.getContextWithLocale
import org.videolan.tools.putSingle import org.videolan.tools.putSingle
@ -87,6 +90,8 @@ class RemoteAccessService : LifecycleService(), CoroutineScope by MainScope() {
if (!::server.isInitialized) return if (!::server.isInitialized) return
when (intent.action) { when (intent.action) {
ACTION_STOP_SERVER -> { ACTION_STOP_SERVER -> {
Settings.getInstance(this@RemoteAccessService)
.putSingle(KEY_REMOTE_ACCESS_LAST_STATE_STOPPED, true)
startServerActor.trySend(ACTION_STOP_SERVER) startServerActor.trySend(ACTION_STOP_SERVER)
} }
ACTION_DISABLE_SERVER -> { ACTION_DISABLE_SERVER -> {
@ -118,14 +123,17 @@ class RemoteAccessService : LifecycleService(), CoroutineScope by MainScope() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
if (AndroidUtil.isOOrLater) forceForeground() if (AndroidUtil.isOOrLater) forceForeground()
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
server = RemoteAccessServer.getInstance(applicationContext) server = RemoteAccessServer.getInstance(applicationContext)
server.start() if (!Settings.getInstance(this@RemoteAccessService)
withContext(Dispatchers.Main) { .getBoolean(KEY_REMOTE_ACCESS_LAST_STATE_STOPPED, false)
server.serverStatus.observe(this@RemoteAccessService) { )
forceForeground() server.start()
} withContext(Dispatchers.Main) {
} server.serverStatus.observe(this@RemoteAccessService) {
forceForeground()
}
}
} }
val filter = IntentFilter() val filter = IntentFilter()
filter.addAction(ACTION_STOP_SERVER) 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 started = ::server.isInitialized && server.serverStatus.value == ServerStatus.STARTED
val notification = NotificationHelper.createRemoteAccessNotification(applicationContext, contentString, started) val notification = NotificationHelper.createRemoteAccessNotification(applicationContext, contentString, started)
try { try {
startForeground(44, notification) startForegroundCompat(44, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
} catch (e: Exception) { } catch (e: Exception) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
Log.w("RemoteAccessService", "ForegroundServiceStartNotAllowedException caught!") Log.w("RemoteAccessService", "ForegroundServiceStartNotAllowedException caught!")

Loading…
Cancel
Save