forked from srolfe/StatusVolX
-
Notifications
You must be signed in to change notification settings - Fork 2
/
StatusVolX.xm
336 lines (281 loc) · 10.6 KB
/
StatusVolX.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
#import "StatusVolX.h"
bool sVolIsVisible = NO;
StatusVolX *svx;
NSString *oldFormatter;
bool IsCurrentAppStatusBarHidden;
// Send indicator command to the statusbar
%hook SBStatusBarStateAggregator
- (void)_resetTimeItemFormatter {
%orig;
NSDateFormatter *timeFormat = MSHookIvar<NSDateFormatter *>(self,"_timeItemDateFormatter");
if (oldFormatter == nil) {
oldFormatter = [timeFormat dateFormat]; // Allows us to reset the format
}
if ([svx showingVolume]) {
[timeFormat setDateFormat:[svx volumeString]];
} else {
[timeFormat setDateFormat:oldFormatter];
}
}
%end
// Hook volume change events
%hook VolumeControl
- (void)_changeVolumeBy:(float)arg1 {
%orig;
int theMode = MSHookIvar<int>(self,"_mode");
if (theMode == 0) {
[svx showVolume:[self getMediaVolume]*16];
} else{
[svx showVolume:[self volume]*16];
}
}
// Force hide volume HUD
- (_Bool)_HUDIsDisplayableForCategory:(id)arg1 {
return NO;
}
- (_Bool)_isCategoryAlwaysHidden:(id)arg1 {
return YES;
}
%end
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)arg1 {
%orig;
// Create StatusVolX inside SpringBoard
svx = [[StatusVolX alloc] init];
}
%end
// StatusVol needs an auto-rotating UIWindow
@implementation svolWindow
// Un-hide after rotation
- (void)_finishedFullRotation:(id)arg1 finished:(id)arg2 context:(id)arg3 {
[super _finishedFullRotation:arg1 finished:arg2 context:arg3];
[self fixSvolWindow];
if (sVolIsVisible) {
[self setHidden:NO]; // Mitigate black box issue
}
}
- (CGRect)getScreenBoundsForOrientation:(int)orientation {
UIScreen *mainScreen = [UIScreen mainScreen];
if ([mainScreen respondsToSelector:@selector(_boundsForInterfaceOrientation:)]) {
return [mainScreen _boundsForInterfaceOrientation:orientation];
}
if (orientation != 3 && orientation != 4) return mainScreen.bounds;
return CGRectMake(mainScreen.bounds.origin.x,
mainScreen.bounds.origin.y,
mainScreen.bounds.size.height,
mainScreen.bounds.size.width);
}
// Fix frame after orientation
- (void)fixSvolWindow {
// Reset frame
// Correctly set status bar rect
// Y origin is set to -20 before animating it back to 0
long orientation = (long)[(SpringBoard *)[UIApplication sharedApplication] _frontMostAppOrientation];
CGRect mainScreenRect = [self getScreenBoundsForOrientation:orientation];
CGRect windowRect = CGRectMake(0, 0, CGRectGetWidth(mainScreenRect), 20);
// Change y position if the volume bar is supposed to be hidden
if (!sVolIsVisible) {
windowRect.origin.y = -20;
}
[self setFrame:windowRect];
}
// Force support auto-rotation. Hide on rotation events
- (BOOL)_shouldAutorotateToInterfaceOrientation:(int)arg1 {
[self setHidden:YES]; // Mitigate black box issue
return YES;
}
@end
@implementation StatusVolX
@synthesize showingVolume;
- (id)init {
self = [super init];
if (self) {
volume = 0;
self.showingVolume = NO;
[self initializeWindow];
}
return self;
}
- (void)showVolume:(float)vol {
volume =(int)vol;
self.showingVolume = YES;
SBStatusBarStateAggregator *sbsa = [%c(SBStatusBarStateAggregator) sharedInstance];
[sbsa _resetTimeItemFormatter];
[sbsa _updateTimeItems];
if (hideTimer != nil) {
[hideTimer invalidate];
}
hideTimer =[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(setNotShowingVolume)
userInfo:nil
repeats:NO];
[self statusPeek];
}
- (bool)isCurrentAppStatusBarHidden {
SpringBoard *springBoard = (SpringBoard *)[UIApplication sharedApplication];
SBApplication *frontApp = (SBApplication *)[springBoard _accessibilityFrontMostApplication];
if (frontApp == nil) return false;
if ([frontApp respondsToSelector:@selector(statusBarHiddenForCurrentOrientation)]) {
return [frontApp statusBarHiddenForCurrentOrientation];
} else {
return IsCurrentAppStatusBarHidden;
}
// if (NSClassFromString(@"SBAppStatusBarManager")) {
// SBAppStatusBarManager *sbAppStatusBarManager = [%c(SBAppStatusBarManager) sharedInstance];
// if (sbAppStatusBarManager != nil && [sbAppStatusBarManager respondsToSelector:@selector(isStatusBarHidden)]) {
// return [sbAppStatusBarManager isStatusBarHidden];
// }
// }
return false;
}
- (void)statusPeek {
if (![self isCurrentAppStatusBarHidden])
return;
// Show and set hide timer
if (!sVolIsVisible || isAnimatingClose) {
// Rotate status bar
long orientation = (long)[(SpringBoard *)[UIApplication sharedApplication] _frontMostAppOrientation];
[sVolWindow _rotateWindowToOrientation:orientation
updateStatusBar:NO
duration:nil
skipCallbacks:YES];
// Window adjustments
if (!isAnimatingClose) {
[sVolWindow fixSvolWindow];
sVolIsVisible = YES;
[sVolWindow setHidden:NO];
} else {
svolCloseInterrupt = YES;
}
// Animate entry
[UIView animateWithDuration:0.3
delay:nil
options:(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction)
animations:^{
CGRect windowRect = sVolWindow.frame;
windowRect.origin.y = 0;
[sVolWindow setFrame:windowRect];
} completion:^(BOOL finished) {
// Reset the timer
svolCloseInterrupt = NO;
if (hideTimer != nil) {
[hideTimer invalidate];
hideTimer = nil;
}
hideTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(setNotShowingVolume)
userInfo:nil
repeats:NO];
}];
} else {
// Reset the timer
if (hideTimer != nil) {
[hideTimer invalidate];
hideTimer = nil;
}
hideTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(setNotShowingVolume)
userInfo:nil
repeats:NO];
}
}
- (void)hideSvolWindow {
// Unset hide timer
hideTimer = nil;
// Animate hide
[UIView animateWithDuration:0.3
delay:0
options:(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction)
animations:^{
isAnimatingClose = YES;
CGRect windowRect = sVolWindow.frame;
// Animation dependent on orientation
windowRect.origin.y = -20;
[sVolWindow setFrame:windowRect];
} completion:^(BOOL finished) {
// Hide the window
isAnimatingClose = NO;
if (finished && !svolCloseInterrupt) {
sVolIsVisible = NO;
[sVolWindow setHidden:YES];
}
}];
}
- (void)initializeWindow {
// Setup window
/*CGRect mainFrame =[[UIScreen mainScreen] bounds];//[UIApplication sharedApplication].keyWindow.frame;
mainFrame.origin.x =0;
mainFrame.origin.y =-20;
mainFrame.size.height =20;*/
CGRect mainFrame = UIApplication.sharedApplication.statusBar.bounds;
sVolWindow = [[svolWindow alloc] initWithFrame:mainFrame];
if ([sVolWindow respondsToSelector:@selector(_setSecure:)]) {
[sVolWindow _setSecure:YES];
}
sVolWindow.windowLevel = 1058;
mainFrame.origin.y = 0;
// Main view controller
UIViewController *primaryVC = [[UIViewController alloc] init];
[primaryVC.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
// Blur
UIBlurEffect *blurEffect = [%c(UIBlurEffect) effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurView = [[%c(UIVisualEffectView) alloc] initWithEffect:blurEffect];
[blurView setFrame:mainFrame];
[blurView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[primaryVC.view addSubview:blurView];
int statusBarStyle = 0x12F; //Normal notification center style
UIInterfaceOrientation orientation = [(SpringBoard *)[UIApplication sharedApplication] _frontMostAppOrientation];
float statusBarHeight = [UIStatusBar.class heightForStyle:statusBarStyle orientation:orientation];
float statusBarWidth = UIApplication.sharedApplication.statusBar.bounds.size.width;
UIStatusBar *statusBar = [[UIStatusBar alloc] initWithFrame:CGRectMake(0, 0, statusBarWidth, statusBarHeight)];
[statusBar requestStyle:statusBarStyle];
statusBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[primaryVC.view addSubview:statusBar];
//After orientation changes
//[statusBar setOrientation:UIApplication.sharedApplication.statusBarOrientation];
// Make visible and hide window
sVolWindow.rootViewController = primaryVC;
[sVolWindow makeKeyAndVisible];
[sVolWindow setHidden:YES];
}
- (void)setNotShowingVolume {
hideTimer =nil;
if (sVolIsVisible) {
hideTimer =[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(hideSvolWindow)
userInfo:nil
repeats:NO];
}
self.showingVolume =NO;
SBStatusBarStateAggregator *sbsa =[%c(SBStatusBarStateAggregator) sharedInstance];
[sbsa _resetTimeItemFormatter];
[sbsa _updateTimeItems];
}
- (NSString *)volumeString {
return [NSString stringWithFormat:@"'#%d'",(int)volume];
}
@end
static void SetCurrentAppStatusBarHidden() {
IsCurrentAppStatusBarHidden = true;
}
static void SetCurrentAppStatusBarVisible() {
IsCurrentAppStatusBarHidden = false;
}
%ctor {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)SetCurrentAppStatusBarHidden,
CFSTR("com.fidele007.statusvolxkit/SetStatusBarHidden"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)SetCurrentAppStatusBarVisible,
CFSTR("com.fidele007.statusvolxkit/SetStatusBarVisible"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}