2017-06-12 14:16:20 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright 2016 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "RTCUIApplicationStatusObserver.h"
|
|
|
|
|
|
|
|
|
|
#if defined(WEBRTC_IOS)
|
|
|
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
|
2017-08-23 06:04:17 -07:00
|
|
|
#include "webrtc/rtc_base/checks.h"
|
|
|
|
|
|
2017-09-13 02:18:36 -07:00
|
|
|
@interface RTCUIApplicationStatusObserver ()
|
|
|
|
|
|
|
|
|
|
@property(nonatomic, assign) BOOL initialized;
|
|
|
|
|
@property(nonatomic, assign) UIApplicationState state;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2017-06-12 14:16:20 +02:00
|
|
|
@implementation RTCUIApplicationStatusObserver {
|
2017-08-23 06:04:17 -07:00
|
|
|
BOOL _initialized;
|
|
|
|
|
dispatch_block_t _initializeBlock;
|
2017-06-12 14:16:20 +02:00
|
|
|
UIApplicationState _state;
|
2017-09-13 02:18:36 -07:00
|
|
|
|
|
|
|
|
id<NSObject> _activeObserver;
|
|
|
|
|
id<NSObject> _backgroundObserver;
|
2017-06-12 14:16:20 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-13 02:18:36 -07:00
|
|
|
@synthesize initialized = _initialized;
|
|
|
|
|
@synthesize state = _state;
|
|
|
|
|
|
2017-06-12 14:16:20 +02:00
|
|
|
+ (instancetype)sharedInstance {
|
|
|
|
|
static id sharedInstance;
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
sharedInstance = [[self alloc] init];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return sharedInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id)init {
|
|
|
|
|
if (self = [super init]) {
|
|
|
|
|
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
2017-09-13 02:18:36 -07:00
|
|
|
__weak RTCUIApplicationStatusObserver *weakSelf = self;
|
|
|
|
|
_activeObserver = [center addObserverForName:UIApplicationDidBecomeActiveNotification
|
|
|
|
|
object:nil
|
|
|
|
|
queue:[NSOperationQueue mainQueue]
|
|
|
|
|
usingBlock:^(NSNotification *note) {
|
|
|
|
|
weakSelf.state =
|
|
|
|
|
[UIApplication sharedApplication].applicationState;
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
_backgroundObserver = [center addObserverForName:UIApplicationDidEnterBackgroundNotification
|
|
|
|
|
object:nil
|
|
|
|
|
queue:[NSOperationQueue mainQueue]
|
|
|
|
|
usingBlock:^(NSNotification *note) {
|
|
|
|
|
weakSelf.state =
|
|
|
|
|
[UIApplication sharedApplication].applicationState;
|
|
|
|
|
}];
|
2017-06-12 14:16:20 +02:00
|
|
|
|
2017-08-23 06:04:17 -07:00
|
|
|
_initialized = NO;
|
|
|
|
|
_initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^{
|
2017-09-13 02:18:36 -07:00
|
|
|
weakSelf.state = [UIApplication sharedApplication].applicationState;
|
|
|
|
|
weakSelf.initialized = YES;
|
2017-08-23 06:04:17 -07:00
|
|
|
});
|
2017-06-12 14:16:20 +02:00
|
|
|
|
2017-08-23 06:04:17 -07:00
|
|
|
dispatch_async(dispatch_get_main_queue(), _initializeBlock);
|
2017-06-12 14:16:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-13 02:18:36 -07:00
|
|
|
- (void)dealloc {
|
|
|
|
|
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
|
|
|
|
[center removeObserver:_activeObserver];
|
|
|
|
|
[center removeObserver:_backgroundObserver];
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 14:16:20 +02:00
|
|
|
- (BOOL)isApplicationActive {
|
2017-08-23 06:04:17 -07:00
|
|
|
if (!_initialized) {
|
2017-08-29 06:52:04 -07:00
|
|
|
long ret = dispatch_block_wait(_initializeBlock,
|
|
|
|
|
dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_PER_SEC));
|
|
|
|
|
RTC_DCHECK_EQ(ret, 0);
|
2017-08-23 06:04:17 -07:00
|
|
|
}
|
2017-06-12 14:16:20 +02:00
|
|
|
return _state == UIApplicationStateActive;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#endif // WEBRTC_IOS
|