2015-03-23 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-03-12 19:51:44 +00:00
|
|
|
|
|
|
|
#import "RCTAppState.h"
|
|
|
|
|
|
|
|
#import "RCTAssert.h"
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
2015-09-22 17:43:56 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-03-12 19:51:44 +00:00
|
|
|
|
|
|
|
static NSString *RCTCurrentAppBackgroundState()
|
|
|
|
{
|
|
|
|
static NSDictionary *states;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
states = @{
|
|
|
|
@(UIApplicationStateActive): @"active",
|
2016-03-10 16:29:52 +00:00
|
|
|
@(UIApplicationStateBackground): @"background"
|
2015-03-12 19:51:44 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-09-22 17:43:56 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
|
|
|
return @"extension";
|
|
|
|
}
|
|
|
|
|
|
|
|
return states[@(RCTSharedApplication().applicationState)] ?: @"unknown";
|
2015-03-12 19:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@implementation RCTAppState
|
|
|
|
{
|
|
|
|
NSString *_lastKnownState;
|
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
2015-04-09 15:46:53 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-03-12 19:51:44 +00:00
|
|
|
#pragma mark - Lifecycle
|
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
2015-03-12 19:51:44 +00:00
|
|
|
{
|
2015-11-25 11:09:00 +00:00
|
|
|
_bridge = bridge;
|
2015-03-12 19:51:44 +00:00
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
// Is this thread-safe?
|
|
|
|
_lastKnownState = RCTCurrentAppBackgroundState();
|
2015-05-31 21:33:59 +00:00
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
|
|
|
|
UIApplicationDidEnterBackgroundNotification,
|
2016-03-10 16:29:52 +00:00
|
|
|
UIApplicationDidFinishLaunchingNotification,
|
|
|
|
UIApplicationWillResignActiveNotification,
|
|
|
|
UIApplicationWillEnterForegroundNotification]) {
|
|
|
|
|
2015-05-31 21:33:59 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
2016-03-10 16:29:52 +00:00
|
|
|
selector:@selector(handleAppStateDidChange:)
|
2015-11-25 11:09:00 +00:00
|
|
|
name:name
|
2015-05-31 21:33:59 +00:00
|
|
|
object:nil];
|
2015-03-12 19:51:44 +00:00
|
|
|
}
|
2015-11-25 11:09:00 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(handleMemoryWarning)
|
|
|
|
name:UIApplicationDidReceiveMemoryWarningNotification
|
|
|
|
object:nil];
|
2015-03-12 19:51:44 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 19:57:08 +00:00
|
|
|
- (void)handleMemoryWarning
|
|
|
|
{
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"memoryWarning"
|
|
|
|
body:nil];
|
|
|
|
}
|
2015-03-12 19:51:44 +00:00
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - App Notification Methods
|
|
|
|
|
2016-03-10 16:29:52 +00:00
|
|
|
- (void)handleAppStateDidChange:(NSNotification *)notification
|
2015-03-12 19:51:44 +00:00
|
|
|
{
|
2016-03-10 16:29:52 +00:00
|
|
|
NSString *newState;
|
|
|
|
|
|
|
|
if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
|
|
|
|
newState = @"inactive";
|
|
|
|
} else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
|
2016-03-22 01:38:13 +00:00
|
|
|
newState = @"background";
|
2016-03-10 16:29:52 +00:00
|
|
|
} else {
|
|
|
|
newState = RCTCurrentAppBackgroundState();
|
|
|
|
}
|
|
|
|
|
2015-03-12 19:51:44 +00:00
|
|
|
if (![newState isEqualToString:_lastKnownState]) {
|
|
|
|
_lastKnownState = newState;
|
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"appStateDidChange"
|
|
|
|
body:@{@"app_state": _lastKnownState}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Public API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current background/foreground state of the app
|
|
|
|
*/
|
2015-04-09 15:46:53 +00:00
|
|
|
RCT_EXPORT_METHOD(getCurrentAppState:(RCTResponseSenderBlock)callback
|
|
|
|
error:(__unused RCTResponseSenderBlock)error)
|
2015-03-12 19:51:44 +00:00
|
|
|
{
|
|
|
|
callback(@[@{@"app_state": _lastKnownState}]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|