@@ -17,14 +17,20 @@ import android.util.Log
1717import androidx.core.app.NotificationCompat
1818import com.sameerasw.airsync.MainActivity
1919import com.sameerasw.airsync.R
20+ import com.sameerasw.airsync.data.local.DataStoreManager
2021import com.sameerasw.airsync.utils.DiscoveryMode
22+ import com.sameerasw.airsync.utils.MacDeviceStatusManager
23+ import com.sameerasw.airsync.utils.ShortcutUtil
2124import com.sameerasw.airsync.utils.UDPDiscoveryManager
25+ import com.sameerasw.airsync.utils.WebDavServer
2226import com.sameerasw.airsync.utils.WebSocketUtil
2327import kotlinx.coroutines.CoroutineScope
2428import kotlinx.coroutines.Dispatchers
2529import kotlinx.coroutines.Job
2630import kotlinx.coroutines.cancel
31+ import kotlinx.coroutines.flow.combine
2732import kotlinx.coroutines.flow.first
33+ import kotlinx.coroutines.launch
2834import kotlinx.coroutines.runBlocking
2935
3036/* *
@@ -38,14 +44,17 @@ class AirSyncService : Service() {
3844 private var connectedDeviceName: String? = null
3945 private var isScanning = false
4046
47+ private var webDavServer: WebDavServer ? = null
48+ private var webDavJob: Job ? = null
49+
4150 // Network state tracking
4251 private var networkCallback: ConnectivityManager .NetworkCallback ? = null
4352
4453 override fun onCreate () {
4554 super .onCreate()
4655 Log .d(TAG , " AirSyncService created" )
4756 createNotificationChannel()
48- com.sameerasw.airsync.utils. MacDeviceStatusManager .startMonitoring(this )
57+ MacDeviceStatusManager .startMonitoring(this )
4958 registerNetworkCallback()
5059 }
5160
@@ -58,7 +67,7 @@ class AirSyncService : Service() {
5867 ACTION_START_SYNC -> {
5968 connectedDeviceName = intent.getStringExtra(EXTRA_DEVICE_NAME ) ? : " Mac"
6069 startSync()
61- com.sameerasw.airsync.utils. ShortcutUtil .refreshShortcuts(this , true )
70+ ShortcutUtil .refreshShortcuts(this , true )
6271 }
6372
6473 ACTION_STOP_SYNC -> stopSync()
@@ -83,7 +92,7 @@ class AirSyncService : Service() {
8392 startForeground(NOTIFICATION_ID , buildNotification())
8493
8594 val dataStoreManager =
86- com.sameerasw.airsync.data.local. DataStoreManager .getInstance(applicationContext)
95+ DataStoreManager .getInstance(applicationContext)
8796 val isDiscoveryEnabled = runBlocking {
8897 dataStoreManager.getDeviceDiscoveryEnabled().first()
8998 }
@@ -101,6 +110,39 @@ class AirSyncService : Service() {
101110 WebSocketUtil .requestAutoReconnect(this )
102111 }
103112
113+ private fun startWebDavServer () {
114+ if (webDavServer == null ) {
115+ webDavServer = WebDavServer (this )
116+ }
117+ webDavServer?.start()
118+ }
119+
120+ private fun stopWebDavServer () {
121+ webDavServer?.stop()
122+ webDavServer = null
123+ }
124+
125+ private fun monitorWebDavRequirements () {
126+ webDavJob?.cancel()
127+ webDavJob = scope.launch {
128+ val dataStoreManager = DataStoreManager .getInstance(applicationContext)
129+ combine(
130+ dataStoreManager.isFileAccessEnabled(),
131+ dataStoreManager.getLastConnectedDevice()
132+ ) { isEnabled, device ->
133+ Log .d(TAG , " WebDAV flow evaluation: isEnabled=$isEnabled , isPlus=${device?.isPlus} " )
134+ isEnabled && device?.isPlus == true
135+ }.collect { shouldStart ->
136+ Log .d(TAG , " WebDAV requirement state updated: shouldStart = $shouldStart " )
137+ if (shouldStart) {
138+ startWebDavServer()
139+ } else {
140+ stopWebDavServer()
141+ }
142+ }
143+ }
144+ }
145+
104146 private fun handleAppForeground () {
105147 if (isScanning) {
106148 Log .d(TAG , " App in foreground, switching to ACTIVE discovery" )
@@ -118,12 +160,16 @@ class AirSyncService : Service() {
118160 }
119161
120162 private fun startSync () {
163+ if (! isScanning && connectedDeviceName != null ) {
164+ Log .d(TAG , " AirSync foreground service already in sync state, ignoring" )
165+ return
166+ }
121167 Log .d(TAG , " Starting AirSync foreground service (connected)" )
122168 isScanning = false
123169 startForeground(NOTIFICATION_ID , buildNotification())
124170
125171 val dataStoreManager =
126- com.sameerasw.airsync.data.local. DataStoreManager .getInstance(applicationContext)
172+ DataStoreManager .getInstance(applicationContext)
127173 val isDiscoveryEnabled = runBlocking {
128174 dataStoreManager.getDeviceDiscoveryEnabled().first()
129175 }
@@ -134,11 +180,15 @@ class AirSyncService : Service() {
134180 UDPDiscoveryManager .setDiscoveryMode(this , DiscoveryMode .PASSIVE )
135181
136182 WakeupService .startService(this )
183+ monitorWebDavRequirements()
137184 }
138185
139186 private fun stopSync () {
140187 Log .d(TAG , " Stopping AirSync foreground service" )
141- com.sameerasw.airsync.utils.ShortcutUtil .refreshShortcuts(this , false )
188+ webDavJob?.cancel()
189+ webDavJob = null
190+ stopWebDavServer()
191+ ShortcutUtil .refreshShortcuts(this , false )
142192 UDPDiscoveryManager .stop(this )
143193 WakeupService .stopService(this )
144194 stopForeground(STOP_FOREGROUND_REMOVE )
@@ -234,8 +284,9 @@ class AirSyncService : Service() {
234284 }
235285 }
236286
237- com.sameerasw.airsync.utils.MacDeviceStatusManager .stopMonitoring()
238- com.sameerasw.airsync.utils.MacDeviceStatusManager .cleanup(this )
287+ stopWebDavServer()
288+ MacDeviceStatusManager .stopMonitoring()
289+ MacDeviceStatusManager .cleanup(this )
239290 scope.coroutineContext.cancel()
240291 super .onDestroy()
241292 }
0 commit comments