Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 394 lines (332 sloc) 10.813 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
/*
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;
@synthesize preferenceManager;

-(id)init
{
self = [super init];

//Let's hope the NSObject init doesn't fail!
if(self != nil)
{
alertWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0,0,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 = UIWindowLevelAlert+500.0f; //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];

//Alloc and init the preferences manager
preferenceManager = [[MNPreferenceManager alloc] init];

//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;
pendingAlertViewController = viewController;

alertIsShowing = YES;

//Change the window size
[alertWindow setFrame:CGRectMake(0, 0, 320, 40)];
//Add the subview
[alertWindow addSubview:viewController.view];
[alertWindow setNeedsDisplay];

//Expand the status bar
                [_delegate setDoubleHighStatusBar:YES];
}
}
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];

if([_delegate deviceIsLocked])
{
[lockscreen expandPendingAlertsList];
}

//Start the timer, if the user prefers it
NSNumber *autoLaterEnabled = [preferenceManager.preferences valueForKey:@"autoLaterAlertsEnabled"];
     bool shouldAutoLater = autoLaterEnabled ? [autoLaterEnabled boolValue] : YES;
    
     if(shouldAutoLater)
     {
alertDismissTimer = [NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(alertShouldGoLaterTimerFired:) userInfo:nil repeats:NO];
}

}
//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)showDashboardFromSwitcher
{
    NSNumber *switcherViewEnabled = [preferenceManager.preferences valueForKey:@"switcherViewEnabled"];
bool shouldShow = switcherViewEnabled ? [switcherViewEnabled boolValue] : YES;

if(!shouldShow)
{
return;
}

[self hidePendingAlert];
[dashboard showDashboard];
}

-(void)showDashboard
{
    [self hidePendingAlert];
[dashboard showDashboard];
}

-(void)fadeDashboardDown
{
[dashboard fadeDashboardDown];
}

-(void)fadeDashboardAway
{
    [dashboard fadeDashboardAway];
}

-(void)showLockscreen
{
NSNumber *lockscreenEnabled = [preferenceManager.preferences valueForKey:@"lockscreenEnabled"];
bool shouldShow = lockscreenEnabled ? [lockscreenEnabled boolValue] : YES;

if(!shouldShow)
{
return;
}

[self hidePendingAlert];
    if([pendingAlerts count] != 0)
    {
        [lockscreen show];
    }
}
-(void)hideLockscreen
{
    [lockscreen hide];
}

-(void)hidePendingAlert
{
    [pendingAlertViewController.view removeFromSuperview];
    alertWindow.frame = CGRectMake(0,0,320,0);
    alertIsShowing = YES;
    [_delegate setDoubleHighStatusBar:NO];
}

-(void)alertShouldGoLaterTimerFired:(id)sender
{
if(!pendingAlertViewController)
    {
return;
    }
//If the alert is expanded, then let's not have the alert go to "later"
if(pendingAlertViewController.alertIsShowingPopOver)
{
return;
}

[pendingAlertViewController laterPushed:nil];
}

-(void)reloadPreferences
{
[preferenceManager reloadPreferences];
}


//Delegate method for MNAlertViewController
-(void)alertViewController:(MNAlertViewController *)viewController hadActionTaken:(int)action
{
[_delegate setDoubleHighStatusBar:NO];
if(action == kAlertSentAway)
{
alertIsShowing = NO;
}
else if(action == kAlertTakeAction)
{
alertIsShowing = NO;
MNAlertData *data = viewController.dataObj;

[self takeActionOnAlertWithData:data];
}
[self hidePendingAlert];
[_delegate setDoubleHighStatusBar:NO];
alertWindow.frame = CGRectMake(0,0,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
[self removeAllPendingAlertsWithSender:data.header];
//Cool! All done!
}

-(void)removeAllPendingAlertsWithSender:(NSString *)sender
{
    //Loop through all pending alerts, and remove all the ones that are from the same sender
    
    NSMutableArray *toRemove = [NSMutableArray array];
    
    for(MNAlertData* dataObj in pendingAlerts)
    {
        if([dataObj.header isEqualToString:sender])
        {
            [dismissedAlerts addObject:dataObj];
            [toRemove addObject:dataObj];
        }
    }
    
    [pendingAlerts removeObjectsInArray:toRemove];
    
    [self saveOut];
    [self refreshAll];
}

-(void)refreshAll
{
    [dashboard refresh];
    [lockscreen refresh];
}

-(UIImage*)iconForBundleID:(NSString *)bundleID;
{
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];

//Hide the dashboard
[dashboard fadeDashboardAway];

//Take action on it
[self takeActionOnAlertWithData:data];
}

-(void)dismissedAlertAtIndex:(int)index
{
    MNAlertData *data = [pendingAlerts objectAtIndex:index];
[dismissedAlerts addObject:data];
[pendingAlerts removeObject:data];
}

-(void)clearPending
{
if([pendingAlerts count] == 0)
    {
        return;
    }
    NSUInteger size = [pendingAlerts count];
    NSUInteger i;
    for(i = 0; i < size; i++)
    {
        MNAlertData* dataObj = [pendingAlerts lastObject];
        if(!dataObj)
        {
            break;
        }
        [dismissedAlerts addObject:dataObj];
        [pendingAlerts removeObject:dataObj];
    }
    [self saveOut];
    [self refreshAll];
}
- (NSMutableArray *)getPendingAlerts
{
return pendingAlerts;
}

- (NSMutableArray *)getDismissedAlerts
{
return dismissedAlerts;
}

-(void)dismissSwitcher
{
[_delegate dismissSwitcher];
}

//MNWhistleBlowerController delegate methods

-(void)wakeDeviceScreen
{
    [self refreshAll];
    [_delegate wakeDeviceScreen];
}

//Libactivator methods
- (void)activator:(LAActivator *)activator receiveEvent:(LAEvent *)event
{
    [self hideLockscreen];
[dashboard toggleDashboard];
}

- (void)activator:(LAActivator *)activator abortEvent:(LAEvent *)event
{
[dashboard fadeDashboardDown];
[self alertViewController:pendingAlertViewController hadActionTaken: kAlertSentAway];
}

@end
Something went wrong with that request. Please try again.