From 82a2282361b225c7b85df4c022be0ab41f3a8ccc Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Mon, 6 Jul 2020 18:55:26 -0700 Subject: [PATCH] chore: format --- .../connectivity/index.android.ts | 231 +++++++++--------- nativescript-core/connectivity/index.d.ts | 16 +- nativescript-core/connectivity/index.ios.ts | 130 +++++----- 3 files changed, 183 insertions(+), 194 deletions(-) diff --git a/nativescript-core/connectivity/index.android.ts b/nativescript-core/connectivity/index.android.ts index 6e7d8ad94..48a04bd3a 100644 --- a/nativescript-core/connectivity/index.android.ts +++ b/nativescript-core/connectivity/index.android.ts @@ -1,18 +1,18 @@ -import { android as androidApp, getNativeApplication } from "../application"; +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"; -const mobile = "mobile"; -const ethernet = "ethernet"; -const bluetooth = "bluetooth"; -const vpn = "vpn"; +const wifi = 'wifi'; +const mobile = 'mobile'; +const ethernet = 'ethernet'; +const bluetooth = 'bluetooth'; +const vpn = 'vpn'; // Get Connection Type function getConnectivityManager(): android.net.ConnectivityManager { @@ -29,128 +29,127 @@ function getActiveNetworkInfo(): android.net.NetworkInfo { } function getNetworkCapabilities() { - 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; - - if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { - return connectionType.wifi; - } - - if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { - return connectionType.mobile; - } - - if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { - return connectionType.ethernet; - } - - if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) { - return connectionType.bluetooth; - } - - if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) { - return connectionType.vpn; - } - + 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; + + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { + return connectionType.wifi; + } + + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { + return connectionType.mobile; + } + + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { + return connectionType.ethernet; + } + + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) { + return connectionType.bluetooth; + } + + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) { + return connectionType.vpn; + } + + 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; - } - - let type = activeNetworkInfo.getTypeName().toLowerCase(); - if (type.indexOf(wifi) !== -1) { - return connectionType.wifi; - } - - if (type.indexOf(mobile) !== -1) { - return connectionType.mobile; - } - - if (type.indexOf(ethernet) !== -1) { - return connectionType.ethernet; - } - - if (type.indexOf(bluetooth) !== -1) { - return connectionType.bluetooth; - } - - if (type.indexOf(vpn) !== -1) { - return connectionType.vpn; - } + if (android.os.Build.VERSION.SDK_INT >= 28) { + return getNetworkCapabilities(); + } else { + let activeNetworkInfo = getActiveNetworkInfo(); + if (!activeNetworkInfo || !activeNetworkInfo.isConnected()) { + return connectionType.none; } - return connectionType.none; + let type = activeNetworkInfo.getTypeName().toLowerCase(); + if (type.indexOf(wifi) !== -1) { + return connectionType.wifi; + } + + if (type.indexOf(mobile) !== -1) { + return connectionType.mobile; + } + + if (type.indexOf(ethernet) !== -1) { + return connectionType.ethernet; + } + + if (type.indexOf(bluetooth) !== -1) { + return connectionType.bluetooth; + } + + if (type.indexOf(vpn) !== -1) { + return connectionType.vpn; + } + } + + 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 = 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 = 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 = 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 = 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); + } } diff --git a/nativescript-core/connectivity/index.d.ts b/nativescript-core/connectivity/index.d.ts index 1e59e9184..16d6b96f2 100644 --- a/nativescript-core/connectivity/index.d.ts +++ b/nativescript-core/connectivity/index.d.ts @@ -29,15 +29,15 @@ export enum connectionType { */ 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, } /** diff --git a/nativescript-core/connectivity/index.ios.ts b/nativescript-core/connectivity/index.ios.ts index bf2e1061e..40f9ff93c 100644 --- a/nativescript-core/connectivity/index.ios.ts +++ b/nativescript-core/connectivity/index.ios.ts @@ -1,10 +1,10 @@ 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 @@ -40,84 +40,75 @@ function _getConnectionType(host?: string): number { } 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; + } - const cfDict = CFNetworkCopySystemProxySettings(); - const nsDict = cfDict.takeUnretainedValue(); - const keys = nsDict.objectForKey("__SCOPED__"); + const cfDict = CFNetworkCopySystemProxySettings(); + const nsDict = cfDict.takeUnretainedValue(); + const 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; - } +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; } + } - 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 { @@ -126,11 +117,10 @@ export function getConnectionType(): number { // 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);