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" | |
18 | |
17 @implementation RTCUIApplicationStatusObserver { | 19 @implementation RTCUIApplicationStatusObserver { |
20 BOOL _initialized; | |
21 dispatch_block_t _initializeBlock; | |
18 UIApplicationState _state; | 22 UIApplicationState _state; |
19 } | 23 } |
20 | 24 |
21 + (instancetype)sharedInstance { | 25 + (instancetype)sharedInstance { |
22 static id sharedInstance; | 26 static id sharedInstance; |
23 static dispatch_once_t onceToken; | 27 static dispatch_once_t onceToken; |
24 dispatch_once(&onceToken, ^{ | 28 dispatch_once(&onceToken, ^{ |
25 sharedInstance = [[self alloc] init]; | 29 sharedInstance = [[self alloc] init]; |
26 }); | 30 }); |
27 | 31 |
(...skipping 10 matching lines...) Expand all Loading... | |
38 _state = [UIApplication sharedApplication].applicationStat e; | 42 _state = [UIApplication sharedApplication].applicationStat e; |
39 }]; | 43 }]; |
40 | 44 |
41 [center addObserverForName:UIApplicationDidEnterBackgroundNotification | 45 [center addObserverForName:UIApplicationDidEnterBackgroundNotification |
42 object:nil | 46 object:nil |
43 queue:[NSOperationQueue mainQueue] | 47 queue:[NSOperationQueue mainQueue] |
44 usingBlock:^(NSNotification *note) { | 48 usingBlock:^(NSNotification *note) { |
45 _state = [UIApplication sharedApplication].applicationStat e; | 49 _state = [UIApplication sharedApplication].applicationStat e; |
46 }]; | 50 }]; |
47 | 51 |
48 dispatch_block_t initializeBlock = ^{ | 52 _initialized = NO; |
53 _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^ { | |
49 _state = [UIApplication sharedApplication].applicationState; | 54 _state = [UIApplication sharedApplication].applicationState; |
50 }; | 55 _initialized = YES; |
56 }); | |
51 | 57 |
52 if ([NSThread isMainThread]) { | 58 dispatch_async(dispatch_get_main_queue(), _initializeBlock); |
53 initializeBlock(); | |
54 } else { | |
55 dispatch_sync(dispatch_get_main_queue(), initializeBlock); | |
56 } | |
57 } | 59 } |
58 | 60 |
59 return self; | 61 return self; |
60 } | 62 } |
61 | 63 |
62 - (BOOL)isApplicationActive { | 64 - (BOOL)isApplicationActive { |
65 if (!_initialized) { | |
66 long ret = | |
67 dispatch_block_wait(_initializeBlock, dispatch_time(DISPATCH_TIME_NOW, 1 .0 * NSEC_PER_SEC)); | |
68 RTC_CHECK_EQ(ret, 0); | |
kthelgason
2017/08/23 13:25:46
just a nit, but in the future be aware that the ar
| |
69 } | |
63 return _state == UIApplicationStateActive; | 70 return _state == UIApplicationStateActive; |
64 } | 71 } |
65 | 72 |
66 @end | 73 @end |
67 | 74 |
68 #endif // WEBRTC_IOS | 75 #endif // WEBRTC_IOS |
OLD | NEW |