-
-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathios_app_delegate.mm
More file actions
226 lines (190 loc) · 9.15 KB
/
Copy pathios_app_delegate.mm
File metadata and controls
226 lines (190 loc) · 9.15 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#ifndef IO_UNDER_QT
#import <ConnectIQ/ConnectIQ.h>
#import "UIKit/UIKit.h"
#import "UserNotifications/UserNotifications.h"
#import <objc/runtime.h>
#include <QDebug>
#include <QMetaObject>
#include "homeform.h"
#include "lockscreen.h"
#include "authutils.h"
// Qt defines QIOSApplicationDelegate internally as a UIResponder-backed
// UIApplicationDelegate. Keep the local declaration aligned with that shape
// so category methods can legally forward unhandled events to super.
@interface QIOSApplicationDelegate : UIResponder <UIApplicationDelegate, IQAppMessageDelegate, IQUIOverrideDelegate, IQDeviceEventDelegate>
@end
@interface QIOSApplicationDelegate (QZApplicationDelegate) <IQAppMessageDelegate, IQUIOverrideDelegate, IQDeviceEventDelegate>
@end
@interface UIApplication (QZKeyboardShortcuts)
- (void)qz_sendEvent:(UIEvent *)event;
@end
@implementation UIApplication (QZKeyboardShortcuts)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method originalMethod = class_getInstanceMethod(self, @selector(sendEvent:));
Method swizzledMethod = class_getInstanceMethod(self, @selector(qz_sendEvent:));
method_exchangeImplementations(originalMethod, swizzledMethod);
});
}
- (void)qz_sendEvent:(UIEvent *)event {
bool didHandleShortcut = false;
if (@available(iOS 13.4, *)) {
if ([event isKindOfClass:[UIPressesEvent class]]) {
UIPressesEvent *pressesEvent = (UIPressesEvent *)event;
for (UIPress *press in pressesEvent.allPresses) {
if (press.phase != UIPressPhaseBegan) {
continue;
}
UIKey *key = press.key;
if (key == nil || key.charactersIgnoringModifiers.length == 0) {
continue;
}
const QString sequence =
QString::fromUtf8(key.charactersIgnoringModifiers.UTF8String).trimmed().toUpper();
if (homeform::singleton() && homeform::singleton()->handleKeyboardShortcut(sequence)) {
didHandleShortcut = true;
}
}
}
}
if (!didHandleShortcut) {
[self qz_sendEvent:event];
}
}
@end
@implementation QIOSApplicationDelegate (QZApplicationDelegate)
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
qDebug() << "QZ iOS launch";
// when CoreBluetooth state restoration is in place iOS can relaunch us in
// background to deliver a BLE event: useful to know it while reading a log
NSArray *restoredCentrals = [launchOptions objectForKey:UIApplicationLaunchOptionsBluetoothCentralsKey];
if (restoredCentrals.count > 0) {
qDebug() << "QZ iOS launch: relaunched by CoreBluetooth for" << (int)restoredCentrals.count << "central(s)";
}
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge
completionHandler:^(BOOL granted, NSError *error){
if(granted == YES) {
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
};
}];
if (@available(iOS 13.0, *)) {
[self setupDynamicQuickActions];
}
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
Q_UNUSED(application)
qDebug() << "QZ iOS lifecycle: applicationWillEnterForeground";
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
Q_UNUSED(application)
qDebug() << "QZ iOS lifecycle: applicationDidBecomeActive";
}
- (void)applicationWillResignActive:(UIApplication *)application {
Q_UNUSED(application)
qDebug() << "QZ iOS lifecycle: applicationWillResignActive";
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// With the background keep alive running the app stays in the "background"
// state instead of being suspended, so every QTimer (homeform::update, the
// per device refresh timers) and the BLE connections keep working.
qDebug() << "QZ iOS lifecycle: applicationDidEnterBackground, remaining background time"
<< application.backgroundTimeRemaining;
}
- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {
bool didHandleShortcut = false;
if (@available(iOS 13.4, *)) {
for (UIPress *press in presses) {
UIKey *key = press.key;
if (key == nil || key.charactersIgnoringModifiers.length == 0) {
continue;
}
const QString sequence =
QString::fromUtf8(key.charactersIgnoringModifiers.UTF8String).trimmed().toUpper();
if (homeform::singleton() && homeform::singleton()->handleKeyboardShortcut(sequence)) {
didHandleShortcut = true;
}
}
}
if (!didHandleShortcut) {
[super pressesBegan:presses withEvent:event];
}
}
- (void)setupDynamicQuickActions {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString *profilesDirectory = [documentsDirectory stringByAppendingPathComponent:@"profiles"];
NSError *error = nil;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:profilesDirectory error:&error];
if (error) {
qWarning() << "Error reading files:" << QString::fromUtf8(error.localizedDescription.UTF8String);
return;
}
NSMutableArray *quickActions = [NSMutableArray array];
for (NSString *fileName in files) {
if (quickActions.count >= 4) {
break;
}
NSString *fileNameWithoutExtension = [fileName stringByDeletingPathExtension];
UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCompose];
UIApplicationShortcutItem *shortcutItem = [[UIApplicationShortcutItem alloc] initWithType:[@"org.cagnulein.qdomyoszwift." stringByAppendingString:fileNameWithoutExtension]
localizedTitle:fileNameWithoutExtension
localizedSubtitle:nil
icon:icon
userInfo:nil];
[quickActions addObject:shortcutItem];
}
[UIApplication sharedApplication].shortcutItems = quickActions;
}
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (@available(iOS 13.0, *)) {
UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];
if ([shortcutItem.type hasPrefix:@"org.cagnulein.qdomyoszwift."]) {
qDebug() << "performActionForShortcutItem";
NSString *fileName = [shortcutItem.type stringByReplacingOccurrencesOfString:@"org.cagnulein.qdomyoszwift." withString:@""];
NSString *fileNameWithExtension = [fileName stringByAppendingString:@".qzs"];
//self.selectedShortcutItem = fileNameWithExtension;
lockscreen::set_action_profile([fileNameWithExtension UTF8String]);
qDebug() << "performActionForShortcutItem" << QString::fromUtf8(lockscreen::get_action_profile());
}
}
return YES;
}
- (void)application:(UIApplication *)application
performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
}
- (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity
restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
Q_UNUSED(application)
Q_UNUSED(restorationHandler)
qDebug() << "QZ iOS continueUserActivity called: activityType="
<< QString::fromUtf8(userActivity.activityType.UTF8String);
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
NSURL *url = userActivity.webpageURL;
qDebug() << "QZ iOS continueUserActivity webpageURL="
<< (url ? sanitizedOAuthCallbackUrl(QString::fromUtf8(url.absoluteString.UTF8String))
: QStringLiteral("(null)"));
if (url != nil && homeform::singleton()) {
const QString callbackUrl = QString::fromUtf8(url.absoluteString.UTF8String);
const QUrl qUrl(callbackUrl);
if (qUrl.isValid() && qUrl.host() == QStringLiteral("www.qzfitness.com") &&
qUrl.path().startsWith(QStringLiteral("/peloton/callback"))) {
qDebug() << "QZ iOS continueUserActivity matched Peloton callback";
QMetaObject::invokeMethod(homeform::singleton(), "handleOAuthCallbackUrl", Qt::QueuedConnection,
Q_ARG(QString, callbackUrl));
return YES;
}
qDebug() << "QZ iOS continueUserActivity ignored URL";
}
}
qDebug() << "QZ iOS continueUserActivity returning NO";
return NO;
}
@end
#endif