diff --git a/build.gradle b/build.gradle
index b92423e..70a2f6a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -20,7 +20,7 @@
* Top-level build file where you can add configuration options common to all sub-projects/modules.
*
* @author Armin Schnabel
- * @version 3.2.2
+ * @version 3.3.0
* @since 1.0.0
*/
@@ -39,6 +39,11 @@ buildscript {
}
}
+plugins {
+ // To generate Proto DataStore
+ id 'com.google.protobuf' version '0.8.19' apply false // Maybe keep in sync with other usages
+}
+
ext {
// This libraries version
cyfaceEnergySettingsVersion = "0.0.0" // Automatically overwritten by CI
@@ -56,9 +61,11 @@ ext {
androidxAnnotationVersion = "1.6.0"
androidxAppCompatVersion = "1.6.1"
androidPreferencesVersion = '1.2.1'
+ datastoreVersion = "1.1.0-alpha04" // only 1.1.0 supports multi-process datastore
// Other dependencies
materialDialogsVersion = '3.3.0'
+ protobufVersion = '3.22.2' // For Proto Datastore. Maybe keep in sync with other versions.
// Testing
junitVersion = "1.1.5"
diff --git a/energy_settings/build.gradle b/energy_settings/build.gradle
index 486ecd1..a9f2080 100644
--- a/energy_settings/build.gradle
+++ b/energy_settings/build.gradle
@@ -20,12 +20,13 @@
* Gradle's build file for this module.
*
* @author Armin Schnabel
- * @version 1.3.0
+ * @version 1.4.0
* @since 1.0.0
*/
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
+apply plugin: 'com.google.protobuf' // For Proto DataStore
android {
namespace "de.cyface.energy_settings"
@@ -70,6 +71,11 @@ android {
}
dependencies {
+ // Proto DataStore with SingleProcess support to store settings
+ implementation "androidx.datastore:datastore:${datastoreVersion}"
+ //implementation "androidx.datastore:datastore-core-android:${datastoreVersion}"
+ implementation "com.google.protobuf:protobuf-javalite:${protobufVersion}"
+
// Other Android libraries
implementation "androidx.appcompat:appcompat:$rootProject.ext.androidxAppCompatVersion"
implementation "androidx.preference:preference-ktx:$rootProject.ext.androidPreferencesVersion"
@@ -94,5 +100,25 @@ dependencies {
testImplementation "org.robolectric:robolectric:$rootProject.ext.robolectricVersion"
}
+// Required for Proto DataStore
+protobuf {
+ protoc {
+ artifact = "com.google.protobuf:protoc:$protobufVersion"
+ }
+
+ // Generates the java Protobuf-lite code for the Protobuf files in this project. See
+ // https://github.com/google/protobuf-gradle-plugin#customizing-protobuf-compilation
+ // for more information.
+ generateProtoTasks {
+ all().each { task ->
+ task.builtins {
+ java {
+ option 'lite'
+ }
+ }
+ }
+ }
+}
+
// The gradle publish tasks is defined in a separate file:
apply from: 'publish.gradle'
diff --git a/energy_settings/src/main/kotlin/de/cyface/energy_settings/CustomPreferences.kt b/energy_settings/src/main/kotlin/de/cyface/energy_settings/CustomPreferences.kt
deleted file mode 100644
index 1a65029..0000000
--- a/energy_settings/src/main/kotlin/de/cyface/energy_settings/CustomPreferences.kt
+++ /dev/null
@@ -1,20 +0,0 @@
-package de.cyface.energy_settings
-
-import android.content.Context
-import androidx.core.content.edit
-import de.cyface.energy_settings.Constants.PREFERENCES_MANUFACTURER_WARNING_SHOWN_KEY
-import de.cyface.utils.AppPreferences
-
-class CustomPreferences(context: Context): AppPreferences(context) {
-
- fun saveWarningShown(warningShown: Boolean) {
- preferences.edit {
- putBoolean(PREFERENCES_MANUFACTURER_WARNING_SHOWN_KEY, warningShown)
- apply()
- }
- }
-
- fun getWarningShown(): Boolean {
- return preferences.getBoolean(PREFERENCES_MANUFACTURER_WARNING_SHOWN_KEY, false)
- }
-}
\ No newline at end of file
diff --git a/energy_settings/src/main/kotlin/de/cyface/energy_settings/EnergySettings.kt b/energy_settings/src/main/kotlin/de/cyface/energy_settings/EnergySettings.kt
new file mode 100644
index 0000000..0351f74
--- /dev/null
+++ b/energy_settings/src/main/kotlin/de/cyface/energy_settings/EnergySettings.kt
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2023 Cyface GmbH
+ *
+ * This file is part of the Cyface Energy Settings for Android.
+ *
+ * The Cyface Energy Settings for Android is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * The Cyface Energy Settings for Android is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with the Cyface Energy Settings for Android. If not, see .
+ */
+package de.cyface.energy_settings
+
+import android.content.Context
+import androidx.core.content.edit
+import de.cyface.energy_settings.Constants.PREFERENCES_MANUFACTURER_WARNING_SHOWN_KEY
+
+/**
+ * Custom settings used by this library.
+ *
+ * Attention:
+ * - Never mix SingleProcessDataStore with MultiProcessDataStore for the same file.
+ * - We use SingleProcessDataStore, so don't access preferences from multiple processes.
+ * - Only create one instance of `DataStore` per file in the same process.
+ * - We use ProtoBuf to ensure type safety. Rebuild after changing the .proto file.
+ *
+ * @author Armin Schnabel
+ * @version 2.0.0
+ * @since 3.4.0
+ */
+class CustomPreferences(context: Context) {
+
+ fun saveWarningShown(warningShown: Boolean) {
+ //FIXME
+ /*preferences.edit {
+ putBoolean(PREFERENCES_MANUFACTURER_WARNING_SHOWN_KEY, warningShown)
+ apply()
+ }*/
+ }
+
+ fun getWarningShown(): Boolean {
+ return true // FIXME
+ //return preferences.getBoolean(PREFERENCES_MANUFACTURER_WARNING_SHOWN_KEY, false)
+ }
+}
\ No newline at end of file
diff --git a/energy_settings/src/main/kotlin/de/cyface/energy_settings/SettingsSerializer.kt b/energy_settings/src/main/kotlin/de/cyface/energy_settings/SettingsSerializer.kt
new file mode 100644
index 0000000..8346a07
--- /dev/null
+++ b/energy_settings/src/main/kotlin/de/cyface/energy_settings/SettingsSerializer.kt
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2023 Cyface GmbH
+ *
+ * This file is part of the Cyface App for Android.
+ *
+ * The Cyface App for Android is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * The Cyface App for Android is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with the Cyface App for Android. If not, see .
+ */
+package de.cyface.energy_settings
+
+import androidx.datastore.core.CorruptionException
+import androidx.datastore.core.Serializer
+import com.google.protobuf.InvalidProtocolBufferException
+import java.io.InputStream
+import java.io.OutputStream
+
+/**
+ * The serializer for the Proto DataStore of the preferences stored in the [Settings] file.
+ *
+ * For details: https://developer.android.com/topic/libraries/architecture/datastore#proto-datastore
+ *
+ * @author Armin Schnabel
+ * @since 3.7.0
+ * @version 1.0.0
+ */
+object SettingsSerializer : Serializer {
+ override val defaultValue: Settings = Settings.getDefaultInstance()
+
+ override suspend fun readFrom(input: InputStream): Settings {
+ try {
+ return Settings.parseFrom(input)
+ } catch (exception: InvalidProtocolBufferException) {
+ throw CorruptionException("Cannot read proto.", exception)
+ }
+ }
+
+ override suspend fun writeTo(
+ t: Settings,
+ output: OutputStream
+ ) = t.writeTo(output)
+}
\ No newline at end of file
diff --git a/energy_settings/src/main/proto/settings.proto b/energy_settings/src/main/proto/settings.proto
new file mode 100644
index 0000000..faeeb61
--- /dev/null
+++ b/energy_settings/src/main/proto/settings.proto
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2023 Cyface GmbH
+ *
+ * This file is part of the Cyface Utils for Android.
+ *
+ * The Cyface Utils for Android is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * The Cyface Utils for Android is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with the Cyface Utils for Android. If not, see .
+ */
+syntax = "proto3";
+
+option java_package = "de.cyface.utils";
+option java_multiple_files = true;
+
+/**
+ * The data types for the values stored in the `AppPreferences`.
+ *
+ * See https://protobuf.dev/programming-guides/proto3/
+ *
+ * Attention: The classes are generated from the file at compile time. Don't forget to rebuild.
+ *
+ * @author: Armin Schnabel
+ * @since: 4.0.0
+ * @version: 1.0.0
+ */
+message Settings {
+ // Whether the map should be automatically centered while moving
+ bool center_map = 1;
+
+ // Whether the app should be able to upload measurementsA.
+ bool upload_enabled = 2;
+
+ // The maximum frequency with which the IMU sensors should collect data, e.g. 100 Hz.
+ int32 sensor_frequency = 3;
+
+ // Whether the app should report to the error monitoring service.
+ bool report_errors = 4;
+
+ // The currently selected modality, e.g. 'CAR'.
+ string modality = 5;
+
+ // The API version of the terms accepted by the user, e.g. 5.
+ int32 accepted_terms = 6;
+}
\ No newline at end of file