-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShareViewController.m
More file actions
271 lines (208 loc) · 7.29 KB
/
Copy pathShareViewController.m
File metadata and controls
271 lines (208 loc) · 7.29 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
#import "ShareViewController.h"
#import "TiVoHTTPServer.h"
#include <syslog.h>
#include <dlfcn.h>
#import <UIKit/UIKit.h>
@implementation ShareViewNavController
{
}
-(id)initWithRootViewController:(id)controller
{
self = [super initWithRootViewController:controller];
return self;
}
-(void)loadView
{
[super loadView];
}
-(void)setTivoObjects: (Beacon*)b withHttpServer:(TiVoHTTPServer*)h
{
beacon = b;
httpd = h;
}
-(void)setDVRMobilePrefs: (DVRMobilePrefs*)p
{
prefs = p;
tivos = [prefs GetTivos];
}
@end;
@implementation ShareViewController
{
}
-(BOOL)isWifiActive
{
/* for now, always return YES */
return YES;
void *libHandle = dlopen("/System/Library/PrivateFrameworks/Preferences.framework/Preferences", RTLD_LAZY);
if (libHandle) {
syslog(LOG_DEBUG, "got a handle to PrivateFrameworks/Preferences");
BOOL (*isEnabled)() = dlsym(libHandle, "PSWiFiIsEnabled");
if (isEnabled) {
syslog(LOG_DEBUG, "imported symbol");
return (isEnabled());
}
}
return NO;
}
-(void)setTivoObjects: (Beacon*)b withHttpServer:(TiVoHTTPServer*)h
{
beacon = b;
httpd = h;
}
-(void)loadView
{
self.view = [[[UIScrollView alloc] initWithFrame: [[UIScreen mainScreen] bounds]] autorelease];
self.view.backgroundColor = [UIColor blackColor];
self.title = @"Share";
spinningGear = [[CustomSpinningGearView alloc] autorelease];
[spinningGear initialize];
[spinningGear setText: @"Starting Service"];
inactiveHTML = @"<html><body bgcolor=\"black\"><font color=\"white\">The <b>DVRMobile</b> Service is currently inactive.<br/><br/>Select <b><i>Start Service</i></b> to begin sharing content with your TiVo.</font></body></html>";
activeHTML = [[NSString alloc] initWithFormat:@"<html><body bgcolor=\"black\"><font color=\"white\">The <b>DVRMobile</b> Service is active.<br/><br/>To view your <b>Photos</b> or <b>Music</b> on your TiVo, enter your TiVo Central menu, then choose: <br><br><b> Music, Photos, & Showcases</b><br> ><i>%@</i><br> ><i>%@</i><br><br></font></body></html>", [prefs GetPhotosContainer], [prefs GetMusicContainer]];
if ([self isWifiActive]) {
syslog(LOG_DEBUG, "WiFi is enabled.");
startServiceBtn = [[UIBarButtonItem alloc] initWithTitle:@"Start Service"
style:UIBarButtonItemStyleBordered target:self
action:@selector(StartTivoService:)];
stopServiceBtn = [[UIBarButtonItem alloc] initWithTitle:@"Stop Service"
style:UIBarButtonItemStyleBordered target:self
action:@selector(StopTivoService:)];
disableDimBtn = [[UIBarButtonItem alloc] initWithTitle:@"Disable Dim"
style:UIBarButtonItemStyleBordered target:self
action:@selector(DisableDim:)];
enableDimBtn = [[UIBarButtonItem alloc] initWithTitle:@"Enable Dim"
style:UIBarButtonItemStyleBordered target:self
action:@selector(EnableDim:)];
self.navigationItem.leftBarButtonItem = startServiceBtn;
self.navigationItem.rightBarButtonItem = disableDimBtn;
statusHtmlLabel = [[UIWebView alloc] initWithFrame:CGRectMake(20,30,280,380)];
statusHtmlLabel.backgroundColor = [UIColor blackColor];
[statusHtmlLabel loadHTMLString: inactiveHTML baseURL:nil];
[statusHtmlLabel setDelegate:self];
[statusHtmlLabel setHidden:YES];
[self.view addSubview: statusHtmlLabel];
[self.view addSubview: spinningGear];
} else {
syslog(LOG_ERR, "WiFi is NOT enabled for DVRMobile.");
UILabel *wifiError = [[[UILabel alloc] initWithFrame:CGRectMake(60,170,180,75)] autorelease];
wifiError.backgroundColor = [UIColor blackColor];
wifiError.textColor = [UIColor whiteColor];
wifiError.text = @"WiFi is not enabled.";
wifiError.font = [UIFont boldSystemFontOfSize:16];
[self.view addSubview: wifiError];
}
}
-(void)viewDidLoad
{
syslog(LOG_DEBUG, "ShareViewController: viewDidLoad..");
if ([prefs GetAutoStart]) {
syslog(LOG_DEBUG, "Auto Starting Service..");
[self StartTivoService: nil];
}
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[self performSelector:@selector(showAboutWebView) withObject:nil afterDelay:.1];
}
- (void)showAboutWebView
{
[statusHtmlLabel setHidden:NO];
}
-(void) EnableDim:(id)sender
{
UIApplication* app = [UIApplication sharedApplication];
app.idleTimerDisabled = NO;
self.navigationItem.rightBarButtonItem = disableDimBtn;
}
-(void) DisableDim:(id)sender
{
UIApplication* app = [UIApplication sharedApplication];
app.idleTimerDisabled = YES;
self.navigationItem.rightBarButtonItem = enableDimBtn;
}
-(void)AskBackground: (id) sender
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Warning"
message:@"This action will send DVRMobile into the background. Continue?"
delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Continue", nil];
[warningAlert show];
[warningAlert release];
}
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger) buttonIndex
{
if (buttonIndex == 1) {
[NSThread detachNewThreadSelector:@selector(Background:)
toTarget:self withObject:nil];
}
}
-(void) Background:(id)sender
{
UIApplication* app = [UIApplication sharedApplication];
[app setBackground: YES];
[app setApplicationBadge:@"On"];
[app performSelector: @selector(suspendWithAnimation:) withObject:nil afterDelay: 0];
}
-(void) StartTivoService:(id)sender
{
syslog(LOG_DEBUG, "starting tivo service..");
[spinningGear show];
[beacon start];
[httpd setSpinningGearView: spinningGear];
[NSThread detachNewThreadSelector: @selector(ServeForever)
toTarget:httpd withObject:nil];
[NSThread detachNewThreadSelector:@selector(StartTivoServiceThread:)
toTarget:self withObject:nil];
}
-(void) StartTivoServiceThread:(id)anObject
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
sleep(1);
while (![httpd isStarted] && [httpd isRunning]) {
sleep(1);
}
[spinningGear stop];
if ([httpd isStarted]) {
self.navigationItem.leftBarButtonItem = stopServiceBtn;
[statusHtmlLabel loadHTMLString:activeHTML baseURL:nil];
} else {
/* unable to start service */
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"An error occured while attempting to start the service. If this problem persists, wait one minute and restart DVRMobile."
delegate:self cancelButtonTitle:nil otherButtonTitles:@"Continue", nil];
[errorAlert show];
[errorAlert release];
}
[pool release];
[NSThread exit];
}
-(void) StopTivoService:(id)sender
{
syslog(LOG_DEBUG, "stopping tivo service..");
UIApplication* app = [UIApplication sharedApplication];
app.idleTimerDisabled = NO;
[app setServiceRunning: NO];
[spinningGear setText: @"Stopping Service"];
[spinningGear show];
[beacon stop];
[httpd stop];
[NSThread detachNewThreadSelector:@selector(StopTivoServiceThread:)
toTarget:self withObject:nil];
}
-(void) StopTivoServiceThread:(id)anObject
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
sleep(1);
while (![httpd isStopped]) {
sleep(1);
}
self.navigationItem.leftBarButtonItem = startServiceBtn;
[spinningGear stop];
[statusHtmlLabel loadHTMLString:inactiveHTML baseURL:nil];
[pool release];
}
-(void)setDVRMobilePrefs: (DVRMobilePrefs*)p
{
prefs = p;
}
@end