From 6a2abab1480c4b89556ce115e50cedb4a511d5a3 Mon Sep 17 00:00:00 2001 From: Nicolas Schmid Date: Sun, 24 Sep 2023 13:18:38 +0200 Subject: [PATCH] feat(network): implement additional network information for android and web (#1806) adds downstreamInKbps for web adds downstreamInKbps, upstreamInKbps and signalStrength for android --- network/README.md | 11 ++++--- .../capacitorjs/plugins/network/Network.java | 5 ++++ .../plugins/network/NetworkPlugin.java | 3 ++ .../plugins/network/NetworkStatus.java | 3 ++ network/src/definitions.ts | 29 +++++++++++++++++-- network/src/web.ts | 13 ++++++--- 6 files changed, 54 insertions(+), 10 deletions(-) diff --git a/network/README.md b/network/README.md index 189aa8b8e..ebfefa8d2 100644 --- a/network/README.md +++ b/network/README.md @@ -95,10 +95,13 @@ Remove all listeners (including the network status changes) for this plugin. Represents the state and type of the network connection. -| Prop | Type | Description | Since | -| -------------------- | --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ----- | -| **`connected`** | boolean | Whether there is an active connection or not. | 1.0.0 | -| **`connectionType`** | ConnectionType | The type of network connection currently in use. If there is no active network connection, `connectionType` will be `'none'`. | 1.0.0 | +| Prop | Type | Description | Since | +| ---------------------- | --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ----- | +| **`connected`** | boolean | Whether there is an active connection or not. | 1.0.0 | +| **`connectionType`** | ConnectionType | The type of network connection currently in use. If there is no active network connection, `connectionType` will be `'none'`. | 1.0.0 | +| **`upstreamInKbps`** | number | The upstream bandwidth in kbps. If the platform does not provide the value will be undefined. | TBD | +| **`downstreamInKbps`** | number | The downstream bandwidth in kbps. If the platform does not provide the value will be undefined. | TBD | +| **`signalStrength`** | number | The signal strength as number. If the platform does not provide the value will be undefined. | TBD | #### PluginListenerHandle diff --git a/network/android/src/main/java/com/capacitorjs/plugins/network/Network.java b/network/android/src/main/java/com/capacitorjs/plugins/network/Network.java index decfd702f..53a06dbbe 100644 --- a/network/android/src/main/java/com/capacitorjs/plugins/network/Network.java +++ b/network/android/src/main/java/com/capacitorjs/plugins/network/Network.java @@ -99,6 +99,11 @@ public NetworkStatus getNetworkStatus() { networkStatus.connected = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); + networkStatus.downstreamInKbps = capabilities.getLinkDownstreamBandwidthKbps(); + networkStatus.upstreamInKbps = capabilities.getLinkUpstreamBandwidthKbps(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + networkStatus.signalStrength = capabilities.getSignalStrength(); + } if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { networkStatus.connectionType = NetworkStatus.ConnectionType.WIFI; } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { diff --git a/network/android/src/main/java/com/capacitorjs/plugins/network/NetworkPlugin.java b/network/android/src/main/java/com/capacitorjs/plugins/network/NetworkPlugin.java index 623b39f75..7f1d4c7a0 100644 --- a/network/android/src/main/java/com/capacitorjs/plugins/network/NetworkPlugin.java +++ b/network/android/src/main/java/com/capacitorjs/plugins/network/NetworkPlugin.java @@ -97,6 +97,9 @@ private JSObject parseNetworkStatus(NetworkStatus networkStatus) { JSObject jsObject = new JSObject(); jsObject.put("connected", networkStatus.connected); jsObject.put("connectionType", networkStatus.connectionType.getConnectionType()); + jsObject.put("upstreamInKbps", networkStatus.upstreamInKbps); + jsObject.put("downstreamInKbps", networkStatus.downstreamInKbps); + jsObject.put("signalStrength", networkStatus.signalStrength); return jsObject; } } diff --git a/network/android/src/main/java/com/capacitorjs/plugins/network/NetworkStatus.java b/network/android/src/main/java/com/capacitorjs/plugins/network/NetworkStatus.java index ac0a7a121..fd64eb81f 100644 --- a/network/android/src/main/java/com/capacitorjs/plugins/network/NetworkStatus.java +++ b/network/android/src/main/java/com/capacitorjs/plugins/network/NetworkStatus.java @@ -21,4 +21,7 @@ public String getConnectionType() { public boolean connected = false; public ConnectionType connectionType = ConnectionType.NONE; + public int upstreamInKbps; + public int downstreamInKbps; + public int signalStrength; } diff --git a/network/src/definitions.ts b/network/src/definitions.ts index 2355a76b0..0025c121d 100644 --- a/network/src/definitions.ts +++ b/network/src/definitions.ts @@ -14,8 +14,8 @@ export interface NetworkPlugin { * @since 1.0.0 */ addListener( - eventName: 'networkStatusChange', - listenerFunc: ConnectionStatusChangeListener, + eventName: 'networkStatusChange', + listenerFunc: ConnectionStatusChangeListener, ): Promise & PluginListenerHandle; /** @@ -47,6 +47,31 @@ export interface ConnectionStatus { * @since 1.0.0 */ connectionType: ConnectionType; + + /** + * The upstream bandwidth in kbps. + * + * If the platform does not provide the value will be undefined. + * + * @since TBD + */ + upstreamInKbps?: number; + /** + * The downstream bandwidth in kbps. + * + * If the platform does not provide the value will be undefined. + * + * @since TBD + */ + downstreamInKbps?: number; + /** + * The signal strength as number. + * + * If the platform does not provide the value will be undefined. + * + * @since TBD + */ + signalStrength?: number; } /** diff --git a/network/src/web.ts b/network/src/web.ts index 6e0fe9285..d38d7ec37 100644 --- a/network/src/web.ts +++ b/network/src/web.ts @@ -14,11 +14,14 @@ declare global { } } +function getConnection() { + return window.navigator.connection || + window.navigator.mozConnection || + window.navigator.webkitConnection; +} + function translatedConnection(): ConnectionType { - const connection = - window.navigator.connection || - window.navigator.mozConnection || - window.navigator.webkitConnection; + const connection = getConnection(); let result: ConnectionType = 'unknown'; const type = connection ? connection.type || connection.effectiveType : null; if (type && typeof type === 'string') { @@ -75,9 +78,11 @@ export class NetworkWeb extends WebPlugin implements NetworkPlugin { const connected = window.navigator.onLine; const connectionType = translatedConnection(); + const downlink = getConnection()?.downlink; const status: ConnectionStatus = { connected, connectionType: connected ? connectionType : 'none', + downstreamInKbps: downlink ? downlink * 1024 : undefined, }; return status;