From 34232a27ad8a7eafd48a562566480fd298bbaf95 Mon Sep 17 00:00:00 2001 From: jacquarg Date: Fri, 18 Feb 2022 08:18:00 +0100 Subject: Use embeded E-trackers list, #4600 --- .../data/repositories/TrackersRepository.kt | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 app/src/main/java/foundation/e/privacycentralapp/data/repositories/TrackersRepository.kt (limited to 'app/src/main/java/foundation/e/privacycentralapp/data') 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 . + */ + +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 = 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 { + 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) { + data class ETracker( + val id: String?, + val hostnames: List?, + 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?, + ) + } +} -- cgit v1.2.3