Skip to content

Commit a3283e3

Browse files
committed
[connection] add network type health check
1 parent 5e6388f commit a3283e3

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

app/src/main/java/me/capcom/smsgateway/modules/connection/ConnectionService.kt

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package me.capcom.smsgateway.modules.connection
22

3+
import android.Manifest
34
import android.content.Context
5+
import android.content.pm.PackageManager
46
import android.net.ConnectivityManager
57
import android.net.Network
68
import android.net.NetworkCapabilities
79
import android.os.Build
10+
import android.telephony.TelephonyManager
11+
import androidx.core.app.ActivityCompat
812
import androidx.lifecycle.LiveData
913
import androidx.lifecycle.MutableLiveData
1014
import me.capcom.smsgateway.modules.health.domain.CheckResult
@@ -14,7 +18,9 @@ import me.capcom.smsgateway.modules.logs.db.LogEntry
1418
import org.koin.core.component.KoinComponent
1519
import org.koin.core.component.inject
1620

17-
class ConnectionService(context: Context) : KoinComponent {
21+
class ConnectionService(
22+
private val context: Context
23+
) : KoinComponent {
1824
private val _status = MutableLiveData(false)
1925
val status: LiveData<Boolean> = _status
2026

@@ -64,17 +70,78 @@ class ConnectionService(context: Context) : KoinComponent {
6470

6571
false -> Status.PASS
6672
}
73+
val value = when (status) {
74+
Status.PASS -> 1L
75+
else -> 0L
76+
}
6777

6878
return mapOf(
6979
"status" to CheckResult(
7080
status,
71-
0L,
72-
"n/a",
81+
value,
82+
"Yes/No",
7383
"Internet connection status"
84+
),
85+
"type" to CheckResult(
86+
when (networkType) {
87+
NetworkType.None -> Status.FAIL
88+
else -> Status.PASS
89+
},
90+
networkType.ordinal.toLong(),
91+
"type index",
92+
"Network type"
7493
)
7594
)
7695
}
7796

97+
val networkType: NetworkType
98+
get() {
99+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return NetworkType.Unknown
100+
101+
val nw = connectivityManager.activeNetwork ?: return NetworkType.None
102+
val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return NetworkType.None
103+
when {
104+
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> return NetworkType.WiFi
105+
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> return NetworkType.Ethernet
106+
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
107+
val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
108+
if (ActivityCompat.checkSelfPermission(
109+
context,
110+
Manifest.permission.READ_PHONE_STATE
111+
) != PackageManager.PERMISSION_GRANTED
112+
) {
113+
return NetworkType.MobileUnknown
114+
}
115+
when (tm.dataNetworkType) {
116+
TelephonyManager.NETWORK_TYPE_GPRS,
117+
TelephonyManager.NETWORK_TYPE_EDGE,
118+
TelephonyManager.NETWORK_TYPE_CDMA,
119+
TelephonyManager.NETWORK_TYPE_1xRTT,
120+
TelephonyManager.NETWORK_TYPE_IDEN,
121+
TelephonyManager.NETWORK_TYPE_GSM -> return NetworkType.Mobile2G
122+
123+
TelephonyManager.NETWORK_TYPE_UMTS,
124+
TelephonyManager.NETWORK_TYPE_EVDO_0,
125+
TelephonyManager.NETWORK_TYPE_EVDO_A,
126+
TelephonyManager.NETWORK_TYPE_HSDPA,
127+
TelephonyManager.NETWORK_TYPE_HSUPA,
128+
TelephonyManager.NETWORK_TYPE_HSPA,
129+
TelephonyManager.NETWORK_TYPE_EVDO_B,
130+
TelephonyManager.NETWORK_TYPE_EHRPD,
131+
TelephonyManager.NETWORK_TYPE_HSPAP,
132+
TelephonyManager.NETWORK_TYPE_TD_SCDMA -> return NetworkType.Mobile3G
133+
134+
TelephonyManager.NETWORK_TYPE_LTE,
135+
TelephonyManager.NETWORK_TYPE_IWLAN, 19 -> return NetworkType.Mobile4G
136+
137+
TelephonyManager.NETWORK_TYPE_NR -> return NetworkType.Mobile5G
138+
}
139+
}
140+
}
141+
142+
return NetworkType.Unknown
143+
}
144+
78145
init {
79146
connectivityManager.registerDefaultNetworkCallback(networkCallback)
80147
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package me.capcom.smsgateway.modules.connection
2+
3+
enum class NetworkType {
4+
None,
5+
Unknown,
6+
WiFi,
7+
Ethernet,
8+
MobileUnknown,
9+
Mobile2G,
10+
Mobile3G,
11+
Mobile4G,
12+
Mobile5G
13+
}

0 commit comments

Comments
 (0)