2015-03-16 20:57:43 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +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-16 20:57:43 +00:00
|
|
|
*
|
|
|
|
* @providesModule NetInfo
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
const Map = require('Map');
|
|
|
|
const NativeModules = require('NativeModules');
|
|
|
|
const Platform = require('Platform');
|
|
|
|
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
|
|
|
const RCTNetInfo = NativeModules.NetInfo;
|
2015-06-02 12:31:30 +00:00
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
const DEVICE_CONNECTIVITY_EVENT = 'networkStatusDidChange';
|
2015-03-16 20:57:43 +00:00
|
|
|
|
|
|
|
type ChangeEventName = $Enum<{
|
|
|
|
change: string;
|
|
|
|
}>;
|
|
|
|
|
2015-03-25 21:55:35 +00:00
|
|
|
type ReachabilityStateIOS = $Enum<{
|
|
|
|
cell: string;
|
|
|
|
none: string;
|
|
|
|
unknown: string;
|
|
|
|
wifi: string;
|
|
|
|
}>;
|
|
|
|
|
2015-06-02 12:31:30 +00:00
|
|
|
type ConnectivityStateAndroid = $Enum<{
|
|
|
|
NONE: string;
|
|
|
|
MOBILE: string;
|
|
|
|
WIFI: string;
|
|
|
|
MOBILE_MMS: string;
|
|
|
|
MOBILE_SUPL: string;
|
|
|
|
MOBILE_DUN: string;
|
|
|
|
MOBILE_HIPRI: string;
|
|
|
|
WIMAX: string;
|
|
|
|
BLUETOOTH: string;
|
|
|
|
DUMMY: string;
|
|
|
|
ETHERNET: string;
|
|
|
|
MOBILE_FOTA: string;
|
|
|
|
MOBILE_IMS: string;
|
|
|
|
MOBILE_CBS: string;
|
|
|
|
WIFI_P2P: string;
|
|
|
|
MOBILE_IA: string;
|
|
|
|
MOBILE_EMERGENCY: string;
|
|
|
|
PROXY: string;
|
|
|
|
VPN: string;
|
|
|
|
UNKNOWN: string;
|
|
|
|
}>;
|
2015-03-16 20:57:43 +00:00
|
|
|
|
2015-12-02 18:50:09 +00:00
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
const _subscriptions = new Map();
|
2015-12-02 18:50:09 +00:00
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
let _isConnected;
|
2015-12-02 18:50:09 +00:00
|
|
|
if (Platform.OS === 'ios') {
|
2015-12-09 22:51:43 +00:00
|
|
|
_isConnected = function(
|
|
|
|
reachability: ReachabilityStateIOS,
|
|
|
|
): bool {
|
2015-12-08 17:30:07 +00:00
|
|
|
return reachability !== 'none' && reachability !== 'unknown';
|
2015-12-02 18:50:09 +00:00
|
|
|
};
|
|
|
|
} else if (Platform.OS === 'android') {
|
2015-12-09 22:51:43 +00:00
|
|
|
_isConnected = function(
|
|
|
|
connectionType: ConnectivityStateAndroid,
|
|
|
|
): bool {
|
2015-12-02 18:50:09 +00:00
|
|
|
return connectionType !== 'NONE' && connectionType !== 'UNKNOWN';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
const _isConnectedSubscriptions = new Map();
|
2015-12-02 18:50:09 +00:00
|
|
|
|
2015-03-16 20:57:43 +00:00
|
|
|
/**
|
|
|
|
* NetInfo exposes info about online/offline status
|
|
|
|
*
|
|
|
|
* ```
|
2015-06-02 12:31:30 +00:00
|
|
|
* NetInfo.fetch().done((reach) => {
|
2015-03-16 20:57:43 +00:00
|
|
|
* console.log('Initial: ' + reach);
|
|
|
|
* });
|
2015-06-02 12:31:30 +00:00
|
|
|
* function handleFirstConnectivityChange(reach) {
|
2015-03-16 20:57:43 +00:00
|
|
|
* console.log('First change: ' + reach);
|
2015-06-02 12:31:30 +00:00
|
|
|
* NetInfo.removeEventListener(
|
2015-03-16 20:57:43 +00:00
|
|
|
* 'change',
|
2015-06-02 12:31:30 +00:00
|
|
|
* handleFirstConnectivityChange
|
2015-03-16 20:57:43 +00:00
|
|
|
* );
|
|
|
|
* }
|
2015-06-02 12:31:30 +00:00
|
|
|
* NetInfo.addEventListener(
|
2015-03-16 20:57:43 +00:00
|
|
|
* 'change',
|
2015-06-02 12:31:30 +00:00
|
|
|
* handleFirstConnectivityChange
|
2015-03-16 20:57:43 +00:00
|
|
|
* );
|
|
|
|
* ```
|
2015-03-25 21:55:35 +00:00
|
|
|
*
|
2015-06-02 12:31:30 +00:00
|
|
|
* ### IOS
|
|
|
|
*
|
|
|
|
* Asynchronously determine if the device is online and on a cellular network.
|
|
|
|
*
|
|
|
|
* - `none` - device is offline
|
|
|
|
* - `wifi` - device is online and connected via wifi, or is the iOS simulator
|
|
|
|
* - `cell` - device is connected via Edge, 3G, WiMax, or LTE
|
|
|
|
* - `unknown` - error case and the network status is unknown
|
|
|
|
*
|
|
|
|
* ### Android
|
|
|
|
*
|
2015-12-02 18:50:09 +00:00
|
|
|
* To request network info, you need to add the following line to your
|
|
|
|
* app's `AndroidManifest.xml`:
|
|
|
|
*
|
|
|
|
* `<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />`
|
2015-06-02 12:31:30 +00:00
|
|
|
* Asynchronously determine if the device is connected and details about that connection.
|
|
|
|
*
|
2015-12-03 15:02:33 +00:00
|
|
|
* Android Connectivity Types.
|
|
|
|
*
|
2015-06-02 12:31:30 +00:00
|
|
|
* - `NONE` - device is offline
|
|
|
|
* - `BLUETOOTH` - The Bluetooth data connection.
|
|
|
|
* - `DUMMY` - Dummy data connection.
|
|
|
|
* - `ETHERNET` - The Ethernet data connection.
|
|
|
|
* - `MOBILE` - The Mobile data connection.
|
|
|
|
* - `MOBILE_DUN` - A DUN-specific Mobile data connection.
|
|
|
|
* - `MOBILE_HIPRI` - A High Priority Mobile data connection.
|
|
|
|
* - `MOBILE_MMS` - An MMS-specific Mobile data connection.
|
|
|
|
* - `MOBILE_SUPL` - A SUPL-specific Mobile data connection.
|
|
|
|
* - `VPN` - A virtual network using one or more native bearers. Requires API Level 21
|
|
|
|
* - `WIFI` - The WIFI data connection.
|
|
|
|
* - `WIMAX` - The WiMAX data connection.
|
|
|
|
* - `UNKNOWN` - Unknown data connection.
|
2015-12-03 15:02:33 +00:00
|
|
|
*
|
2015-06-02 12:31:30 +00:00
|
|
|
* The rest ConnectivityStates are hidden by the Android API, but can be used if necessary.
|
|
|
|
*
|
2015-12-08 17:30:07 +00:00
|
|
|
* ### isConnectionExpensive
|
2015-06-02 12:31:30 +00:00
|
|
|
*
|
|
|
|
* Available on Android. Detect if the current active connection is metered or not. A network is
|
|
|
|
* classified as metered when the user is sensitive to heavy data usage on that connection due to
|
|
|
|
* monetary costs, data limitations or battery/performance issues.
|
|
|
|
*
|
2015-12-03 15:02:33 +00:00
|
|
|
* ```
|
2015-12-08 17:30:07 +00:00
|
|
|
* NetInfo.isConnectionExpensive((isConnectionExpensive) => {
|
|
|
|
* console.log('Connection is ' + (isConnectionExpensive ? 'Expensive' : 'Not Expensive'));
|
2015-06-02 12:31:30 +00:00
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
2015-03-25 21:55:35 +00:00
|
|
|
* ### isConnected
|
|
|
|
*
|
2015-04-21 23:37:54 +00:00
|
|
|
* Available on all platforms. Asynchronously fetch a boolean to determine
|
2015-03-25 21:55:35 +00:00
|
|
|
* internet connectivity.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* NetInfo.isConnected.fetch().done((isConnected) => {
|
|
|
|
* console.log('First, is ' + (isConnected ? 'online' : 'offline'));
|
|
|
|
* });
|
|
|
|
* function handleFirstConnectivityChange(isConnected) {
|
|
|
|
* console.log('Then, is ' + (isConnected ? 'online' : 'offline'));
|
|
|
|
* NetInfo.isConnected.removeEventListener(
|
|
|
|
* 'change',
|
|
|
|
* handleFirstConnectivityChange
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* NetInfo.isConnected.addEventListener(
|
|
|
|
* 'change',
|
|
|
|
* handleFirstConnectivityChange
|
|
|
|
* );
|
|
|
|
* ```
|
2015-03-16 20:57:43 +00:00
|
|
|
*/
|
2015-12-08 17:30:07 +00:00
|
|
|
const NetInfo = {
|
|
|
|
addEventListener(
|
2015-06-02 12:31:30 +00:00
|
|
|
eventName: ChangeEventName,
|
|
|
|
handler: Function
|
|
|
|
): void {
|
2015-12-08 17:30:07 +00:00
|
|
|
const listener = RCTDeviceEventEmitter.addListener(
|
2015-12-02 18:50:09 +00:00
|
|
|
DEVICE_CONNECTIVITY_EVENT,
|
2015-06-02 12:31:30 +00:00
|
|
|
(appStateData) => {
|
2015-12-02 18:50:09 +00:00
|
|
|
handler(appStateData.network_info);
|
2015-03-16 20:57:43 +00:00
|
|
|
}
|
2015-06-02 12:31:30 +00:00
|
|
|
);
|
2015-06-12 21:57:23 +00:00
|
|
|
_subscriptions.set(handler, listener);
|
2015-06-02 12:31:30 +00:00
|
|
|
},
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
removeEventListener(
|
2015-06-02 12:31:30 +00:00
|
|
|
eventName: ChangeEventName,
|
|
|
|
handler: Function
|
|
|
|
): void {
|
2015-12-08 17:30:07 +00:00
|
|
|
const listener = _subscriptions.get(handler);
|
2015-06-12 21:57:23 +00:00
|
|
|
if (!listener) {
|
2015-06-02 12:31:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-06-12 21:57:23 +00:00
|
|
|
listener.remove();
|
|
|
|
_subscriptions.delete(handler);
|
2015-06-02 12:31:30 +00:00
|
|
|
},
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
fetch(): Promise {
|
2015-06-02 12:31:30 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2015-12-02 18:50:09 +00:00
|
|
|
RCTNetInfo.getCurrentConnectivity(
|
2015-06-02 12:31:30 +00:00
|
|
|
function(resp) {
|
2015-10-12 02:07:35 +00:00
|
|
|
resolve(resp.network_info);
|
2015-06-02 12:31:30 +00:00
|
|
|
},
|
|
|
|
reject
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
2015-03-16 20:57:43 +00:00
|
|
|
|
2015-09-05 00:00:21 +00:00
|
|
|
isConnected: {
|
2015-12-08 17:30:07 +00:00
|
|
|
addEventListener(
|
2015-09-05 00:00:21 +00:00
|
|
|
eventName: ChangeEventName,
|
|
|
|
handler: Function
|
|
|
|
): void {
|
2015-12-08 17:30:07 +00:00
|
|
|
const listener = (connection) => {
|
2015-09-05 00:00:21 +00:00
|
|
|
handler(_isConnected(connection));
|
|
|
|
};
|
|
|
|
_isConnectedSubscriptions.set(handler, listener);
|
|
|
|
NetInfo.addEventListener(
|
|
|
|
eventName,
|
|
|
|
listener
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
removeEventListener(
|
2015-09-05 00:00:21 +00:00
|
|
|
eventName: ChangeEventName,
|
|
|
|
handler: Function
|
|
|
|
): void {
|
2015-12-08 17:30:07 +00:00
|
|
|
const listener = _isConnectedSubscriptions.get(handler);
|
2015-09-05 00:00:21 +00:00
|
|
|
NetInfo.removeEventListener(
|
|
|
|
eventName,
|
|
|
|
listener
|
|
|
|
);
|
|
|
|
_isConnectedSubscriptions.delete(handler);
|
|
|
|
},
|
2015-06-02 12:31:30 +00:00
|
|
|
|
2015-12-08 17:30:07 +00:00
|
|
|
fetch(): Promise {
|
2015-09-05 00:00:21 +00:00
|
|
|
return NetInfo.fetch().then(
|
|
|
|
(connection) => _isConnected(connection)
|
|
|
|
);
|
|
|
|
},
|
2015-06-02 12:31:30 +00:00
|
|
|
},
|
|
|
|
|
2015-12-08 20:02:17 +00:00
|
|
|
isConnectionExpensive(callback: (metered: ?boolean, error?: string) => void): void {
|
2015-12-08 17:30:07 +00:00
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
RCTNetInfo.isConnectionMetered((_isMetered) => {
|
|
|
|
callback(_isMetered);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// TODO t9296080 consider polyfill and more features later on
|
|
|
|
callback(null, "Unsupported");
|
|
|
|
}
|
|
|
|
},
|
2015-06-02 12:31:30 +00:00
|
|
|
};
|
|
|
|
|
2015-03-16 20:57:43 +00:00
|
|
|
module.exports = NetInfo;
|