mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-20 04:03:26 +01:00
[AltStore] Skip running apps when background refreshing
This commit is contained in:
@@ -12,11 +12,37 @@ import UserNotifications
|
|||||||
import AltSign
|
import AltSign
|
||||||
import Roxas
|
import Roxas
|
||||||
|
|
||||||
|
private extension CFNotificationName
|
||||||
|
{
|
||||||
|
static let requestAppState = CFNotificationName("com.altstore.RequestAppState" as CFString)
|
||||||
|
static let appIsRunning = CFNotificationName("com.altstore.AppState.Running" as CFString)
|
||||||
|
|
||||||
|
static func requestAppState(for appID: String) -> CFNotificationName
|
||||||
|
{
|
||||||
|
let name = String(CFNotificationName.requestAppState.rawValue) + "." + appID
|
||||||
|
return CFNotificationName(name as CFString)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func appIsRunning(for appID: String) -> CFNotificationName
|
||||||
|
{
|
||||||
|
let name = String(CFNotificationName.appIsRunning.rawValue) + "." + appID
|
||||||
|
return CFNotificationName(name as CFString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private let ReceivedApplicationState: @convention(c) (CFNotificationCenter?, UnsafeMutableRawPointer?, CFNotificationName?, UnsafeRawPointer?, CFDictionary?) -> Void =
|
||||||
|
{ (center, observer, name, object, userInfo) in
|
||||||
|
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate, let name = name else { return }
|
||||||
|
appDelegate.receivedApplicationState(notification: name)
|
||||||
|
}
|
||||||
|
|
||||||
@UIApplicationMain
|
@UIApplicationMain
|
||||||
class AppDelegate: UIResponder, UIApplicationDelegate {
|
class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||||
|
|
||||||
var window: UIWindow?
|
var window: UIWindow?
|
||||||
|
|
||||||
|
private var runningApplications: Set<String>?
|
||||||
|
|
||||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
|
||||||
{
|
{
|
||||||
ServerManager.shared.startDiscovering()
|
ServerManager.shared.startDiscovering()
|
||||||
@@ -75,7 +101,23 @@ extension AppDelegate
|
|||||||
let installedApps = InstalledApp.fetchAppsForBackgroundRefresh(in: DatabaseManager.shared.viewContext)
|
let installedApps = InstalledApp.fetchAppsForBackgroundRefresh(in: DatabaseManager.shared.viewContext)
|
||||||
guard !installedApps.isEmpty else { return completionHandler(.noData) }
|
guard !installedApps.isEmpty else { return completionHandler(.noData) }
|
||||||
|
|
||||||
print("Apps to refresh:", installedApps.map { $0.app.identifier })
|
self.runningApplications = []
|
||||||
|
|
||||||
|
let identifiers = installedApps.compactMap { $0.app?.identifier }
|
||||||
|
print("Apps to refresh:", identifiers)
|
||||||
|
|
||||||
|
DispatchQueue.global().async {
|
||||||
|
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
|
||||||
|
|
||||||
|
for identifier in identifiers
|
||||||
|
{
|
||||||
|
let appIsRunningNotification = CFNotificationName.appIsRunning(for: identifier)
|
||||||
|
CFNotificationCenterAddObserver(notificationCenter, nil, ReceivedApplicationState, appIsRunningNotification.rawValue, nil, .deliverImmediately)
|
||||||
|
|
||||||
|
let requestAppStateNotification = CFNotificationName.requestAppState(for: identifier)
|
||||||
|
CFNotificationCenterPostNotification(notificationCenter, requestAppStateNotification, nil, nil, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ServerManager.shared.startDiscovering()
|
ServerManager.shared.startDiscovering()
|
||||||
|
|
||||||
@@ -91,27 +133,13 @@ extension AppDelegate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sleep for three seconds to:
|
||||||
|
// a) give us time to discover AltServers
|
||||||
|
// b) give other processes a chance to respond to requestAppState notification
|
||||||
|
// NOTE: We need to sleep, *not* DispatchQueue.asyncAfter, or else background audio might fail to start.
|
||||||
|
Thread.sleep(forTimeInterval: 3)
|
||||||
|
|
||||||
BackgroundTaskManager.shared.performExtendedBackgroundTask { (taskResult, taskCompletionHandler) in
|
BackgroundTaskManager.shared.performExtendedBackgroundTask { (taskResult, taskCompletionHandler) in
|
||||||
if let error = taskResult.error
|
|
||||||
{
|
|
||||||
print("Error starting extended background task. Aborting.", error)
|
|
||||||
|
|
||||||
let content = UNMutableNotificationContent()
|
|
||||||
content.title = NSLocalizedString("Failed to refresh apps.", comment: "")
|
|
||||||
content.body = taskResult.error?.localizedDescription ?? ""
|
|
||||||
|
|
||||||
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.01, repeats: false)
|
|
||||||
|
|
||||||
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
|
|
||||||
UNUserNotificationCenter.current().add(request) { (error) in
|
|
||||||
if let error = error {
|
|
||||||
print(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait a few seconds so we have a chance to discover nearby AltServers.
|
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
|
|
||||||
|
|
||||||
func finish(_ result: Result<[String: Result<InstalledApp, Error>], Error>)
|
func finish(_ result: Result<[String: Result<InstalledApp, Error>], Error>)
|
||||||
{
|
{
|
||||||
@@ -165,7 +193,18 @@ extension AppDelegate
|
|||||||
taskCompletionHandler()
|
taskCompletionHandler()
|
||||||
}
|
}
|
||||||
|
|
||||||
let group = AppManager.shared.refresh(installedApps, presentingViewController: nil)
|
if let error = taskResult.error
|
||||||
|
{
|
||||||
|
print("Error starting extended background task. Aborting.", error)
|
||||||
|
finish(.failure(error))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
let filteredApps = installedApps.filter { !(self.runningApplications?.contains($0.app.identifier) ?? false) }
|
||||||
|
print("Filtered Apps to Refresh:", filteredApps.map { $0.app.identifier })
|
||||||
|
|
||||||
|
let group = AppManager.shared.refresh(filteredApps, presentingViewController: nil)
|
||||||
group.beginInstallationHandler = { (installedApp) in
|
group.beginInstallationHandler = { (installedApp) in
|
||||||
guard installedApp.app.identifier == App.altstoreAppID else { return }
|
guard installedApp.app.identifier == App.altstoreAppID else { return }
|
||||||
|
|
||||||
@@ -191,4 +230,12 @@ extension AppDelegate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func receivedApplicationState(notification: CFNotificationName)
|
||||||
|
{
|
||||||
|
let baseName = String(CFNotificationName.appIsRunning.rawValue)
|
||||||
|
|
||||||
|
let appID = String(notification.rawValue).replacingOccurrences(of: baseName + ".", with: "")
|
||||||
|
self.runningApplications?.insert(appID)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user