Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #394 from corona-warn-app/jm-todos
Browse files Browse the repository at this point in the history
Jm todos
  • Loading branch information
harambasicluka authored May 30, 2020
2 parents 67574a4 + 818f437 commit d379d70
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import de.rki.coronawarnapp.storage.tracing.TracingIntervalEntity
import de.rki.coronawarnapp.util.Converters
import net.sqlcipher.database.SQLiteDatabase
import net.sqlcipher.database.SupportFactory
import java.util.UUID

@Database(
entities = [ExposureSummaryEntity::class, KeyCacheEntity::class, TracingIntervalEntity::class],
Expand All @@ -35,9 +36,14 @@ abstract class AppDatabase : RoomDatabase() {
}
}

fun resetInstance(context: Context) = { instance = null }.also { getInstance(context) }

private fun buildDatabase(context: Context): AppDatabase {
if (LocalData.databasePassword() == null) {
LocalData.databasePassword(UUID.randomUUID().toString().toCharArray())
}
return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME)
.openHelperFactory(SupportFactory(SQLiteDatabase.getBytes(LocalData.getDatabasePassword())))
.openHelperFactory(SupportFactory(SQLiteDatabase.getBytes(LocalData.databasePassword())))
.build()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import de.rki.coronawarnapp.CoronaWarnApplication
import de.rki.coronawarnapp.R
import de.rki.coronawarnapp.util.security.SecurityHelper.globalEncryptedSharedPreferencesInstance
import java.util.Date
import java.util.UUID

/**
* LocalData is responsible for all access to the shared preferences. Each preference is accessible
Expand Down Expand Up @@ -474,24 +473,21 @@ object LocalData {
* DATABASE PASSWORD
****************************************************/

fun getDatabasePassword(): CharArray {
val pw = getSharedPreferenceInstance().getString(
CoronaWarnApplication.getAppContext()
.getString(R.string.preference_database_password),
null
)
if (pw == null) {
with(getSharedPreferenceInstance().edit()) {
putString(
CoronaWarnApplication.getAppContext()
.getString(R.string.preference_database_password),
UUID.randomUUID().toString()
)
commit()
}
return getDatabasePassword()
fun databasePassword(): CharArray? = getSharedPreferenceInstance().getString(
CoronaWarnApplication.getAppContext()
.getString(R.string.preference_database_password),
null
)?.toCharArray()

fun databasePassword(password: CharArray) {
with(getSharedPreferenceInstance().edit()) {
putString(
CoronaWarnApplication.getAppContext()
.getString(R.string.preference_database_password),
password.toString()
)
commit()
}
return pw.toCharArray()
}

/****************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class SettingsResetFragment : BaseFragment() {
).show()
}
withContext(Dispatchers.IO) {
// todo include link to google settings for resetting BLE data
deleteLocalAppContent()
}
navigateToOnboarding()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ object CachedKeyFileHolder {
keyCache.deleteOutdatedEntries()
// queries will be executed after the "query plan" was set
val deferredQueries: MutableCollection<Deferred<Any>> = mutableListOf()
val missingDays = getMissingDaysFromDiff()
val serverDates = getDatesFromServer()
val missingDays = getMissingDaysFromDiff(serverDates)
if (missingDays.isNotEmpty()) {
// we have a date difference
deferredQueries.addAll(
Expand All @@ -80,12 +81,16 @@ object CachedKeyFileHolder {
// if we have a date difference we need to refetch the current hours
keyCache.clearHours()
}
// we have an hour difference
deferredQueries.addAll(
getMissingHoursFromDiff(currentDate)
.map { getURLForHour(currentDate.toServerFormat(), it) }
.map { url -> async { url.createHourEntryForUrl() } }
)
val currentDateServerFormat = currentDate.toServerFormat()
// just fetch the hours if the date is available
if (serverDates.contains(currentDateServerFormat)) {
// we have an hour difference
deferredQueries.addAll(
getMissingHoursFromDiff(currentDate)
.map { getURLForHour(currentDate.toServerFormat(), it) }
.map { url -> async { url.createHourEntryForUrl() } }
)
}
// execute the query plan
try {
deferredQueries.awaitAll()
Expand All @@ -100,9 +105,9 @@ object CachedKeyFileHolder {
/**
* Calculates the missing days based on current missing entries in the cache
*/
private suspend fun getMissingDaysFromDiff(): List<String> {
private suspend fun getMissingDaysFromDiff(datesFromServer: Collection<String>): List<String> {
val cacheEntries = keyCache.getDates()
return getDatesFromServer()
return datesFromServer
.also { Log.d(TAG, "${it.size} days from server") }
.filter { it.dateEntryCacheMiss(cacheEntries) }
.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import android.util.Log
import de.rki.coronawarnapp.storage.AppDatabase
import de.rki.coronawarnapp.storage.FileStorageHelper
import de.rki.coronawarnapp.storage.LocalData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.UUID

/**
* Helper for supplying functionality regarding Data Retention
Expand All @@ -37,12 +36,15 @@ object DataRetentionHelper {
* Deletes all data known to the Application
*
*/
suspend fun clearAllLocalData(context: Context) = withContext(Dispatchers.Default) {
fun clearAllLocalData(context: Context) {
Log.w(TAG, "CWA LOCAL DATA DELETION INITIATED.")
// Shared Preferences Reset
LocalData.getSharedPreferenceInstance().edit().clear().apply()
// Database Reset
AppDatabase.getInstance(context).clearAllTables()
// Shared Preferences Reset
LocalData.getSharedPreferenceInstance().edit().clear().apply()
// Delete Database Instance
AppDatabase.resetInstance(context)
LocalData.databasePassword(UUID.randomUUID().toString().toCharArray())
// Export File Reset
FileStorageHelper.getAllFilesInKeyExportDirectory().forEach { it.delete() }
Log.w(TAG, "CWA LOCAL DATA DELETION COMPLETED.")
Expand Down

0 comments on commit d379d70

Please sign in to comment.