aboutsummaryrefslogtreecommitdiff
path: root/VKPC/Statistics.m
blob: 5b7e5b70eeffd29604ad355bab59996b8427ccc0 (plain)
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
//
//  Statistics.m
//  VKPC
//
//  Created by Eugene on 11/12/14.
//  Copyright (c) 2014 Eugene Z. All rights reserved.
//

#import "Statistics.h"

@implementation Statistics {
    NSMutableData *responseData;
}

static BOOL initialized = NO;
static Statistics *instance;
static NSTimer *timer;

static const float kNormalTimeout = 3600 * 6;
static const float kAfterFailureTimeout = 1200;

+ (void)initialize {
    if (initialized) {
        return;
    }
    
    instance = [[Statistics alloc] init];
    
    long ts = GetTimestamp();
    long reported = [[NSUserDefaults standardUserDefaults] integerForKey:VKPCPreferencesStatisticReportedTimestamp];
    
//    NSLog(@"[Statistics initialize] ts=%ld, reported=%ld", ts, reported);
    
    if (reported == 0 || ts - reported >= kNormalTimeout) {
//        NSLog(@"[Statistics initalize] report now");
        [self report];
    } else {
        [self initializeTimerWithTimeout:kNormalTimeout - (ts - reported)];
    }
    
    initialized = YES;
}

//+ (void)timerCallback:(NSTimer *)timer {
//    long ts = GetTimestamp();
//    long reported = [[NSUserDefaults standardUserDefaults] integerForKey:VKPCPreferencesStatisticReportedTimestamp];
//    
//    NSLog(@"[Statistics timerCallback] ts=%ld, reported=%ld", ts, reported);
//    
//    if (ts - reported >= 3600 * 8) {
//        [self report];
//    } else {
//        [self initializeTimerWithTimeout:kNormalTimeout];
//    }
//}

+ (void)initializeTimerWithTimeout:(float)timeout {
    if (timer != nil) {
        [timer invalidate];
        timer = nil;
    }
    
    timer = [NSTimer scheduledTimerWithTimeInterval:timeout
                                     target:[Statistics class]
                                   selector:@selector(report)
                                   userInfo:nil
                                    repeats:NO];
}

+ (void)report {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                    initWithURL:[NSURL URLWithString:@"https://ch1p.com/vkpc/usage.php"]];
    NSString *postData = [NSString stringWithFormat:@"app_v=%@&osx_v=%@&uuid=%@", getAppVersion(), getOSXVersion(), getUUID()];
    
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
 
    [instance mrProper];
    [[NSURLConnection alloc] initWithRequest:request delegate:instance];
}

+ (void)requestDone:(NSData *)data {
    NSError *error;
    NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
    if (error || !json) {
        NSLog(@"[Statistics requestDone] error while parsing json: %@", error);
        [self initializeTimerWithTimeout:kAfterFailureTimeout];
        return;
    }
    
    NSString *result = json[0];
    NSLog(@"[Statistics requestDone] result: %@", result);
    if ([result isEqualToString:@"ok"]) {
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:GetTimestamp()] forKey:VKPCPreferencesStatisticReportedTimestamp];
        [self initializeTimerWithTimeout:kNormalTimeout];
    } else {
        [self initializeTimerWithTimeout:kAfterFailureTimeout];
    }
}

+ (void)requestFailed:(NSError *)error {
    NSLog(@"[Statistics requestFailed] error: %@", [error description]);
    [self initializeTimerWithTimeout:kAfterFailureTimeout];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [Statistics requestFailed:error];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [Statistics requestDone:responseData];
}

- (void)mrProper {
    responseData = nil;
}

static NSString *getAppVersion() {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:kCFBundleVersion];
}

static NSString *getOSXVersion() {
    SInt32 major, minor, bugfix;
    Gestalt(gestaltSystemVersionMajor, &major);
    Gestalt(gestaltSystemVersionMinor, &minor);
    Gestalt(gestaltSystemVersionBugFix, &bugfix);
    return [NSString stringWithFormat:@"%d.%d.%d", major, minor, bugfix];
}

static NSString *getUUID() {
    return [[NSUserDefaults standardUserDefaults] stringForKey:VKPCPreferencesUUID];
}

@end