-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[health] expose connection, battery and charging statuses
- Loading branch information
Showing
11 changed files
with
249 additions
and
13 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
app/src/main/java/me/capcom/smsgateway/domain/HealthResponse.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,16 @@ | ||
package me.capcom.smsgateway.domain | ||
|
||
import me.capcom.smsgateway.BuildConfig | ||
import me.capcom.smsgateway.modules.health.domain.CheckResult | ||
import me.capcom.smsgateway.modules.health.domain.HealthResult | ||
import me.capcom.smsgateway.modules.health.domain.Status | ||
|
||
class HealthResponse( | ||
healthResult: HealthResult, | ||
|
||
val version: String = BuildConfig.VERSION_NAME, | ||
val releaseId: Int = BuildConfig.VERSION_CODE, | ||
) { | ||
val status: Status = healthResult.status | ||
val checks: Map<String, CheckResult> = healthResult.checks | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/me/capcom/smsgateway/modules/connection/CellularNetworkType.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 | ||
|
||
enum class CellularNetworkType { | ||
None, | ||
Unknown, | ||
Mobile2G, | ||
Mobile3G, | ||
Mobile4G, | ||
Mobile5G, | ||
} |
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
10 changes: 10 additions & 0 deletions
10
app/src/main/java/me/capcom/smsgateway/modules/connection/TransportType.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 | ||
|
||
enum class TransportType( | ||
val value: Int | ||
) { | ||
Unknown(1), | ||
Cellular(2), | ||
WiFi(4), | ||
Ethernet(8), | ||
} |
11 changes: 10 additions & 1 deletion
11
app/src/main/java/me/capcom/smsgateway/modules/health/HealthService.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
5 changes: 4 additions & 1 deletion
5
app/src/main/java/me/capcom/smsgateway/modules/health/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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import me.capcom.smsgateway.modules.health.HealthService | ||
import me.capcom.smsgateway.modules.health.monitors.BatteryMonitor | ||
import org.koin.core.module.dsl.singleOf | ||
import org.koin.dsl.module | ||
|
||
val healthModule = module { | ||
single { HealthService(get()) } | ||
singleOf(::BatteryMonitor) | ||
singleOf(::HealthService) | ||
} |
63 changes: 63 additions & 0 deletions
63
app/src/main/java/me/capcom/smsgateway/modules/health/monitors/BatteryMonitor.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,63 @@ | ||
package me.capcom.smsgateway.modules.health.monitors | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.os.BatteryManager | ||
import me.capcom.smsgateway.modules.health.domain.CheckResult | ||
import me.capcom.smsgateway.modules.health.domain.Status | ||
|
||
class BatteryMonitor( | ||
private val context: Context | ||
) { | ||
fun healthCheck(): Map<String, CheckResult> { | ||
val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED) | ||
.let { ifilter -> | ||
context.registerReceiver(null, ifilter) | ||
} | ||
|
||
val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1 | ||
val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING | ||
|
||
// How are we charging? | ||
val chargePlug: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) ?: -1 | ||
val usbCharge: Boolean = chargePlug == BatteryManager.BATTERY_PLUGGED_USB | ||
val acCharge: Boolean = chargePlug == BatteryManager.BATTERY_PLUGGED_AC | ||
|
||
val batteryPct: Float? = batteryStatus?.let { intent -> | ||
val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) | ||
val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) | ||
level * 100 / scale.toFloat() | ||
} | ||
|
||
val levelStatus = batteryPct?.let { | ||
when { | ||
it < 10 -> Status.FAIL | ||
it < 25 -> Status.WARN | ||
else -> Status.PASS | ||
} | ||
} ?: Status.PASS | ||
|
||
return mapOf( | ||
"level" to CheckResult( | ||
levelStatus, | ||
batteryPct?.toLong() ?: 0L, | ||
"percent", | ||
"Battery level in percent" | ||
), | ||
"charging" to CheckResult( | ||
Status.PASS, | ||
when { | ||
acCharge -> 2L | ||
usbCharge -> 4L | ||
else -> 0L | ||
} + when (isCharging) { | ||
true -> 1L | ||
false -> 0L | ||
}, | ||
"flags", | ||
"Is the phone charging?" | ||
), | ||
) | ||
} | ||
} |
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
5 changes: 4 additions & 1 deletion
5
app/src/main/java/me/capcom/smsgateway/modules/ping/events/PingEvent.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
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