mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-20 07:26:11 +08:00

* chore(tslint): fix tslint config & errors * chore(tslint): enable double quotes, whitespace, and arrow-return-shorthand rules and fix errors
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import { getNativeApplication, android as androidApp } from "../application";
|
|
|
|
export enum connectionType {
|
|
none = 0,
|
|
wifi = 1,
|
|
mobile = 2,
|
|
ethernet = 3
|
|
}
|
|
|
|
const wifi = "wifi";
|
|
const mobile = "mobile";
|
|
const ethernet = "ethernet";
|
|
|
|
// Get Connection Type
|
|
function getConnectivityManager(): android.net.ConnectivityManager {
|
|
return getNativeApplication().getApplicationContext().getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
|
|
}
|
|
|
|
function getActiveNetworkInfo(): android.net.NetworkInfo {
|
|
let connectivityManager = getConnectivityManager();
|
|
if (!connectivityManager) {
|
|
return null;
|
|
}
|
|
|
|
return connectivityManager.getActiveNetworkInfo();
|
|
}
|
|
|
|
export function getConnectionType(): number {
|
|
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;
|
|
}
|
|
|
|
return connectionType.none;
|
|
}
|
|
|
|
export function startMonitoring(connectionTypeChangedCallback: (newConnectionType: number) => void): void {
|
|
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);
|
|
}
|
|
|
|
export function stopMonitoring(): void {
|
|
androidApp.unregisterBroadcastReceiver(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
|
|
}
|