-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
354 lines (322 loc) · 10.6 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#import <dlfcn.h>
#import <objc/runtime.h>
#include <sys/sysctl.h>
#import <substrate.h>
#import <notify.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <net/if.h>
#define NSLog(...)
#define PLIST_PATH_Settings "/var/mobile/Library/Preferences/com.julioverne.ntspeed.plist"
static BOOL Enabled;
static int kWidth = 40;
static int kHeight = 15;
static int kLocX = 5;
static int kLocY = 20;
static float kAlpha = 0.5f;
static float kRadius = 6;
static BOOL forceNewLocation;
static float kScreenW;
static float kScreenH;
static __strong NSString* kBs = @"%ldB/s";
static __strong NSString* kKs = @"%.1fK/s";
static __strong NSString* kMs = @"%.2fM/s";
static __strong NSString* kGs = @"%.3fG/s";
static NSString *bytesFormat(long bytes)
{
@autoreleasepool {
if(bytes < 1024) {
return [NSString stringWithFormat:kBs, bytes];
} else if(bytes >= 1024 && bytes < 1024 * 1024) {
return [NSString stringWithFormat:kKs, (double)bytes / 1024];
} else if(bytes >= 1024 * 1024 && bytes < 1024 * 1024 * 1024) {
return [NSString stringWithFormat:kMs, (double)bytes / (1024 * 1024)];
} else {
return [NSString stringWithFormat:kGs, (double)bytes / (1024 * 1024 * 1024)];
}
}
}
static long getBytesTotal()
{
@autoreleasepool {
if(!Enabled) {
return 0;
}
uint32_t iBytes = 0;
uint32_t oBytes = 0;
struct ifaddrs *ifa_list = NULL, *ifa;
if ((getifaddrs(&ifa_list) < 0) || !ifa_list || ifa_list==0) {
return 0;
}
for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL) {
continue;
}
if (AF_LINK != ifa->ifa_addr->sa_family) {
continue;
}
if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING)) {
continue;
}
if (ifa->ifa_data == NULL || ifa->ifa_data == 0) {
continue;
}
struct if_data *if_data = (struct if_data *)ifa->ifa_data;
iBytes += if_data->ifi_ibytes;
oBytes += if_data->ifi_obytes;
}
if(ifa_list) {
freeifaddrs(ifa_list);
}
return iBytes + oBytes;
}
}
@interface UIWindow ()
- (void)_setSecure:(BOOL)arg1;
@end
@interface UIApplication ()
- (UIDeviceOrientation)_frontMostAppOrientation;
@end
@interface NtSpeed : NSObject
{
UIWindow* springboardWindow;
UILabel *label;
UIView *backView;
UIView *content;
}
@property (nonatomic, strong) UIWindow* springboardWindow;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *content;
+ (id)sharedInstance;
+ (BOOL)sharedInstanceExist;
+ (void)notifyOrientationChange;
- (void)firstload;
- (void)orientationChanged;
- (void)updateFrame;
@end
static void orientationChanged()
{
[NtSpeed notifyOrientationChange];
}
static long oldSpeed = 0;
static UIDeviceOrientation orientationOld;
@implementation NtSpeed
@synthesize springboardWindow, label, backView, content;
__strong static id _sharedObject;
+ (id)sharedInstance
{
if (!_sharedObject) {
_sharedObject = [[self alloc] init];
[NSTimer scheduledTimerWithTimeInterval:1 target:_sharedObject selector:@selector(update) 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);
}
return _sharedObject;
}
+ (BOOL)sharedInstanceExist
{
if (_sharedObject) {
return YES;
}
return NO;
}
+ (void)notifyOrientationChange
{
if([NtSpeed sharedInstanceExist]) {
if (NtSpeed* NTShared = [NtSpeed sharedInstance]) {
[NTShared orientationChanged];
}
}
}
- (void)firstload
{
return;
}
-(id)init
{
self = [super init];
if(self != nil) {
@try {
kScreenW = [[UIScreen mainScreen] bounds].size.width;
kScreenH = [[UIScreen mainScreen] bounds].size.height;
springboardWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
springboardWindow.windowLevel = 9999999999;
[springboardWindow setHidden:NO];
springboardWindow.alpha = 1;
[springboardWindow _setSecure:YES];
[springboardWindow setUserInteractionEnabled:YES];
springboardWindow.layer.cornerRadius = kRadius;
springboardWindow.layer.masksToBounds = YES;
springboardWindow.layer.shouldRasterize = NO;
backView = [UIView new];
backView.frame = CGRectMake(0, 0, springboardWindow.frame.size.width, springboardWindow.frame.size.height);
backView.backgroundColor = [UIColor colorWithWhite: 0.50 alpha:1];
backView.alpha = kAlpha;
[(UIView *)springboardWindow addSubview:backView];
content = [UIView new];
content.alpha = 0.9f;
content.frame = CGRectMake(4, 0, springboardWindow.frame.size.width-8, springboardWindow.frame.size.height);
label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, content.frame.size.width, content.frame.size.height)];
[self update];
label.numberOfLines = 1;
label.textColor = [UIColor whiteColor];
label.baselineAdjustment = YES;
label.adjustsFontSizeToFitWidth = YES;
label.allowsDefaultTighteningForTruncation = YES;
label.textAlignment = NSTextAlignmentCenter;
[content addSubview:label];
[(UIView *)springboardWindow addSubview:content];
[springboardWindow addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]];
[self orientationChanged];
} @catch (NSException * e) {
}
}
return self;
}
- (void)panAction:(UIPanGestureRecognizer *)recognizer {
CGPoint translationPoint = [recognizer translationInView:content.superview];
CGPoint center = recognizer.view.center;
NSMutableDictionary *CydiaEnablePrefsCheck = [[NSMutableDictionary alloc] initWithContentsOfFile:@PLIST_PATH_Settings]?:[NSMutableDictionary dictionary];
NSLog(@"ntspeed_value%@",recognizer.view);
recognizer.view.center = CGPointMake(center.x + translationPoint.x, center.y + translationPoint.y);
[recognizer setTranslation:CGPointZero inView:content.superview];
[CydiaEnablePrefsCheck setObject:@(center.x + translationPoint.x) forKey:@"kLocX"];
[CydiaEnablePrefsCheck setObject:@(center.y + translationPoint.y) forKey:@"kLocY"];
[CydiaEnablePrefsCheck writeToFile:@PLIST_PATH_Settings atomically:YES];
}
- (void)updateFrame
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_updateFrame) object:nil];
[self performSelector:@selector(_updateFrame) withObject:nil afterDelay:0.3];
}
- (void)_updateFrame
{
backView.alpha = kAlpha;
springboardWindow.layer.cornerRadius = kRadius;
springboardWindow.frame = CGRectMake(0, 0, kWidth, kHeight);
backView.frame = CGRectMake(0, 0, springboardWindow.frame.size.width, springboardWindow.frame.size.height);
content.frame = CGRectMake(4, 0, springboardWindow.frame.size.width-8, springboardWindow.frame.size.height);
label.frame = CGRectMake(0, 0, content.frame.size.width, content.frame.size.height);
forceNewLocation = YES;
[springboardWindow setHidden:NO];
[self orientationChanged];
}
- (void)update
{
@autoreleasepool {
long nowData = getBytesTotal();
if(!oldSpeed) {
oldSpeed = nowData;
}
if(nowData<=0) {
if(springboardWindow) {
[springboardWindow setHidden:YES];
}
return;
}
if(label&&springboardWindow) {
long speed = nowData-oldSpeed;
if(!forceNewLocation) {
[springboardWindow setHidden:(speed<=0)?YES:NO];
}
label.text = bytesFormat(speed);
}
oldSpeed = nowData;
}
}
- (void)orientationChanged
{
UIDeviceOrientation orientation = [[UIApplication sharedApplication] _frontMostAppOrientation];
if(orientation == orientationOld && !forceNewLocation) {
return;
}
forceNewLocation = NO;
BOOL isLandscape;
__block CGAffineTransform newTransform;
__block int xLoc;
__block int yLoc;
#define DegreesToRadians(degrees) (degrees * M_PI / 180)
switch (orientation) {
case UIDeviceOrientationLandscapeRight: {
isLandscape = YES;
yLoc = kLocX;
xLoc = kLocY;
newTransform = CGAffineTransformMakeRotation(-DegreesToRadians(90));
break;
}
case UIDeviceOrientationLandscapeLeft: {
isLandscape = YES;
yLoc = (kScreenH-kWidth-kLocX);
xLoc = (kScreenW-kHeight-kLocY);
newTransform = CGAffineTransformMakeRotation(DegreesToRadians(90));
break;
}
case UIDeviceOrientationPortraitUpsideDown: {
isLandscape = NO;
yLoc = (kScreenH-kHeight-kLocY);
xLoc = kLocX;
newTransform = CGAffineTransformMakeRotation(DegreesToRadians(180));
break;
}
case UIDeviceOrientationPortrait:
default: {
isLandscape = NO;
yLoc = kLocY;
xLoc = (kScreenW-kWidth-kLocX);
newTransform = CGAffineTransformMakeRotation(DegreesToRadians(0));
break;
}
}
[UIView animateWithDuration:0.3f animations:^{
[springboardWindow setTransform:newTransform];
CGRect frame = springboardWindow.frame;
frame.origin.y = yLoc;
frame.origin.x = xLoc;
springboardWindow.frame = frame;
orientationOld = orientation;
} completion:nil];
}
@end
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)application
{
%orig;
[[NtSpeed sharedInstance] firstload];
}
%end
static void settingsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
@autoreleasepool {
NSDictionary *TweakPrefs = [[[NSDictionary alloc] initWithContentsOfFile:@PLIST_PATH_Settings]?:[NSDictionary dictionary] copy];
Enabled = (BOOL)[[TweakPrefs objectForKey:@"Enabled"]?:@YES boolValue];
int newkLocX = (int)[[TweakPrefs objectForKey:@"kLocX"]?:@(5) intValue];
int newkLocY = (int)[[TweakPrefs objectForKey:@"kLocY"]?:@(20) intValue];
int newkWidth = (int)[[TweakPrefs objectForKey:@"kWidth"]?:@(40) intValue];
int newkHeight = (int)[[TweakPrefs objectForKey:@"kHeight"]?:@(15) intValue];
float newkAlpha = (float)[[TweakPrefs objectForKey:@"kAlpha"]?:@(0.5) floatValue];
float newkRadius = (float)[[TweakPrefs objectForKey:@"kRadius"]?:@(6) floatValue];
BOOL needUpdateUI = NO;
if(newkLocX!=kLocX || newkLocY!=kLocY || newkWidth!=kWidth || newkHeight!=kHeight || newkAlpha!=kAlpha || newkRadius!=kRadius) {
needUpdateUI = YES;
}
kLocX = newkLocX;
kLocY = newkLocY;
kWidth = newkWidth;
kHeight = newkHeight;
kAlpha = newkAlpha;
kRadius = newkRadius;
if(needUpdateUI && [NtSpeed sharedInstanceExist]) {
if (NtSpeed* NTShared = [NtSpeed sharedInstance]) {
[NTShared updateFrame];
}
}
}
}
%ctor
{
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, settingsChanged, CFSTR("com.julioverne.ntspeed/Settings"), NULL, CFNotificationSuspensionBehaviorCoalesce);
settingsChanged(NULL, NULL, NULL, NULL, NULL);
%init;
}