Commit 19dd8993 authored by stopcovid@lunabee.com's avatar stopcovid@lunabee.com
Browse files

JSON fetch optimisation

parent 491d860a
......@@ -19,6 +19,10 @@ object UiConstants {
CAMERA, LOCATION
}
object SharePrefs {
const val LAST_STRINGS_REFRESH: String = "Last.Strings.Refresh"
}
enum class Notification(val channelId: String, val notificationId: Int) {
AT_RISK("atRisk", 1),
PROXIMITY("proximity", 2),
......
......@@ -12,6 +12,8 @@ package com.lunabeestudio.stopcovid.coreui.manager
import android.content.Context
import androidx.annotation.WorkerThread
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import com.google.gson.Gson
import com.lunabeestudio.stopcovid.coreui.BuildConfig
import com.lunabeestudio.stopcovid.coreui.UiConstants
......@@ -20,6 +22,7 @@ import timber.log.Timber
import java.io.File
import java.lang.reflect.Type
import java.util.Locale
import java.util.concurrent.TimeUnit
abstract class ServerManager {
......@@ -29,6 +32,7 @@ abstract class ServerManager {
protected abstract fun prefix(context: Context): String
protected abstract fun fallbackFileName(context: Context): String
protected abstract fun type(): Type
protected abstract fun lastRefreshSharedPrefsKey(): String
protected open fun transform(input: String): String = input
protected open fun extension(): String = ".json"
protected open fun url(): String = BuildConfig.SERVER_URL
......@@ -36,10 +40,11 @@ abstract class ServerManager {
@WorkerThread
protected fun fetchLast(context: Context, languageCode: String): Boolean {
return try {
if (!BuildConfig.USE_LOCAL_DATA) {
if (shouldRefresh(context)) {
val filename = "${prefix(context)}${languageCode}${extension()}"
Timber.d("Fetching remote data at ${url()}$filename")
"${url()}$filename".saveTo(context, File(context.filesDir, filename))
saveLastRefresh(context)
true
} else {
Timber.d("Only use local data")
......@@ -85,4 +90,16 @@ abstract class ServerManager {
transform(it.readBytes().toString(Charsets.UTF_8))
}, type())
}
private fun shouldRefresh(context: Context): Boolean {
return !BuildConfig.USE_LOCAL_DATA
&& System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1L) > PreferenceManager.getDefaultSharedPreferences(context)
.getLong(lastRefreshSharedPrefsKey(), 0L)
}
private fun saveLastRefresh(context: Context) {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putLong(lastRefreshSharedPrefsKey(), System.currentTimeMillis())
}
}
}
\ No newline at end of file
......@@ -56,6 +56,8 @@ class StringsManager : ServerManager() {
override fun prefix(context: Context): String = context.getString(R.string.string_prefix)
override fun fallbackFileName(context: Context): String = "${prefix(context)}${UiConstants.DEFAULT_LANGUAGE}${extension()}"
override fun type(): Type = object : TypeToken<HashMap<String, String>>() {}.type
override fun lastRefreshSharedPrefsKey(): String = UiConstants.SharePrefs.LAST_STRINGS_REFRESH
override fun transform(input: String): String = input.fixFormatter()
}
......
......@@ -42,7 +42,7 @@ android {
applicationId "fr.gouv.android.stopcovid"
minSdkVersion 21
targetSdkVersion 29
versionCode 25
versionCode 28
versionName "1.0.0"
testInstrumentationRunner = 'com.lunabeestudio.stopcovid.TestRunner'
......
......@@ -13,6 +13,8 @@ package com.lunabeestudio.stopcovid
object Constants {
object SharedPrefs {
const val ON_BOARDING_DONE: String = "On.Boarding.Done"
const val LAST_PRIVACY_REFRESH: String = "Last.Privacy.Refresh"
const val LAST_MAINTENANCE_REFRESH: String = "Last.Maintenance.Refresh"
}
object WorkerNames {
......@@ -22,4 +24,4 @@ object Constants {
object ServerConstant {
val ACCEPTED_REPORT_CODE_LENGTH: List<Int> = listOf(6, 36)
}
}
}
\ No newline at end of file
......@@ -15,11 +15,15 @@ import android.content.Intent
import android.content.SharedPreferences
import android.os.Build
import androidx.annotation.DrawableRes
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import com.google.gson.Gson
import com.lunabeestudio.stopcovid.Constants
import com.lunabeestudio.stopcovid.model.Info
import com.lunabeestudio.stopcovid.network.LBMaintenanceHttpClient
import org.json.JSONObject
import timber.log.Timber
import java.util.concurrent.TimeUnit
/**
* Main class of this lib. The singleton manager which do the work to block the app or not
......@@ -72,12 +76,20 @@ object AppMaintenanceManager {
* Call this to check if the app needs to be blocked or not
*/
fun checkForMaintenanceUpgrade(context: Context) {
if (isActivityOpened) {
return
} else {
updateCheckForMaintenanceUpgrade(context,
null,
null)
when {
isActivityOpened -> {
return
}
shouldRefresh(context) -> {
updateCheckForMaintenanceUpgrade(context,
null,
null)
}
else -> {
useLastResult(context,
null,
null)
}
}
}
......@@ -97,6 +109,7 @@ object AppMaintenanceManager {
info,
appIsFreeCompletion,
appIsBlockedCompletion)
saveLastRefresh(context)
} catch (e: Exception) {
// In case of a malformed JSON we don't safe it and use the last one instead
useLastResult(context,
......@@ -175,6 +188,17 @@ object AppMaintenanceManager {
return sharedPrefs.getString(JSON_STRING_SHARED_PREFS_KEY, null)
}
private fun shouldRefresh(context: Context): Boolean {
return System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(5L) > PreferenceManager.getDefaultSharedPreferences(context)
.getLong(Constants.SharedPrefs.LAST_MAINTENANCE_REFRESH, 0L)
}
private fun saveLastRefresh(context: Context) {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putLong(Constants.SharedPrefs.LAST_MAINTENANCE_REFRESH, System.currentTimeMillis())
}
}
private const val SHARED_PREFS_NAME: String = "AppMaintenanceManagerPrefNames"
private const val JSON_STRING_SHARED_PREFS_KEY: String = "json.string.shared.prefs.key"
}
\ No newline at end of file
......@@ -13,6 +13,7 @@ package com.lunabeestudio.stopcovid.manager
import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.google.gson.reflect.TypeToken
import com.lunabeestudio.stopcovid.Constants
import com.lunabeestudio.stopcovid.coreui.manager.ServerManager
import com.lunabeestudio.stopcovid.model.PrivacySection
import kotlinx.coroutines.CoroutineScope
......@@ -56,4 +57,5 @@ class PrivacyManager : ServerManager() {
override fun prefix(context: Context): String = "privacy-"
override fun fallbackFileName(context: Context): String = "privacy-en.json"
override fun type(): Type = object : TypeToken<List<PrivacySection>>() {}.type
override fun lastRefreshSharedPrefsKey(): String = Constants.SharedPrefs.LAST_PRIVACY_REFRESH
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment