OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "RTCUIApplicationStatusObserver.h" | 11 #include "RTCUIApplicationStatusObserver.h" |
12 | 12 |
13 #if defined(WEBRTC_IOS) | 13 #if defined(WEBRTC_IOS) |
14 | 14 |
15 #import <UIKit/UIKit.h> | 15 #import <UIKit/UIKit.h> |
16 | 16 |
17 #include "webrtc/rtc_base/checks.h" | 17 #include "webrtc/rtc_base/checks.h" |
18 | 18 |
| 19 @interface RTCUIApplicationStatusObserver () |
| 20 |
| 21 @property(nonatomic, assign) BOOL initialized; |
| 22 @property(nonatomic, assign) UIApplicationState state; |
| 23 |
| 24 @end |
| 25 |
19 @implementation RTCUIApplicationStatusObserver { | 26 @implementation RTCUIApplicationStatusObserver { |
20 BOOL _initialized; | 27 BOOL _initialized; |
21 dispatch_block_t _initializeBlock; | 28 dispatch_block_t _initializeBlock; |
22 UIApplicationState _state; | 29 UIApplicationState _state; |
| 30 |
| 31 id<NSObject> _activeObserver; |
| 32 id<NSObject> _backgroundObserver; |
23 } | 33 } |
24 | 34 |
| 35 @synthesize initialized = _initialized; |
| 36 @synthesize state = _state; |
| 37 |
25 + (instancetype)sharedInstance { | 38 + (instancetype)sharedInstance { |
26 static id sharedInstance; | 39 static id sharedInstance; |
27 static dispatch_once_t onceToken; | 40 static dispatch_once_t onceToken; |
28 dispatch_once(&onceToken, ^{ | 41 dispatch_once(&onceToken, ^{ |
29 sharedInstance = [[self alloc] init]; | 42 sharedInstance = [[self alloc] init]; |
30 }); | 43 }); |
31 | 44 |
32 return sharedInstance; | 45 return sharedInstance; |
33 } | 46 } |
34 | 47 |
35 - (id)init { | 48 - (id)init { |
36 if (self = [super init]) { | 49 if (self = [super init]) { |
37 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | 50 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
38 [center addObserverForName:UIApplicationDidBecomeActiveNotification | 51 __weak RTCUIApplicationStatusObserver *weakSelf = self; |
39 object:nil | 52 _activeObserver = [center addObserverForName:UIApplicationDidBecomeActiveNot
ification |
40 queue:[NSOperationQueue mainQueue] | 53 object:nil |
41 usingBlock:^(NSNotification *note) { | 54 queue:[NSOperationQueue mainQueue] |
42 _state = [UIApplication sharedApplication].applicationStat
e; | 55 usingBlock:^(NSNotification *note) { |
43 }]; | 56 weakSelf.state = |
| 57 [UIApplication sharedApplication].ap
plicationState; |
| 58 }]; |
44 | 59 |
45 [center addObserverForName:UIApplicationDidEnterBackgroundNotification | 60 _backgroundObserver = [center addObserverForName:UIApplicationDidEnterBackgr
oundNotification |
46 object:nil | 61 object:nil |
47 queue:[NSOperationQueue mainQueue] | 62 queue:[NSOperationQueue mainQueue
] |
48 usingBlock:^(NSNotification *note) { | 63 usingBlock:^(NSNotification *note) { |
49 _state = [UIApplication sharedApplication].applicationStat
e; | 64 weakSelf.state = |
50 }]; | 65 [UIApplication sharedApplication
].applicationState; |
| 66 }]; |
51 | 67 |
52 _initialized = NO; | 68 _initialized = NO; |
53 _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^
{ | 69 _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^
{ |
54 _state = [UIApplication sharedApplication].applicationState; | 70 weakSelf.state = [UIApplication sharedApplication].applicationState; |
55 _initialized = YES; | 71 weakSelf.initialized = YES; |
56 }); | 72 }); |
57 | 73 |
58 dispatch_async(dispatch_get_main_queue(), _initializeBlock); | 74 dispatch_async(dispatch_get_main_queue(), _initializeBlock); |
59 } | 75 } |
60 | 76 |
61 return self; | 77 return self; |
62 } | 78 } |
63 | 79 |
| 80 - (void)dealloc { |
| 81 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
| 82 [center removeObserver:_activeObserver]; |
| 83 [center removeObserver:_backgroundObserver]; |
| 84 } |
| 85 |
64 - (BOOL)isApplicationActive { | 86 - (BOOL)isApplicationActive { |
65 if (!_initialized) { | 87 if (!_initialized) { |
66 long ret = dispatch_block_wait(_initializeBlock, | 88 long ret = dispatch_block_wait(_initializeBlock, |
67 dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_
PER_SEC)); | 89 dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_
PER_SEC)); |
68 RTC_DCHECK_EQ(ret, 0); | 90 RTC_DCHECK_EQ(ret, 0); |
69 } | 91 } |
70 return _state == UIApplicationStateActive; | 92 return _state == UIApplicationStateActive; |
71 } | 93 } |
72 | 94 |
73 @end | 95 @end |
74 | 96 |
75 #endif // WEBRTC_IOS | 97 #endif // WEBRTC_IOS |
OLD | NEW |