aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/foundation/e/privacycentralapp/data
diff options
context:
space:
mode:
authorGuillaume Jacquart <guillaume.jacquart@hoodbrains.com>2022-02-21 20:20:43 +0000
committerGuillaume Jacquart <guillaume.jacquart@hoodbrains.com>2022-02-21 20:20:43 +0000
commita80f529d4b14a6820cf2b7d47b1087e9d06f0ae8 (patch)
tree196eacef8231fea58703744917a0d56894071684 /app/src/main/java/foundation/e/privacycentralapp/data
parentaa24315969cebf4ee10baecb761947f80f3042cc (diff)
parent9d63c322d2a79c8bd8308997368976a69037149e (diff)
downloadadvanced-privacy-a80f529d4b14a6820cf2b7d47b1087e9d06f0ae8.tar.gz
Merge branch 'e_trackers_list' into 'main'
Use embeded E-trackers list, #4600 See merge request e/privacy-central/privacycentralapp!14
Diffstat (limited to 'app/src/main/java/foundation/e/privacycentralapp/data')
-rw-r--r--app/src/main/java/foundation/e/privacycentralapp/data/repositories/TrackersRepository.kt102
1 files changed, 102 insertions, 0 deletions
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
new file mode 100644
index 0000000..c7efa84
--- /dev/null
+++ b/app/src/main/java/foundation/e/privacycentralapp/data/repositories/TrackersRepository.kt
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2022 E FOUNDATION
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package foundation.e.privacycentralapp.data.repositories
+
+import android.content.Context
+import android.util.Log
+import com.google.gson.Gson
+import com.google.gson.annotations.SerializedName
+import foundation.e.privacymodules.trackers.Tracker
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
+import retrofit2.http.GET
+import java.io.InputStreamReader
+import java.lang.Exception
+
+class TrackersRepository(private val context: Context) {
+
+ var trackers: List<Tracker> = emptyList()
+ private set
+
+ init {
+ initFromAssets()
+ }
+
+ private fun initFromAssets() {
+ try {
+ val reader = InputStreamReader(context.getAssets().open("e_trackers.json"), "UTF-8")
+
+ val trackerResponse =
+ Gson().fromJson(reader, ETrackersApi.ETrackersResponse::class.java)
+
+ trackers = mapper(trackerResponse)
+ } catch (e: Exception) {
+ Log.e("TrackersRepository", "While parsing trackers in assets", e)
+ }
+ }
+
+ private fun mapper(response: ETrackersApi.ETrackersResponse): List<Tracker> {
+ return response.trackers.mapNotNull {
+ try {
+ it.toTracker()
+ } catch (e: Exception) {
+ null
+ }
+ }
+ }
+
+ fun ETrackersApi.ETrackersResponse.ETracker.toTracker(): Tracker {
+ return Tracker(
+ id = id!!,
+ hostnames = hostnames!!.toSet(),
+ label = name!!,
+ description = description,
+ website = website,
+ )
+ }
+}
+
+interface ETrackersApi {
+ companion object {
+ fun build(): ETrackersApi {
+ val retrofit = Retrofit.Builder()
+ .baseUrl("TODO")
+ .addConverterFactory(GsonConverterFactory.create())
+ .build()
+ return retrofit.create(ETrackersApi::class.java)
+ }
+ }
+
+ @GET("TODO")
+ suspend fun trackers(): ETrackersResponse
+
+ data class ETrackersResponse(val trackers: List<ETracker>) {
+ data class ETracker(
+ val id: String?,
+ val hostnames: List<String>?,
+ val name: String?,
+
+ val description: String?,
+ @SerializedName("creation_date") val creationDate: String?,
+ @SerializedName("code_signature") val codeSignature: String?,
+ @SerializedName("network_signature") val networkSignature: String?,
+ val website: String?,
+ val categories: List<String>?,
+ )
+ }
+}