summaryrefslogtreecommitdiff
path: root/app/src/main/java/foundation/e/advancedprivacy/data/repositories/TrackersRepository.kt
blob: 568d76b97e0921ff345d67e32d0dd087e10e3487 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * 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.advancedprivacy.data.repositories

import android.content.Context
import com.google.gson.Gson
import foundation.e.privacymodules.trackers.api.Tracker
import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET
import timber.log.Timber
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 {
        initTrackersFile()
    }

    suspend fun update() {
        val api = ETrackersApi.build()
        try {
            saveData(eTrackerFile, api.trackers())
            initTrackersFile()
        } catch (e: Exception) {
            Timber.e("While updating trackers", e)
        }
    }

    private fun initTrackersFile() {
        try {
            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) {
            Timber.e("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
            }
        }
    }

    private fun ETrackersApi.ETrackersResponse.ETracker.toTracker(): Tracker {
        return Tracker(
            id = id!!,
            hostnames = hostnames!!.toSet(),
            label = name!!,
            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 {
    companion object {
        fun build(): ETrackersApi {
            val retrofit = Retrofit.Builder()
                .baseUrl("https://gitlab.e.foundation/e/os/tracker-list/-/raw/main/")
                .addConverterFactory(ScalarsConverterFactory.create())
                .build()
            return retrofit.create(ETrackersApi::class.java)
        }
    }

    @GET("list/e_trackers.json")
    suspend fun trackers(): String

    data class ETrackersResponse(val trackers: List<ETracker>) {
        data class ETracker(
            val id: String?,
            val hostnames: List<String>?,
            val name: String?,
            val exodusId: String?
        )
    }
}