-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tweak.xm
337 lines (288 loc) · 16 KB
/
Tweak.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
/*
Copyright (C) 2024 Serge Alagon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <notify.h>
#import <objc/runtime.h>
#import "Headers.h"
#import "Immortalizer.h"
static BOOL immortalizerEnabled;
static BOOL isFolderTransitioning = false;
static BOOL isIndicatorEnabled;
static BOOL isToastEnabled;
static BOOL isLockIndicatorEnabled;
%group init
%hook SpringBoard
- (void)frontDisplayDidChange:(id)arg1 {
%orig;
notify_post("com.sergy.immortalizer.updatecc");
}
%end
%hook SBIconView
-(long long)currentLabelAccessoryType {
long long accessory = %orig;
if (immortalizerEnabled && isIndicatorEnabled) {
BOOL isDocked = [self.location containsString:@"Dock"];
if (isDocked) {
NSArray *immortalBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"ImmortalForegroundBundleIDs"];
if ([immortalBundleIDs containsObject:[self.icon applicationBundleID]]) {
SBApplication *app = [[%c(SBApplicationController) sharedInstance] applicationWithBundleIdentifier:[self.icon applicationBundleID]];
accessory = app.processState ? 4 : 2;
}
}
if ([self.icon isKindOfClass:[%c(SBFolderIcon) class]]) {
SBFolder *folder = [(SBFolderIcon *)self.icon folder];
NSArray *folderIcons = [folder icons];
NSArray *immortalBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"ImmortalForegroundBundleIDs"];
for (SBIcon *icon in folderIcons) {
if ([immortalBundleIDs containsObject:[icon applicationBundleID]] && !isFolderTransitioning) {
SBApplication *app = [[%c(SBApplicationController) sharedInstance] applicationWithBundleIdentifier:[icon applicationBundleID]];
accessory = app.processState ? 4 : 2;
}
}
}
}
return accessory;
}
-(NSArray *)applicationShortcutItems {
NSArray *orig = %orig;
if (immortalizerEnabled) {
NSString *bundleID = [self.icon applicationBundleID];
if (!bundleID) return orig;
NSArray *immortalBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"ImmortalForegroundBundleIDs"];
BOOL isImmortal = [immortalBundleIDs containsObject:bundleID];
SBSApplicationShortcutItem* immortalItem = [[%c(SBSApplicationShortcutItem) alloc] init];
if (isImmortal) {
immortalItem.localizedTitle = [NSString stringWithFormat:@"Disable Immortal Foreground"];
} else {
immortalItem.localizedTitle = [NSString stringWithFormat:@"Enable Immortal Foreground"];
}
immortalItem.type = @"com.sergy.immortalizer.immortalForeground.item";
UIImage *iconImage = [UIImage systemImageNamed:@"hourglass.bottomhalf.fill"];
immortalItem.icon = [[%c(SBSApplicationShortcutCustomImageIcon) alloc] initWithImageData:UIImagePNGRepresentation(iconImage) dataType:0 isTemplate:1];
immortalItem.bundleIdentifierToLaunch = bundleID;
return [orig arrayByAddingObject:immortalItem];
} else
return orig;
}
+(void)activateShortcut:(SBSApplicationShortcutItem*)item withBundleIdentifier:(NSString*)bundleID forIconView:(id)iconView {
if (immortalizerEnabled && [[item type] isEqualToString:@"com.sergy.immortalizer.immortalForeground.item"]) {
Immortalizer *immortalizer = [Immortalizer sharedInstance];
NSMutableArray *immortalBundleIDs = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"ImmortalForegroundBundleIDs"] mutableCopy];
if (!immortalBundleIDs) immortalBundleIDs = [NSMutableArray array];
if ([bundleID isEqualToString:@"com.apple.camera"] && ![immortalBundleIDs containsObject:bundleID]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Immortalizer"
message:@"You pervert! Kidding. But Immortalizer doesn't work for camera for recording. I don't know why, but if I got time, I'll try to investigate. I'll still let you Immortalize this app anyway."
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Lmao"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
}]];
UIWindow *keyWindow = nil;
for (UIWindow *window in UIApplication.sharedApplication.windows) {
if (window.isKeyWindow) {
keyWindow = window;
break;
}
}
if (!keyWindow) keyWindow = UIApplication.sharedApplication.windows.firstObject;
UIViewController *rootViewController = keyWindow.rootViewController;
[rootViewController presentViewController:alertController animated:YES completion:nil];
});
}
if ([immortalBundleIDs containsObject:bundleID]) {
SBApplication *app = [[%c(SBApplicationController) sharedInstance] applicationWithBundleIdentifier:bundleID];
if (app.processState != nil) [[%c(FBSSystemService) sharedService] openApplication:bundleID options:nil withResult:nil];
if (isToastEnabled) [immortalizer showToastWithTitle:[immortalizer getAppNameForBundle:bundleID] subtitle:@"At rest" icon:[UIImage systemImageNamed:@"arrow.uturn.left.circle.fill"] autoHide:3.0];
[immortalBundleIDs removeObject:bundleID];
} else {
if (isToastEnabled) [immortalizer showToastWithTitle:[immortalizer getAppNameForBundle:bundleID] subtitle:@"Immortalized" icon:[UIImage systemImageNamed:@"hourglass.bottomhalf.fill"] autoHide:3.0];
[[%c(FBSSystemService) sharedService] openApplication:bundleID options:nil withResult:nil];
[immortalBundleIDs addObject:bundleID];
}
[[NSUserDefaults standardUserDefaults] setObject:immortalBundleIDs forKey:@"ImmortalForegroundBundleIDs"];
[[NSUserDefaults standardUserDefaults] synchronize];
[immortalizer updateAccessoryForBundle:bundleID];
} else {
%orig;
}
}
%end
%hook FBScene
-(void)updateSettings:(id)arg1 withTransitionContext:(id)arg2 completion:(id)arg3{
FBProcess *process = self.clientProcess;
if (immortalizerEnabled && process) {
NSString *bundleIdentifier = process.bundleIdentifier;
NSArray *immortalBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"ImmortalForegroundBundleIDs"];
if ([immortalBundleIDs containsObject:bundleIdentifier] && arg2 == nil) {
Immortalizer *immortalizer = [Immortalizer sharedInstance];
[immortalizer updateAccessoryForBundle:bundleIdentifier];
return;
}
}
%orig;
}
%end
%hook SBApplication
-(long long)labelAccessoryTypeForIcon:(id)arg1 {
long long accessory = %orig;
if (immortalizerEnabled && isIndicatorEnabled) {
NSArray *immortalBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"ImmortalForegroundBundleIDs"];
if ([immortalBundleIDs containsObject:self.bundleIdentifier]) {
SBApplication *app = [[%c(SBApplicationController) sharedInstance] applicationWithBundleIdentifier:self.bundleIdentifier];
accessory = app.processState ? 4 : 2;
}
}
return accessory;
}
-(NSString *)displayName {
NSString *originalName = %orig;
NSString *bundleID = self.bundleIdentifier;
NSArray *lockedBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"LockedBundleIDs"];
NSString *status = @"";
if (immortalizerEnabled && lockedBundleIDs && [lockedBundleIDs containsObject:bundleID] && isLockIndicatorEnabled) status = @"🔒";
return [NSString stringWithFormat:@"%@%@", originalName, status];
}
-(void)_didExitWithContext:(id)arg1 {
%orig;
if (immortalizerEnabled) {
Immortalizer *immortalizer = [Immortalizer sharedInstance];
[immortalizer updateAccessoryForBundle:self.bundleIdentifier];
NSArray *immortalBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"ImmortalForegroundBundleIDs"];
BOOL isImmortalized = [immortalBundleIDs containsObject:self.bundleIdentifier];
if (isImmortalized && isToastEnabled) {
[immortalizer showToastWithTitle:[immortalizer getAppNameForBundle:self.bundleIdentifier] subtitle:@"Terminated" icon:[UIImage systemImageNamed:@"exclamationmark.triangle.fill"] autoHide:3.0];
}
}
}
%end
%hook SBFolderView
-(void)willTransitionAnimated:(BOOL)arg1 withSettings:(id)arg2 {
if (immortalizerEnabled && isIndicatorEnabled) isFolderTransitioning = true;
%orig;
}
-(void)didTransitionAnimated:(BOOL)arg1 {
if (immortalizerEnabled && isIndicatorEnabled) {
isFolderTransitioning = false;
[self.folder.icon _notifyAccessoriesDidUpdate];
}
%orig;
}
%end
%hook UNSUserNotificationServer
-(void)willPresentNotification:(id)arg1 forBundleIdentifier:(id)arg2 withCompletionHandler:(id)arg3 {
if (immortalizerEnabled) {
Immortalizer *immortalizer = [Immortalizer sharedInstance];
if ([immortalizer isNotificationEnabledForBundleIdentifier:arg2]) {
[self _didChangeApplicationState:4 forBundleIdentifier:arg2];
}
}
%orig;
}
%end
%hook UIMutableApplicationSceneSettings
-(void)setDeactivationReasons:(unsigned long long)arg1 {
if (arg1 != 0)
return;
%orig;
}
%end
%hook SBFluidSwitcherItemContainer
-(void)setKillable:(BOOL)arg1 {
BOOL shouldTerminate = arg1;
if (immortalizerEnabled) {
SBAppLayout *appLayout = [self appLayout];
NSDictionary *bundles = nil;
NSArray *lockedBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"LockedBundleIDs"];
if (@available(iOS 16.0, *)) bundles = [appLayout itemsToLayoutAttributesMap];
else bundles = (NSDictionary *)object_getIvar(appLayout, class_getInstanceVariable([%c(SBAppLayout) class], "_rolesToLayoutItemsMap"));
if (bundles) {
if (@available(iOS 16.0, *)) {
for (SBDisplayItem *displayItem in bundles) {
NSString *bundleIdentifier = [displayItem bundleIdentifier];
if ([lockedBundleIDs containsObject:bundleIdentifier]) shouldTerminate = false;
}
}
else {
SBDisplayItem *displayItem = bundles[@1];
NSString *bundleIdentifier = nil;
if ([displayItem respondsToSelector:@selector(bundleIdentifier)])
bundleIdentifier = [displayItem bundleIdentifier];
if (bundleIdentifier && [lockedBundleIDs containsObject:bundleIdentifier]) {
shouldTerminate = false;
}
}
}
}
%orig(shouldTerminate);
}
%end
%end
static void prefsLockIndicatorChanged() {
Immortalizer *immortalizer = [Immortalizer sharedInstance];
NSArray *lockedBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"LockedBundleIDs"];
NSUserDefaults *const prefs = [[NSUserDefaults alloc] initWithSuiteName:@"com.sergy.immortalizer.prefs"];
isLockIndicatorEnabled = [prefs objectForKey:@"isLockIndicatorEnabled"] ? [prefs boolForKey:@"isLockIndicatorEnabled"] : YES;
for (NSString *bundleIdentifier in lockedBundleIDs)
[immortalizer updateAccessoryForBundle:bundleIdentifier];
}
static void immortalizerPreferencesChanged() {
Immortalizer *immortalizer = [Immortalizer sharedInstance];
NSArray *immortalBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"ImmortalForegroundBundleIDs"];
NSUserDefaults *const prefs = [[NSUserDefaults alloc] initWithSuiteName:@"com.sergy.immortalizer.prefs"];
immortalizerEnabled = [prefs objectForKey:@"isEnabled"] ? [prefs boolForKey:@"isEnabled"] : YES;
for (NSString *bundleIdentifier in immortalBundleIDs)
[immortalizer updateAccessoryForBundle:bundleIdentifier];
notify_post("com.sergy.immortalizer.updatecc");
}
static void prefsNotifsChanged() {
Immortalizer *immortalizer = [Immortalizer sharedInstance];
NSArray *immortalBundleIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:@"ImmortalForegroundBundleIDs"];
for (NSString * immortalApp in immortalBundleIDs) {
if ([immortalizer isNotificationEnabledForBundleIdentifier:immortalApp]) {
[[%c(UNSUserNotificationServer) sharedInstance] _didChangeApplicationState:4 forBundleIdentifier:immortalApp];
} else {
[[%c(UNSUserNotificationServer) sharedInstance] _didChangeApplicationState:8 forBundleIdentifier:immortalApp];
}
}
}
static void prefsIndicatorChanged() {
NSUserDefaults *const indicatorPrefs = [[NSUserDefaults alloc] initWithSuiteName:@"com.sergy.immortalizer.prefs"];
isIndicatorEnabled = [indicatorPrefs objectForKey:@"isIndicatorEnabled"] ? [indicatorPrefs boolForKey:@"isIndicatorEnabled"] : YES;
}
static void prefsToastChanged() {
NSUserDefaults *const toastPrefs = [[NSUserDefaults alloc] initWithSuiteName:@"com.sergy.immortalizer.prefs"];
isToastEnabled = [toastPrefs objectForKey:@"isToastEnabled"] ? [toastPrefs boolForKey:@"isToastEnabled"] : YES;
}
static id observer;
static void loadAllImmortalizerPrefs() {
observer = [NSNotificationCenter.defaultCenter addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *_) {
immortalizerPreferencesChanged();
prefsNotifsChanged();
prefsIndicatorChanged();
prefsToastChanged();
prefsLockIndicatorChanged();
[NSNotificationCenter.defaultCenter removeObserver: observer];
}];
}
%ctor {
%init(init);
loadAllImmortalizerPrefs();
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)immortalizerPreferencesChanged, CFSTR("com.sergy.immortalizer.preferenceschanged"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)prefsNotifsChanged, CFSTR("com.sergy.immortalizer.preferenceschanged.notifs"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)prefsIndicatorChanged, CFSTR("com.sergy.immortalizer.preferenceschanged.indicator"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)prefsToastChanged, CFSTR("com.sergy.immortalizer.preferenceschanged.toast"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)prefsLockIndicatorChanged, CFSTR("com.sergy.immortalizer.preferenceschanged.lockindicator"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
}