-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[connection] add network status monitoring
- Loading branch information
Showing
8 changed files
with
123 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/src/main/java/me/capcom/smsgateway/modules/connection/ConnectionService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package me.capcom.smsgateway.modules.connection | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.os.Build | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import me.capcom.smsgateway.modules.logs.LogsService | ||
import me.capcom.smsgateway.modules.logs.db.LogEntry | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
|
||
class ConnectionService(context: Context) : KoinComponent { | ||
private val _status = MutableLiveData(false) | ||
val status: LiveData<Boolean> = _status | ||
|
||
private val connectivityManager = | ||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
private val logsService by inject<LogsService>() | ||
|
||
private val networkCallback = object : ConnectivityManager.NetworkCallback() { | ||
override fun onLost(network: Network) { | ||
logsService.insert( | ||
LogEntry.Priority.WARN, | ||
MODULE_NAME, | ||
"Internet connection lost" | ||
) | ||
_status.postValue(false) | ||
|
||
super.onLost(network) | ||
} | ||
|
||
override fun onCapabilitiesChanged( | ||
network: Network, | ||
networkCapabilities: NetworkCapabilities | ||
) { | ||
val hasInternet = | ||
networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
&& (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || networkCapabilities.hasCapability( | ||
NetworkCapabilities.NET_CAPABILITY_VALIDATED | ||
)) | ||
logsService.insert( | ||
LogEntry.Priority.INFO, | ||
MODULE_NAME, | ||
"Internet connection status: $hasInternet" | ||
) | ||
|
||
_status.postValue(hasInternet) | ||
|
||
super.onCapabilitiesChanged(network, networkCapabilities) | ||
} | ||
} | ||
|
||
init { | ||
connectivityManager.registerDefaultNetworkCallback(networkCallback) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/me/capcom/smsgateway/modules/connection/Module.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package me.capcom.smsgateway.modules.connection | ||
|
||
import org.koin.core.module.dsl.singleOf | ||
import org.koin.dsl.module | ||
|
||
val connectionModule = module { | ||
singleOf(::ConnectionService) | ||
} | ||
|
||
val MODULE_NAME = "connection" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<!-- State: Disconnected --> | ||
<item android:color="@android:color/holo_red_light" android:state_enabled="false" /> | ||
|
||
<!-- State: Connected --> | ||
<item android:color="@android:color/holo_green_light" android:state_enabled="true" /> | ||
|
||
<!-- Default State --> | ||
<item android:color="@android:color/darker_gray" /> | ||
</selector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<corners android:radius="8dp" /> | ||
<solid android:color="@android:color/white" /> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters