mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: default tabs
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { android as androidApp, getNativeApplication } from '../application';
|
||||
export enum connectionType {
|
||||
none = 0,
|
||||
wifi = 1,
|
||||
mobile = 2,
|
||||
ethernet = 3,
|
||||
bluetooth = 4,
|
||||
vpn = 5,
|
||||
none = 0,
|
||||
wifi = 1,
|
||||
mobile = 2,
|
||||
ethernet = 3,
|
||||
bluetooth = 4,
|
||||
vpn = 5,
|
||||
}
|
||||
|
||||
const wifi = 'wifi';
|
||||
@@ -16,140 +16,140 @@ const vpn = 'vpn';
|
||||
|
||||
// Get Connection Type
|
||||
function getConnectivityManager(): android.net.ConnectivityManager {
|
||||
return getNativeApplication().getApplicationContext().getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
|
||||
return getNativeApplication().getApplicationContext().getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
|
||||
}
|
||||
|
||||
function getActiveNetworkInfo(): android.net.NetworkInfo {
|
||||
let connectivityManager = getConnectivityManager();
|
||||
if (!connectivityManager) {
|
||||
return null;
|
||||
}
|
||||
let connectivityManager = getConnectivityManager();
|
||||
if (!connectivityManager) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return connectivityManager.getActiveNetworkInfo();
|
||||
return connectivityManager.getActiveNetworkInfo();
|
||||
}
|
||||
|
||||
function getNetworkCapabilities() {
|
||||
const connectivityManager = getConnectivityManager() as any;
|
||||
const network = connectivityManager.getActiveNetwork();
|
||||
const capabilities = connectivityManager.getNetworkCapabilities(network);
|
||||
if (capabilities == null) {
|
||||
return connectionType.none;
|
||||
}
|
||||
const connectivityManager = getConnectivityManager() as any;
|
||||
const network = connectivityManager.getActiveNetwork();
|
||||
const capabilities = connectivityManager.getNetworkCapabilities(network);
|
||||
if (capabilities == null) {
|
||||
return connectionType.none;
|
||||
}
|
||||
|
||||
const NetworkCapabilities = (android as any).net.NetworkCapabilities;
|
||||
const NetworkCapabilities = (android as any).net.NetworkCapabilities;
|
||||
|
||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
|
||||
return connectionType.wifi;
|
||||
}
|
||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
|
||||
return connectionType.wifi;
|
||||
}
|
||||
|
||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
||||
return connectionType.mobile;
|
||||
}
|
||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
||||
return connectionType.mobile;
|
||||
}
|
||||
|
||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
|
||||
return connectionType.ethernet;
|
||||
}
|
||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
|
||||
return connectionType.ethernet;
|
||||
}
|
||||
|
||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
|
||||
return connectionType.bluetooth;
|
||||
}
|
||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
|
||||
return connectionType.bluetooth;
|
||||
}
|
||||
|
||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
|
||||
return connectionType.vpn;
|
||||
}
|
||||
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
|
||||
return connectionType.vpn;
|
||||
}
|
||||
|
||||
return connectionType.none;
|
||||
return connectionType.none;
|
||||
}
|
||||
|
||||
export function getConnectionType(): number {
|
||||
if (android.os.Build.VERSION.SDK_INT >= 28) {
|
||||
return getNetworkCapabilities();
|
||||
} else {
|
||||
let activeNetworkInfo = getActiveNetworkInfo();
|
||||
if (!activeNetworkInfo || !activeNetworkInfo.isConnected()) {
|
||||
return connectionType.none;
|
||||
}
|
||||
if (android.os.Build.VERSION.SDK_INT >= 28) {
|
||||
return getNetworkCapabilities();
|
||||
} else {
|
||||
let activeNetworkInfo = getActiveNetworkInfo();
|
||||
if (!activeNetworkInfo || !activeNetworkInfo.isConnected()) {
|
||||
return connectionType.none;
|
||||
}
|
||||
|
||||
let type = activeNetworkInfo.getTypeName().toLowerCase();
|
||||
if (type.indexOf(wifi) !== -1) {
|
||||
return connectionType.wifi;
|
||||
}
|
||||
let type = activeNetworkInfo.getTypeName().toLowerCase();
|
||||
if (type.indexOf(wifi) !== -1) {
|
||||
return connectionType.wifi;
|
||||
}
|
||||
|
||||
if (type.indexOf(mobile) !== -1) {
|
||||
return connectionType.mobile;
|
||||
}
|
||||
if (type.indexOf(mobile) !== -1) {
|
||||
return connectionType.mobile;
|
||||
}
|
||||
|
||||
if (type.indexOf(ethernet) !== -1) {
|
||||
return connectionType.ethernet;
|
||||
}
|
||||
if (type.indexOf(ethernet) !== -1) {
|
||||
return connectionType.ethernet;
|
||||
}
|
||||
|
||||
if (type.indexOf(bluetooth) !== -1) {
|
||||
return connectionType.bluetooth;
|
||||
}
|
||||
if (type.indexOf(bluetooth) !== -1) {
|
||||
return connectionType.bluetooth;
|
||||
}
|
||||
|
||||
if (type.indexOf(vpn) !== -1) {
|
||||
return connectionType.vpn;
|
||||
}
|
||||
}
|
||||
if (type.indexOf(vpn) !== -1) {
|
||||
return connectionType.vpn;
|
||||
}
|
||||
}
|
||||
|
||||
return connectionType.none;
|
||||
return connectionType.none;
|
||||
}
|
||||
|
||||
function startMonitoringLegacy(connectionTypeChangedCallback) {
|
||||
let onReceiveCallback = function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
|
||||
let newConnectionType = getConnectionType();
|
||||
connectionTypeChangedCallback(newConnectionType);
|
||||
};
|
||||
let zoneCallback = <any>zonedCallback(onReceiveCallback);
|
||||
androidApp.registerBroadcastReceiver(android.net.ConnectivityManager.CONNECTIVITY_ACTION, zoneCallback);
|
||||
let onReceiveCallback = function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
|
||||
let newConnectionType = getConnectionType();
|
||||
connectionTypeChangedCallback(newConnectionType);
|
||||
};
|
||||
let zoneCallback = <any>zonedCallback(onReceiveCallback);
|
||||
androidApp.registerBroadcastReceiver(android.net.ConnectivityManager.CONNECTIVITY_ACTION, zoneCallback);
|
||||
}
|
||||
|
||||
let callback;
|
||||
let networkCallback;
|
||||
let notifyCallback;
|
||||
export function startMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void {
|
||||
if (android.os.Build.VERSION.SDK_INT >= 28) {
|
||||
const manager = getConnectivityManager() as any;
|
||||
if (manager) {
|
||||
notifyCallback = () => {
|
||||
let newConnectionType = getConnectionType();
|
||||
let zoneCallback = <any>zonedCallback(connectionTypeChangedCallback);
|
||||
zoneCallback(newConnectionType);
|
||||
};
|
||||
const ConnectivityManager = (android as any).net.ConnectivityManager;
|
||||
if (!networkCallback) {
|
||||
networkCallback = ConnectivityManager.NetworkCallback.extend({
|
||||
onAvailable(network) {
|
||||
notifyCallback();
|
||||
},
|
||||
onCapabilitiesChanged(network, networkCapabilities) {
|
||||
notifyCallback();
|
||||
},
|
||||
onLost(network) {
|
||||
notifyCallback();
|
||||
},
|
||||
onUnavailable() {
|
||||
notifyCallback();
|
||||
},
|
||||
});
|
||||
}
|
||||
callback = new networkCallback();
|
||||
manager.registerDefaultNetworkCallback(callback);
|
||||
}
|
||||
} else {
|
||||
startMonitoringLegacy(connectionTypeChangedCallback);
|
||||
}
|
||||
if (android.os.Build.VERSION.SDK_INT >= 28) {
|
||||
const manager = getConnectivityManager() as any;
|
||||
if (manager) {
|
||||
notifyCallback = () => {
|
||||
let newConnectionType = getConnectionType();
|
||||
let zoneCallback = <any>zonedCallback(connectionTypeChangedCallback);
|
||||
zoneCallback(newConnectionType);
|
||||
};
|
||||
const ConnectivityManager = (android as any).net.ConnectivityManager;
|
||||
if (!networkCallback) {
|
||||
networkCallback = ConnectivityManager.NetworkCallback.extend({
|
||||
onAvailable(network) {
|
||||
notifyCallback();
|
||||
},
|
||||
onCapabilitiesChanged(network, networkCapabilities) {
|
||||
notifyCallback();
|
||||
},
|
||||
onLost(network) {
|
||||
notifyCallback();
|
||||
},
|
||||
onUnavailable() {
|
||||
notifyCallback();
|
||||
},
|
||||
});
|
||||
}
|
||||
callback = new networkCallback();
|
||||
manager.registerDefaultNetworkCallback(callback);
|
||||
}
|
||||
} else {
|
||||
startMonitoringLegacy(connectionTypeChangedCallback);
|
||||
}
|
||||
}
|
||||
|
||||
export function stopMonitoring(): void {
|
||||
if (android.os.Build.VERSION.SDK_INT >= 28) {
|
||||
const manager = getConnectivityManager() as any;
|
||||
if (manager && callback) {
|
||||
manager.unregisterNetworkCallback(callback);
|
||||
notifyCallback = null;
|
||||
callback = null;
|
||||
}
|
||||
} else {
|
||||
androidApp.unregisterBroadcastReceiver(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
}
|
||||
if (android.os.Build.VERSION.SDK_INT >= 28) {
|
||||
const manager = getConnectivityManager() as any;
|
||||
if (manager && callback) {
|
||||
manager.unregisterNetworkCallback(callback);
|
||||
notifyCallback = null;
|
||||
callback = null;
|
||||
}
|
||||
} else {
|
||||
androidApp.unregisterBroadcastReceiver(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
}
|
||||
}
|
||||
|
||||
48
nativescript-core/connectivity/index.d.ts
vendored
48
nativescript-core/connectivity/index.d.ts
vendored
@@ -9,35 +9,35 @@ export function getConnectionType(): number;
|
||||
* Defines the different connection types.
|
||||
*/
|
||||
export enum connectionType {
|
||||
/**
|
||||
* Denotes no connection.
|
||||
*/
|
||||
none = 0,
|
||||
/**
|
||||
* Denotes no connection.
|
||||
*/
|
||||
none = 0,
|
||||
|
||||
/**
|
||||
* Denotes a WiFi connection.
|
||||
*/
|
||||
wifi = 1,
|
||||
/**
|
||||
* Denotes a WiFi connection.
|
||||
*/
|
||||
wifi = 1,
|
||||
|
||||
/**
|
||||
* Denotes a mobile connection, i.e. cellular network or WAN.
|
||||
*/
|
||||
mobile = 2,
|
||||
/**
|
||||
* Denotes a mobile connection, i.e. cellular network or WAN.
|
||||
*/
|
||||
mobile = 2,
|
||||
|
||||
/**
|
||||
* Denotes an ethernet connection
|
||||
*/
|
||||
ethernet = 3,
|
||||
/**
|
||||
* Denotes an ethernet connection
|
||||
*/
|
||||
ethernet = 3,
|
||||
|
||||
/**
|
||||
* Denotes a bluetooth connection
|
||||
*/
|
||||
bluetooth = 4,
|
||||
/**
|
||||
* Denotes a bluetooth connection
|
||||
*/
|
||||
bluetooth = 4,
|
||||
|
||||
/**
|
||||
* Denotes a vpn connection
|
||||
*/
|
||||
vpn = 5,
|
||||
/**
|
||||
* Denotes a vpn connection
|
||||
*/
|
||||
vpn = 5,
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,131 +1,131 @@
|
||||
export enum connectionType {
|
||||
none = 0,
|
||||
wifi = 1,
|
||||
mobile = 2,
|
||||
ethernet = 3,
|
||||
bluetooth = 4,
|
||||
vpn = 5,
|
||||
none = 0,
|
||||
wifi = 1,
|
||||
mobile = 2,
|
||||
ethernet = 3,
|
||||
bluetooth = 4,
|
||||
vpn = 5,
|
||||
}
|
||||
|
||||
// Get Connection Type
|
||||
declare const sockaddr;
|
||||
function _createReachability(host?: string): any {
|
||||
if (host) {
|
||||
return SCNetworkReachabilityCreateWithName(null, host);
|
||||
} else {
|
||||
const zeroAddress = new interop.Reference<sockaddr>(sockaddr, {
|
||||
sa_len: 16,
|
||||
sa_family: 2,
|
||||
});
|
||||
if (host) {
|
||||
return SCNetworkReachabilityCreateWithName(null, host);
|
||||
} else {
|
||||
const zeroAddress = new interop.Reference<sockaddr>(sockaddr, {
|
||||
sa_len: 16,
|
||||
sa_family: 2,
|
||||
});
|
||||
|
||||
return SCNetworkReachabilityCreateWithAddress(null, zeroAddress);
|
||||
}
|
||||
return SCNetworkReachabilityCreateWithAddress(null, zeroAddress);
|
||||
}
|
||||
}
|
||||
|
||||
function _getReachabilityFlags(host?: string): number {
|
||||
const reachability = _createReachability(host);
|
||||
const flagsRef = new interop.Reference<number>();
|
||||
const gotFlags = SCNetworkReachabilityGetFlags(reachability, flagsRef);
|
||||
if (!gotFlags) {
|
||||
return null;
|
||||
}
|
||||
const reachability = _createReachability(host);
|
||||
const flagsRef = new interop.Reference<number>();
|
||||
const gotFlags = SCNetworkReachabilityGetFlags(reachability, flagsRef);
|
||||
if (!gotFlags) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return flagsRef.value;
|
||||
return flagsRef.value;
|
||||
}
|
||||
|
||||
function _getConnectionType(host?: string): number {
|
||||
const flags = _getReachabilityFlags(host);
|
||||
const flags = _getReachabilityFlags(host);
|
||||
|
||||
return _getConnectionTypeFromFlags(flags);
|
||||
return _getConnectionTypeFromFlags(flags);
|
||||
}
|
||||
|
||||
function _getConnectionTypeFromFlags(flags: number): number {
|
||||
if (!flags) {
|
||||
return connectionType.none;
|
||||
}
|
||||
if (!flags) {
|
||||
return connectionType.none;
|
||||
}
|
||||
|
||||
const isReachable = flags & SCNetworkReachabilityFlags.kSCNetworkReachabilityFlagsReachable;
|
||||
const connectionRequired = flags & SCNetworkReachabilityFlags.kSCNetworkReachabilityFlagsConnectionRequired;
|
||||
if (!isReachable || connectionRequired) {
|
||||
return connectionType.none;
|
||||
}
|
||||
const isReachable = flags & SCNetworkReachabilityFlags.kSCNetworkReachabilityFlagsReachable;
|
||||
const connectionRequired = flags & SCNetworkReachabilityFlags.kSCNetworkReachabilityFlagsConnectionRequired;
|
||||
if (!isReachable || connectionRequired) {
|
||||
return connectionType.none;
|
||||
}
|
||||
|
||||
const isWWAN = flags & SCNetworkReachabilityFlags.kSCNetworkReachabilityFlagsIsWWAN;
|
||||
if (isWWAN) {
|
||||
return connectionType.mobile;
|
||||
}
|
||||
const isWWAN = flags & SCNetworkReachabilityFlags.kSCNetworkReachabilityFlagsIsWWAN;
|
||||
if (isWWAN) {
|
||||
return connectionType.mobile;
|
||||
}
|
||||
|
||||
let keys: any;
|
||||
if (typeof CFNetworkCopySystemProxySettings !== 'undefined') {
|
||||
const cfDict = CFNetworkCopySystemProxySettings();
|
||||
if (cfDict && cfDict.takeUnretainedValue) {
|
||||
const nsDict = cfDict.takeUnretainedValue();
|
||||
keys = nsDict.objectForKey('__SCOPED__');
|
||||
}
|
||||
}
|
||||
let keys: any;
|
||||
if (typeof CFNetworkCopySystemProxySettings !== 'undefined') {
|
||||
const cfDict = CFNetworkCopySystemProxySettings();
|
||||
if (cfDict && cfDict.takeUnretainedValue) {
|
||||
const nsDict = cfDict.takeUnretainedValue();
|
||||
keys = nsDict.objectForKey('__SCOPED__');
|
||||
}
|
||||
}
|
||||
|
||||
if (isVPNConnected(keys)) {
|
||||
return connectionType.vpn;
|
||||
}
|
||||
if (isVPNConnected(keys)) {
|
||||
return connectionType.vpn;
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
TODO try improving with CBCentralManager since toggling bluetooth
|
||||
with multiple connections fails to detect switch, require key added
|
||||
to Info.plist.
|
||||
*/
|
||||
if (isBluetoothConnected(keys)) {
|
||||
return connectionType.bluetooth;
|
||||
}
|
||||
if (isBluetoothConnected(keys)) {
|
||||
return connectionType.bluetooth;
|
||||
}
|
||||
|
||||
return connectionType.wifi;
|
||||
return connectionType.wifi;
|
||||
}
|
||||
|
||||
function isBluetoothConnected(keys) {
|
||||
if (!keys) {
|
||||
return false;
|
||||
}
|
||||
const allKeys = keys.allKeys;
|
||||
const size = allKeys.count;
|
||||
let isBlueTooth = false;
|
||||
for (let i = 0; i < size; i++) {
|
||||
const key = allKeys.objectAtIndex(i);
|
||||
if (key === 'en4') {
|
||||
isBlueTooth = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!keys) {
|
||||
return false;
|
||||
}
|
||||
const allKeys = keys.allKeys;
|
||||
const size = allKeys.count;
|
||||
let isBlueTooth = false;
|
||||
for (let i = 0; i < size; i++) {
|
||||
const key = allKeys.objectAtIndex(i);
|
||||
if (key === 'en4') {
|
||||
isBlueTooth = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return isBlueTooth;
|
||||
return isBlueTooth;
|
||||
}
|
||||
|
||||
function isVPNConnected(keys) {
|
||||
if (!keys) {
|
||||
return false;
|
||||
}
|
||||
const allKeys = keys.allKeys;
|
||||
const size = allKeys.count;
|
||||
let isVPN = false;
|
||||
for (let i = 0; i < size; i++) {
|
||||
const key = allKeys.objectAtIndex(i);
|
||||
if (key === 'tap' || key === 'tun' || key === 'ppp' || key === 'ipsec' || key === 'ipsec0' || key === 'utun1') {
|
||||
isVPN = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!keys) {
|
||||
return false;
|
||||
}
|
||||
const allKeys = keys.allKeys;
|
||||
const size = allKeys.count;
|
||||
let isVPN = false;
|
||||
for (let i = 0; i < size; i++) {
|
||||
const key = allKeys.objectAtIndex(i);
|
||||
if (key === 'tap' || key === 'tun' || key === 'ppp' || key === 'ipsec' || key === 'ipsec0' || key === 'utun1') {
|
||||
isVPN = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return isVPN;
|
||||
return isVPN;
|
||||
}
|
||||
|
||||
export function getConnectionType(): number {
|
||||
return _getConnectionType();
|
||||
return _getConnectionType();
|
||||
}
|
||||
|
||||
// Start/Stop Monitoring
|
||||
function _reachabilityCallback(target: any, flags: number, info: any) {
|
||||
if (_connectionTypeChangedCallback) {
|
||||
const newConnectionType = _getConnectionTypeFromFlags(flags);
|
||||
_connectionTypeChangedCallback(newConnectionType);
|
||||
}
|
||||
if (_connectionTypeChangedCallback) {
|
||||
const newConnectionType = _getConnectionTypeFromFlags(flags);
|
||||
_connectionTypeChangedCallback(newConnectionType);
|
||||
}
|
||||
}
|
||||
|
||||
const _reachabilityCallbackFunctionRef = new interop.FunctionReference(_reachabilityCallback);
|
||||
@@ -134,19 +134,19 @@ let _monitorReachabilityRef: any;
|
||||
let _connectionTypeChangedCallback: (newConnectionType: number) => void;
|
||||
|
||||
export function startMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void {
|
||||
if (!_monitorReachabilityRef) {
|
||||
_monitorReachabilityRef = _createReachability();
|
||||
_connectionTypeChangedCallback = <any>zonedCallback(connectionTypeChangedCallback);
|
||||
SCNetworkReachabilitySetCallback(_monitorReachabilityRef, _reachabilityCallbackFunctionRef, null);
|
||||
SCNetworkReachabilityScheduleWithRunLoop(_monitorReachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
||||
_connectionTypeChangedCallback(_getConnectionType());
|
||||
}
|
||||
if (!_monitorReachabilityRef) {
|
||||
_monitorReachabilityRef = _createReachability();
|
||||
_connectionTypeChangedCallback = <any>zonedCallback(connectionTypeChangedCallback);
|
||||
SCNetworkReachabilitySetCallback(_monitorReachabilityRef, _reachabilityCallbackFunctionRef, null);
|
||||
SCNetworkReachabilityScheduleWithRunLoop(_monitorReachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
||||
_connectionTypeChangedCallback(_getConnectionType());
|
||||
}
|
||||
}
|
||||
|
||||
export function stopMonitoring(): void {
|
||||
if (_monitorReachabilityRef) {
|
||||
SCNetworkReachabilityUnscheduleFromRunLoop(_monitorReachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
||||
_monitorReachabilityRef = undefined;
|
||||
_connectionTypeChangedCallback = undefined;
|
||||
}
|
||||
if (_monitorReachabilityRef) {
|
||||
SCNetworkReachabilityUnscheduleFromRunLoop(_monitorReachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
||||
_monitorReachabilityRef = undefined;
|
||||
_connectionTypeChangedCallback = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user