diff options
author | jacquarg <guillaume.jacquart@hoodbrains.com> | 2022-02-18 08:18:00 +0100 |
---|---|---|
committer | jacquarg <guillaume.jacquart@hoodbrains.com> | 2022-02-18 08:18:00 +0100 |
commit | 34232a27ad8a7eafd48a562566480fd298bbaf95 (patch) | |
tree | a9bcc19651ff4bc061da094b1d5763d75597b42b /app/src/main/java/foundation/e | |
parent | aa24315969cebf4ee10baecb761947f80f3042cc (diff) |
Use embeded E-trackers list, #4600
Diffstat (limited to 'app/src/main/java/foundation/e')
6 files changed, 116 insertions, 16 deletions
diff --git a/app/src/main/java/foundation/e/privacycentralapp/DependencyContainer.kt b/app/src/main/java/foundation/e/privacycentralapp/DependencyContainer.kt index 102d970..e2cca4a 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/DependencyContainer.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/DependencyContainer.kt @@ -21,6 +21,7 @@ import android.app.Application import android.content.Context import android.os.Process import foundation.e.privacycentralapp.data.repositories.LocalStateRepository +import foundation.e.privacycentralapp.data.repositories.TrackersRepository import foundation.e.privacycentralapp.domain.usecases.AppListUseCase import foundation.e.privacycentralapp.domain.usecases.FakeLocationStateUseCase import foundation.e.privacycentralapp.domain.usecases.GetQuickPrivacyStateUseCase @@ -39,8 +40,8 @@ import foundation.e.privacymodules.location.FakeLocationModule import foundation.e.privacymodules.location.IFakeLocationModule import foundation.e.privacymodules.permissions.PermissionsPrivacyModule import foundation.e.privacymodules.permissions.data.ApplicationDescription -import foundation.e.trackerfilter.api.BlockTrackersPrivacyModule -import foundation.e.trackerfilter.api.TrackTrackersPrivacyModule +import foundation.e.privacymodules.trackers.api.BlockTrackersPrivacyModule +import foundation.e.privacymodules.trackers.api.TrackTrackersPrivacyModule import kotlinx.coroutines.GlobalScope import lineageos.blockers.BlockerInterface @@ -68,10 +69,11 @@ class DependencyContainer constructor(val app: Application) { } private val blockTrackersPrivacyModule by lazy { BlockTrackersPrivacyModule.getInstance(context) } + private val trackTrackersPrivacyModule by lazy { TrackTrackersPrivacyModule.getInstance(context) } // Repositories private val localStateRepository by lazy { LocalStateRepository(context) } - private val trackTrackersPrivacyModule by lazy { TrackTrackersPrivacyModule.getInstance(context) } + private val trackersRepository by lazy { TrackersRepository(context) } // Usecases private val getQuickPrivacyStateUseCase by lazy { @@ -87,7 +89,7 @@ class DependencyContainer constructor(val app: Application) { } private val trackersStateUseCase by lazy { - TrackersStateUseCase(blockTrackersPrivacyModule, trackTrackersPrivacyModule, permissionsModule, localStateRepository, GlobalScope) + TrackersStateUseCase(blockTrackersPrivacyModule, trackTrackersPrivacyModule, permissionsModule, localStateRepository, trackersRepository, GlobalScope) } private val fakeLocationStateUseCase by lazy { 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>?, + ) + } +} diff --git a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/AppListUseCase.kt b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/AppListUseCase.kt index e44aa76..acb8a36 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/AppListUseCase.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/AppListUseCase.kt @@ -20,7 +20,7 @@ package foundation.e.privacycentralapp.domain.usecases import android.Manifest import foundation.e.privacymodules.permissions.PermissionsPrivacyModule import foundation.e.privacymodules.permissions.data.ApplicationDescription -import foundation.e.trackerfilter.api.BlockTrackersPrivacyModule +import foundation.e.privacymodules.trackers.api.BlockTrackersPrivacyModule import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow diff --git a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/TrackersStateUseCase.kt b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/TrackersStateUseCase.kt index 51bdc35..e140b19 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/TrackersStateUseCase.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/TrackersStateUseCase.kt @@ -18,6 +18,7 @@ package foundation.e.privacycentralapp.domain.usecases import foundation.e.privacycentralapp.data.repositories.LocalStateRepository +import foundation.e.privacycentralapp.data.repositories.TrackersRepository import foundation.e.privacymodules.permissions.PermissionsPrivacyModule import foundation.e.privacymodules.permissions.data.ApplicationDescription import foundation.e.privacymodules.trackers.IBlockTrackersPrivacyModule @@ -34,6 +35,7 @@ class TrackersStateUseCase( private val trackersPrivacyModule: ITrackTrackersPrivacyModule, private val permissionsPrivacyModule: PermissionsPrivacyModule, private val localStateRepository: LocalStateRepository, + private val trackersRepository: TrackersRepository, coroutineScope: CoroutineScope ) { @@ -44,6 +46,7 @@ class TrackersStateUseCase( val areAllTrackersBlocked: StateFlow<Boolean> = _areAllTrackersBlocked init { + trackersPrivacyModule.start(trackersRepository.trackers, enableNotification = false) coroutineScope.launch { localStateRepository.quickPrivacyEnabledFlow.collect { enabled -> if (enabled) { @@ -73,7 +76,7 @@ class TrackersStateUseCase( return blockTrackersPrivacyModule.isWhitelisted(appUid) } - fun getTrackersWhitelistIds(appUid: Int): List<Int> { + fun getTrackersWhitelistIds(appUid: Int): List<String> { return blockTrackersPrivacyModule.getWhiteList(appUid).map { it.id } } diff --git a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/apptrackers/AppTrackersFeature.kt b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/apptrackers/AppTrackersFeature.kt index 14ee921..11bcf0c 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/apptrackers/AppTrackersFeature.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/apptrackers/AppTrackersFeature.kt @@ -49,7 +49,7 @@ class AppTrackersFeature( val appDesc: ApplicationDescription? = null, val isBlockingActivated: Boolean = false, val trackers: List<Tracker>? = null, - val whitelist: List<Int>? = null + val whitelist: List<String>? = null ) { fun getTrackersStatus(): List<Pair<Tracker, Boolean>>? { if (trackers != null && whitelist != null) { @@ -76,9 +76,9 @@ class AppTrackersFeature( data class AvailableTrackersListEffect( val isBlockingActivated: Boolean, val trackers: List<Tracker>, - val whitelist: List<Int> + val whitelist: List<String> ) : Effect() - data class TrackersWhitelistUpdateEffect(val whitelist: List<Int>) : Effect() + data class TrackersWhitelistUpdateEffect(val whitelist: List<String>) : Effect() // object QuickPrivacyDisabledWarningEffect : Effect() data class ErrorEffect(val message: String) : Effect() diff --git a/app/src/main/java/foundation/e/privacycentralapp/main/MainActivity.kt b/app/src/main/java/foundation/e/privacycentralapp/main/MainActivity.kt index 8d91425..1b92cb2 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/main/MainActivity.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/main/MainActivity.kt @@ -23,11 +23,8 @@ import android.os.Bundle import androidx.fragment.app.FragmentActivity import androidx.fragment.app.add import androidx.fragment.app.commit -import dnsfilter.android.AndroidEnvironment import foundation.e.privacycentralapp.R import foundation.e.privacycentralapp.features.dashboard.DashboardFragment -import foundation.e.trackerfilter.DNSBlockerService -import foundation.e.trackerfilter.StatsIntentService open class MainActivity : FragmentActivity(R.layout.activity_main) { @@ -44,10 +41,6 @@ open class MainActivity : FragmentActivity(R.layout.activity_main) { override fun onPostCreate(savedInstanceState: Bundle?) { super.onPostCreate(savedInstanceState) handleIntent(intent) - // TODO move into tracker module. - AndroidEnvironment.initEnvironment(this) - startService(Intent(this, DNSBlockerService::class.java)) - startService(Intent(this, StatsIntentService::class.java)) } override fun onNewIntent(intent: Intent) { |