-
Notifications
You must be signed in to change notification settings - Fork 12
[10/39] feat(app): add Compose sensor flows #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Foxpace
wants to merge
1
commit into
architecture/09-phone-recording
from
architecture/10-phone-sensors
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
293 changes: 293 additions & 0 deletions
293
app/src/main/java/com/motionapps/sensorbox/presentation/main/SensorDetailsScreen.kt
This file contains hidden or 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,293 @@ | ||
| package com.motionapps.sensorbox.presentation.main | ||
|
|
||
| import android.Manifest | ||
| import android.content.Context | ||
| import android.content.pm.PackageManager | ||
| import android.hardware.Sensor | ||
| import android.location.Location | ||
| import androidx.activity.compose.rememberLauncherForActivityResult | ||
| import androidx.activity.result.contract.ActivityResultContracts | ||
| import androidx.compose.foundation.layout.Arrangement | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.PaddingValues | ||
| import androidx.compose.foundation.layout.Row | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.lazy.LazyColumn | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.DisposableEffect | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.compose.ui.res.pluralStringResource | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.core.content.ContextCompat | ||
| import com.google.android.gms.location.LocationAvailability | ||
| import com.motionapps.sensorbox.R | ||
| import com.motionapps.sensorbox.domain.sensors.SensorDescriptor | ||
| import com.motionapps.sensorservices.handlers.GPSHandler | ||
|
|
||
| @Composable | ||
| fun SensorDetailsScreen(state: MainState, onBack: () -> Unit, onPreview: () -> Unit, modifier: Modifier = Modifier) { | ||
| val sensor = state.detailsSensorType?.let { type -> state.sensors.firstOrNull { it.type == type } } | ||
| val canPreview = state.detailsSensorType == null || sensor?.type != Sensor.TYPE_STEP_DETECTOR | ||
| val title = when { | ||
| state.detailsSensorType == null -> stringResource(R.string.gps) | ||
| sensor != null -> sensor.name | ||
| else -> stringResource(R.string.sensor_unavailable) | ||
| } | ||
| LazyColumn( | ||
| modifier = modifier.fillMaxSize(), | ||
| contentPadding = PaddingValues(horizontal = 20.dp, vertical = 24.dp), | ||
| verticalArrangement = Arrangement.spacedBy(12.dp), | ||
| ) { | ||
| item { SensorBoxTopAppBar(title, onBack) } | ||
| if (state.detailsSensorType == null) { | ||
| item { GpsDetails(state) } | ||
| } else if (sensor != null) { | ||
| item { HardwareSensorDetails(sensor) } | ||
| } | ||
| if ((state.detailsSensorType == null || sensor != null) && canPreview) { | ||
| item { | ||
| SensorBoxPrimaryButton( | ||
| label = stringResource(R.string.preview), | ||
| onClick = onPreview, | ||
| modifier = Modifier.fillMaxWidth(), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| private fun HardwareSensorDetails(sensor: SensorDescriptor) { | ||
| SensorBoxPanel { | ||
| Column(Modifier.fillMaxWidth().padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) { | ||
| val unit = sensorUnit(sensor.type) | ||
| val type = if (sensor.stringType.isBlank()) { | ||
| stringResource(R.string.detail_sensor_type_value, sensor.type) | ||
| } else { | ||
| sensor.stringType | ||
| } | ||
| DetailRow(stringResource(R.string.detail_name), sensor.name) | ||
| DetailRow(stringResource(R.string.detail_version), sensor.version.toString()) | ||
| DetailRow(stringResource(R.string.detail_vendor), sensor.vendor) | ||
| DetailRow(stringResource(R.string.detail_resolution, unit), sensor.resolution.toString()) | ||
| DetailRow(stringResource(R.string.detail_power), formatDecimal(sensor.power)) | ||
| DetailRow(stringResource(R.string.detail_maximum_range, unit), formatDecimal(sensor.maximumRange)) | ||
| DetailRow( | ||
| stringResource(R.string.detail_minimum_delay), | ||
| stringResource(R.string.detail_delay_value, sensor.minimumDelayMicros), | ||
| ) | ||
| DetailRow( | ||
| stringResource(R.string.detail_maximum_delay), | ||
| stringResource(R.string.detail_delay_value, sensor.maximumDelayMicros), | ||
| ) | ||
| DetailRow( | ||
| stringResource(R.string.detail_android_sensor_type), | ||
| type, | ||
| ) | ||
| DetailRow(stringResource(R.string.detail_reporting_mode), reportingModeLabel(sensor.reportingMode)) | ||
| DetailRow( | ||
| stringResource(R.string.detail_wakeup_sensor), | ||
| stringResource(if (sensor.isWakeUpSensor) R.string.yes else R.string.no), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| private fun GpsDetails(state: MainState) { | ||
| var permissionRevision by remember { mutableStateOf(0) } | ||
| val permissionRequest = rememberLauncherForActivityResult( | ||
| ActivityResultContracts.RequestMultiplePermissions(), | ||
| ) { | ||
| permissionRevision += 1 | ||
| } | ||
| val details = rememberGpsDetails( | ||
| intervalSeconds = state.preferences.gpsIntervalSeconds, | ||
| minimumDistanceMeters = state.preferences.gpsMinDistanceMeters, | ||
| permissionRevision = permissionRevision, | ||
| ) | ||
| val unavailableValue = stringResource(if (details.hasPermission) R.string.waiting else R.string.unavailable) | ||
| SensorBoxPanel { | ||
| Column(Modifier.fillMaxWidth().padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) { | ||
| GpsPermission(details.hasPermission) { | ||
| permissionRequest.launch( | ||
| arrayOf( | ||
| Manifest.permission.ACCESS_FINE_LOCATION, | ||
| Manifest.permission.ACCESS_COARSE_LOCATION, | ||
| ), | ||
| ) | ||
| } | ||
| GpsDetailRows(details, state, unavailableValue) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| private fun GpsDetailRows(details: GpsDetailsState, state: MainState, unavailableValue: String) { | ||
| DetailRow( | ||
| stringResource(R.string.detail_latitude), | ||
| details.location?.latitude?.toString() ?: unavailableValue, | ||
| ) | ||
| DetailRow( | ||
| stringResource(R.string.detail_longitude), | ||
| details.location?.longitude?.toString() ?: unavailableValue, | ||
| ) | ||
| DetailRow(stringResource(R.string.detail_altitude), localizedValue(details.location?.altitude, unavailableValue)) | ||
| DetailRow(stringResource(R.string.detail_accuracy), localizedValue(details.location?.accuracy, unavailableValue)) | ||
| DetailRow(stringResource(R.string.detail_speed), localizedValue(details.location?.speed, unavailableValue)) | ||
| DetailRow(stringResource(R.string.detail_bearing), localizedValue(details.location?.bearing, unavailableValue)) | ||
| DetailRow(stringResource(R.string.detail_provider), details.location?.provider ?: unavailableValue) | ||
| DetailRow(stringResource(R.string.detail_available), locationAvailabilityLabel(details.isAvailable)) | ||
| DetailRow( | ||
| stringResource(R.string.detail_update_interval), | ||
| pluralStringResource( | ||
| R.plurals.seconds_count, | ||
| state.preferences.gpsIntervalSeconds, | ||
| state.preferences.gpsIntervalSeconds, | ||
| ), | ||
| ) | ||
| DetailRow( | ||
| stringResource(R.string.detail_minimum_distance), | ||
| pluralStringResource( | ||
| R.plurals.meters_count, | ||
| state.preferences.gpsMinDistanceMeters, | ||
| state.preferences.gpsMinDistanceMeters, | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| private fun localizedValue(value: Number?, unavailableValue: String): String = | ||
| value?.let(::formatDecimal) ?: unavailableValue | ||
|
|
||
| @Composable | ||
| internal fun GpsPermission(hasPermission: Boolean, onRequest: () -> Unit) { | ||
| DetailRow( | ||
| stringResource(R.string.location_permission), | ||
| stringResource(if (hasPermission) R.string.granted else R.string.required), | ||
| ) | ||
| if (hasPermission) return | ||
| Text( | ||
| stringResource(R.string.location_permission_explanation), | ||
| color = MaterialTheme.colorScheme.error, | ||
| ) | ||
| SensorBoxSecondaryButton( | ||
| label = stringResource(R.string.grant_location_permission), | ||
| onClick = onRequest, | ||
| modifier = Modifier.fillMaxWidth(), | ||
| ) | ||
| } | ||
|
|
||
| @Composable | ||
| internal fun rememberGpsDetails( | ||
| intervalSeconds: Int, | ||
| minimumDistanceMeters: Int, | ||
| permissionRevision: Int, | ||
| ): GpsDetailsState { | ||
| val context = LocalContext.current | ||
| val hasPermission = remember(context, permissionRevision) { context.hasLocationPermission() } | ||
| var location by remember { mutableStateOf<Location?>(null) } | ||
| var isAvailable by remember { mutableStateOf<Boolean?>(null) } | ||
| val gpsHandler = remember { GPSHandler() } | ||
|
|
||
| DisposableEffect(gpsHandler, hasPermission, intervalSeconds, minimumDistanceMeters) { | ||
| var active = true | ||
| if (hasPermission) { | ||
| gpsHandler.configure(intervalSeconds, minimumDistanceMeters) | ||
| gpsHandler.addCallback( | ||
| context, | ||
| GpsDetailsCallback( | ||
| onLocation = { if (active && it != null) location = it }, | ||
| onAvailability = { if (active) isAvailable = it }, | ||
| ), | ||
| ) | ||
| } | ||
| onDispose { | ||
| active = false | ||
| gpsHandler.gpsOff() | ||
| } | ||
| } | ||
| return GpsDetailsState(hasPermission, location, isAvailable) | ||
| } | ||
|
|
||
| @Composable | ||
| internal fun DetailRow(label: String, value: String) { | ||
| Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.Top) { | ||
| Text(label, modifier = Modifier.weight(1f), color = MaterialTheme.colorScheme.onSurfaceVariant) | ||
| Text(value, modifier = Modifier.weight(1f), color = MaterialTheme.colorScheme.onSurface) | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| private fun reportingModeLabel(mode: Int): String = when (mode) { | ||
| 0 -> stringResource(R.string.reporting_continuous) | ||
| 1 -> stringResource(R.string.reporting_on_change) | ||
| 2 -> stringResource(R.string.reporting_one_shot) | ||
| 3 -> stringResource(R.string.reporting_special_trigger) | ||
| else -> stringResource(R.string.unknown_with_value, mode) | ||
| } | ||
|
|
||
| @Composable | ||
| internal fun sensorUnit(type: Int): String = when (type) { | ||
| Sensor.TYPE_ACCELEROMETER, Sensor.TYPE_GRAVITY, Sensor.TYPE_LINEAR_ACCELERATION -> | ||
| stringResource(R.string.unit_acceleration) | ||
|
|
||
| Sensor.TYPE_GYROSCOPE -> stringResource(R.string.unit_angular_velocity) | ||
|
|
||
| Sensor.TYPE_RELATIVE_HUMIDITY -> stringResource(R.string.unit_percent) | ||
|
|
||
| Sensor.TYPE_MAGNETIC_FIELD -> stringResource(R.string.unit_magnetic_field) | ||
|
|
||
| Sensor.TYPE_PROXIMITY -> stringResource(R.string.unit_centimeters) | ||
|
|
||
| Sensor.TYPE_PRESSURE -> stringResource(R.string.unit_pressure) | ||
|
|
||
| Sensor.TYPE_LIGHT -> stringResource(R.string.unit_lux) | ||
|
|
||
| Sensor.TYPE_AMBIENT_TEMPERATURE -> stringResource(R.string.unit_temperature) | ||
|
|
||
| Sensor.TYPE_STEP_COUNTER, Sensor.TYPE_STEP_DETECTOR -> stringResource(R.string.unit_steps) | ||
|
|
||
| Sensor.TYPE_HEART_RATE -> stringResource(R.string.unit_heart_rate) | ||
|
|
||
| else -> stringResource(R.string.unit_none) | ||
| } | ||
|
|
||
| internal fun formatDecimal(value: Number): String = "%.2f".format(value.toDouble()) | ||
|
|
||
| @Composable | ||
| private fun locationAvailabilityLabel(isAvailable: Boolean?): String = when (isAvailable) { | ||
| true -> stringResource(R.string.yes) | ||
| false -> stringResource(R.string.no) | ||
| null -> stringResource(R.string.unknown) | ||
| } | ||
|
|
||
| private fun Context.hasLocationPermission(): Boolean = | ||
| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == | ||
| PackageManager.PERMISSION_GRANTED || | ||
| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == | ||
| PackageManager.PERMISSION_GRANTED | ||
|
|
||
| internal data class GpsDetailsState(val hasPermission: Boolean, val location: Location?, val isAvailable: Boolean?) | ||
|
|
||
| private class GpsDetailsCallback( | ||
| private val onLocation: (Location?) -> Unit, | ||
| private val onAvailability: (Boolean?) -> Unit, | ||
| ) : GPSHandler.OnLocationChangedCallback { | ||
| override fun onLocationChanged(location: Location?) = onLocation(location) | ||
|
|
||
| override fun onLastLocationSuccess(location: Location?) = onLocation(location) | ||
|
|
||
| override fun onAvailabilityChanged(locationAvailability: LocationAvailability?) = | ||
| onAvailability(locationAvailability?.isLocationAvailable) | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/motionapps/sensorbox/presentation/main/SensorIconResource.kt
This file contains hidden or 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,29 @@ | ||
| package com.motionapps.sensorbox.presentation.main | ||
|
|
||
| import android.hardware.Sensor | ||
| import androidx.annotation.DrawableRes | ||
| import com.motionapps.sensorbox.R | ||
|
|
||
| @DrawableRes | ||
| fun sensorIconResource(sensorType: Int): Int = SENSOR_ICON_RESOURCES[sensorType] ?: R.drawable.ic_acceleration_icon | ||
|
|
||
| private val SENSOR_ICON_RESOURCES = mapOf( | ||
| Sensor.TYPE_ACCELEROMETER to R.drawable.ic_acceleration_icon, | ||
| Sensor.TYPE_LINEAR_ACCELERATION to R.drawable.ic_linear_acceleration_icon, | ||
| Sensor.TYPE_GRAVITY to R.drawable.ic_gravity_icon, | ||
| Sensor.TYPE_GYROSCOPE to R.drawable.ic_gyroscope_icon, | ||
| Sensor.TYPE_GYROSCOPE_UNCALIBRATED to R.drawable.ic_gyroscope_icon, | ||
| Sensor.TYPE_RELATIVE_HUMIDITY to R.drawable.ic_water_drop, | ||
| Sensor.TYPE_MAGNETIC_FIELD to R.drawable.ic_magnet, | ||
| Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED to R.drawable.ic_magnet, | ||
| Sensor.TYPE_PROXIMITY to R.drawable.ic_proximity, | ||
| Sensor.TYPE_ROTATION_VECTOR to R.drawable.ic_rotation_icon, | ||
| Sensor.TYPE_GAME_ROTATION_VECTOR to R.drawable.ic_rotation_icon, | ||
| Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR to R.drawable.ic_rotation_icon, | ||
| Sensor.TYPE_PRESSURE to R.drawable.ic_pressure, | ||
| Sensor.TYPE_LIGHT to R.drawable.ic_light, | ||
| Sensor.TYPE_AMBIENT_TEMPERATURE to R.drawable.ic_temperature, | ||
| Sensor.TYPE_STEP_COUNTER to R.drawable.ic_steps, | ||
| Sensor.TYPE_STEP_DETECTOR to R.drawable.ic_steps_detector, | ||
| Sensor.TYPE_HEART_RATE to R.drawable.ic_heart_rate, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.