summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/build.gradle2
-rw-r--r--app/src/main/java/foundation/e/privacycentralapp/data/repositories/TrackersRepository.kt50
2 files changed, 41 insertions, 11 deletions
diff --git a/app/build.gradle b/app/build.gradle
index f6fecd2..23d6ecd 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -119,7 +119,7 @@ dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
- implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
+ implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
implementation project(":flow-mvi")
implementation Libs.Kotlin.stdlib
diff --git a/app/src/main/java/foundation/e/privacycentralapp/data/repositories/TrackersRepository.kt b/app/src/main/java/foundation/e/privacycentralapp/data/repositories/TrackersRepository.kt
index 8a7f0a7..2b2c1dd 100644
--- a/app/src/main/java/foundation/e/privacycentralapp/data/repositories/TrackersRepository.kt
+++ b/app/src/main/java/foundation/e/privacycentralapp/data/repositories/TrackersRepository.kt
@@ -22,33 +22,47 @@ import android.util.Log
import com.google.gson.Gson
import foundation.e.privacymodules.trackers.Tracker
import retrofit2.Retrofit
-import retrofit2.converter.gson.GsonConverterFactory
+import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET
+import java.io.File
+import java.io.FileInputStream
+import java.io.FileWriter
+import java.io.IOException
import java.io.InputStreamReader
+import java.io.PrintWriter
class TrackersRepository(private val context: Context) {
+ private val eTrackerFileName = "e_trackers.json"
+ private val eTrackerFile = File(context.filesDir.absolutePath, eTrackerFileName)
+
var trackers: List<Tracker> = emptyList()
private set
init {
- initFromAssets()
+ initTrackersFile()
}
suspend fun update() {
val api = ETrackersApi.build()
- val response = api.trackers()
- trackers = mapper(response)
+ saveData(eTrackerFile, api.trackers())
+ initTrackersFile()
}
- private fun initFromAssets() {
+ private fun initTrackersFile() {
try {
- val reader = InputStreamReader(context.getAssets().open("e_trackers.json"), "UTF-8")
-
+ var inputStream = context.assets.open(eTrackerFileName)
+ if (eTrackerFile.exists()) {
+ inputStream = FileInputStream(eTrackerFile)
+ }
+ val reader = InputStreamReader(inputStream, "UTF-8")
val trackerResponse =
Gson().fromJson(reader, ETrackersApi.ETrackersResponse::class.java)
trackers = mapper(trackerResponse)
+
+ reader.close()
+ inputStream.close()
} catch (e: Exception) {
Log.e("TrackersRepository", "While parsing trackers in assets", e)
}
@@ -64,7 +78,7 @@ class TrackersRepository(private val context: Context) {
}
}
- fun ETrackersApi.ETrackersResponse.ETracker.toTracker(): Tracker {
+ private fun ETrackersApi.ETrackersResponse.ETracker.toTracker(): Tracker {
return Tracker(
id = id!!,
hostnames = hostnames!!.toSet(),
@@ -72,6 +86,22 @@ class TrackersRepository(private val context: Context) {
exodusId = exodusId
)
}
+
+ private fun saveData(file: File, data: String): Boolean {
+ try {
+ val fos = FileWriter(file, false)
+ val ps = PrintWriter(fos)
+ ps.apply {
+ print(data)
+ flush()
+ close()
+ }
+ return true
+ } catch (e: IOException) {
+ e.printStackTrace()
+ }
+ return false
+ }
}
interface ETrackersApi {
@@ -79,14 +109,14 @@ interface ETrackersApi {
fun build(): ETrackersApi {
val retrofit = Retrofit.Builder()
.baseUrl("https://gitlab.e.foundation/e/os/tracker-list/-/raw/main/")
- .addConverterFactory(GsonConverterFactory.create())
+ .addConverterFactory(ScalarsConverterFactory.create())
.build()
return retrofit.create(ETrackersApi::class.java)
}
}
@GET("list/e_trackers.json")
- suspend fun trackers(): ETrackersResponse
+ suspend fun trackers(): String
data class ETrackersResponse(val trackers: List<ETracker>) {
data class ETracker(