Skip to content

Commit

Permalink
feat: add new data state
Browse files Browse the repository at this point in the history
  • Loading branch information
luisbytes committed Mar 11, 2024
1 parent d3745dc commit bd0c601
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ getNetworkType(options?: { withBasicPermission?: boolean | undefined; } | undefi

#### TelephonyDataState

| Members | Value |
| ------------------ | --------------------------- |
| **`CONNECTED`** | <code>'CONNECTED'</code> |
| **`DISCONNECTED`** | <code>'DISCONNECTED'</code> |
| Members | Value |
| -------------------------- | ----------------------------------- |
| **`DISCONNECTED`** | <code>'DISCONNECTED'</code> |
| **`CONNECTING`** | <code>'CONNECTING'</code> |
| **`CONNECTED`** | <code>'CONNECTED'</code> |
| **`SUSPENDED`** | <code>'SUSPENDED'</code> |
| **`DISCONNECTING`** | <code>'DISCONNECTING'</code> |
| **`HANDOVER_IN_PROGRESS`** | <code>'HANDOVER_IN_PROGRESS'</code> |


#### TelephonySignalStrengthLevel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package dev.luisbytes.capacitor.telephony;

import static android.telephony.TelephonyManager.DATA_CONNECTED;
import static android.telephony.TelephonyManager.DATA_CONNECTING;
import static android.telephony.TelephonyManager.DATA_DISCONNECTED;
import static android.telephony.TelephonyManager.DATA_DISCONNECTING;
import static android.telephony.TelephonyManager.DATA_HANDOVER_IN_PROGRESS;
import static android.telephony.TelephonyManager.DATA_SUSPENDED;

import android.Manifest;
import android.annotation.SuppressLint;
Expand All @@ -9,9 +14,10 @@
import android.os.Build;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.getcapacitor.JSObject;

public class Telephony {
Expand Down Expand Up @@ -46,27 +52,26 @@ public String getDataNetworkType(@Nullable Boolean withBasicPermission) {
return "UNKNOWN";
}

@SuppressLint("MissingPermission")
final int dataNetworkType = this.telephonyManager.getDataNetworkType();
@SuppressLint("MissingPermission") final int dataNetworkType = this.telephonyManager.getDataNetworkType();

if (
dataNetworkType == TelephonyManager.NETWORK_TYPE_GPRS ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_EDGE ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_CDMA ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_1xRTT ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_IDEN
dataNetworkType == TelephonyManager.NETWORK_TYPE_GPRS ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_EDGE ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_CDMA ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_1xRTT ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_IDEN
) {
return "2G";
} else if (
dataNetworkType == TelephonyManager.NETWORK_TYPE_UMTS ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_EVDO_0 ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_EVDO_A ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_HSDPA ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_HSUPA ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_HSPA ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_EVDO_B ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_EHRPD ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_HSPAP
dataNetworkType == TelephonyManager.NETWORK_TYPE_UMTS ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_EVDO_0 ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_EVDO_A ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_HSDPA ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_HSUPA ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_HSPA ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_EVDO_B ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_EHRPD ||
dataNetworkType == TelephonyManager.NETWORK_TYPE_HSPAP
) {
return "3G";
} else if (dataNetworkType == TelephonyManager.NETWORK_TYPE_LTE) {
Expand Down Expand Up @@ -99,9 +104,14 @@ private String getSignalStrengthLevel() {
private String getDataState() {
int dataState = this.telephonyManager.getDataState();

if (dataState == DATA_DISCONNECTED) return "DISCONNECTED";
if (dataState == DATA_CONNECTING) return "CONNECTING";
if (dataState == DATA_CONNECTED) return "CONNECTED";
if (dataState == DATA_SUSPENDED) return "SUSPENDED";
if (dataState == DATA_DISCONNECTING) return "DISCONNECTING";
if (dataState == DATA_HANDOVER_IN_PROGRESS) return "HANDOVER_IN_PROGRESS";

return "DISCONNECTED";
return "UNKNOWN";
}

@RequiresApi(api = Build.VERSION_CODES.M)
Expand Down
7 changes: 6 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ export enum TelephonyNetworkType {
}

export enum TelephonyDataState {
CONNECTED = 'CONNECTED',
DISCONNECTED = 'DISCONNECTED',
CONNECTING = 'CONNECTING',
CONNECTED = 'CONNECTED',
SUSPENDED = 'SUSPENDED',
DISCONNECTING = 'DISCONNECTING',
HANDOVER_IN_PROGRESS = 'HANDOVER_IN_PROGRESS',
UNKNOWN = 'UNKNOWN'
}

export interface TelephonyInfo {
Expand Down

0 comments on commit bd0c601

Please sign in to comment.