-
Notifications
You must be signed in to change notification settings - Fork 7
/
DarkMessages_SB.xm
353 lines (274 loc) · 10.1 KB
/
DarkMessages_SB.xm
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//
// DarkMessages_SB.xm
// DarkMessages
//
// ©2017 Sticktron
//
#define DEBUG_PREFIX @"[DarkMessages_SB]"
#import "DebugLog.h"
#import "DarkMessages.h"
#import <spawn.h>
// Function defs
static void setDarkMode(BOOL enabled);
static void loadSettings();
static void applySettings();
static void performSetupForNoctis();
static void syncStateWithTriggers();
static BOOL isNoctisOn();
static BOOL isNightShiftOn();
static void relaunchMessagesApp();
static void closeQR();
static void killProcess(const char *name);
// Global vars
static BOOL isDark;
static BOOL nightShiftControlEnabled;
static BOOL noctisControlEnabled;
static NSString *blueBalloonColor;
static NSString *greenBalloonColor;
static NSString *grayBalloonColor;
static NCNotificationViewController *qrViewController;
// Functions -------------------------------------------------------------------
static void setDarkMode(BOOL enabled) {
DebugLogC(@"setDarkMode(%@)", enabled ? @"ON" : @"OFF");
// ignore if already in desired mode
if (enabled == isDark) {
return;
}
isDark = enabled;
// update prefs
NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithContentsOfFile:kPrefsPlistPath];
settings[@"Enabled"] = [NSNumber numberWithBool:isDark];
DebugLogC(@"writing new setting: Enabled=%d", [settings[@"Enabled"] boolValue]);
[settings writeToFile:kPrefsPlistPath atomically:YES];
applySettings();
}
static void loadSettings() {
DebugLogC(@"loadSettings()");
NSDictionary *settings = [NSMutableDictionary dictionaryWithContentsOfFile:kPrefsPlistPath];
isDark = settings[@"Enabled"] ? [settings[@"Enabled"] boolValue] : YES;
nightShiftControlEnabled = settings[@"NightShiftControl"] ? [settings[@"NightShiftControl"] boolValue] : NO;
noctisControlEnabled = settings[@"NoctisControl"] ? [settings[@"NoctisControl"] boolValue] : NO;
DebugLogC(@"DarkMode=%@; NightShiftControl=%@; NoctisControl=%@", isDark?@"yes":@"no", nightShiftControlEnabled?@"yes":@"no", noctisControlEnabled?@"yes":@"no");
blueBalloonColor = settings[@"BlueBalloonColor"] ?: @"default";
greenBalloonColor = settings[@"GreenBalloonColor"] ?: @"default";
grayBalloonColor = settings[@"GrayBalloonColor"] ?: @"default";
DebugLogC(@"BlueBalloonColor=%@; GreenBalloonColor=%@; GrayBalloonColor=%@", blueBalloonColor, greenBalloonColor, grayBalloonColor);
}
static void applySettings() {
DebugLogC(@"applySettings()");
// close Messages...
DebugLogC(@"********** Asking Messages to quit !!! **********");
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
kQuitMessagesNotification, NULL, NULL, true);
// close the QR popup ...
DebugLogC(@"********** Asking QuickReply to quit !!! **********");
closeQR();
}
static void performSetupForNoctis() {
DebugLogC(@"performSetupForNoctis()");
[[NSNotificationCenter defaultCenter]
addObserverForName:@"com.laughingquoll.noctis.enablenotification"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
DebugLogC(@"*** Notice: Noctis was toggled on");
if (noctisControlEnabled) {
DebugLogC(@"*** NoctisControl is on, handling...");
setDarkMode(YES);
}
}
];
[[NSNotificationCenter defaultCenter]
addObserverForName:@"com.laughingquoll.noctis.disablenotification"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
DebugLogC(@"*** Notice: Noctis was toggled off");
if (noctisControlEnabled) {
DebugLogC(@"*** NoctisControl is on, handling...");
setDarkMode(NO);
}
}
];
}
static void syncStateWithTriggers() {
DebugLogC(@"syncStateWithTriggers()");
// Note: only one trigger should be enabled at a time,
// but we'll handle the edge case where both are enabled just in case.
if (nightShiftControlEnabled && !noctisControlEnabled) {
DebugLogC(@"syncing to NightShift");
setDarkMode(isNightShiftOn());
} else if (noctisControlEnabled && !nightShiftControlEnabled) {
DebugLogC(@"syncing to Noctis");
setDarkMode(isNoctisOn());
} else if (nightShiftControlEnabled && noctisControlEnabled) {
DebugLogC(@"trying to sync to both NightShift and Noctis :S");
if (isNightShiftOn() || isNoctisOn()) {
setDarkMode(YES);
} else {
setDarkMode(NO);
}
}
}
static BOOL isNoctisOn() {
DebugLogC(@"isNoctisOn()");
BOOL on = YES; // on by default
CFPreferencesAppSynchronize(kNoctisAppID);
Boolean valid = NO;
BOOL value = CFPreferencesGetAppBooleanValue(kNoctisEnabledKey, kNoctisAppID, &valid);
if (valid) {
on = value;
}
DebugLogC(@"Noctis is: %@", on?@"on":@"off");
return on;
}
static BOOL isNightShiftOn() {
DebugLogC(@"isNightShiftOn()");
AXBackBoardServer *bbserver = [%c(AXBackBoardServer) server];
BOOL on = [bbserver blueLightStatusEnabled];
DebugLogC(@"NightShift is: %@", on?@"on":@"off");
return on;
}
/* Suspend, quit, then relaunch the Messages App. */
static void relaunchMessagesApp() {
DebugLogC(@"relaunchMessagesApp()");
SBApplicationController *sbac = [%c(SBApplicationController) sharedInstance];
SBApplication *sbapp = [sbac applicationWithBundleIdentifier:@"com.apple.MobileSMS"];
// 1) suspend MobileSMS before killing it, looks nicer.
[sbac applicationService:[%c(FBUIApplicationService) sharedInstance] suspendApplicationWithBundleIdentifier:@"com.apple.MobileSMS"];
// 2) quit MobileSMS
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
killProcess("MobileSMS");
// 3) wait a sec, then re-launch MobileSMS
DebugLogC(@"relaunching MobileSMS...");
[[%c(SBUIController) sharedInstance] performSelector:@selector(activateApplication:) withObject:sbapp afterDelay:1.0];
});
}
/* Dismiss, then quit QuickReply. */
static void closeQR() {
DebugLogC(@"closeQR()");
// close and kill the quick reply extension (it will auto-launch when needed again).
DebugLogC(@"dismissing QR controller: %@", qrViewController);
if (qrViewController && [qrViewController respondsToSelector:@selector(dismissPresentedViewControllerAndClearNotification:animated:)]) {
[qrViewController dismissPresentedViewControllerAndClearNotification:YES animated:YES];
}
killProcess("MessagesNotificationExtension");
killProcess("MessagesNotificationExtension"); // kill with fire
killProcess("MessagesNotificationExtension"); // die u cruel cruel bastard
}
static void killProcess(const char *name) {
DebugLogC(@"killProcess(%s)", name);
pid_t pid;
const char* args[] = { "killall", name, NULL };
posix_spawn(&pid, "/usr/bin/killall", NULL, NULL, (char* const*)args, NULL);
}
// Notification Handlers -------------------------------------------------------
static void handleSettingsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
DebugLogC(@"*** Notice: %@", name);
DebugLogC(@"handleSettingsChanged()");
BOOL wasDark = isDark;
NSString *oldBlue = blueBalloonColor;
NSString *oldGreen = greenBalloonColor;
NSString *oldGray = grayBalloonColor;
loadSettings();
// Some settings require Messages/QR to restart if changed...
BOOL needsApply = NO;
if (isDark != wasDark) { // dark mode changed
needsApply = YES;
} else if ([blueBalloonColor isEqualToString:oldBlue] == NO) { // color changed
needsApply = YES;
} else if ([greenBalloonColor isEqualToString:oldGreen] == NO) { // color changed
needsApply = YES;
} else if ([grayBalloonColor isEqualToString:oldGray] == NO) { // color changed
needsApply = YES;
}
if (needsApply) {
DebugLogC(@"### New settings need restart!");
applySettings();
} else {
if (nightShiftControlEnabled || noctisControlEnabled) {
syncStateWithTriggers();
}
}
}
static void handleRelaunchMessagesApp(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
DebugLogC(@"*** Notice: %@", name);
DebugLogC(@"handleRelaunchMessagesApp()");
relaunchMessagesApp();
}
// Hooks -----------------------------------------------------------------------
@interface SpringBoard (DM)
- (void)_dm_setDarkMode:(BOOL)enabled;
@end
%hook SpringBoard
- (void)applicationDidFinishLaunching:(UIApplication *)application {
DebugLog0;
%orig;
// loadSettings();
// syncStateWithTriggers()
}
%new
- (void)_dm_setDarkMode:(BOOL)enabled {
DebugLog0;
setDarkMode(enabled);
}
%end
// QR host controller
%hook NCNotificationViewController
- (id)initWithNotificationRequest:(NCNotificationRequest *)request {
DebugLog0;
self = %orig;
if ([request.sectionIdentifier isEqualToString:@"com.apple.MobileSMS"] && [request.categoryIdentifier isEqualToString:@"MessageExtension"]) {
qrViewController = self;
DebugLogC(@"got QuickReply ViewController: %@", qrViewController);
}
return self;
}
%end
// Night Shift detection
%hook CBBlueLightClient
- (BOOL)setEnabled:(BOOL)enabled {
BOOL result = %orig;
DebugLog(@"NightShift turned %@ (success=%d)", enabled?@"ON":@"OFF", result);
if (nightShiftControlEnabled) {
setDarkMode(enabled);
}
return result;
}
- (BOOL)setEnabled:(BOOL)enabled withOption:(int)option {
BOOL result = %orig;
DebugLog(@"NightShift turned %@ with option:%d (success=%d)", enabled?@"ON":@"OFF", option, result);
if (nightShiftControlEnabled) {
setDarkMode(enabled);
}
return result;
}
%end
// Init ------------------------------------------------------------------------
%ctor {
@autoreleasepool {
DebugLogC(@"Loaded into SpringBoard");
loadSettings();
// init Noctis support (if installed)
if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Library/MobileSubstrate/DynamicLibraries/Noctis.dylib"]) {
DebugLogC(@"Noctis is installed");
performSetupForNoctis();
}
// listen for changes to settings:
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)handleSettingsChanged,
kSettingsChangedNotification,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately
);
// listen for requests from Messages to relaunch:
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)handleRelaunchMessagesApp,
kRelaunchMessagesNotification,
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately
);
}
}