-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Ding.xm
241 lines (215 loc) · 6.68 KB
/
Ding.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
#import "Ding.h"
HBPreferences *preferences;
BOOL isEnabled;
BOOL bannerActive = NO;
BOOL prevIsRingerMuted = NO;
BOOL alwaysRingtone;
BOOL hapticFeedback;
BOOL actionMute;
NSInteger switchAction;
NSInteger mode;
extern "C" {
Boolean CTCellularDataPlanGetIsEnabled(void);
void CTCellularDataPlanSetIsEnabled(Boolean enabled);
}
BOOL findActionBool(int position) {
// position (arg1)
// ---------------
// 0 = Muted
// 1 = Unmuted
return (position == 0) == actionMute;
}
%group CallGroup
%hook SBInCallTransientOverlayViewController
- (void)viewWillAppear:(BOOL)arg1 {
bannerActive = YES;
if (alwaysRingtone) [[%c(SBRingerControl) ding_sharedInstance] setRingerMuted:NO];
%orig;
}
- (void)viewWillDisappear:(BOOL)arg1 {
bannerActive = NO;
if (alwaysRingtone) [[%c(SBRingerControl) ding_sharedInstance] setRingerMuted:prevIsRingerMuted];
%orig;
}
%end
%hook SBRemoteTransientOverlayHostViewController
- (void)viewWillAppear:(BOOL)arg1 {
bannerActive = YES;
if (alwaysRingtone) [[%c(SBRingerControl) ding_sharedInstance] setRingerMuted:NO];
%orig;
}
- (void)viewWillDisappear:(BOOL)arg1 {
bannerActive = NO;
if (alwaysRingtone) [[%c(SBRingerControl) ding_sharedInstance] setRingerMuted:prevIsRingerMuted];
%orig;
}
%end
%hook SBInCallBannerPresentableViewController
- (void)presentableWillAppearAsBanner:(id)arg1 {
bannerActive = YES;
if (alwaysRingtone) [[%c(SBRingerControl) ding_sharedInstance] setRingerMuted:NO];
%orig;
}
- (void)presentableWillDisappearAsBanner:(id)arg1 withReason:(id)arg2 {
bannerActive = NO;
if (alwaysRingtone) [[%c(SBRingerControl) ding_sharedInstance] setRingerMuted:prevIsRingerMuted];
%orig;
}
%end
%end
%hook SpringBoard
- (void)_updateRingerState:(int)arg1 withVisuals:(BOOL)arg2 updatePreferenceRegister:(BOOL)arg3 {
if (!(switchAction == 1) && hapticFeedback) {
UIImpactFeedbackGenerator *feedback = [[UIImpactFeedbackGenerator alloc] initWithStyle:(UIImpactFeedbackStyleMedium)];
[feedback prepare];
[feedback impactOccurred];
}
prevIsRingerMuted = (arg1 == 0) ? YES : NO;
// Switch Actions
// --------------
// 1 = None
// 2 = Flashlight
// 3 = Orientation Lock
// 4 = Airplane Mode
// 5 = Wi-Fi
// 6 = Bluetooth
// 7 = Low Power Mode
// 8 = Do Not Disturb
// 9 = Cellular Data
// 10 = Dark Mode
switch (switchAction) {
case 1: break;
case 2: {
if (actionMute) {
[[%c(SBUIFlashlightController) sharedInstance] setLevel:(arg1 == 0) ? 4 : 0];
} else {
[[%c(SBUIFlashlightController) sharedInstance] setLevel:(arg1 == 0) ? 0 : 4];
}
break;
}
case 3: {
BOOL lock = ((actionMute && arg1 == 0) || (!actionMute && arg1));
lock ? [[%c(SBOrientationLockManager) sharedInstance] lock] : [[%c(SBOrientationLockManager) sharedInstance] unlock];
break;
}
case 4: {
[[[%c(RadiosPreferences) alloc] init] setAirplaneMode:findActionBool(arg1)];
break;
}
case 5: {
[[%c(SBWiFiManager) sharedInstance] setWiFiEnabled:findActionBool(arg1)];
break;
}
case 6: {
[[%c(BluetoothManager) sharedInstance] setEnabled:findActionBool(arg1)];
[[%c(BluetoothManager) sharedInstance] setPowered:findActionBool(arg1)];
break;
}
case 7: {
if (@available(iOS 15.0, *)) {
if (actionMute) [[%c(_PMLowPowerMode) sharedInstance] setPowerMode:(arg1 == 0) ? 1 : 0 fromSource:@"SpringBoard"];
else [[%c(_PMLowPowerMode) sharedInstance] setPowerMode:arg1 fromSource:@"SpringBoard"];
} else {
if (actionMute) [[%c(_CDBatterySaver) batterySaver] setPowerMode:(arg1 == 0) ? 1 : 0 error:nil];
else [[%c(_CDBatterySaver) batterySaver] setPowerMode:arg1 error:nil];
}
break;
}
case 8: {
DNDModeAssertionService *assertionService = (DNDModeAssertionService *)[objc_getClass("DNDModeAssertionService") serviceForClientIdentifier:@"com.apple.donotdisturb.control-center.module"];
if (arg1 == 0) {
if (actionMute) {
DNDModeAssertionDetails *newAssertion = [objc_getClass("DNDModeAssertionDetails") userRequestedAssertionDetailsWithIdentifier:@"com.apple.control-center.manual-toggle" modeIdentifier:@"com.apple.donotdisturb.mode.default" lifetime:nil];
[assertionService takeModeAssertionWithDetails:newAssertion error:NULL];
} else {
[assertionService invalidateAllActiveModeAssertionsWithError:NULL];
}
} else {
if (actionMute) {
[assertionService invalidateAllActiveModeAssertionsWithError:NULL];
} else {
DNDModeAssertionDetails *newAssertion = [objc_getClass("DNDModeAssertionDetails") userRequestedAssertionDetailsWithIdentifier:@"com.apple.control-center.manual-toggle" modeIdentifier:@"com.apple.donotdisturb.mode.default" lifetime:nil];
[assertionService takeModeAssertionWithDetails:newAssertion error:NULL];
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"SBQuietModeStatusChangedNotification" object:nil];
break;
}
case 9: {
CTCellularDataPlanSetIsEnabled(findActionBool(arg1));
break;
}
case 10: {
UISUserInterfaceStyleMode *styleMode = [[%c(UISUserInterfaceStyleMode) alloc] init];
BOOL dark = ((actionMute && arg1 == 0) || (!actionMute && arg1));
dark ? styleMode.modeValue = 2 : styleMode.modeValue = 1;
break;
}
default: {
NSLog(@"[Ding] Unsure what you did...");
break;
}
}
%orig(arg1, (mode == 2) ? YES : NO, arg3);
}
%end
%hook SBRingerControl
static SBRingerControl *__strong ding_sharedInstance;
// Init for iOS 15 and lower
- (id)initWithHUDController:(id)arg1 soundController:(id)arg2 {
ding_sharedInstance = %orig;
return ding_sharedInstance;
}
// Init for iOS 16+
- (id)initWithBannerManager:(id)arg1 soundController:(id)arg2 {
ding_sharedInstance = %orig;
return ding_sharedInstance;
}
%new
+ (id)ding_sharedInstance {
return ding_sharedInstance;
}
- (BOOL)isRingerMuted {
if (bannerActive && alwaysRingtone) {
return NO;
} else {
if (mode == 2) {
return %orig;
} else {
if (mode == 1) {
return YES;
} else {
return NO;
}
}
}
}
- (void)setRingerMuted:(BOOL)arg1 {
if (bannerActive && alwaysRingtone) {
%orig(NO);
} else {
if (mode == 2) {
%orig;
} else {
if (mode == 1) {
%orig(YES);
} else {
%orig(NO);
}
}
}
}
%end
%ctor {
NSLog(@"Ding started!");
preferences = [[HBPreferences alloc] initWithIdentifier:@"me.nebula.ding.prefs"];
[preferences registerBool:&isEnabled default:YES forKey:@"isEnabled"];
[preferences registerInteger:&mode default:1 forKey:@"mode"];
[preferences registerBool:&alwaysRingtone default:NO forKey:@"alwaysRingtone"];
[preferences registerBool:&hapticFeedback default:YES forKey:@"hapticFeedback"];
[preferences registerInteger:&switchAction default:1 forKey:@"switchAction"];
[preferences registerBool:&actionMute default:NO forKey:@"actionMute"];
if (!isEnabled) return;
%init;
if (alwaysRingtone) %init(CallGroup);
}