Skip to content

Commit

Permalink
Add Proto DataStore dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
hb0 committed Aug 31, 2023
1 parent 4d43b99 commit 1bc6aa9
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 22 deletions.
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand All @@ -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
Expand All @@ -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"
Expand Down
28 changes: 27 additions & 1 deletion energy_settings/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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'

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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)
}
}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Settings> {
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)
}
53 changes: 53 additions & 0 deletions energy_settings/src/main/proto/settings.proto
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}

0 comments on commit 1bc6aa9

Please sign in to comment.