Skip to content

Commit

Permalink
Moved database upload into separate service, and added progress notif…
Browse files Browse the repository at this point in the history
…ication
  • Loading branch information
Ixam97 committed Jul 18, 2024
1 parent 06cb402 commit 3baeb4f
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 137 deletions.
4 changes: 2 additions & 2 deletions automotive/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 29
targetSdkVersion 34
versionCode 245
versionName "0.27.0.0038"
versionCode 246
versionName "0.27.0.0039"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
3 changes: 3 additions & 0 deletions automotive/src/carapp/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
android:foregroundServiceType="location"
android:enabled="true"
android:exported="true"/>
<service android:name=".liveDataApi.uploadService.UploadService"
android:enabled="true"
android:exported="true"/>
</application>

</manifest>
3 changes: 3 additions & 0 deletions automotive/src/legacy/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
android:foregroundServiceType="location"
android:enabled="true"
android:exported="true"/>
<service android:name=".liveDataApi.uploadService.UploadService"
android:enabled="true"
android:exported="true"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class CarStatsViewer : Application() {
const val RESTART_NOTIFICATION_ID = 1
const val FOREGROUND_CHANNEL_ID = "ForegroundChannel"
const val FOREGROUND_NOTIFICATION_ID = 2
const val UPLOAD_CHANNEL_ID = "UploadChannel"
const val UPLOAD_NOTIFICATION_ID = 3

var screenshotBitmap = arrayListOf<Bitmap>()

Expand Down Expand Up @@ -325,9 +327,18 @@ class CarStatsViewer : Application() {
description = FOREGROUND_CHANNEL_ID
}

val uploadChannel = NotificationChannel(
UPLOAD_CHANNEL_ID,
UPLOAD_CHANNEL_ID,
NotificationManager.IMPORTANCE_DEFAULT
).apply {
description = FOREGROUND_CHANNEL_ID
}

notificationManager.createNotificationChannels(listOf(
restartChannel,
foregroundChannel
foregroundChannel,
uploadChannel
))
return notificationManager
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,16 @@ class LocalTripDataSource(
return tripDao.getAllChargingSessions()
}

override suspend fun getDrivingPointsSize(): Int {
return tripDao.getDrivingPointsSize()
}

override suspend fun getDrivingPointsChunk(startTimestamp: Long, chunkSize: Int): List<DrivingPoint> {
return tripDao.getDrivingPointsChunk(startTimestamp, chunkSize)
}

override suspend fun getChargingSessionsSize(): Int {
return tripDao.getChargingSessionsSize()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,14 @@ interface TripDao {
@Query("SELECT * FROM ChargingSession")
fun getAllChargingSessions(): List<ChargingSession>

@Query("SELECT COUNT(driving_point_epoch_time) FROM DrivingPoint")
fun getDrivingPointsSize(): Int

@Query("SELECT * FROM DrivingPoint WHERE driving_point_epoch_time > :startTimestamp ORDER BY driving_point_epoch_time ASC LIMIT :chunkSize")
fun getDrivingPointsChunk(startTimestamp: Long, chunkSize: Int): List<DrivingPoint>

@Query("SELECT COUNT(charging_session_id) FROM ChargingSession")
fun getChargingSessionsSize(): Int

}

Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ interface TripDataSource {
suspend fun getAllDrivingPoints(): List<DrivingPoint>

suspend fun getAllChargingSessions(): List<ChargingSession>

suspend fun getDrivingPointsSize(): Int

suspend fun getDrivingPointsChunk(startTimestamp: Long, chunkSize: Int): List<DrivingPoint>

suspend fun getChargingSessionsSize(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ class HttpLiveData (
})
}

suspend fun sendWithDrivingPoint(realTimeData: RealTimeData, drivingPoints: List<DrivingPoint>? = null, chargingSessions: List<ChargingSession>? = null): ConnectionStatus? {
suspend fun sendWithDrivingPoint(
realTimeData: RealTimeData,
drivingPoints: List<DrivingPoint>? = null,
chargingSessions: List<ChargingSession>? = null,
useBacklog: Boolean = true
): ConnectionStatus? {
// Wrap with mutex lock to prevent concurrent reads of the backlog.
mutex.withLock {
if (!AppPreferences(CarStatsViewer.appContext).httpLiveDataEnabled) {
Expand All @@ -177,30 +182,42 @@ class HttpLiveData (
}


if (CarStatsViewer.appPreferences.httpApiTelemetryType > 0) {
// Don't send driving points if disabled in settings
if (drivingPoints != null) {
if (!AppPreferences(CarStatsViewer.appContext).httpLiveDataLocation) {
drivingPoints?.forEach {drivingPoint ->
drivingPointBacklog.add(drivingPoint.copy(lat = null, lon = null, alt = null))
if (useBacklog) {
if (CarStatsViewer.appPreferences.httpApiTelemetryType > 0) {
// Don't send driving points if disabled in settings
if (drivingPoints != null) {
if (!AppPreferences(CarStatsViewer.appContext).httpLiveDataLocation) {
drivingPoints?.forEach { drivingPoint ->
drivingPointBacklog.add(
drivingPoint.copy(
lat = null,
lon = null,
alt = null
)
)
}
} else {
drivingPointBacklog.addAll(drivingPoints)
}
} else {
drivingPointBacklog.addAll(drivingPoints)
}
}
if (chargingSessions != null) {
if (!AppPreferences(CarStatsViewer.appContext).httpLiveDataLocation) {
chargingSessions?.forEach { chargingSession ->
chargingSessionBacklog.add(chargingSession.copy(lat = null, lon = null))
if (chargingSessions != null) {
if (!AppPreferences(CarStatsViewer.appContext).httpLiveDataLocation) {
chargingSessions?.forEach { chargingSession ->
chargingSessionBacklog.add(
chargingSession.copy(
lat = null,
lon = null
)
)
}
} else {
chargingSessionBacklog.addAll(chargingSessions)
}
}
else {
chargingSessionBacklog.addAll(chargingSessions)
}
} else {
drivingPointBacklog.clear()
chargingSessionBacklog.clear()
}
} else {
drivingPointBacklog.clear()
chargingSessionBacklog.clear()
}

if (!realTimeData.isInitialized()) return null
Expand All @@ -225,8 +242,15 @@ class HttpLiveData (
// ABRP debug
abrpPackage = if (CarStatsViewer.appPreferences.httpLiveDataSendABRPDataset) (CarStatsViewer.liveDataApis[0] as AbrpLiveData).lastPackage else null,

drivingPoints = if (drivingPointBacklog.size == 0) null else drivingPointBacklog,
chargingSessions = if (chargingSessionBacklog.size == 0) null else chargingSessionBacklog,
drivingPoints = if (!useBacklog) {
drivingPoints
} else if (drivingPointBacklog.size == 0){
null
} else drivingPointBacklog,
chargingSessions = if (!useBacklog) {
chargingSessions
} else if (chargingSessionBacklog.size == 0) null else chargingSessionBacklog
,

appVersion = BuildConfig.VERSION_NAME,
apiVersion = "2.1"
Expand All @@ -249,6 +273,8 @@ class HttpLiveData (
ConnectionStatus.ERROR
}

InAppLogger.v("Backlog sizes: driving points: ${drivingPointBacklog.size}, charging sessions: ${chargingSessionBacklog.size}")

return connectionStatus
}
}
Expand Down
Loading

0 comments on commit 3baeb4f

Please sign in to comment.