Skip to content
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

feat(network): implement additional network information for android and web (#1806) #1809

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions network/README.md
Expand Up @@ -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`** | <code>boolean</code> | Whether there is an active connection or not. | 1.0.0 |
| **`connectionType`** | <code><a href="#connectiontype">ConnectionType</a></code> | 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`** | <code>boolean</code> | Whether there is an active connection or not. | 1.0.0 |
| **`connectionType`** | <code><a href="#connectiontype">ConnectionType</a></code> | The type of network connection currently in use. If there is no active network connection, `connectionType` will be `'none'`. | 1.0.0 |
| **`upstreamInKbps`** | <code>number</code> | The upstream bandwidth in kbps. If the platform does not provide the value will be undefined. | TBD |
| **`downstreamInKbps`** | <code>number</code> | The downstream bandwidth in kbps. If the platform does not provide the value will be undefined. | TBD |
| **`signalStrength`** | <code>number</code> | The signal strength as number. If the platform does not provide the value will be undefined. | TBD |


#### PluginListenerHandle
Expand Down
Expand Up @@ -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)) {
Expand Down
Expand Up @@ -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;
}
}
Expand Up @@ -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;
}
25 changes: 25 additions & 0 deletions network/src/definitions.ts
Expand Up @@ -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;
}

/**
Expand Down
13 changes: 9 additions & 4 deletions network/src/web.ts
Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
Expand Down