Skip to content

Commit

Permalink
[connection] add network status monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Jan 8, 2025
1 parent 66db6fe commit 502f564
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/me/capcom/smsgateway/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import healthModule
import me.capcom.smsgateway.data.dbModule
import me.capcom.smsgateway.helpers.SettingsHelper
import me.capcom.smsgateway.modules.connection.connectionModule
import me.capcom.smsgateway.modules.encryption.encryptionModule
import me.capcom.smsgateway.modules.events.eventBusModule
import me.capcom.smsgateway.modules.gateway.GatewayService
Expand Down Expand Up @@ -45,6 +46,7 @@ class App: Application() {
webhooksModule,
localserverModule,
pingModule,
connectionModule,
orchestratorModule,
)
}
Expand Down
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)
}
}
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"
13 changes: 13 additions & 0 deletions app/src/main/java/me/capcom/smsgateway/ui/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import kotlinx.coroutines.launch
import me.capcom.smsgateway.R
import me.capcom.smsgateway.databinding.FragmentSettingsBinding
import me.capcom.smsgateway.helpers.SettingsHelper
import me.capcom.smsgateway.modules.connection.ConnectionService
import me.capcom.smsgateway.modules.events.EventBus
import me.capcom.smsgateway.modules.gateway.GatewayService
import me.capcom.smsgateway.modules.gateway.GatewaySettings
Expand All @@ -47,6 +48,7 @@ class HomeFragment : Fragment() {
private val settingsHelper: SettingsHelper by inject()
private val localServerSettings: LocalServerSettings by inject()
private val gatewaySettings: GatewaySettings by inject()
private val connectionService: ConnectionService by inject()

private val events: EventBus by inject()

Expand Down Expand Up @@ -79,6 +81,7 @@ class HomeFragment : Fragment() {
binding.switchUseRemoteServer.setOnCheckedChangeListener { _, isChecked ->
gatewaySettings.enabled = isChecked
binding.layoutRemoteServer.isVisible = isChecked
binding.textConnectionStatus.isVisible = isChecked

restartRequiredNotification()
}
Expand Down Expand Up @@ -162,6 +165,16 @@ class HomeFragment : Fragment() {
stateLiveData.observe(viewLifecycleOwner) {
binding.buttonStart.isChecked = it
}

connectionService.status.observe(viewLifecycleOwner) {
binding.textConnectionStatus.apply {
isEnabled = it
text = when (it) {
true -> context.getString(R.string.internet_connection_available)
false -> context.getString(R.string.internet_connection_unavailable)
}
}
}
}

private fun makeCopyableLink(source: Spanned): Spanned {
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/color/connection_status_background.xml
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>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/rounded_background.xml
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>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
android:orientation="vertical"
tools:context=".ui.HomeFragment">

<TextView
android:id="@+id/textConnectionStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="@drawable/rounded_background"
android:backgroundTint="@color/connection_status_background"
android:enabled="true"
android:gravity="center"
android:padding="8dp"
android:textColor="@android:color/white"
android:visibility="gone"
tools:text="@string/internet_connection_available" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
Expand Down
16 changes: 9 additions & 7 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
<string name="dots">…</string>
<string name="enabled">Enabled</string>
<string name="encryption">Encryption</string>
<string name="failed_to_change_password">Failed to change password: %1$s</string>
<string name="if_sim_number_is_not_specified_use">If SIM number is not specified, use</string>
<string name="ignored_for_public_server">Ignored for public server</string>
<string name="information">Information</string>
<string name="internet_connection_available">Internet connection: available</string>
<string name="internet_connection_unavailable">Internet connection: unavailable</string>
<string name="interval_seconds">Interval (seconds)</string>
<string name="invalid_url">Invalid URL</string>
<string name="is_not_a_valid_port_must_be_between_1024_and_65535">%1$s is not a valid port. Must be between 1024 and 65535</string>
<string name="label_password">Password:</string>
<string name="label_username">Username:</string>
Expand All @@ -31,14 +36,18 @@
<string name="local_sms_gateway_notifications">Local SMS Gateway notifications</string>
<string name="logs">Logs</string>
<string name="maximum">Maximum</string>
<string name="messages">Messages…</string>
<string name="messages_count">Messages count</string>
<string name="messages_header">Messages</string>
<string name="minimum">Minimum</string>
<string name="more_settings">More settings…</string>
<string name="not_set">Not set</string>
<string name="notification_title">SMS Gateway</string>
<string name="online_status_at_the_cost_of_battery_life">Online status at the cost of battery life</string>
<string name="passphrase">Passphrase</string>
<string name="password">Password</string>
<string name="password_changed_successfully">Password changed successfully</string>
<string name="password_must_be_at_least_14_characters">Password must be at least 14 characters</string>
<string name="password_must_be_at_least_8_characters">Password must be at least 8 characters</string>
<string name="period">Period</string>
<string name="ping">Ping</string>
Expand Down Expand Up @@ -76,11 +85,4 @@
<string name="username_must_be_at_least_3_characters">Username must be at least 3 characters</string>
<string name="view">View</string>
<string name="webhooks">Webhooks</string>
<string name="invalid_url">Invalid URL</string>
<string name="not_set">Not set</string>
<string name="password_must_be_at_least_14_characters">Password must be at least 14 characters</string>
<string name="password_changed_successfully">Password changed successfully</string>
<string name="failed_to_change_password">Failed to change password: %1$s</string>
<string name="messages">Messages…</string>
<string name="if_sim_number_is_not_specified_use">If SIM number is not specified, use</string>
</resources>

0 comments on commit 502f564

Please sign in to comment.