diff --git a/StikJIT/StikJITApp.swift b/StikJIT/StikJITApp.swift index 7538ea3..a4709b0 100644 --- a/StikJIT/StikJITApp.swift +++ b/StikJIT/StikJITApp.swift @@ -134,9 +134,6 @@ struct HeartbeatApp: App { if UserDefaults.standard.bool(forKey: "keepAliveAudio") { BackgroundAudioManager.shared.start() } - if UserDefaults.standard.bool(forKey: "keepAliveLocation") { - BackgroundLocationManager.shared.start() - } if let fixMethod = class_getInstanceMethod(UIDocumentPickerViewController.self, #selector(UIDocumentPickerViewController.fix_init(forOpeningContentTypes:asCopy:))), let origMethod = class_getInstanceMethod(UIDocumentPickerViewController.self, #selector(UIDocumentPickerViewController.init(forOpeningContentTypes:asCopy:))) { method_exchangeImplementations(origMethod, fixMethod) diff --git a/StikJIT/Utilities/BackgroundLocationManager.swift b/StikJIT/Utilities/BackgroundLocationManager.swift index ac14603..0fe6cab 100644 --- a/StikJIT/Utilities/BackgroundLocationManager.swift +++ b/StikJIT/Utilities/BackgroundLocationManager.swift @@ -10,6 +10,7 @@ final class BackgroundLocationManager: NSObject, CLLocationManagerDelegate { private let locationManager = CLLocationManager() private var isRunning = false + private var activityCount = 0 private override init() { super.init() @@ -39,6 +40,20 @@ final class BackgroundLocationManager: NSObject, CLLocationManagerDelegate { locationManager.stopUpdatingLocation() } + func requestStart() { + activityCount += 1 + if activityCount == 1, UserDefaults.standard.bool(forKey: "keepAliveLocation") { + start() + } + } + + func requestStop() { + activityCount = max(activityCount - 1, 0) + if activityCount == 0 { + stop() + } + } + func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { guard isRunning else { return } switch manager.authorizationStatus { diff --git a/StikJIT/Views/HomeView.swift b/StikJIT/Views/HomeView.swift index e825485..5a802e0 100644 --- a/StikJIT/Views/HomeView.swift +++ b/StikJIT/Views/HomeView.swift @@ -449,6 +449,7 @@ struct HomeView: View { private func startJITInBackground(bundleID: String? = nil, pid: Int? = nil, scriptData: Data? = nil, scriptName: String? = nil, triggeredByURLScheme: Bool = false) { isProcessing = true LogManager.shared.addInfoLog("Starting Debug for \(bundleID ?? String(pid ?? 0))") + BackgroundLocationManager.shared.requestStart() if triggeredByURLScheme { pubHeartBeat = false @@ -463,6 +464,7 @@ struct HomeView: View { let finishProcessing = { DispatchQueue.main.async { isProcessing = false + BackgroundLocationManager.shared.requestStop() } } diff --git a/StikJIT/Views/MapSelectionView.swift b/StikJIT/Views/MapSelectionView.swift index 3c226a6..b555abd 100644 --- a/StikJIT/Views/MapSelectionView.swift +++ b/StikJIT/Views/MapSelectionView.swift @@ -195,6 +195,9 @@ struct LocationSimulationView: View { } .onDisappear { stopResendLoop() + if backgroundTaskID != .invalid { + BackgroundLocationManager.shared.requestStop() + } endBackgroundTask() } } @@ -225,6 +228,7 @@ struct LocationSimulationView: View { if code == 0 { beginBackgroundTask() startResendLoop() + BackgroundLocationManager.shared.requestStart() } else { alertTitle = "Simulation Failed" alertMessage = "Could not simulate location (error \(code)). Make sure the device is connected and the DDI is mounted." @@ -245,6 +249,7 @@ struct LocationSimulationView: View { if code == 0 { coordinate = nil endBackgroundTask() + BackgroundLocationManager.shared.requestStop() } else { alertTitle = "Clear Failed" alertMessage = "Could not clear simulated location (error \(code))." diff --git a/StikJIT/Views/SettingsView.swift b/StikJIT/Views/SettingsView.swift index 664c776..efc0136 100644 --- a/StikJIT/Views/SettingsView.swift +++ b/StikJIT/Views/SettingsView.swift @@ -117,18 +117,16 @@ struct SettingsView: View { Toggle(isOn: $keepAliveLocation) { VStack(alignment: .leading, spacing: 2) { Text("Background Location") - Text("Uses low-accuracy location to stay alive even when another app plays audio.") + Text("Uses low-accuracy location to stay alive when an activity needs it.") .font(.caption).foregroundStyle(.secondary) } } .onChange(of: keepAliveLocation) { _, enabled in - if enabled { BackgroundLocationManager.shared.start() } - else { BackgroundLocationManager.shared.stop() } + if !enabled { BackgroundLocationManager.shared.stop() } } + } header: { Text("Background Keep-Alive") - } footer: { - Text("For Background Location to work reliably, go to **Settings → Privacy & Security → Location Services → StikDebug** and select **Always**.") } // 6) Behavior