-
Notifications
You must be signed in to change notification settings - Fork 8
/
NetworkSpeed13.xm
488 lines (421 loc) · 14.4 KB
/
NetworkSpeed13.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#import "NetworkSpeed13.h"
#import "SparkColourPickerUtils.h"
#import <Cephei/HBPreferences.h>
#import <ifaddrs.h>
#import <net/if.h>
#define DegreesToRadians(degrees) (degrees * M_PI / 180)
static const long KILOBITS = 1000;
static const long MEGABITS = 1000000;
static const long KILOBYTES = 1 << 10;
static const long MEGABYTES = 1 << 20;
static double screenWidth;
static double screenHeight;
static UIDeviceOrientation orientationOld;
__strong static id networkSpeedObject;
static BOOL shouldUpdateSpeedLabel;
static long oldUpSpeed = 0, oldDownSpeed = 0;
typedef struct
{
uint32_t inputBytes;
uint32_t outputBytes;
} UpDownBytes;
static HBPreferences *pref;
static BOOL enabled;
static BOOL showOnLockScreen;
static BOOL showDownloadSpeedFirst;
static BOOL showSecondSpeedInNewLine;
static BOOL showUploadSpeed;
static NSString *uploadPrefix;
static BOOL showDownloadSpeed;
static NSString *downloadPrefix;
static NSString *separator;
static long dataUnit;
static BOOL backgroundColorEnabled;
static float backgroundCornerRadius;
static BOOL customBackgroundColorEnabled;
static UIColor *customBackgroundColor;
static BOOL showAlways;
static double portraitX;
static double portraitY;
static double landscapeX;
static double landscapeY;
static BOOL followDeviceOrientation;
static double width;
static double height;
static long fontSize;
static BOOL boldFont;
static BOOL customTextColorEnabled;
static UIColor *customTextColor;
static long alignment;
static double updateInterval;
// Got some help from similar network speed tweaks by julioverne & n3d1117
NSString* formatSpeed(long bytes)
{
if(dataUnit == 0) // BYTES
{
if (bytes < KILOBYTES) return @"0KB/s";
else if (bytes < MEGABYTES) return [NSString stringWithFormat:@"%.0fKB/s", (double)bytes / KILOBYTES];
else return [NSString stringWithFormat:@"%.2fMB/s", (double)bytes / MEGABYTES];
}
else // BITS
{
if (bytes < KILOBITS) return @"0Kb/s";
else if (bytes < MEGABITS) return [NSString stringWithFormat:@"%.0fKb/s", (double)bytes / KILOBITS];
else return [NSString stringWithFormat:@"%.2fMb/s", (double)bytes / MEGABITS];
}
}
UpDownBytes getUpDownBytes()
{
@autoreleasepool
{
struct ifaddrs *ifa_list = 0, *ifa;
UpDownBytes upDownBytes;
upDownBytes.inputBytes = 0;
upDownBytes.outputBytes = 0;
if (getifaddrs(&ifa_list) == -1) return upDownBytes;
for (ifa = ifa_list; ifa; ifa = ifa->ifa_next)
{
if (AF_LINK != ifa->ifa_addr->sa_family ||
(!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING)) ||
ifa->ifa_data == 0) continue;
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
upDownBytes.inputBytes += if_data->ifi_ibytes;
upDownBytes.outputBytes += if_data->ifi_obytes;
}
freeifaddrs(ifa_list);
return upDownBytes;
}
}
static NSMutableString* formattedString()
{
@autoreleasepool
{
NSMutableString* mutableString = [[NSMutableString alloc] init];
UpDownBytes upDownBytes = getUpDownBytes();
long upDiff = (upDownBytes.outputBytes - oldUpSpeed) / updateInterval;
long downDiff = (upDownBytes.inputBytes - oldDownSpeed) / updateInterval;
oldUpSpeed = upDownBytes.outputBytes;
oldDownSpeed = upDownBytes.inputBytes;
if(!showAlways && (upDiff < 2 * KILOBYTES && downDiff < 2 * KILOBYTES) || upDiff > 500 * MEGABYTES && downDiff > 500 * MEGABYTES)
{
shouldUpdateSpeedLabel = NO;
return nil;
}
else shouldUpdateSpeedLabel = YES;
if(dataUnit == 1) // BITS
{
upDiff *= 8;
downDiff *= 8;
}
if(showDownloadSpeedFirst)
{
if(showDownloadSpeed) [mutableString appendString: [NSString stringWithFormat: @"%@%@", downloadPrefix, formatSpeed(downDiff)]];
if(showUploadSpeed)
{
if([mutableString length] > 0)
{
if(showSecondSpeedInNewLine) [mutableString appendString: @"\n"];
else [mutableString appendString: separator];
}
[mutableString appendString: [NSString stringWithFormat: @"%@%@", uploadPrefix, formatSpeed(upDiff)]];
}
}
else
{
if(showUploadSpeed) [mutableString appendString: [NSString stringWithFormat: @"%@%@", uploadPrefix, formatSpeed(upDiff)]];
if(showDownloadSpeed)
{
if([mutableString length] > 0)
{
if(showSecondSpeedInNewLine) [mutableString appendString: @"\n"];
else [mutableString appendString: separator];
}
[mutableString appendString: [NSString stringWithFormat: @"%@%@", downloadPrefix, formatSpeed(downDiff)]];
}
}
return [mutableString copy];
}
}
static void orientationChanged()
{
if(followDeviceOrientation && networkSpeedObject)
[networkSpeedObject updateOrientation];
}
static void loadDeviceScreenDimensions()
{
UIDeviceOrientation orientation = [[UIApplication sharedApplication] _frontMostAppOrientation];
if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)
{
screenWidth = [[UIScreen mainScreen] bounds].size.height;
screenHeight = [[UIScreen mainScreen] bounds].size.width;
}
else
{
screenWidth = [[UIScreen mainScreen] bounds].size.width;
screenHeight = [[UIScreen mainScreen] bounds].size.height;
}
}
@implementation UILabelWithInsets
- (void)drawTextInRect: (CGRect)rect
{
UIEdgeInsets insets = {0, 5, 0, 5};
[super drawTextInRect: UIEdgeInsetsInsetRect(rect, insets)];
}
@end
@implementation NetworkSpeed
- (id)init
{
self = [super init];
if(self)
{
@try
{
networkSpeedWindow = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, width, height)];
[networkSpeedWindow setHidden: NO];
[networkSpeedWindow setAlpha: 1];
[networkSpeedWindow _setSecure: YES];
[networkSpeedWindow setUserInteractionEnabled: NO];
[[networkSpeedWindow layer] setAnchorPoint: CGPointZero];
networkSpeedLabel = [[UILabelWithInsets alloc] initWithFrame: CGRectMake(0, 0, width, height)];
[[networkSpeedLabel layer] setMasksToBounds: YES];
[(UIView *)networkSpeedWindow addSubview: networkSpeedLabel];
[self updateFrame];
[NSTimer scheduledTimerWithTimeInterval: updateInterval target: self selector: @selector(updateText) userInfo: nil repeats: YES];
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)&orientationChanged, CFSTR("com.apple.springboard.screenchanged"), NULL, 0);
CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), NULL, (CFNotificationCallback)&orientationChanged, CFSTR("UIWindowDidRotateNotification"), NULL, CFNotificationSuspensionBehaviorCoalesce);
}
@catch (NSException *e) {}
}
return self;
}
- (void)updateFrame
{
[NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(_updateFrame) object: nil];
[self performSelector: @selector(_updateFrame) withObject: nil afterDelay: 0.3];
}
- (void)_updateFrame
{
if(showOnLockScreen) [networkSpeedWindow setWindowLevel: 1050];
else [networkSpeedWindow setWindowLevel: 1000];
[self updateNetworkSpeedLabelProperties];
[self updateNetworkSpeedSize];
orientationOld = nil;
[self updateOrientation];
}
- (void)updateNetworkSpeedLabelProperties
{
if(boldFont) [networkSpeedLabel setFont: [UIFont boldSystemFontOfSize: fontSize]];
else [networkSpeedLabel setFont: [UIFont systemFontOfSize: fontSize]];
[networkSpeedLabel setNumberOfLines: showSecondSpeedInNewLine ? 2 : 1];
[networkSpeedLabel setTextAlignment: alignment];
if(customTextColorEnabled)
[networkSpeedLabel setTextColor: customTextColor];
if(!backgroundColorEnabled)
[networkSpeedLabel setBackgroundColor: [UIColor clearColor]];
else
{
[[networkSpeedLabel layer] setCornerRadius: backgroundCornerRadius];
[[networkSpeedLabel layer] setContinuousCorners: YES];
if(customBackgroundColorEnabled)
[networkSpeedLabel setBackgroundColor: customBackgroundColor];
}
}
- (void)updateNetworkSpeedSize
{
CGRect frame = [networkSpeedLabel frame];
frame.size.width = width;
frame.size.height = height;
[networkSpeedLabel setFrame: frame];
frame = [networkSpeedWindow frame];
frame.size.width = width;
frame.size.height = height;
[networkSpeedWindow setFrame: frame];
}
- (void)updateOrientation
{
if(!followDeviceOrientation)
{
CGRect frame = [networkSpeedWindow frame];
frame.origin.x = portraitX;
frame.origin.y = portraitY;
[networkSpeedWindow setFrame: frame];
}
else
{
UIDeviceOrientation orientation = [[UIApplication sharedApplication] _frontMostAppOrientation];
if(orientation == orientationOld)
return;
CGAffineTransform newTransform;
CGRect frame = [networkSpeedWindow frame];
switch (orientation)
{
case UIDeviceOrientationLandscapeRight:
{
frame.origin.x = landscapeY;
frame.origin.y = screenHeight - landscapeX;
newTransform = CGAffineTransformMakeRotation(-DegreesToRadians(90));
break;
}
case UIDeviceOrientationLandscapeLeft:
{
frame.origin.x = screenWidth - landscapeY;
frame.origin.y = landscapeX;
newTransform = CGAffineTransformMakeRotation(DegreesToRadians(90));
break;
}
case UIDeviceOrientationPortraitUpsideDown:
{
frame.origin.x = screenWidth - portraitX;
frame.origin.y = screenHeight - portraitY;
newTransform = CGAffineTransformMakeRotation(DegreesToRadians(180));
break;
}
case UIDeviceOrientationPortrait:
default:
{
frame.origin.x = portraitX;
frame.origin.y = portraitY;
newTransform = CGAffineTransformMakeRotation(DegreesToRadians(0));
break;
}
}
[UIView animateWithDuration: 0.3f animations:
^{
[networkSpeedWindow setTransform: newTransform];
[networkSpeedWindow setFrame: frame];
orientationOld = orientation;
} completion: nil];
}
}
- (void)updateText
{
if(networkSpeedWindow && networkSpeedLabel)
{
if(![[%c(SBCoverSheetPresentationManager) sharedInstance] _isEffectivelyLocked])
{
NSString *speed = formattedString();
if(shouldUpdateSpeedLabel)
{
[networkSpeedWindow setHidden: NO];
[networkSpeedLabel setText: speed];
}
else [networkSpeedWindow setHidden: YES];
}
else [networkSpeedWindow setHidden: YES];
}
}
- (void)updateTextColor: (UIColor*)color
{
CGFloat r;
[color getRed: &r green: nil blue: nil alpha: nil];
if(r == 0 || r == 1)
{
if(!customTextColorEnabled) [networkSpeedLabel setTextColor: color];
if(backgroundColorEnabled && !customBackgroundColorEnabled)
{
if(r == 0) [networkSpeedLabel setBackgroundColor: [[UIColor whiteColor] colorWithAlphaComponent: 0.5]];
else [networkSpeedLabel setBackgroundColor: [[UIColor blackColor] colorWithAlphaComponent: 0.5]];
}
}
}
@end
%hook SpringBoard
- (void)applicationDidFinishLaunching: (id)application
{
%orig;
loadDeviceScreenDimensions();
if(!networkSpeedObject)
networkSpeedObject = [[NetworkSpeed alloc] init];
}
%end
%hook _UIStatusBar
-(void)setForegroundColor: (UIColor*)color
{
%orig;
if(networkSpeedObject && [self styleAttributes] && [[self styleAttributes] imageTintColor])
[networkSpeedObject updateTextColor: [[self styleAttributes] imageTintColor]];
}
%end
static void settingsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
if(!pref) pref = [[HBPreferences alloc] initWithIdentifier: @"com.johnzaro.networkspeed13prefs"];
enabled = [pref boolForKey: @"enabled"];
showOnLockScreen = [pref boolForKey: @"showOnLockScreen"];
showDownloadSpeedFirst = [pref boolForKey: @"showDownloadSpeedFirst"];
showSecondSpeedInNewLine = [pref boolForKey: @"showSecondSpeedInNewLine"];
showUploadSpeed = [pref boolForKey: @"showUploadSpeed"];
uploadPrefix = [pref objectForKey: @"uploadPrefix"];
showDownloadSpeed = [pref boolForKey: @"showDownloadSpeed"];
downloadPrefix = [pref objectForKey: @"downloadPrefix"];
separator = [pref objectForKey: @"separator"];
dataUnit = [pref integerForKey: @"dataUnit"];
backgroundColorEnabled = [pref boolForKey: @"backgroundColorEnabled"];
backgroundCornerRadius = [pref floatForKey: @"backgroundCornerRadius"];
customBackgroundColorEnabled = [pref boolForKey: @"customBackgroundColorEnabled"];
portraitX = [pref floatForKey: @"portraitX"];
portraitY = [pref floatForKey: @"portraitY"];
landscapeX = [pref floatForKey: @"landscapeX"];
landscapeY = [pref floatForKey: @"landscapeY"];
followDeviceOrientation = [pref boolForKey: @"followDeviceOrientation"];
width = [pref floatForKey: @"width"];
height = [pref floatForKey: @"height"];
fontSize = [pref integerForKey: @"fontSize"];
boldFont = [pref boolForKey: @"boldFont"];
customTextColorEnabled = [pref boolForKey: @"customTextColorEnabled"];
alignment = [pref integerForKey: @"alignment"];
showAlways = [pref boolForKey: @"showAlways"];
updateInterval = [pref doubleForKey: @"updateInterval"];
if(backgroundColorEnabled && customBackgroundColorEnabled || customTextColorEnabled)
{
NSDictionary *preferencesDictionary = [NSDictionary dictionaryWithContentsOfFile: @"/var/mobile/Library/Preferences/com.johnzaro.networkspeed13prefs.colors.plist"];
customBackgroundColor = [SparkColourPickerUtils colourWithString: [preferencesDictionary objectForKey: @"customBackgroundColor"] withFallback: @"#000000:0.50"];
customTextColor = [SparkColourPickerUtils colourWithString: [preferencesDictionary objectForKey: @"customTextColor"] withFallback: @"#FF9400"];
}
if(networkSpeedObject)
{
[networkSpeedObject updateFrame];
[networkSpeedObject updateText];
}
}
%ctor
{
@autoreleasepool
{
pref = [[HBPreferences alloc] initWithIdentifier: @"com.johnzaro.networkspeed13prefs"];
[pref registerDefaults:
@{
@"enabled": @NO,
@"showOnLockScreen": @NO,
@"showAlways": @NO,
@"showDownloadSpeedFirst": @NO,
@"showSecondSpeedInNewLine": @NO,
@"showUploadSpeed": @NO,
@"uploadPrefix": @"↑",
@"showDownloadSpeed": @NO,
@"downloadPrefix": @"↓",
@"separator": @" ",
@"dataUnit": @0,
@"backgroundColorEnabled": @NO,
@"backgroundCornerRadius": @6,
@"customBackgroundColorEnabled": @NO,
@"portraitX": @280,
@"portraitY": @32,
@"landscapeX": @735,
@"landscapeY": @32,
@"followDeviceOrientation": @NO,
@"width": @95,
@"height": @12,
@"fontSize": @8,
@"boldFont": @NO,
@"customTextColorEnabled": @NO,
@"alignment": @1,
@"updateInterval": @1.0
}];
settingsChanged(NULL, NULL, NULL, NULL, NULL);
if(enabled)
{
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, settingsChanged, CFSTR("com.johnzaro.networkspeed13prefs/reloadprefs"), NULL, CFNotificationSuspensionBehaviorCoalesce);
%init;
}
}
}