Skip to content

Commit

Permalink
Merge pull request #167 from Semper-Viventem/master
Browse files Browse the repository at this point in the history
Release 0.27.3-beta
  • Loading branch information
Semper-Viventem authored Jan 25, 2025
2 parents f1dbad2 + 1f57e84 commit b2fb7db
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ android {
minSdk = 29
targetSdk = 35

versionCode = 1708536363
versionName = "0.27.1-beta"
versionCode = 1708536365
versionName = "0.27.3-beta"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class PermissionHelper(

private var pending: (() -> Unit)? = null
private var permissionRequestTime: Long? = null
private val backgroundPermissionState = MutableStateFlow(checkBackgroundLocationPermition())
private val backgroundPermissionState = MutableStateFlow(backgroundLocationAllowed())

fun checkBlePermissions(
fun checkOrRequestPermission(
onRequestPermissions: (permissions: Array<String>, permissionRequestCode: Int, pendingFun: () -> Unit) -> Unit = ::requestPermissions,
permissions: Array<String> = BLE_PERMISSIONS,
permissionRequestCode: Int = PERMISSIONS_REQUEST_CODE,
Expand Down Expand Up @@ -82,6 +82,10 @@ class PermissionHelper(
)
}

fun blePermissionsAllowed(): Boolean {
return BLE_PERMISSIONS.all { checkPermission(it) }
}

fun checkAllPermissions(): Boolean {
return (BLE_PERMISSIONS + BACKGROUND_LOCATION).all { checkPermission(it) }
}
Expand All @@ -90,15 +94,15 @@ class PermissionHelper(
return backgroundPermissionState
}

fun checkBackgroundLocationPermition(): Boolean {
fun backgroundLocationAllowed(): Boolean {
return checkPermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
}

private fun fetchBackgroundLocationPermission() {
backgroundPermissionState.value = checkBackgroundLocationPermition()
backgroundPermissionState.value = backgroundLocationAllowed()
}

fun checkLocationPermission(): Boolean {
fun locationAllowed(): Boolean {
return checkPermission(Manifest.permission.ACCESS_FINE_LOCATION)
}

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/f/cking/software/domain/model/DeviceData.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package f.cking.software.domain.model

import android.content.Context
import f.cking.software.dateTimeStringFormatLocalized
import f.cking.software.getTimePeriodStr
import java.time.format.FormatStyle

data class DeviceData(
val address: String,
Expand All @@ -25,10 +27,18 @@ data class DeviceData(
return (System.currentTimeMillis() - firstDetectTimeMs).getTimePeriodStr(context)
}

fun firstDetectionExactTime(context: Context, formatStyle: FormatStyle = FormatStyle.SHORT): String {
return firstDetectTimeMs.dateTimeStringFormatLocalized(formatStyle)
}

fun lastDetectionPeriod(context: Context): String {
return (System.currentTimeMillis() - lastDetectTimeMs).getTimePeriodStr(context)
}

fun lastDetectionExactTime(context: Context, formatStyle: FormatStyle = FormatStyle.SHORT): String {
return lastDetectTimeMs.dateTimeStringFormatLocalized(formatStyle)
}

fun hasBeenSeenTimeAgo(): Long {
return System.currentTimeMillis() - lastDetectTimeMs
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/f/cking/software/service/BgScanService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class BgScanService : Service() {
ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE or ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION,
)

permissionHelper.checkBlePermissions(
permissionHelper.checkOrRequestPermission(
onRequestPermissions = { _, _, _ ->
reportError(IllegalStateException("BLE Service is started but permissins are not granted"))
stopSelf()
Expand Down Expand Up @@ -170,7 +170,7 @@ class BgScanService : Service() {
return when {
!locationProvider.isLocationAvailable() -> handleLocationDisabled()
!bleScannerHelper.isBluetoothEnabled() -> handleBleIsTurnedOffError()
permissionHelper.checkBackgroundLocationPermition() -> handleBackgroundLocationRestricted()
!permissionHelper.backgroundLocationAllowed() -> handleBackgroundLocationRestricted()
else -> NotificationsHelper.ServiceNotificationContent.NoDataYet
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BootBroadcastReceiver : BroadcastReceiver() {

private fun tryToRunService(context: Context) {
if (settingsRepository.getRunOnStartup()) {
if (permissionHelper.checkAllPermissions()) {
if (permissionHelper.blePermissionsAllowed()) {
try {
BgScanService.start(context)
} catch (error: Exception) {
Expand All @@ -38,13 +38,23 @@ class BootBroadcastReceiver : BroadcastReceiver() {
title = "[Launch on system startup error]: ${error.message ?: error::class.java}",
stackTrace = error.stackTraceToString(),
)
scope.launch {
saveReportInteractor.execute(report)
}
report(report)
}
} else {
Timber.e("Not all permissions granted, can't start service from the boot receiver")
report(
JournalEntry.Report.Error(
title = "[Launch on system startup error]: Not all permissions granted",
stackTrace = IllegalStateException("Not all permissions granted").stackTraceToString()
)
)
}
}
}

private fun report(report: JournalEntry.Report.Error) {
Timber.e(report.stackTrace)
scope.launch {
saveReportInteractor.execute(report)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class BackgroundLocationRequestViewModel(
}

fun grantPermission() {
permissionHelper.checkBlePermissions {
permissionHelper.checkBlePermissions(permissions = PermissionHelper.BACKGROUND_LOCATION) {
permissionHelper.checkOrRequestPermission {
permissionHelper.checkOrRequestPermission(permissions = PermissionHelper.BACKGROUND_LOCATION) {
onBack()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import org.osmdroid.views.overlay.simplefastpoint.SimpleFastPointOverlay
import org.osmdroid.views.overlay.simplefastpoint.SimpleFastPointOverlayOptions
import org.osmdroid.views.overlay.simplefastpoint.SimplePointTheme
import timber.log.Timber
import java.time.format.FormatStyle

@OptIn(ExperimentalMaterial3Api::class)
object DeviceDetailsScreen {
Expand Down Expand Up @@ -284,18 +285,14 @@ object DeviceDetailsScreen {
Spacer(Modifier.width(4.dp))
Text(text = deviceData.detectCount.toString())
}
Spacer(modifier = Modifier.height(8.dp))

Text(text = stringResource(R.string.device_details_first_detection), fontWeight = FontWeight.Bold)
Text(
text = stringResource(R.string.time_ago, deviceData.firstDetectionPeriod(LocalContext.current))
)
Spacer(modifier = Modifier.height(8.dp))
Text(text = stringResource(R.string.device_details_first_detection), fontWeight = FontWeight.Bold)
Text(text = deviceData.firstDetectionExactTime(LocalContext.current, formatStyle = FormatStyle.MEDIUM))

Spacer(modifier = Modifier.height(8.dp))
Text(text = stringResource(R.string.device_details_last_detection), fontWeight = FontWeight.Bold)
Text(
text = stringResource(R.string.time_ago, deviceData.lastDetectionPeriod(LocalContext.current))
)
Text(text = deviceData.lastDetectionExactTime(LocalContext.current, formatStyle = FormatStyle.MEDIUM))
Spacer(modifier = Modifier.height(16.dp))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class DeviceDetailsViewModel(
}

private fun observeLocation() {
permissionHelper.checkBlePermissions {
permissionHelper.checkOrRequestPermission {
viewModelScope.launch {
locationProvider.fetchOnce()
locationProvider.observeLocation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class MainViewModel(
}

private fun checkPermissions(granted: () -> Unit) {
permissionHelper.checkBlePermissions {
permissionHelper.checkOrRequestPermission {
permissionHelper.checkDozeModePermission()
granted.invoke()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ object SelectLocationScreen {
mapView.controller.setZoom(MapConfig.DEFAULT_MAP_ZOOM)
} else {
scope.launch {
if (permissionHelper.checkLocationPermission()) {
if (permissionHelper.locationAllowed()) {
locationProvider.observeLocation()
.filterNotNull()
.take(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class SettingsViewModel(
if (locationProvider.isActive()) {
locationProvider.stopLocationListening()
locationProvider.startLocationFetching()
} else if (permissionHelper.checkLocationPermission()) {
} else if (permissionHelper.locationAllowed()) {
locationProvider.fetchOnce()
}
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/f/cking/software/utils/ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.regex.PatternSyntaxException
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
Expand Down Expand Up @@ -66,6 +67,11 @@ fun Long.dateTimeStringFormat(format: String, timeZone: ZoneId = ZoneId.systemDe
.format(DateTimeFormatter.ofPattern(format))
}

fun Long.dateTimeStringFormatLocalized(formatStyle: FormatStyle = FormatStyle.SHORT, timeZone: ZoneId = ZoneId.systemDefault()): String {
return LocalDateTime.of(toLocalDate(timeZone), toLocalTime(timeZone))
.format(DateTimeFormatter.ofLocalizedDateTime(formatStyle))
}

fun LocalTime.dateTimeFormat(format: String): String {
return format(DateTimeFormatter.ofPattern(format))
}
Expand Down

0 comments on commit b2fb7db

Please sign in to comment.