Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 43 additions & 27 deletions YYKit/Base/UIKit/UIDevice+YYAdd.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,31 @@ - (BOOL)isSimulator {

- (BOOL)isJailbroken {
if ([self isSimulator]) return NO; // Dont't check simulator

// iOS9 URL Scheme query changed ...
// NSURL *cydiaURL = [NSURL URLWithString:@"cydia://package"];
// if ([[UIApplication sharedApplication] canOpenURL:cydiaURL]) return YES;

NSArray *paths = @[@"/Applications/Cydia.app",
@"/private/var/lib/apt/",
@"/private/var/lib/cydia",
@"/private/var/stash"];
for (NSString *path in paths) {
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) return YES;
}

FILE *bash = fopen("/bin/bash", "r");
if (bash != NULL) {
fclose(bash);
return YES;
}

NSString *path = [NSString stringWithFormat:@"/private/%@", [NSString stringWithUUID]];
if ([@"test" writeToFile : path atomically : YES encoding : NSUTF8StringEncoding error : NULL]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
return YES;
}

return NO;
}

Expand Down Expand Up @@ -109,15 +109,15 @@ - (NSString *)ipAddressWithIfaName:(NSString *)name {
address = [NSString stringWithUTF8String:str];
}
} break;

case AF_INET6: { // IPv6
char str[INET6_ADDRSTRLEN] = {0};
inet_ntop(family, &(((struct sockaddr_in6 *)addr->ifa_addr)->sin6_addr), str, sizeof(str));
if (strlen(str) > 0) {
address = [NSString stringWithUTF8String:str];
}
}

default: break;
}
if (address) break;
Expand Down Expand Up @@ -179,7 +179,7 @@ static yy_net_interface_counter yy_get_net_interface_counter() {
sharedOutCounters = [NSMutableDictionary new];
lock = dispatch_semaphore_create(1);
});

yy_net_interface_counter counter = {0};
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
Expand All @@ -194,11 +194,11 @@ static yy_net_interface_counter yy_get_net_interface_counter() {
uint64_t counter_in = ((NSNumber *)sharedInCounters[name]).unsignedLongLongValue;
counter_in = yy_net_counter_add(counter_in, data->ifi_ibytes);
sharedInCounters[name] = @(counter_in);

uint64_t counter_out = ((NSNumber *)sharedOutCounters[name]).unsignedLongLongValue;
counter_out = yy_net_counter_add(counter_out, data->ifi_obytes);
sharedOutCounters[name] = @(counter_out);

if ([name hasPrefix:@"en"]) {
counter.en_in += counter_in;
counter.en_out += counter_out;
Expand All @@ -216,7 +216,7 @@ static yy_net_interface_counter yy_get_net_interface_counter() {
dispatch_semaphore_signal(lock);
freeifaddrs(addrs);
}

return counter;
}

Expand Down Expand Up @@ -252,14 +252,14 @@ - (NSString *)machineModelName {
@"Watch2,4" : @"Apple Watch Series 2 42mm",
@"Watch2,6" : @"Apple Watch Series 1 38mm",
@"Watch1,7" : @"Apple Watch Series 1 42mm",

@"iPod1,1" : @"iPod touch 1",
@"iPod2,1" : @"iPod touch 2",
@"iPod3,1" : @"iPod touch 3",
@"iPod4,1" : @"iPod touch 4",
@"iPod5,1" : @"iPod touch 5",
@"iPod7,1" : @"iPod touch 6",

@"iPhone1,1" : @"iPhone 1G",
@"iPhone1,2" : @"iPhone 3G",
@"iPhone2,1" : @"iPhone 3GS",
Expand All @@ -282,7 +282,17 @@ - (NSString *)machineModelName {
@"iPhone9,2" : @"iPhone 7 Plus",
@"iPhone9,3" : @"iPhone 7",
@"iPhone9,4" : @"iPhone 7 Plus",

@"iPhone10,1" : @"iPhone 8",
@"iPhone10,4" : @"iPhone 8",
@"iPhone10,2" : @"iPhone 8 Plus",
@"iPhone10,5" : @"iPhone 8 Plus",
@"iPhone10,3" : @"iPhone X",
@"iPhone10,6" : @"iPhone X",
@"iPhone11,2" : @"iPhone XS",
@"iPhone11,4" : @"iPhone XS Max",
@"iPhone11,6" : @"iPhone XS Max China",
@"iPhone11,8" : @"iPhone XR",

@"iPad1,1" : @"iPad 1",
@"iPad2,1" : @"iPad 2 (WiFi)",
@"iPad2,2" : @"iPad 2 (GSM)",
Expand Down Expand Up @@ -314,12 +324,18 @@ - (NSString *)machineModelName {
@"iPad6,4" : @"iPad Pro (9.7 inch)",
@"iPad6,7" : @"iPad Pro (12.9 inch)",
@"iPad6,8" : @"iPad Pro (12.9 inch)",

@"iPad6,11" : @"iPad 5 (WiFi)",
@"iPad6,12" : @"iPad 5 (Wifi + Cellular)",
@"iPad7,1" : @"iPad Pro 2 12.9 (Wifi)",
@"iPad7,2" : @"iPad Pro 2 12.9 (Wifi + Cellular)",
@"iPad7,3" : @"iPad PRO 10.5 (Wifi)",
@"iPad7,4" : @"iPad PRO 10.5 (Wifi + Cellular)",

@"AppleTV2,1" : @"Apple TV 2",
@"AppleTV3,1" : @"Apple TV 3",
@"AppleTV3,2" : @"Apple TV 3",
@"AppleTV5,3" : @"Apple TV 4",

@"i386" : @"Simulator x86",
@"x86_64" : @"Simulator x64",
};
Expand Down Expand Up @@ -373,7 +389,7 @@ - (int64_t)memoryUsed {
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;

kern = host_page_size(host_port, &page_size);
if (kern != KERN_SUCCESS) return -1;
kern = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
Expand All @@ -387,7 +403,7 @@ - (int64_t)memoryFree {
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;

kern = host_page_size(host_port, &page_size);
if (kern != KERN_SUCCESS) return -1;
kern = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
Expand All @@ -401,7 +417,7 @@ - (int64_t)memoryActive {
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;

kern = host_page_size(host_port, &page_size);
if (kern != KERN_SUCCESS) return -1;
kern = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
Expand All @@ -415,7 +431,7 @@ - (int64_t)memoryInactive {
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;

kern = host_page_size(host_port, &page_size);
if (kern != KERN_SUCCESS) return -1;
kern = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
Expand All @@ -429,7 +445,7 @@ - (int64_t)memoryWired {
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;

kern = host_page_size(host_port, &page_size);
if (kern != KERN_SUCCESS) return -1;
kern = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
Expand All @@ -443,7 +459,7 @@ - (int64_t)memoryPurgable {
vm_size_t page_size;
vm_statistics_data_t vm_stat;
kern_return_t kern;

kern = host_page_size(host_port, &page_size);
if (kern != KERN_SUCCESS) return -1;
kern = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
Expand All @@ -470,20 +486,20 @@ - (NSArray *)cpuUsagePerProcessor {
mach_msg_type_number_t _numCPUInfo, _numPrevCPUInfo = 0;
unsigned _numCPUs;
NSLock *_cpuUsageLock;

int _mib[2U] = { CTL_HW, HW_NCPU };
size_t _sizeOfNumCPUs = sizeof(_numCPUs);
int _status = sysctl(_mib, 2U, &_numCPUs, &_sizeOfNumCPUs, NULL, 0U);
if (_status)
_numCPUs = 1;

_cpuUsageLock = [[NSLock alloc] init];

natural_t _numCPUsU = 0U;
kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &_numCPUsU, &_cpuInfo, &_numCPUInfo);
if (err == KERN_SUCCESS) {
[_cpuUsageLock lock];

NSMutableArray *cpus = [NSMutableArray new];
for (unsigned i = 0U; i < _numCPUs; ++i) {
Float32 _inUse, _total;
Expand All @@ -500,7 +516,7 @@ - (NSArray *)cpuUsagePerProcessor {
}
[cpus addObject:@(_inUse / _total)];
}

[_cpuUsageLock unlock];
if (_prevCPUInfo) {
size_t prevCpuInfoSize = sizeof(integer_t) * _numPrevCPUInfo;
Expand Down
118 changes: 118 additions & 0 deletions YYKit/Image/YYWebImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
#import "YYWebImageOperation.h"
#import "YYImageCoder.h"

@interface YYWebImageManager () <NSURLSessionDelegate>

// The session in which data tasks will run
@property (strong, nonatomic) NSURLSession *session;

@end

@implementation YYWebImageManager

+ (instancetype)sharedManager {
Expand Down Expand Up @@ -46,9 +53,29 @@ - (instancetype)initWithCache:(YYImageCache *)cache queue:(NSOperationQueue *)qu
} else {
_headers = @{ @"Accept" : @"image/*;q=0.8" };
}

[self createNewSessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
return self;
}

- (void)createNewSessionWithConfiguration:(NSURLSessionConfiguration *)sessionConfiguration {

if (self.session) {
[self.session invalidateAndCancel];
}

sessionConfiguration.timeoutIntervalForRequest = _timeout;

/**
* Create the session for this task
* We send nil as delegate queue so that the session creates a serial operation queue for performing all delegate
* method calls and completion handler calls.
*/
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:nil];
}

- (YYWebImageOperation *)requestImageWithURL:(NSURL *)url
options:(YYWebImageOptions)options
progress:(YYWebImageProgressBlock)progress
Expand All @@ -67,6 +94,7 @@ - (YYWebImageOperation *)requestImageWithURL:(NSURL *)url
options:options
cache:_cache
cacheKey:[self cacheKeyForURL:url]
session:_session
progress:progress
transform:transform ? transform : _sharedTransformBlock
completion:completion];
Expand Down Expand Up @@ -95,4 +123,94 @@ - (NSString *)cacheKeyForURL:(NSURL *)url {
return _cacheKeyFilter ? _cacheKeyFilter(url) : url.absoluteString;
}

- (YYWebImageOperation *)operationWithTask:(NSURLSessionTask *)task {
YYWebImageOperation *returnOperation = nil;
for (YYWebImageOperation *operation in self.queue.operations) {
if (operation.dataTask.taskIdentifier == task.taskIdentifier) {
returnOperation = operation;
break;
}
}
return returnOperation;
}

#pragma mark NSURLSessionDataDelegate

- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {

// Identify the operation that runs this task and pass it the delegate method
YYWebImageOperation *dataOperation = [self operationWithTask:dataTask];
if ([dataOperation respondsToSelector:@selector(URLSession:dataTask:didReceiveResponse:completionHandler:)]) {
[dataOperation URLSession:session dataTask:dataTask didReceiveResponse:response completionHandler:completionHandler];
} else {
if (completionHandler) {
completionHandler(NSURLSessionResponseAllow);
}
}
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {

// Identify the operation that runs this task and pass it the delegate method
YYWebImageOperation *dataOperation = [self operationWithTask:dataTask];
if ([dataOperation respondsToSelector:@selector(URLSession:dataTask:didReceiveData:)]) {
[dataOperation URLSession:session dataTask:dataTask didReceiveData:data];
}
}

- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
willCacheResponse:(NSCachedURLResponse *)proposedResponse
completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler {

// Identify the operation that runs this task and pass it the delegate method
YYWebImageOperation *dataOperation = [self operationWithTask:dataTask];
if ([dataOperation respondsToSelector:@selector(URLSession:dataTask:willCacheResponse:completionHandler:)]) {
[dataOperation URLSession:session dataTask:dataTask willCacheResponse:proposedResponse completionHandler:completionHandler];
} else {
if (completionHandler) {
completionHandler(proposedResponse);
}
}
}

#pragma mark NSURLSessionTaskDelegate

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {

// Identify the operation that runs this task and pass it the delegate method
YYWebImageOperation *dataOperation = [self operationWithTask:task];
if ([dataOperation respondsToSelector:@selector(URLSession:task:didCompleteWithError:)]) {
[dataOperation URLSession:session task:task didCompleteWithError:error];
}
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler {

// Identify the operation that runs this task and pass it the delegate method
YYWebImageOperation *dataOperation = [self operationWithTask:task];
if ([dataOperation respondsToSelector:@selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:)]) {
[dataOperation URLSession:session task:task willPerformHTTPRedirection:response newRequest:request completionHandler:completionHandler];
} else {
if (completionHandler) {
completionHandler(request);
}
}
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {

// Identify the operation that runs this task and pass it the delegate method
YYWebImageOperation *dataOperation = [self operationWithTask:task];
if ([dataOperation respondsToSelector:@selector(URLSession:task:didReceiveChallenge:completionHandler:)]) {
[dataOperation URLSession:session task:task didReceiveChallenge:challenge completionHandler:completionHandler];
} else {
if (completionHandler) {
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}
}
}
@end
Loading