mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Added connectivity module.
This commit is contained in:
5
connectivity/connectivity-common.ts
Normal file
5
connectivity/connectivity-common.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export module connectionType {
|
||||
export var none = 0;
|
||||
export var wifi = 1;
|
||||
export var mobile = 2;
|
||||
}
|
||||
31
connectivity/connectivity.android.ts
Normal file
31
connectivity/connectivity.android.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import appModule = require("application");
|
||||
import common = require("connectivity/connectivity-common");
|
||||
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(common, exports);
|
||||
|
||||
var WIFI = "WIFI";
|
||||
var MOBILE = "MOBILE";
|
||||
|
||||
function getActiveNetworkInfo(): android.net.NetworkInfo {
|
||||
if (!appModule.android || !appModule.android.context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return appModule.android.context.getSystemService(android.content.Context.CONNECTIVITY_SERVICE).getActiveNetworkInfo();
|
||||
}
|
||||
|
||||
export function getConnectionType(): number {
|
||||
var activeNetworkInfo = getActiveNetworkInfo();
|
||||
if (!activeNetworkInfo || !activeNetworkInfo.isConnected()) {
|
||||
return common.connectionType.none;
|
||||
}
|
||||
|
||||
var connectionType = activeNetworkInfo.getTypeName();
|
||||
switch (connectionType) {
|
||||
case WIFI:
|
||||
return common.connectionType.wifi;
|
||||
case MOBILE:
|
||||
return common.connectionType.mobile;
|
||||
}
|
||||
}
|
||||
30
connectivity/connectivity.d.ts
vendored
Normal file
30
connectivity/connectivity.d.ts
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Contains connectivity utility methods.
|
||||
*/
|
||||
declare module "connectivity" {
|
||||
/**
|
||||
* Gets the type of connection.
|
||||
* Returns a value from the connectivity.connectionType enumeration.
|
||||
*/
|
||||
export function getConnectionType(): number;
|
||||
|
||||
/**
|
||||
* Defines the different connection types.
|
||||
*/
|
||||
export module connectionType {
|
||||
/**
|
||||
* Denotes no connection.
|
||||
*/
|
||||
export var none: number;
|
||||
|
||||
/**
|
||||
* Denotes a WiFi connection.
|
||||
*/
|
||||
export var wifi: number;
|
||||
|
||||
/**
|
||||
* Denotes a mobile connection, i.e. cellular network or WAN
|
||||
*/
|
||||
export var mobile: number;
|
||||
}
|
||||
}
|
||||
54
connectivity/connectivity.ios.ts
Normal file
54
connectivity/connectivity.ios.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import common = require("connectivity/connectivity-common");
|
||||
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(common, exports);
|
||||
|
||||
declare var sockaddr;
|
||||
|
||||
function getNetworkReachability(host?: string): any {
|
||||
if (host) {
|
||||
return SCNetworkReachabilityCreateWithName(null, host);
|
||||
}
|
||||
else {
|
||||
var zeroAddress = new interop.Reference(sockaddr, {
|
||||
sa_len: 16,
|
||||
sa_family: 2
|
||||
});
|
||||
return SCNetworkReachabilityCreateWithAddress(null, zeroAddress);
|
||||
}
|
||||
}
|
||||
|
||||
function getReachabilityFlags(host?: string): number {
|
||||
var reachability = getNetworkReachability(host);
|
||||
var flagsRef = new interop.Reference<number>();
|
||||
var gotFlags = SCNetworkReachabilityGetFlags(reachability, flagsRef);
|
||||
CFRelease(reachability);
|
||||
if (!gotFlags) {
|
||||
return null;
|
||||
}
|
||||
return flagsRef.value;
|
||||
}
|
||||
|
||||
function getConnectionTypeToHost(host?: string): number {
|
||||
var flags = getReachabilityFlags(host);
|
||||
if (!flags) {
|
||||
return common.connectionType.none;
|
||||
}
|
||||
|
||||
var isReachable = flags & kSCNetworkReachabilityFlagsReachable;
|
||||
var connectionRequired = flags & kSCNetworkReachabilityFlagsConnectionRequired;
|
||||
if (!isReachable || connectionRequired) {
|
||||
return common.connectionType.none;
|
||||
}
|
||||
|
||||
var isWWAN = flags & kSCNetworkReachabilityFlagsIsWWAN;
|
||||
if (isWWAN) {
|
||||
return common.connectionType.mobile;
|
||||
}
|
||||
|
||||
return common.connectionType.wifi;
|
||||
}
|
||||
|
||||
export function getConnectionType(): number {
|
||||
return getConnectionTypeToHost();
|
||||
}
|
||||
2
connectivity/package.json
Normal file
2
connectivity/package.json
Normal file
@@ -0,0 +1,2 @@
|
||||
{ "name" : "connectivity",
|
||||
"main" : "connectivity.js" }
|
||||
Reference in New Issue
Block a user