-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shimmer.m
289 lines (218 loc) · 6.79 KB
/
Shimmer.m
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
#import "Shimmer.h"
@interface Shimmer (Private)
-(void)setRemoteInfo:(NSDictionary *)remoteDict;
-(NSDictionary *)remoteInfo;
-(void)setupAndDownload;
-(int)_expectedLength;
-(void)setExpected:(int)expect;
@end
@implementation Shimmer
- (id)init
{
self = [super init];
if (self) {
NSLog(@"initialize");
leon = NO;
customView = NO;
aURLString = [@"" retain];
progressAmount = 0.0;
currentAmount = 0.0;
downloadSize = 0.0;
remoteInfo = [[[NSDictionary alloc] init] retain];
NSLog(@"done");
}
return self;
}
- (void)dealloc
{
//NSLog(@"%s %@", _cmd, self);
[aURLString release];
[remoteInfo release];
[super dealloc];
}
-(BOOL)checkForUpdateHere:(NSString *)aURL
{
NSLog(@"Looking for update info here:%@", aURL);
NSBundle *bundle = [NSBundle mainBundle];
NSString *infoPlist = [bundle pathForResource:@"Info" ofType:@"plist"];
//is this phone set up for pxl packages? DDo some checks...
NSFileManager *man =[NSFileManager defaultManager];
if(![man fileExistsAtPath:@"/private/var/root/Media/PXL"])
//fail silently and return no to the app
return NO;
NSLog(@"pxl is installed...");
//UIAlertSheet *checkUp = [[UIAlertSheet alloc ]init];
/*[checkUp setTitle:@"Checking for update..."];
[checkUp setDimsBackground:YES];
//[checkUp setRunsModal:YES];
//[checkUp setAlertSheetStyle:3];
[checkUp setBlocksInteraction:YES];
[checkUp popupAlertAnimated:YES];
*/
if(infoPlist){
NSLog(@"Local Info:%@",[infoPlist description]);
installedVersion = [NSDictionary dictionaryWithContentsOfFile:infoPlist];
NSLog(@"pulling remote info...");
remoteVersion = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:aURL]];
NSLog(@"pulled...");
if(!remoteVersion){
leon = NO;
}else{
[self setRemoteInfo:remoteVersion];
NSLog(@"%@",[[self remoteInfo] description]);
//check versions
NSString *current = [installedVersion objectForKey:@"appVersion"];
NSString *remote = [[self remoteInfo] objectForKey:@"appVersion"];
if(!current){
NSLog(@"local app has no version key, stop...");
leon = NO;
}
if(![current isEqualToString:remote])
leon = YES;
}
}else{
leon = NO;
}
//[checkUp dismissAnimated:YES];
return leon;
}
- (void) alertSheet:(id) sheet buttonClicked:(int) bnum
{
if(bnum == 1){
[hotSheet dismissAnimated:YES];
[self setupAndDownload];
}else{
[hotSheet dismissAnimated:YES];
leon = NO;
//[self reportToApp];
}
}
-(void)setupAndDownload
{
theProgress = [[UIProgressBar alloc] initWithFrame:CGRectMake(38.0f, 50.0f, 200.0f, 20.0f)];
[theProgress setProgress:progressAmount];
//[theProgress setStyle:2];
//UIAlertSheet *downloader = [[UIAlertSheet alloc ] initWithFrame:CGRectMake(0.0f, 0.0f, 240.0f, 150.0f) ];
downloader = [[UIAlertSheet alloc ] init];
[downloader setTitle:@"Downloading Update..."];
[downloader setDelegate:self];
[downloader setContext:self];
[downloader setAlpha:0.5];
[downloader addSubview:theProgress];
[downloader setDimsBackground:YES];
//[downloader setAlertSheetStyle:1];
[downloader setNumberOfRows:2];
[downloader setTableShouldShowMinimumContent:NO];
[downloader setBlocksInteraction:YES];
[downloader _slideSheetOut:YES];
[downloader layoutAnimated:YES];
[downloader popupAlertAnimated:YES atOffset:0.0];
[self startDownloadingURL:self];
}
-(NSString *)aURLString
{
return aURLString;
}
-(void)setURLString:(NSString *)thisString
{
aURLString = thisString;
}
-(BOOL)reportToApp
{
return leon;
}
-(void)setRemoteInfo:(NSDictionary *)remoteDict
{
remoteInfo = remoteDict;
}
-(NSDictionary *)remoteInfo
{
return remoteInfo;
}
-(void)doUpdate
{
NSMutableArray *buttons = [[NSMutableArray alloc] init];
[buttons addObject:[NSString stringWithString:@"Update"]];
[buttons addObject:[NSString stringWithString:@"Cancel"]];
hotSheet = [[UIAlertSheet alloc]
initWithTitle:[@"Update Available: v" stringByAppendingString:[[self remoteInfo] objectForKey:@"appVersion"]]
buttons:buttons
defaultButtonIndex:1
delegate:self
context:self];
[self setURLString:[[self remoteInfo] objectForKey:@"pxlPackURL"]];
_expectedLength = [[[self remoteInfo] objectForKey:@"pxlPackBytes"] intValue];
[self setExpected:[[[self remoteInfo] objectForKey:@"pxlPackBytes"] intValue]];
NSLog(@"expected from Dict: %i", [[[self remoteInfo] objectForKey:@"pxlPackBytes"] intValue]);
NSLog(@"expect as set: %i", [self _expectedLength]);
leon = YES;
[hotSheet setBodyText:[NSString stringWithFormat:@"%@. %@ %@", [[self remoteInfo] objectForKey:@"description"], @"Source:",[self aURLString]]];
[hotSheet setDimsBackground:YES];
[hotSheet _slideSheetOut:YES];
[hotSheet setRunsModal:YES];
[hotSheet setShowsOverSpringBoardAlerts:YES];
if(customView){
[hotSheet popupAlertAnimated:YES];
}else{
[hotSheet presentSheetToAboveView:[self aboveThisView]];
[hotSheet popupAlertAnimated:YES];
}
}
-(void)setAboveThisView:(UIView *)someView
{
aboveThisView = someView;
}
-(UIView *)aboveThisView
{
return aboveThisView;
}
-(void)setExpected:(int)expect
{
_expectedLength = expect;
}
-(void)setUseCustomView:(BOOL)meh
{
customView = meh;
}
-(BOOL)useCustomView
{
return customView;
}
-(int)_expectedLength
{
return _expectedLength;
}
- (void)startDownloadingURL:sender
{
NSURL *theDownload = [NSURL URLWithString:[self aURLString]];
[theDownload loadResourceDataNotifyingClient:self usingCache:NO];
if (theDownload) {
dataIn = [[NSMutableData data] retain];
[dataIn setLength:0];
} else {
// let user know it didn't work out
}
}
-(void)URL:(NSURL *)sender resourceDataDidBecomeAvailable:(NSData *)newBytes
{
[dataIn appendData:newBytes];
_bytesReceived = [dataIn length];
float f = _bytesReceived;
float g = [self _expectedLength];
[theProgress setProgress:f/g];
}
- (void)URLResourceDidFinishLoading:(NSURL *)sender
{
NSString *saveTo = [@"/private/var/root/Media/PXL/Dropoff" stringByAppendingPathComponent:[[[[self aURLString] lastPathComponent] stringByDeletingPathExtension] stringByAppendingPathExtension:@"pxl"]];
[dataIn writeToFile:saveTo atomically:YES];
[dataIn release];
//create the commmand file for installing:
NSDictionary *innerCommands = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"install",[saveTo lastPathComponent],nil] forKeys:[NSArray arrayWithObjects:@"command",@"package",nil]];
NSLog(@"%@",[innerCommands description]);
NSDictionary *dictForPlist = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSArray arrayWithObjects:innerCommands,nil],nil] forKeys:[NSArray arrayWithObjects:@"commands",nil]];
NSLog(@"%@",[dictForPlist description]);
[dictForPlist writeToFile:@"/private/var/root/Media/PXL/Dropoff/PxlPickup" atomically:YES];
[downloader dismissAnimated:YES];
leon = YES;
}
@end