-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSWWidgetContainerView.m
More file actions
409 lines (369 loc) · 15.7 KB
/
SWWidgetContainerView.m
File metadata and controls
409 lines (369 loc) · 15.7 KB
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
#import "SWWidgetContainerView.h"
#import <objc/runtime.h>
#import "CoreTelephonyClient.h"
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <stdint.h>
@interface UIColor (ios13)
+(id)systemBackgroundColor ;
+(id)secondarySystemBackgroundColor ;
+(id)tableCellGroupedBackgroundColor ;
@end
@interface CTDataUsage : NSObject
@property (assign,nonatomic) unsigned long long cellularHome; //@synthesize cellularHome=_cellularHome - In the implementation block
@property (assign,nonatomic) unsigned long long cellularRoaming; //@synthesize cellularRoaming=_cellularRoaming - In the implementation block
@property (assign,nonatomic) unsigned long long wifi; //@synthesize wifi=_wifi - In the implementation block
-(id)safeValueForKey:(NSString *)key;
@end
@interface CTDeviceDataUsage : NSObject
- (CTDataUsage *)totalDataUsageForPeriod:(unsigned long long)arg1;
- (CTDataUsage *)totalDataUsedForPeriod:(unsigned long long)arg1;
@end
@class CoreTelephonyClient;
@interface PSUICoreTelephonyDataCache : NSObject
+(instancetype)sharedInstance;
@property (nonatomic,retain) CoreTelephonyClient *client;
@end
@interface STStorageSpace : NSObject
-(uint64_t)freeBytes;
-(uint64_t)usedBytes;
@end
@interface CALayer (Undocumented)
@property (assign) BOOL continuousCorners;
@end
@interface STStorageDiskMonitor : NSObject
+(id)sharedMonitor;
-(void)updateDiskSpace;
-(uint64_t)deviceSize;
-(uint64_t)lastFree;
-(STStorageSpace *)storageSpace;
@end
@interface BatteryHealthUIController
-(NSString *)getChargeCapacityRemaining;
-(void)updateData;
@end
static NSString *getIPAddress() {
NSString *address = @"error";
BOOL hasFound = NO;
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while (temp_addr != NULL) {
if (temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if (!strcmp(temp_addr->ifa_name, "en0")) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
hasFound = YES;
}
}
temp_addr = temp_addr->ifa_next;
}
}
if (!hasFound) {
address = @"Wi-Fi Not Connected";
}
// Free memory
freeifaddrs(interfaces);
return address;
}
/*
static NSString *getIPAddress() {
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
bool didFindCellular, didFindWifi;
didFindCellular = false;
didFindWifi = false;
// returns 0 on success
int ifaddresses = getifaddrs(&interfaces);
if (ifaddresses == 0) {
temp_addr = interfaces;
while (temp_addr != NULL) {
if (temp_addr->ifa_addr->sa_family == AF_INET || temp_addr->ifa_addr->sa_family == AF_INET6) {
if (!strcmp(temp_addr->ifa_name, "en0")) {
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
didFindWifi = true;
} else if (!strcmp(temp_addr->ifa_name, "pdp_ip0")) {
//address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
didFindCellular = true;
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
}*/
@implementation SWWidgetContainerView
-(CGSize)intrinsicContentSize {
return CGSizeMake(0, 120);
}
-(void)setupWidgetsWithType1:(SWWidgetType)widgetType1 type2:(SWWidgetType)widgetType2 transparentBackground:(BOOL)transparent widgetInset:(int)inset cornerRadius:(int)cornerRadius {
//widgetType1 = SWWidgetTypeNone;
_widgetType1 = widgetType1;
_widgetType2 = widgetType2;
_transparentWidgetBackgrounds = transparent;
Class _widgetView1Class;
Class _widgetView2Class;
int maxWidgetCount = 5;
const Class classList[] = {[SWBatteryWidgetView class], [SWStorageWidgetView class], [SWWifiWidgetView class], [SWCellularWidgetView class], [SWUtilityWidgetView class]};
if (widgetType1 != SWWidgetTypeNone) {
if (widgetType1 < maxWidgetCount) {
_widgetView1Class = classList[widgetType1];
//_widgetView1Page = pageList[widgetType1];
}
}
if (widgetType2 != SWWidgetTypeNone) {
if (widgetType2 < maxWidgetCount) {
_widgetView2Class = classList[widgetType2];
//_widgetView2Page = pageList[widgetType2];
}
}
if (!_widgetView1Class)
_widgetView1Class = [UIView class];
if (!_widgetView2Class)
_widgetView2Class = [UIView class];
if (widgetType1 != SWWidgetTypeNone) {
_widgetView1 = [[_widgetView1Class alloc] init];
_widgetView1.layer.cornerRadius = cornerRadius;
if (@available(iOS 13.0, *)) {
_widgetView1.layer.cornerCurve = kCACornerCurveContinuous;
} else {
_widgetView1.layer.continuousCorners = YES;
}
_widgetView1.translatesAutoresizingMaskIntoConstraints = NO;
UITapGestureRecognizer *widget1TapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:_widgetView1 action:@selector(openPrefsURL)];
[_widgetView1 addGestureRecognizer:widget1TapGestureRecognizer];
if ([_widgetView1 respondsToSelector:@selector(setup)])
[_widgetView1 setup];
[self addSubview: _widgetView1];
}
if (widgetType2 != SWWidgetTypeNone) {
_widgetView2 = [[_widgetView2Class alloc] init];
_widgetView2.layer.cornerRadius = cornerRadius;
if (@available(iOS 13.0, *)) {
_widgetView2.layer.cornerCurve = kCACornerCurveContinuous;
} else {
_widgetView2.layer.continuousCorners = YES;
}
_widgetView2.translatesAutoresizingMaskIntoConstraints = NO;
UITapGestureRecognizer *widget2TapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:_widgetView2 action:@selector(openPrefsURL)];
[_widgetView2 addGestureRecognizer:widget2TapGestureRecognizer];
if ([_widgetView2 respondsToSelector:@selector(setup)])
[_widgetView2 setup];
[self addSubview: _widgetView2];
}
/*if (widgetType1 != SWWidgetTypeNone) {
[_widgetView1.leadingAnchor constraintEqualToAnchor: self.leadingAnchor constant:inset].active = YES;
[_widgetView1.trailingAnchor constraintEqualToAnchor: self.centerXAnchor constant:-7.5].active = YES;
[_widgetView1.topAnchor constraintEqualToAnchor: self.topAnchor constant:5].active = YES;
[_widgetView1.bottomAnchor constraintEqualToAnchor: self.bottomAnchor constant:-15].active = YES;
} else*/
if (widgetType1 == SWWidgetTypeNone || widgetType2 == SWWidgetTypeNone) {
if (widgetType1 == SWWidgetTypeNone) {
[_widgetView2.leadingAnchor constraintEqualToAnchor: self.leadingAnchor constant:inset].active = YES;
[_widgetView2.trailingAnchor constraintEqualToAnchor: self.trailingAnchor constant:-inset].active = YES;
[_widgetView2.topAnchor constraintEqualToAnchor: self.topAnchor constant:5].active = YES;
[_widgetView2.bottomAnchor constraintEqualToAnchor: self.bottomAnchor constant:-15].active = YES;
[_widgetView2 updateForBigWidgetView];
} else {
[_widgetView1.leadingAnchor constraintEqualToAnchor: self.leadingAnchor constant:inset].active = YES;
[_widgetView1.trailingAnchor constraintEqualToAnchor: self.trailingAnchor constant:-inset].active = YES;
[_widgetView1.topAnchor constraintEqualToAnchor: self.topAnchor constant:5].active = YES;
[_widgetView1.bottomAnchor constraintEqualToAnchor: self.bottomAnchor constant:-15].active = YES;
[_widgetView1 updateForBigWidgetView];
}
} else {
[_widgetView1.leadingAnchor constraintEqualToAnchor: self.leadingAnchor constant:inset].active = YES;
[_widgetView1.trailingAnchor constraintEqualToAnchor: self.centerXAnchor constant:-7.5].active = YES;
[_widgetView1.topAnchor constraintEqualToAnchor: self.topAnchor constant:5].active = YES;
[_widgetView1.bottomAnchor constraintEqualToAnchor: self.bottomAnchor constant:-15].active = YES;
[_widgetView2.leadingAnchor constraintEqualToAnchor: self.centerXAnchor constant:7.5].active = YES;
[_widgetView2.trailingAnchor constraintEqualToAnchor: self.trailingAnchor constant:-inset].active = YES;
[_widgetView2.topAnchor constraintEqualToAnchor: self.topAnchor constant:5].active = YES;
[_widgetView2.bottomAnchor constraintEqualToAnchor: self.bottomAnchor constant:-15].active = YES;
}
/*if (widgetType1 == widgetType2 != SWWidgetTypeNone) {
[_widgetView2.leadingAnchor constraintEqualToAnchor: self.centerXAnchor constant:7.5].active = YES;
[_widgetView2.trailingAnchor constraintEqualToAnchor: self.trailingAnchor constant:-inset].active = YES;
[_widgetView2.topAnchor constraintEqualToAnchor: self.topAnchor constant:5].active = YES;
[_widgetView2.bottomAnchor constraintEqualToAnchor: self.bottomAnchor constant:-15].active = YES;
}*/
[self loadWidgetData];
}
-(BOOL)shouldLoadBatteryData {
if ([_widgetView1 isKindOfClass:[SWBatteryWidgetView class]] || [_widgetView2 isKindOfClass:[SWBatteryWidgetView class]]) {
return YES;
}
return NO;
}
-(BOOL)shouldLoadStorageData {
if ([_widgetView1 isKindOfClass:[SWStorageWidgetView class]] || [_widgetView2 isKindOfClass:[SWStorageWidgetView class]]) {
return YES;
}
return NO;
}
-(BOOL)shouldLoadWifiData {
if ([_widgetView1 isKindOfClass:[SWWifiWidgetView class]] || [_widgetView2 isKindOfClass:[SWWifiWidgetView class]]) {
return YES;
}
return NO;
}
-(BOOL)shouldLoadCellularData {
if ([_widgetView1 isKindOfClass:[SWCellularWidgetView class]] || [_widgetView2 isKindOfClass:[SWCellularWidgetView class]]) {
return YES;
}
return NO;
}
-(void)loadWidgetData {
if ([self shouldLoadBatteryData]) {
static dispatch_once_t batteryDataToken;
static NSString *g_maximumCapacityPercent;
dispatch_once(&batteryDataToken, ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSBundle *storageSettingsBundle = [NSBundle bundleWithPath:@"/System/Library/PreferenceBundles/BatteryUsageUI.bundle"];
[storageSettingsBundle load];
Class healthClass = objc_getClass("BatteryHealthUIController");
if (healthClass) {
BatteryHealthUIController *instance = [healthClass new];
[instance updateData];
g_maximumCapacityPercent = [instance getChargeCapacityRemaining];
}
__block BOOL useBatteryHealth = NO;
if (![g_maximumCapacityPercent isEqual: @"—"])
useBatteryHealth = YES;
dispatch_async(dispatch_get_main_queue(), ^{
//_healthLabel.text = _useBatteryHealth ? [NSString stringWithFormat:@"Max Capacity: %@", _maximumCapacityPercent] : [NSString stringWithFormat:@"Battery Level: %.0f%%", [UIDevice currentDevice].batteryLevel*100];
NSDictionary *data = @{@"useBatteryHealth": @(useBatteryHealth), @"maximumCapacityPercent": g_maximumCapacityPercent};
if (_widgetType1 == SWWidgetTypeBattery)
([_widgetView1 updateForData:data]);
if (_widgetType2 == SWWidgetTypeBattery)
[_widgetView2 updateForData:data];
});
});
});
}
if ([self shouldLoadStorageData]) {
static dispatch_once_t dataToken;
static uint64_t g_totalDiskSpace;
static uint64_t g_usedDiskSpace;
dispatch_once(&dataToken, ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSBundle *storageSettingsBundle = [NSBundle bundleWithPath:@"/System/Library/PreferenceBundles/StorageSettings.bundle"];
[storageSettingsBundle load];
STStorageDiskMonitor *monitor = [objc_getClass("STStorageDiskMonitor") new];
uint64_t totalDiskSpace = monitor.deviceSize;
uint64_t usedDiskSpace;
if (monitor && [monitor respondsToSelector:@selector(updateDiskSpace)])
[monitor updateDiskSpace];
// iOS 14
if ([monitor respondsToSelector:@selector(storageSpace)]) {
usedDiskSpace = monitor.storageSpace.usedBytes;
} else {
usedDiskSpace = totalDiskSpace - monitor.lastFree;
}
g_totalDiskSpace = totalDiskSpace;
g_usedDiskSpace = usedDiskSpace;
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *data = @{@"usedDiskSpace": @(g_usedDiskSpace), @"totalDiskSpace": @(g_totalDiskSpace)};
if (_widgetType1 == SWWidgetTypeStorage)
[_widgetView1 updateForData:data];
if (_widgetType2 == SWWidgetTypeStorage)
[_widgetView2 updateForData:data];
});
});
});
}
if ([self shouldLoadWifiData]) {
NSString *result = getIPAddress();
NSDictionary *data = @{@"ipAddress": result};
if (_widgetType1 == SWWidgetTypeWifi)
[_widgetView1 updateForData:data];
if (_widgetType2 == SWWidgetTypeWifi)
[_widgetView2 updateForData:data];
//[NSNotificationCenter.defaultCenter postNotificationName:@"SWWiFiDataProcessedNotification" object:nil userInfo:data];
}
if ([self shouldLoadCellularData]) {
Class cls = objc_getClass("PSUICoreTelephonyDataCache");
BOOL failed = NO;
if (!cls)
failed = YES;
PSUICoreTelephonyDataCache *dataCache = [cls sharedInstance];
if (dataCache) {
CoreTelephonyClient *client = dataCache.client;
if (client) {
[client dataUsageForLastPeriods:2 completion:^(CTDeviceDataUsage *dataUsage, NSError *arg2) {
BOOL block_failed = NO;
if (dataUsage) {
CTDataUsage *usage = nil;
if ([dataUsage respondsToSelector:@selector(totalDataUsageForPeriod:)])
usage = [dataUsage totalDataUsageForPeriod:0];
else if ([dataUsage respondsToSelector:@selector(totalDataUsedForPeriod:)])
usage = [dataUsage totalDataUsedForPeriod:0];
else
block_failed = YES;
if (usage) {
unsigned long long actualDataUsageBytes = (unsigned long long)[usage safeValueForKey:@"_cellularHome"] + (unsigned long long)[usage safeValueForKey:@"_cellularRoaming"];
//NSLog(@"jew %llu", [usage cellularHome]);
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *data = @{@"cellularUsage": @(actualDataUsageBytes)};
if (_widgetType1 == SWWidgetTypeCellular)
[_widgetView1 updateForData:data];
if (_widgetType2 == SWWidgetTypeCellular)
[_widgetView2 updateForData:data];
});
} else {
block_failed = YES;
}
} else {
block_failed = YES;
}
if (block_failed) {
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *data = @{@"cellularUsage": @0};
if (_widgetType1 == SWWidgetTypeCellular)
[_widgetView1 updateForData:data];
if (_widgetType2 == SWWidgetTypeCellular)
[_widgetView2 updateForData:data];
});
}
}];
} else {
failed = YES;
}
} else {
failed = YES;
}
if (failed) {
NSDictionary *data = @{@"cellularUsage": @0};
if (_widgetType1 == SWWidgetTypeCellular)
[_widgetView1 updateForData:data];
if (_widgetType2 == SWWidgetTypeCellular)
[_widgetView2 updateForData:data];
}
}
}
-(void)doNothing {
}
-(void)setWidgetBackgrounds {
if (_transparentWidgetBackgrounds) {
_widgetView1.backgroundColor = UIColor.clearColor;
_widgetView2.backgroundColor = UIColor.clearColor;
return;
}
if (@available(iOS 13, *)) {
if ([UITraitCollection currentTraitCollection].userInterfaceStyle == UIUserInterfaceStyleDark) {
_widgetView1.backgroundColor = UIColor.tableCellGroupedBackgroundColor;
_widgetView2.backgroundColor = UIColor.tableCellGroupedBackgroundColor;
return;
}
}
_widgetView1.backgroundColor = [UIColor whiteColor];
_widgetView2.backgroundColor = [UIColor whiteColor];
}
@end