forked from peterhajas/MobileNotifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNAlertManager.m
279 lines (240 loc) · 8.01 KB
/
MNAlertManager.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
/*
Copyright (c) 2010-2011, Peter Hajas
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Peter Hajas nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL PETER HAJAS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "MNAlertManager.h"
@implementation MNAlertManager
@synthesize pendingAlerts, dismissedAlerts;
@synthesize delegate = _delegate;
@synthesize alertWindow, pendingAlertViewController;
@synthesize whistleBlower;
@synthesize dashboard;
-(id)init
{
self = [super init];
//Let's hope the NSObject init doesn't fail!
if(self != nil)
{
alertWindow = [[MNAlertWindow alloc] initWithFrame:CGRectMake(0,20,320,0)]; //Measured to be zero, we don't want to mess up interaction with views below! Also, we live below the status bar
alertWindow.windowLevel = 990; //Don't mess around with WindowPlaner or SBSettings if the user has it installed :)
alertWindow.userInteractionEnabled = YES;
alertWindow.hidden = NO;
alertWindow.clipsToBounds = NO;
alertWindow.backgroundColor = [UIColor clearColor];
//If the directory doesn't exist, create it!
if(![[NSFileManager defaultManager] fileExistsAtPath:@"/var/mobile/Library/MobileNotifier/"])
{
[[NSFileManager defaultManager] createDirectoryAtPath:@"/var/mobile/Library/MobileNotifier" withIntermediateDirectories:NO attributes:nil error:NULL];
}
//Load data from files
pendingAlerts = [[NSKeyedUnarchiver unarchiveObjectWithFile:@"/var/mobile/Library/MobileNotifier/pending.plist"] retain] ?: [[NSMutableArray alloc] init];
dismissedAlerts = [[NSKeyedUnarchiver unarchiveObjectWithFile:@"/var/mobile/Library/MobileNotifier/dismissed.plist"] retain] ?: [[NSMutableArray alloc] init];
alertIsShowing = NO;
//Allocate and init the whistle blower
whistleBlower = [[MNWhistleBlowerController alloc] initWithDelegate:self];
//Alloc and init the dashboard
dashboard = [[MNAlertDashboardViewController alloc] initWithDelegate:self];
//Alloc and init the lockscreen view controller
lockscreen = [[MNLockScreenViewController alloc] initWithDelegate:self];
//Register for libactivator events
[[LAActivator sharedInstance] registerListener:self forName:@"com.peterhajassoftware.mobilenotifier"];
}
return self;
}
-(void)newAlertWithData:(MNAlertData *)data
{
//Add to pending alerts
[pendingAlerts insertObject:data atIndex:0];
//New foreground alert!
if(data.status == kNewAlertForeground)
{
if(!pendingAlertViewController.alertIsShowingPopOver)
{
//Build a new MNAlertViewController
if(alertIsShowing)
{
[pendingAlertViewController.view removeFromSuperview];
}
if(![dashboard isShowing] && ![lockscreen isShowing])
{
MNAlertViewController *viewController = [[MNAlertViewController alloc] initWithMNData:data];
viewController.delegate = self;
[viewController.view setFrame:CGRectMake(0,0,320,60)];
pendingAlertViewController = viewController;
alertIsShowing = YES;
//Change the window size
[alertWindow setFrame:CGRectMake(0, 20, 320, 60)];
//Add the subview
[alertWindow addSubview:viewController.view];
[alertWindow setNeedsDisplay];
}
}
else
{
//The user is interacting with an alert!
//Let's send this to pending, and let them
//continue with what they're doing
}
//Make noise
[whistleBlower alertArrived];
}
//Not a foreground alert, but a background alert
else if(data.status == kNewAlertBackground)
{
//We do nothing currently (already in pending)
}
[self saveOut];
[lockscreen refresh];
[dashboard refresh];
}
-(void)saveOut
{
[NSKeyedArchiver archiveRootObject:pendingAlerts toFile:@"/var/mobile/Library/MobileNotifier/pending.plist"];
[NSKeyedArchiver archiveRootObject:dismissedAlerts toFile:@"/var/mobile/Library/MobileNotifier/dismissed.plist"];
}
-(void)showDashboard
{
[self hidePendingAlert];
[dashboard showDashboard];
}
-(void)hideDashboard
{
[dashboard hideDashboard];
}
-(void)showLockscreen
{
[self hidePendingAlert];
if([pendingAlerts count] != 0)
{
[lockscreen show];
}
}
-(void)hideLockscreen
{
[lockscreen hide];
}
-(void)hidePendingAlert
{
[pendingAlertViewController.view removeFromSuperview];
alertWindow.frame = CGRectMake(0,20,320,0);
alertIsShowing = YES;
}
//Delegate method for MNAlertViewController
-(void)alertViewController:(MNAlertViewController *)viewController hadActionTaken:(int)action
{
if(action == kAlertSentAway)
{
alertIsShowing = NO;
}
else if(action == kAlertTakeAction)
{
alertIsShowing = NO;
MNAlertData *data = viewController.dataObj;
//Launch the bundle
[_delegate launchAppInSpringBoardWithBundleID:data.bundleID];
//Move alert into dismissedAlerts from pendingAlerts
[dismissedAlerts addObject:data];
[pendingAlerts removeObject:data];
}
alertWindow.frame = CGRectMake(0,20,320,0);
[self saveOut];
[dashboard refresh];
[lockscreen refresh];
}
-(void)takeActionOnAlertWithData:(MNAlertData *)data
{
//Launch the bundle
[_delegate launchAppInSpringBoardWithBundleID:data.bundleID];
//Move alert into dismissed alerts from either pendingAlerts or sentAwayAlerts
[dismissedAlerts addObject:data];
[pendingAlerts removeObject:data];
[self saveOut];
[dashboard refresh];
[lockscreen refresh];
//Cool! All done!
}
-(UIImage*)iconForBundleID:(NSString *)bundleID;
{
NSLog(@"At MNAlertManager! Delegate: %@", _delegate);
return [_delegate iconForBundleID:bundleID];
}
-(void)alertShowingPopOver:(bool)isShowingPopOver;
{
if(isShowingPopOver)
{
CGRect frame = alertWindow.frame;
frame.size.height += 93;
alertWindow.frame = frame;
}
else
{
CGRect frame = alertWindow.frame;
frame.size.height -= 93;
alertWindow.frame = frame;
}
}
//MNAlertDashboardViewControllerProtocol Methods:
- (void)actionOnAlertAtIndex:(int)index
{
//Create the data object
MNAlertData *data;
data = [pendingAlerts objectAtIndex:index];
//Take action on it
[self takeActionOnAlertWithData:data];
//Hide the dashboard
[dashboard hideDashboard];
}
-(void)dismissedAlertAtIndex:(int)index
{
MNAlertData *data = [pendingAlerts objectAtIndex:index];
[dismissedAlerts addObject:data];
[pendingAlerts removeObject:data];
}
- (NSMutableArray *)getPendingAlerts
{
return pendingAlerts;
}
- (NSMutableArray *)getDismissedAlerts
{
return dismissedAlerts;
}
-(void)dismissSwitcher
{
[_delegate dismissSwitcher];
}
//MNWhistleBlowerController delegate methods
-(void)wakeDeviceScreen
{
[_delegate wakeDeviceScreen];
}
//Libactivator methods
- (void)activator:(LAActivator *)activator receiveEvent:(LAEvent *)event
{
[dashboard toggleDashboard];
}
- (void)activator:(LAActivator *)activator abortEvent:(LAEvent *)event
{
[dashboard hideDashboard];
[self alertViewController:pendingAlertViewController hadActionTaken: kAlertSentAway];
}
@end