improve the location stuff

This commit is contained in:
Stephen B 2026-02-25 16:15:24 -05:00
parent b61fecb630
commit 1bdf72ac2f
5 changed files with 25 additions and 8 deletions

View file

@ -134,9 +134,6 @@ struct HeartbeatApp: App {
if UserDefaults.standard.bool(forKey: "keepAliveAudio") { if UserDefaults.standard.bool(forKey: "keepAliveAudio") {
BackgroundAudioManager.shared.start() 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:))), if let fixMethod = class_getInstanceMethod(UIDocumentPickerViewController.self, #selector(UIDocumentPickerViewController.fix_init(forOpeningContentTypes:asCopy:))),
let origMethod = class_getInstanceMethod(UIDocumentPickerViewController.self, #selector(UIDocumentPickerViewController.init(forOpeningContentTypes:asCopy:))) { let origMethod = class_getInstanceMethod(UIDocumentPickerViewController.self, #selector(UIDocumentPickerViewController.init(forOpeningContentTypes:asCopy:))) {
method_exchangeImplementations(origMethod, fixMethod) method_exchangeImplementations(origMethod, fixMethod)

View file

@ -10,6 +10,7 @@ final class BackgroundLocationManager: NSObject, CLLocationManagerDelegate {
private let locationManager = CLLocationManager() private let locationManager = CLLocationManager()
private var isRunning = false private var isRunning = false
private var activityCount = 0
private override init() { private override init() {
super.init() super.init()
@ -39,6 +40,20 @@ final class BackgroundLocationManager: NSObject, CLLocationManagerDelegate {
locationManager.stopUpdatingLocation() 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) { func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
guard isRunning else { return } guard isRunning else { return }
switch manager.authorizationStatus { switch manager.authorizationStatus {

View file

@ -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) { private func startJITInBackground(bundleID: String? = nil, pid: Int? = nil, scriptData: Data? = nil, scriptName: String? = nil, triggeredByURLScheme: Bool = false) {
isProcessing = true isProcessing = true
LogManager.shared.addInfoLog("Starting Debug for \(bundleID ?? String(pid ?? 0))") LogManager.shared.addInfoLog("Starting Debug for \(bundleID ?? String(pid ?? 0))")
BackgroundLocationManager.shared.requestStart()
if triggeredByURLScheme { if triggeredByURLScheme {
pubHeartBeat = false pubHeartBeat = false
@ -463,6 +464,7 @@ struct HomeView: View {
let finishProcessing = { let finishProcessing = {
DispatchQueue.main.async { DispatchQueue.main.async {
isProcessing = false isProcessing = false
BackgroundLocationManager.shared.requestStop()
} }
} }

View file

@ -195,6 +195,9 @@ struct LocationSimulationView: View {
} }
.onDisappear { .onDisappear {
stopResendLoop() stopResendLoop()
if backgroundTaskID != .invalid {
BackgroundLocationManager.shared.requestStop()
}
endBackgroundTask() endBackgroundTask()
} }
} }
@ -225,6 +228,7 @@ struct LocationSimulationView: View {
if code == 0 { if code == 0 {
beginBackgroundTask() beginBackgroundTask()
startResendLoop() startResendLoop()
BackgroundLocationManager.shared.requestStart()
} else { } else {
alertTitle = "Simulation Failed" alertTitle = "Simulation Failed"
alertMessage = "Could not simulate location (error \(code)). Make sure the device is connected and the DDI is mounted." 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 { if code == 0 {
coordinate = nil coordinate = nil
endBackgroundTask() endBackgroundTask()
BackgroundLocationManager.shared.requestStop()
} else { } else {
alertTitle = "Clear Failed" alertTitle = "Clear Failed"
alertMessage = "Could not clear simulated location (error \(code))." alertMessage = "Could not clear simulated location (error \(code))."

View file

@ -117,18 +117,16 @@ struct SettingsView: View {
Toggle(isOn: $keepAliveLocation) { Toggle(isOn: $keepAliveLocation) {
VStack(alignment: .leading, spacing: 2) { VStack(alignment: .leading, spacing: 2) {
Text("Background Location") 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) .font(.caption).foregroundStyle(.secondary)
} }
} }
.onChange(of: keepAliveLocation) { _, enabled in .onChange(of: keepAliveLocation) { _, enabled in
if enabled { BackgroundLocationManager.shared.start() } if !enabled { BackgroundLocationManager.shared.stop() }
else { BackgroundLocationManager.shared.stop() }
} }
} header: { } header: {
Text("Background Keep-Alive") 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 // 6) Behavior