-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathAppDelegate.swift
155 lines (138 loc) · 5.96 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// AppDelegate.swift
// Cryptomator
//
// Created by Philipp Schmid on 27.04.20.
// Copyright © 2020 Skymatic GmbH. All rights reserved.
//
import CocoaLumberjackSwift
import CryptomatorCloudAccess
import CryptomatorCloudAccessCore
import CryptomatorCommon
import CryptomatorCommonCore
import Dependencies
import MSAL
import ObjectiveDropboxOfficial
import StoreKit
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var coordinator: MainCoordinator?
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Set up logger
LoggerSetup.oneTimeSetup()
// Set up IAP Checker
setupIAP()
// Set up database
DatabaseManager.shared = DatabaseManager()
VaultDBManager.shared.recoverMissingFileProviderDomains().catch { error in
DDLogError("Recover missing FileProvider domains failed with error: \(error)")
}
cleanup()
// Set up cloud storage services
DropboxSetup.constants = DropboxSetup(appKey: CloudAccessSecrets.dropboxAppKey, sharedContainerIdentifier: nil, keychainService: CryptomatorConstants.mainAppBundleId, forceForegroundSession: true)
GoogleDriveSetup.constants = GoogleDriveSetup(clientId: CloudAccessSecrets.googleDriveClientId, redirectURL: CloudAccessSecrets.googleDriveRedirectURL!, sharedContainerIdentifier: nil)
do {
let microsoftGraphConfiguration = MSALPublicClientApplicationConfig(clientId: CloudAccessSecrets.microsoftGraphClientId, redirectUri: CloudAccessSecrets.microsoftGraphRedirectURI, authority: nil)
microsoftGraphConfiguration.cacheConfig.keychainSharingGroup = CryptomatorConstants.mainAppBundleId
let microsoftGraphClientApplication = try MSALPublicClientApplication(configuration: microsoftGraphConfiguration)
MicrosoftGraphSetup.constants = MicrosoftGraphSetup(clientApplication: microsoftGraphClientApplication, sharedContainerIdentifier: nil)
} catch {
DDLogError("Setting up OneDrive failed with error: \(error)")
}
PCloudSetup.constants = PCloudSetup(appKey: CloudAccessSecrets.pCloudAppKey, sharedContainerIdentifier: nil)
BoxSetup.constants = BoxSetup(clientId: CloudAccessSecrets.boxClientId, clientSecret: CloudAccessSecrets.boxClientSecret, sharedContainerIdentifier: nil)
// Set up payment queue
SKPaymentQueue.default().add(StoreObserver.shared)
// Create window
coordinator = MainCoordinator()
#if SNAPSHOTS
coordinator = SnapshotCoordinator()
UIView.setAnimationsEnabled(false)
#endif
coordinator?.start()
StoreObserver.shared.fallbackDelegate = coordinator
window = UIWindow(frame: UIScreen.main.bounds)
window?.tintColor = .cryptomatorPrimary
window?.rootViewController = coordinator?.rootViewController
window?.makeKeyAndVisible()
return true
}
func application(_: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
if url.scheme == CloudAccessSecrets.dropboxURLScheme {
return DBClientsManager.handleRedirectURL(url) { authResult in
guard let authResult = authResult else {
return
}
if authResult.isSuccess() {
let tokenUid = authResult.accessToken.uid
let credential = DropboxCredential(tokenUID: tokenUid)
DropboxAuthenticator.pendingAuthentication?.fulfill(credential)
} else if authResult.isCancel() {
DropboxAuthenticator.pendingAuthentication?.reject(CocoaError(.userCancelled))
} else if authResult.isError() {
DropboxAuthenticator.pendingAuthentication?.reject(authResult.nsError)
}
}
} else if url.scheme == CloudAccessSecrets.googleDriveRedirectURLScheme {
return GoogleDriveAuthenticator.currentAuthorizationFlow?.resumeExternalUserAgentFlow(with: url) ?? false
} else if url.scheme == CloudAccessSecrets.microsoftGraphRedirectURIScheme {
return MSALPublicClientApplication.handleMSALResponse(url, sourceApplication: options[.sourceApplication] as? String)
}
return false
}
func applicationDidBecomeActive(_ application: UIApplication) {
PremiumManager.shared.refreshStatus()
}
func applicationWillTerminate(_ application: UIApplication) {
SKPaymentQueue.default().remove(StoreObserver.shared)
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
switch userActivity.activityType {
case "OpenVaultIntent":
return handleOpenInFilesApp(for: userActivity)
default:
DDLogInfo("Received an unsupported userActivity of type: \(String(describing: userActivity.activityType))")
return false
}
}
private func handleOpenInFilesApp(for userActivity: NSUserActivity) -> Bool {
guard let vaultUID = userActivity.userInfo?["vaultUID"] as? String else {
DDLogError("Received a userActivity of type: \(String(describing: userActivity.activityType)) which has no vaultUID.")
return false
}
FilesAppUtil.showFilesApp(forVaultUID: vaultUID)
return true
}
private func cleanup() {
_ = VaultDBManager.shared.removeAllUnusedFileProviderDomains()
do {
let webDAVAccountUIDs = try CloudProviderAccountDBManager.shared.getAllAccountUIDs(for: .webDAV(type: .custom))
try WebDAVCredentialManager.shared.removeUnusedWebDAVCredentials(existingAccountUIDs: webDAVAccountUIDs)
} catch {
DDLogError("Clean up unused WebDAV Credentials failed with error: \(error)")
}
}
private func setupIAP() {
#if ALWAYS_PREMIUM
DDLogDebug("Always activated premium")
CryptomatorUserDefaults.shared.fullVersionUnlocked = true
#else
DDLogDebug("Freemium version")
#endif
}
}
/**
Define the liveValue in the main target since compilation flags do not work on Swift Package Manager level.
Be aware that it is needed to set the default value once per app launch (+ also when launching the FileProviderExtension).
*/
extension FullVersionCheckerKey: DependencyKey {
public static var liveValue: FullVersionChecker {
#if ALWAYS_PREMIUM
return AlwaysActivatedPremium.default
#else
return UserDefaultsFullVersionChecker.default
#endif
}
}