YH_PerformanceAppInfo.m 6.31 KB
//
//  YH_PerformanceAppInfo.m
//  YH_Mall
//
//  Created by John Tai on 22/8/2017.
//  Copyright © 2017 YOHO. All rights reserved.
//

#import "YH_PerformanceAppInfo.h"
#import "YH_SessionUtility.h"
#import "YHEventReportMacros.h"
#import "JTSHardwareInfo.h"
#import "YH_Reachability.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <UIKit/UIKit.h>
#import "YH_EventUtil.h"
#import "YHEventReport.h"
#import "NSString+ER_URL.h"
#import <UserNotifications/UserNotifications.h>
#import "YH_Reachability.h"
#import "OpenUDID.h"

@implementation YH_PerformanceAppInfo

- (id)init {
    self = [super init];
    if (self) {
        _ps = @"0";
        _net = @"0";
        [self update];
    }
    return self;
}

- (void)update {
    self.ch = [[YHEventReport sharedInstance] channelId];
    self.udid = [OpenUDID value];
    self.os = @"iOS";
    self.osv = [[UIDevice currentDevice] systemVersion] ?: @"Unknown";
    self.av =[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] ?: @"";
    self.ab = [[NSBundle mainBundle] objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey] ?: @"";
    [self loadCarrierInfo];
    [self loadPushStatus];
    [self loadDm];
}

- (void)loadPushStatus {
    if (IOS10) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
            switch (settings.authorizationStatus) {
                case UNAuthorizationStatusNotDetermined:
                case UNAuthorizationStatusDenied:
                    self.ps = @"0";
                    break;
                default:
                    self.ps = @"1";
                    break;
            }
        }];
    } else {
        if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {
            self.ps = @"1";
        }else {
            self.ps = @"0";
        }
    }
}

-(void)loadDm{
    if (!_dm) {
        _dm = [JTSHardwareInfo hardwareIdentifier];
        NSRange range = [_dm rangeOfString:@","];
        if (range.location != NSNotFound) {
            _dm = [_dm stringByReplacingCharactersInRange:range withString:@"_"];
        }
        if (!_dm) {
            _dm = @"";
        }
    }
}

- (void)loadCarrierInfo
{
    NSString *currentCa = kCarrierCodeChinaUnknown;
    CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [netInfo subscriberCellularProvider];
    NSString *const chinaMCC = @"460";
    _mcc = [carrier.mobileCountryCode copy] ?: @"";
    _mnc = [carrier.mobileNetworkCode copy] ?: @"";
    if (carrier.mobileCountryCode) {
        if ([carrier.mobileCountryCode isEqualToString:chinaMCC]) {
            NSArray *const chinaMobile = @[ @"00", @"02", @"07" , @"08"];// 中国移动
            NSArray *const chinaUnicom = @[ @"01", @"06", @"09" ];// 中国联通
            NSArray *const chinaTelecom = @[ @"03", @"05", @"11" ];// 中国电信
            NSArray *const chinaTietong = @[ @"20" ];// 中国铁通
            NSString *const mnc = [carrier.mobileNetworkCode copy];
            if ([chinaMobile containsObject:mnc]) {
                currentCa = kCarrierCodeChinaMobile;
            } else if ([chinaUnicom containsObject:mnc]) {
                currentCa = kCarrierCodeChinaUnicom;
            } else if ([chinaTelecom containsObject:mnc]) {
                currentCa = kCarrierCodeChinaTelecom;
            } else if ([chinaTietong containsObject:mnc]) {
                currentCa = kCarrierCodeChinaTietong;
            }
        } else {
            currentCa = kCarrierCodeOverseas;
        }
    }
    if ([currentCa isEqualToString:kCarrierCodeChinaUnknown]) {
        currentCa = [[NSUserDefaults standardUserDefaults] objectForKey:NSStringFromSelector(_cmd)] ?: kCarrierCodeChinaUnknown;
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:currentCa forKey:NSStringFromSelector(_cmd)];
    }
    _ca = [currentCa copy];
}

- (void)updateNetStatusWithReachability:(YH_Reachability *)reachability {
    if (reachability.currentReachabilityStatus == YHNetworkStatusNotReachable) {
        self.net = YOHOAppReportKeykNetWorkStatusUnknow;
    } else if (reachability.currentReachabilityStatus == YHNetworkStatusReachableViaWiFi) {
        self.net = YOHOAppReportKeykNetWorkStatusWIFI;
    } else if (reachability.currentReachabilityStatus == YHNetworkStatusReachableViaWWAN) {
        CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
        if ([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]
            || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]
            || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
            self.net = YOHOAppReportKeykNetWorkStatus2G;
        } else if([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]
                  || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]
                  || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]
                  || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]
                  || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]
                  || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]
                  || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
            self.net = YOHOAppReportKeykNetWorkStatus3G;
        } else if([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
            self.net = YOHOAppReportKeykNetWorkStatus4G;
        } else {
            self.net = YOHOAppReportKeykNetWorkStatusUnknow;
        }
    }
}

-(NSString *)uid{
     _uid = [[YHEventReport sharedInstance] uid];
     return [_uid er_md5String];
}

-(NSString *)sid{
    return [YH_SessionUtility sharedInstance].session;
}

-(NSString *)ts{
    NSTimeInterval date = [[NSDate date] timeIntervalSince1970]*1000;
    int num = (arc4random() % 1000000);
    return [NSString stringWithFormat:@"%.0f%.6d", date,num]?:@"";
}

@end