libs-base/Source/NSBackgroundActivityScheduler.m

177 lines
4.1 KiB
Mathematica
Raw Normal View History

2019-10-26 11:45:19 +00:00
/* Implementation of class NSBackgroundActivityScheduler
Copyright (C) 2019 Free Software Foundation, Inc.
By: Gregory John Casamento <greg.casamento@gmail.com>
Date: Fri Oct 25 00:52:54 EDT 2019
This file is part of the GNUstep Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
2019-10-26 11:45:19 +00:00
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
2019-10-26 11:45:19 +00:00
*/
#import "Foundation/NSBackgroundActivityScheduler.h"
#import "Foundation/NSString.h"
#import "Foundation/NSTimer.h"
2019-10-26 11:45:19 +00:00
@implementation NSBackgroundActivityScheduler
2019-10-26 12:11:59 +00:00
- (instancetype) initWithIdentifier: (NSString *)identifier
{
self = [super init];
if(self != nil)
{
_identifier = identifier;
_qualityOfService = NSQualityOfServiceDefault;
_repeats = NO;
_interval = 0;
_tolerance = 0;
_shouldDefer = NO;
_timer = nil;
2019-10-28 18:31:42 +00:00
_opts = 0;
_token = nil;
_reason = [NSString stringWithFormat: @"Reason-%@", self];
}
return self;
2019-10-26 12:11:59 +00:00
}
2019-10-28 18:31:42 +00:00
- (void) dealloc
{
RELEASE(_identifier);
RELEASE(_token);
RELEASE(_reason);
[super dealloc];
}
2019-10-26 12:11:59 +00:00
- (NSString *) identifier
{
return _identifier;
2019-10-26 12:11:59 +00:00
}
- (void) setIdentifier: (NSString *)identifier
{
ASSIGNCOPY(_identifier, identifier);
2019-10-26 12:11:59 +00:00
}
- (NSQualityOfService) qualityOfService
{
return _qualityOfService;
2019-10-26 12:11:59 +00:00
}
- (void) setQualityOfService: (NSQualityOfService)qualityOfService
{
_qualityOfService = qualityOfService;
2019-10-26 12:11:59 +00:00
}
- (BOOL) repeats
{
return _repeats;
2019-10-26 12:11:59 +00:00
}
- (void) setRepeats: (BOOL)flag
{
_repeats = flag;
2019-10-26 12:11:59 +00:00
}
- (NSTimeInterval) interval
{
return _interval;
2019-10-26 12:11:59 +00:00
}
- (void) setInterval: (NSTimeInterval)interval
{
_interval = interval;
2019-10-26 12:11:59 +00:00
}
- (NSTimeInterval) tolerance
{
return _tolerance;
2019-10-26 12:11:59 +00:00
}
- (void) setTolerance: (NSTimeInterval)tolerance
2019-10-26 12:11:59 +00:00
{
_tolerance = tolerance;
2019-10-26 12:11:59 +00:00
}
- (BOOL) shouldDefer
{
return _shouldDefer;
2019-10-26 12:11:59 +00:00
}
- (void) setShouldDefer: (BOOL)flag
{
_shouldDefer = flag;
2019-10-26 12:11:59 +00:00
}
2019-10-28 18:31:42 +00:00
- (void) _performActivity
{
2020-12-03 17:48:22 +00:00
# if __has_feature(blocks)
2019-10-28 18:31:42 +00:00
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
[pinfo performActivityWithOptions: _opts
reason: _reason
usingBlock: ^{
// TODO: Need to implement the NSProcessInfo performActivity... methods.
2019-10-28 18:31:42 +00:00
}];
# else
NSLog(@"No block support, so not running background activity....");
# endif
}
2019-10-26 12:11:59 +00:00
- (void) scheduleWithBlock: (GSScheduledBlock)block
{
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
2019-10-28 18:31:42 +00:00
ASSIGN(_block, (id)block);
2019-10-28 18:31:42 +00:00
switch(_qualityOfService)
{
case NSQualityOfServiceUserInteractive:
2019-10-28 18:31:42 +00:00
_opts = NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled;
break;
case NSQualityOfServiceUserInitiated:
2019-10-28 18:31:42 +00:00
_opts = NSActivityUserInitiated;
break;
case NSQualityOfServiceUtility:
2019-10-28 18:31:42 +00:00
_opts = NSActivityUserInitiated | NSActivityIdleDisplaySleepDisabled;
break;
case NSQualityOfServiceBackground:
2019-10-28 18:31:42 +00:00
_opts = NSActivityBackground;
break;
case NSQualityOfServiceDefault:
2019-10-28 18:31:42 +00:00
_opts = NSActivityLatencyCritical;
break;
}
2019-10-28 18:31:42 +00:00
_token = [pinfo beginActivityWithOptions: _opts
reason: _reason];
_timer = [NSTimer scheduledTimerWithTimeInterval: _interval
target: self
selector: @selector(_performActivity)
userInfo: nil
repeats: _repeats];
2019-10-26 12:11:59 +00:00
}
- (void) invalidate
{
2019-10-28 18:31:42 +00:00
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
[_timer invalidate];
[pinfo endActivity: _token];
2019-10-26 12:11:59 +00:00
}
2019-10-26 11:45:19 +00:00
@end