Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(android): #8733 #8800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions packages/core/connectivity/index.android.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { android as androidApp, getNativeApplication } from '../application';
import {android as androidApp, getNativeApplication} from '../application';

export enum connectionType {
none = 0,
wifi = 1,
Expand All @@ -20,7 +21,7 @@ function getConnectivityManager(): android.net.ConnectivityManager {
}

function getActiveNetworkInfo(): android.net.NetworkInfo {
let connectivityManager = getConnectivityManager();
const connectivityManager = getConnectivityManager();
if (!connectivityManager) {
return null;
}
Expand All @@ -29,13 +30,17 @@ function getActiveNetworkInfo(): android.net.NetworkInfo {
}

function getNetworkCapabilities() {
const connectivityManager = getConnectivityManager() as any;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const connectivityManager: any = getConnectivityManager();
const network = connectivityManager.getActiveNetwork();
const capabilities = connectivityManager.getNetworkCapabilities(network);
if (capabilities == null) {
return connectionType.none;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const NetworkCapabilities = (android as any).net.NetworkCapabilities;

if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
Expand Down Expand Up @@ -65,12 +70,12 @@ export function getConnectionType(): number {
if (android.os.Build.VERSION.SDK_INT >= 28) {
return getNetworkCapabilities();
} else {
let activeNetworkInfo = getActiveNetworkInfo();
const activeNetworkInfo = getActiveNetworkInfo();
if (!activeNetworkInfo || !activeNetworkInfo.isConnected()) {
return connectionType.none;
}

let type = activeNetworkInfo.getTypeName().toLowerCase();
const type = activeNetworkInfo.getTypeName().toLowerCase();
if (type.indexOf(wifi) !== -1) {
return connectionType.wifi;
}
Expand All @@ -96,43 +101,62 @@ export function getConnectionType(): number {
}

function startMonitoringLegacy(connectionTypeChangedCallback) {
let onReceiveCallback = function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
let newConnectionType = getConnectionType();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const onReceiveCallback = function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
const newConnectionType = getConnectionType();
connectionTypeChangedCallback(newConnectionType);
};
let zoneCallback = <any>zonedCallback(onReceiveCallback);
const zoneCallback = zonedCallback(onReceiveCallback);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
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();
if (manager) {
notifyCallback = () => {
let newConnectionType = getConnectionType();
let zoneCallback = <any>zonedCallback(connectionTypeChangedCallback);
const newConnectionType = getConnectionType();
const zoneCallback = zonedCallback(connectionTypeChangedCallback);
zoneCallback(newConnectionType);
};
const ConnectivityManager = android.net.ConnectivityManager;
if (!networkCallback) {
@NativeClass
class NetworkCallbackImpl extends ConnectivityManager.NetworkCallback {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onAvailable(network: android.net.Network) {
notifyCallback();
if (notifyCallback) {
notifyCallback();
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
onCapabilitiesChanged(network: android.net.Network, networkCapabilities: android.net.NetworkCapabilities) {
notifyCallback();
if (notifyCallback) {
notifyCallback();
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
onLost(network) {
notifyCallback();
if (notifyCallback) {
notifyCallback();
}
}

onUnavailable() {
notifyCallback();
if (notifyCallback) {
notifyCallback();
}
}
}

networkCallback = NetworkCallbackImpl;
}
callback = new networkCallback();
Expand All @@ -145,7 +169,9 @@ export function startMonitoring(connectionTypeChangedCallback: (newConnectionTyp

export function stopMonitoring(): void {
if (android.os.Build.VERSION.SDK_INT >= 28) {
const manager = getConnectivityManager() as any;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const manager = getConnectivityManager();
if (manager && callback) {
manager.unregisterNetworkCallback(callback);
notifyCallback = null;
Expand Down