2019-05-09 12:45:30 -07:00
|
|
|
//
|
|
|
|
|
// AppDelegate.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 5/9/19.
|
|
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2019-06-04 18:29:50 -07:00
|
|
|
import UserNotifications
|
2019-05-09 12:45:30 -07:00
|
|
|
|
2019-05-30 17:10:50 -07:00
|
|
|
import AltSign
|
|
|
|
|
import Roxas
|
|
|
|
|
|
2019-05-09 12:45:30 -07:00
|
|
|
@UIApplicationMain
|
|
|
|
|
class AppDelegate: UIResponder, UIApplicationDelegate {
|
|
|
|
|
|
|
|
|
|
var window: UIWindow?
|
|
|
|
|
|
2019-05-20 21:24:53 +02:00
|
|
|
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
|
|
|
|
|
{
|
2019-05-30 17:10:50 -07:00
|
|
|
ServerManager.shared.startDiscovering()
|
|
|
|
|
|
2019-05-20 21:24:53 +02:00
|
|
|
DatabaseManager.shared.start { (error) in
|
|
|
|
|
if let error = error
|
|
|
|
|
{
|
|
|
|
|
print("Failed to start DatabaseManager.", error)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
print("Started DatabaseManager")
|
2019-06-04 13:53:21 -07:00
|
|
|
|
2019-06-17 16:31:10 -07:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
AppManager.shared.update()
|
|
|
|
|
}
|
2019-05-20 21:24:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-06-04 18:29:50 -07:00
|
|
|
|
|
|
|
|
if UserDefaults.standard.firstLaunch == nil
|
|
|
|
|
{
|
2019-06-06 14:46:23 -07:00
|
|
|
Keychain.shared.reset()
|
2019-06-04 18:29:50 -07:00
|
|
|
UserDefaults.standard.firstLaunch = Date()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.prepareForBackgroundFetch()
|
2019-05-30 17:10:50 -07:00
|
|
|
|
2019-05-09 12:45:30 -07:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 17:10:50 -07:00
|
|
|
func applicationDidEnterBackground(_ application: UIApplication)
|
|
|
|
|
{
|
|
|
|
|
ServerManager.shared.stopDiscovering()
|
2019-05-09 12:45:30 -07:00
|
|
|
}
|
|
|
|
|
|
2019-05-30 17:10:50 -07:00
|
|
|
func applicationWillEnterForeground(_ application: UIApplication)
|
|
|
|
|
{
|
2019-06-04 18:29:50 -07:00
|
|
|
AppManager.shared.update()
|
2019-05-30 17:10:50 -07:00
|
|
|
ServerManager.shared.startDiscovering()
|
2019-05-09 12:45:30 -07:00
|
|
|
}
|
2019-06-04 18:29:50 -07:00
|
|
|
}
|
2019-05-09 12:45:30 -07:00
|
|
|
|
2019-06-04 18:29:50 -07:00
|
|
|
extension AppDelegate
|
|
|
|
|
{
|
|
|
|
|
private func prepareForBackgroundFetch()
|
|
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
// "Fetch" every hour, but then refresh only those that need to be refreshed (so we don't drain the battery).
|
2019-06-10 15:03:47 -07:00
|
|
|
UIApplication.shared.setMinimumBackgroundFetchInterval(60 * 60)
|
2019-06-04 18:29:50 -07:00
|
|
|
|
|
|
|
|
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in
|
|
|
|
|
}
|
2019-05-09 12:45:30 -07:00
|
|
|
}
|
2019-06-04 18:29:50 -07:00
|
|
|
|
|
|
|
|
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
|
|
|
|
|
{
|
2019-06-21 11:20:03 -07:00
|
|
|
let installedApps = InstalledApp.fetchAppsForBackgroundRefresh(in: DatabaseManager.shared.viewContext)
|
|
|
|
|
guard !installedApps.isEmpty else { return completionHandler(.noData) }
|
|
|
|
|
|
|
|
|
|
print("Apps to refresh:", installedApps.map { $0.app.identifier })
|
|
|
|
|
|
2019-06-04 18:29:50 -07:00
|
|
|
ServerManager.shared.startDiscovering()
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
let content = UNMutableNotificationContent()
|
|
|
|
|
content.title = NSLocalizedString("Refreshing apps...", comment: "")
|
|
|
|
|
|
|
|
|
|
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.
|
2019-06-10 15:03:47 -07:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
func finish(_ result: Result<[String: Result<InstalledApp, Error>], Error>)
|
|
|
|
|
{
|
2019-06-04 18:29:50 -07:00
|
|
|
ServerManager.shared.stopDiscovering()
|
|
|
|
|
|
|
|
|
|
let content = UNMutableNotificationContent()
|
2019-06-21 11:20:03 -07:00
|
|
|
var shouldPresentAlert = true
|
2019-06-04 18:29:50 -07:00
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
let results = try result.get()
|
2019-06-21 11:20:03 -07:00
|
|
|
shouldPresentAlert = !results.isEmpty
|
2019-06-04 18:29:50 -07:00
|
|
|
|
|
|
|
|
for (_, result) in results
|
|
|
|
|
{
|
|
|
|
|
guard case let .failure(error) = result else { continue }
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
content.title = NSLocalizedString("Refreshed all apps!", comment: "")
|
2019-06-04 18:29:50 -07:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
print("Failed to refresh apps in background.", error)
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
content.title = NSLocalizedString("Failed to Refresh Apps", comment: "")
|
2019-06-04 18:29:50 -07:00
|
|
|
content.body = error.localizedDescription
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
shouldPresentAlert = true
|
2019-06-04 18:29:50 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-21 11:20:03 -07:00
|
|
|
if shouldPresentAlert
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
}
|
2019-06-04 18:29:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-06-21 11:20:03 -07:00
|
|
|
|
|
|
|
|
switch result
|
|
|
|
|
{
|
|
|
|
|
case .failure(ConnectionError.serverNotFound): completionHandler(.newData)
|
|
|
|
|
case .failure: completionHandler(.failed)
|
|
|
|
|
case .success: completionHandler(.newData)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let group = AppManager.shared.refresh(installedApps, presentingViewController: nil)
|
|
|
|
|
group.beginInstallationHandler = { (installedApp) in
|
|
|
|
|
guard installedApp.app.identifier == App.altstoreAppID else { return }
|
|
|
|
|
|
|
|
|
|
// We're starting to install AltStore, which means the app is about to quit.
|
|
|
|
|
// So, we say we were successful even though we technically don't know 100% yet.
|
|
|
|
|
// Also since AltServer has already received the app, it can finish installing even if we're no longer running in background.
|
|
|
|
|
|
|
|
|
|
if let error = group.error
|
|
|
|
|
{
|
|
|
|
|
finish(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var results = group.results
|
|
|
|
|
results[installedApp.app.identifier] = .success(installedApp)
|
|
|
|
|
|
|
|
|
|
finish(.success(results))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
group.completionHandler = { (result) in
|
|
|
|
|
finish(result)
|
2019-06-04 18:29:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-09 12:45:30 -07:00
|
|
|
}
|
|
|
|
|
}
|