Skip to content

Commit

Permalink
Create new interface for diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Sep 2, 2023
1 parent 7ff97ab commit 4ba7f44
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.kylecorry.trail_sense.diagnostics.diagnostic2

import com.kylecorry.andromeda.core.coroutines.onDefault
import kotlinx.coroutines.Job
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout

class AggregateDiagnostic(
private val diagnostics: List<Diagnostic>,
private val progressChanged: (Int, Int) -> Unit = { _, _ -> }
) : Diagnostic {
override suspend fun scan(): List<DiagnosticCode2> = onDefault {
// Run diagnostics in parallel
val codes = mutableSetOf<DiagnosticCode2>()
val codeLock = Any()
val progressLock = Any()
val jobs = mutableListOf<Job>()
var progress = 0
for (diagnostic in diagnostics) {
jobs.add(
launch {
withTimeout(10000) {
val results = diagnostic.scan()
synchronized(codeLock) {
codes.addAll(results)
}
synchronized(progressLock) {
progress++
progressChanged(progress, diagnostics.size)
}
}
}
)
}

jobs.joinAll()

codes.toList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kylecorry.trail_sense.diagnostics.diagnostic2

interface Diagnostic {
suspend fun scan(): List<DiagnosticCode2>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package com.kylecorry.trail_sense.diagnostics.diagnostic2

import com.kylecorry.trail_sense.shared.database.Identifiable

enum class DiagnosticCode2(override val id: Long) : Identifiable {
/**
* There is no compass sensor
*/
NoCompass(1),

/**
* The compass sensor is poor quality
*/
PoorCompass(2),

/**
* There is no GPS
*/
NoGPS(3),

/**
* The GPS is poor quality
*/
PoorGPS(4),

/**
* The fine location permission is not granted
*/
NoFineLocationPermission(5),

/**
* The background location permission is not granted
*/
NoBackgroundLocationPermission(6),

/**
* The GPS is using a manual location
*/
ManualLocation(7),

/**
* The GPS is using a manual location, but it is unset
*/
UnsetManualLocation(8),

/**
* The GPS has timed out
*/
TimedOutGPS(9),

/**
* The GPS exists, but is disabled
*/
DisabledGPS(10),

/**
* There is no barometer sensor
*/
NoBarometer(11),

/**
* The barometer sensor is poor quality
*/
PoorBarometer(12),

/**
* The altimeter is using a manual elevation
*/
ManualElevation(13),

/**
* There is no pedometer sensor
*/
NoPedometer(14),

/**
* The activity recognition permission is not granted
*/
NoActivityRecognitionPermission(15),

/**
* There is no gyroscope sensor
*/
NoGyroscope(16),

/**
* The gyroscope sensor is poor quality
*/
PoorGyroscope(17),

/**
* There is no camera
*/
NoCamera(18),

/**
* The camera permission is not granted
*/
NoCameraPermission(19),

/**
* The exact alarm permission is not granted
*/
NoScheduleExactAlarmPermission(20),

/**
* The power saving mode is on
*/
PowerSavingMode(21),

/**
* The battery usage is restricted
*/
BatteryUsageRestricted(22),

/**
* The battery health is poor
*/
BatteryHealthPoor(23),

/**
* There is no flashlight
*/
NoFlashlight(24),

/**
* There is no light sensor
*/
NoLightSensor(25),

/**
* All notifications are blocked
*/
NotificationsBlocked(26),

/**
* The flashlight notification is blocked
*/
FlashlightNotificationsBlocked(27),

/**
* The sunset notification is blocked
*/
SunsetAlertsBlocked(28),

/**
* The storm notification is blocked
*/
StormAlertsBlocked(29),

/**
* The daily forecast notification is blocked
*/
DailyForecastNotificationsBlocked(30),

/**
* The pedometer notification is blocked
*/
PedometerNotificationsBlocked(31),

/**
* The weather notification is blocked
*/
WeatherNotificationsBlocked(32),

/**
* The astronomy notification is blocked
*/
AstronomyAlertsBlocked(33),

/**
* The clock sync notification is blocked
*/
ClockSyncNotificationBlocked(34),

/**
* The water boil notification is blocked
*/
WaterBoilNotificationBlocked(35),

/**
* The white noise notification is blocked
*/
WhiteNoiseNotificationBlocked(36),

/**
* The distance alert notification is blocked
*/
DistanceAlertsBlocked(37),

/**
* The backtrack notification is blocked
*/
BacktrackNotificationBlocked(38),

/**
* The weather monitor is disabled
*/
WeatherMonitorDisabled(39),

/**
* The sunset alerts are disabled
*/
SunsetAlertsDisabled(40),

/**
* The pedometer is disabled
*/
PedometerDisabled(41),

/**
* Backtrack is disabled
*/
BacktrackDisabled(42),

/**
* There is no accelerometer
*/
NoAccelerometer(43),

/**
* The accelerometer is poor quality
*/
PoorAccelerometer(44),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kylecorry.trail_sense.diagnostics.diagnostic2.notifications

import android.content.Context
import com.kylecorry.andromeda.permissions.Permissions
import com.kylecorry.andromeda.permissions.SpecialPermission
import com.kylecorry.trail_sense.diagnostics.diagnostic2.Diagnostic
import com.kylecorry.trail_sense.diagnostics.diagnostic2.DiagnosticCode2

class AlarmDiagnostic(private val context: Context) : Diagnostic {
override suspend fun scan(): List<DiagnosticCode2> {
if (!Permissions.hasPermission(context, SpecialPermission.SCHEDULE_EXACT_ALARMS)) {
return listOf(DiagnosticCode2.NoScheduleExactAlarmPermission)
}

return emptyList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.kylecorry.trail_sense.diagnostics.diagnostic2.sensors

import android.content.Context
import android.hardware.Sensor
import com.kylecorry.andromeda.sense.Sensors
import com.kylecorry.andromeda.sense.accelerometer.Accelerometer
import com.kylecorry.andromeda.sense.accelerometer.IAccelerometer
import com.kylecorry.trail_sense.diagnostics.diagnostic2.DiagnosticCode2
import com.kylecorry.trail_sense.shared.sensors.SensorService

class AccelerometerDiagnostic(context: Context) : BaseSensorDiagnostic<IAccelerometer>(context) {

override fun getNoSensorCode(): DiagnosticCode2 {
return DiagnosticCode2.NoAccelerometer
}

override fun getPoorSensorCode(): DiagnosticCode2 {
return DiagnosticCode2.PoorAccelerometer
}

override fun getSensor(context: Context): IAccelerometer? {
return if (Sensors.hasSensor(context, Sensor.TYPE_ACCELEROMETER)) {
Accelerometer(context, SensorService.MOTION_SENSOR_DELAY)
} else {
null
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.kylecorry.trail_sense.diagnostics.diagnostic2.sensors

import android.content.Context
import android.hardware.Sensor
import com.kylecorry.andromeda.sense.Sensors
import com.kylecorry.andromeda.sense.barometer.IBarometer
import com.kylecorry.trail_sense.diagnostics.diagnostic2.DiagnosticCode2
import com.kylecorry.trail_sense.shared.sensors.SensorService

class BarometerDiagnostic(context: Context) : BaseSensorDiagnostic<IBarometer>(context) {

override fun getNoSensorCode(): DiagnosticCode2 {
return DiagnosticCode2.NoBarometer
}

override fun getPoorSensorCode(): DiagnosticCode2 {
return DiagnosticCode2.PoorBarometer
}

override fun getSensor(context: Context): IBarometer? {
val sensors = SensorService(context)
return if (Sensors.hasSensor(context, Sensor.TYPE_PRESSURE)) {
sensors.getBarometer()
} else {
null
}
}

}
Loading

0 comments on commit 4ba7f44

Please sign in to comment.