diff options
author | TheScarastic <warabhishek@e.email> | 2022-07-05 12:25:23 +0530 |
---|---|---|
committer | TheScarastic <warabhishek@e.email> | 2022-07-05 12:53:51 +0530 |
commit | 5c4dd9007d28bea285b8e626d1bc7ced680c19f4 (patch) | |
tree | 5af647114f4510d322a0ee11b7582931db65f1a5 | |
parent | 4077b8e123dde3df749eef9731280baa63a4816e (diff) |
advancedPrivacy: Save trackers file and use it if available
-rw-r--r-- | app/build.gradle | 2 | ||||
-rw-r--r-- | app/src/main/java/foundation/e/privacycentralapp/data/repositories/TrackersRepository.kt | 50 |
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( |