2016-03-28 10:07:40 +08:00
|
|
|
|
import {Plugin, Cordova, CordovaProperty} from './plugin';
|
2016-06-03 22:05:07 +08:00
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
2016-03-28 10:07:40 +08:00
|
|
|
|
|
2016-04-30 11:56:49 +08:00
|
|
|
|
declare var navigator: any;
|
2016-03-28 10:07:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @name Network
|
|
|
|
|
* @description
|
|
|
|
|
* Requires Cordova plugin: cordova-plugin-network-information. For more info, please see the [Network plugin docs](https://github.com/apache/cordova-plugin-network-information).
|
|
|
|
|
*
|
|
|
|
|
* @usage
|
|
|
|
|
* ```js
|
|
|
|
|
* import {Network, Connection} from 'ionic-native';
|
|
|
|
|
*
|
|
|
|
|
* // watch network for a disconnect
|
|
|
|
|
* let disconnectSubscription = Network.onDisconnect().subscribe(() => {
|
|
|
|
|
* console.log('network was disconnected :-( ')
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* // stop disconnect watch
|
|
|
|
|
* disconnectSubscription.unsubscribe();
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* // watch network for a connection
|
|
|
|
|
* let connectSubscription = Network.onConnect().subscribe(() => {
|
2016-03-28 12:17:07 +08:00
|
|
|
|
* console.log('network connected!');
*
|
|
|
|
|
* // We just got a connection but we need to wait briefly
|
2016-05-21 04:59:18 +08:00
|
|
|
|
*
// before we determine the connection type. Might need to wait
|
2016-03-28 12:17:07 +08:00
|
|
|
|
* // prior to doing any api requests as well.
|
2016-03-28 10:07:40 +08:00
|
|
|
|
* setTimeout(() => {
|
|
|
|
|
* console.log(Network.connection);
|
|
|
|
|
* if (Network.connection === Connection.WIFI) {
|
|
|
|
|
* console.log('we got a wifi connection, woohoo!');
|
|
|
|
|
* }
|
2016-05-01 03:58:46 +08:00
|
|
|
|
* }, 3000);
|
2016-03-28 10:07:40 +08:00
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* // stop connect watch
|
|
|
|
|
* connectSubscription.unsubscribe();
|
|
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
@Plugin({
|
|
|
|
|
plugin: 'cordova-plugin-network-information',
|
|
|
|
|
repo: 'https://github.com/apache/cordova-plugin-network-information',
|
|
|
|
|
platforms: ['Amazon Fire OS', 'iOS', 'Android', 'BlackBerry 10', 'Windows Phone 7', 'Windows Phone 8', 'Windows', 'Firefox OS', 'Browser'],
|
|
|
|
|
pluginRef: 'navigator.connection'
|
|
|
|
|
})
|
|
|
|
|
export class Network {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the network connection type
|
|
|
|
|
*/
|
|
|
|
|
@CordovaProperty
|
2016-04-30 11:56:49 +08:00
|
|
|
|
static get connection(): Connection { return navigator.connection.type; }
|
2016-03-28 10:07:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2016-04-30 11:56:49 +08:00
|
|
|
|
* Get notified when the device goes offline
|
2016-03-28 10:07:40 +08:00
|
|
|
|
* @returns {Observable<any>} Returns an observable.
|
|
|
|
|
*/
|
|
|
|
|
@Cordova({
|
|
|
|
|
eventObservable: true,
|
|
|
|
|
event: 'offline'
|
|
|
|
|
})
|
2016-04-30 11:56:49 +08:00
|
|
|
|
static onDisconnect(): Observable<any> { return; }
|
2016-03-28 10:07:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2016-04-30 11:56:49 +08:00
|
|
|
|
* Get notified when the device goes online
|
2016-03-28 10:07:40 +08:00
|
|
|
|
* @returns {Observable<any>} Returns an observable.
|
|
|
|
|
*/
|
|
|
|
|
@Cordova({
|
|
|
|
|
eventObservable: true,
|
|
|
|
|
event: 'online'
|
|
|
|
|
})
|
2016-04-30 11:56:49 +08:00
|
|
|
|
static onConnect(): Observable<any> { return; }
|
2016-03-28 10:07:40 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-09 03:19:13 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2016-03-28 10:07:40 +08:00
|
|
|
|
export class Connection {
|
2016-04-30 11:56:49 +08:00
|
|
|
|
static get UNKNOWN() { return 'unknown'; }
|
|
|
|
|
static get ETHERNET() { return 'ethernet'; }
|
|
|
|
|
static get WIFI() { return 'wifi'; }
|
|
|
|
|
static get CELL_2G() { return '2g'; }
|
|
|
|
|
static get CELL_3G() { return '3g'; }
|
|
|
|
|
static get CELL_4G() { return '4g'; }
|
|
|
|
|
static get CELL() { return 'cellular'; }
|
|
|
|
|
static get NONE() { return 'none'; }
|
2016-03-28 10:07:40 +08:00
|
|
|
|
}
|