summaryrefslogtreecommitdiff
path: root/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/IpScramblingStateUseCase.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/foundation/e/advancedprivacy/domain/usecases/IpScramblingStateUseCase.kt')
-rw-r--r--app/src/main/java/foundation/e/advancedprivacy/domain/usecases/IpScramblingStateUseCase.kt99
1 files changed, 33 insertions, 66 deletions
diff --git a/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/IpScramblingStateUseCase.kt b/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/IpScramblingStateUseCase.kt
index 9c89329..79c79f7 100644
--- a/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/IpScramblingStateUseCase.kt
+++ b/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/IpScramblingStateUseCase.kt
@@ -23,74 +23,40 @@ import foundation.e.advancedprivacy.common.isStandaloneBuild
import foundation.e.advancedprivacy.data.repositories.AppListsRepository
import foundation.e.advancedprivacy.data.repositories.LocalStateRepository
import foundation.e.advancedprivacy.domain.entities.ApplicationDescription
-import foundation.e.advancedprivacy.domain.entities.InternetPrivacyMode
-import foundation.e.advancedprivacy.domain.entities.InternetPrivacyMode.HIDE_IP
-import foundation.e.advancedprivacy.domain.entities.InternetPrivacyMode.HIDE_IP_LOADING
-import foundation.e.advancedprivacy.domain.entities.InternetPrivacyMode.REAL_IP
-import foundation.e.advancedprivacy.domain.entities.InternetPrivacyMode.REAL_IP_LOADING
+import foundation.e.advancedprivacy.domain.entities.FeatureServiceState
import foundation.e.advancedprivacy.externalinterfaces.permissions.IPermissionsPrivacyModule
-import foundation.e.advancedprivacy.ipscrambler.IpScramblerModule
+import foundation.e.advancedprivacy.ipscrambler.OrbotServiceSupervisor
+import foundation.e.advancedprivacy.trackers.domain.externalinterfaces.TrackersServiceSupervisor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.channels.awaitClose
-import kotlinx.coroutines.delay
-import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
-import kotlinx.coroutines.flow.callbackFlow
-import kotlinx.coroutines.flow.stateIn
+import kotlinx.coroutines.flow.launchIn
+import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
class IpScramblingStateUseCase(
- private val ipScramblerModule: IpScramblerModule,
+ private val orbotServiceSupervisor: OrbotServiceSupervisor,
private val permissionsPrivacyModule: IPermissionsPrivacyModule,
private val appDesc: ApplicationDescription,
private val localStateRepository: LocalStateRepository,
private val appListsRepository: AppListsRepository,
+ private val trackersServiceSupervisor: TrackersServiceSupervisor,
private val coroutineScope: CoroutineScope
) {
- val internetPrivacyMode: StateFlow<InternetPrivacyMode> = callbackFlow {
- val listener = object : IpScramblerModule.Listener {
- override fun onStatusChanged(newStatus: IpScramblerModule.Status) {
- trySend(map(newStatus))
- }
-
- override fun log(message: String) {}
- override fun onTrafficUpdate(
- upload: Long,
- download: Long,
- read: Long,
- write: Long
- ) {
- }
- }
- ipScramblerModule.addListener(listener)
- ipScramblerModule.requestStatus()
- awaitClose { ipScramblerModule.removeListener(listener) }
- }.stateIn(
- scope = coroutineScope,
- started = SharingStarted.Eagerly,
- initialValue = REAL_IP
- )
+ val internetPrivacyMode: StateFlow<FeatureServiceState> = orbotServiceSupervisor.state
init {
+ orbotServiceSupervisor.requestStatus()
+
coroutineScope.launch(Dispatchers.Default) {
localStateRepository.ipScramblingSetting.collect {
applySettings(it)
}
}
- coroutineScope.launch(Dispatchers.IO) {
- internetPrivacyMode.collect {
- if (
- it == REAL_IP &&
- localStateRepository.internetPrivacyMode.value == REAL_IP_LOADING
- ) {
- // Wait for orbot to relax before allowing user to reactivate it.
- delay(1000)
- }
- localStateRepository.internetPrivacyMode.value = it
- }
- }
+ orbotServiceSupervisor.state.map {
+ localStateRepository.internetPrivacyMode.value = it
+ }.launchIn(coroutineScope)
}
fun toggle(hideIp: Boolean) {
@@ -102,7 +68,7 @@ class IpScramblingStateUseCase(
}
val bypassTorApps: Set<String> get() {
- var whitelist = ipScramblerModule.appList
+ var whitelist = orbotServiceSupervisor.appList
if (getHiddenPackageNames().any { it in whitelist }) {
val mutable = whitelist.toMutableSet()
mutable.removeAll(getHiddenPackageNames())
@@ -120,7 +86,7 @@ class IpScramblingStateUseCase(
fun toggleBypassTor(packageName: String) {
val visibleList = bypassTorApps.toMutableSet()
- val rawList = ipScramblerModule.appList.toMutableSet()
+ val rawList = orbotServiceSupervisor.appList.toMutableSet()
if (visibleList.contains(packageName)) {
if (packageName == appListsRepository.dummySystemApp.packageName) {
@@ -139,24 +105,34 @@ class IpScramblingStateUseCase(
rawList.add(packageName)
}
}
- ipScramblerModule.appList = rawList
+ orbotServiceSupervisor.appList = rawList
+ }
+
+ val availablesLocations: List<String> = orbotServiceSupervisor.getAvailablesLocations().sorted()
+
+ val exitCountry: String get() = orbotServiceSupervisor.getExitCountryCode()
+
+ suspend fun setExitCountry(locationId: String) {
+ if (locationId != exitCountry) {
+ orbotServiceSupervisor.setExitCountryCode(locationId)
+ }
}
private suspend fun applySettings(isIpScramblingEnabled: Boolean) {
val currentMode = localStateRepository.internetPrivacyMode.value
when {
- isIpScramblingEnabled && currentMode in setOf(REAL_IP, REAL_IP_LOADING) ->
+ isIpScramblingEnabled && currentMode in setOf(FeatureServiceState.OFF, FeatureServiceState.STOPPING) ->
applyStartIpScrambling()
- !isIpScramblingEnabled && currentMode in setOf(HIDE_IP, HIDE_IP_LOADING) ->
- ipScramblerModule.stop()
+ !isIpScramblingEnabled && currentMode in setOf(FeatureServiceState.ON, FeatureServiceState.STARTING) ->
+ orbotServiceSupervisor.stop()
else -> {}
}
}
private suspend fun applyStartIpScrambling() {
- val authorizeVpnIntent = ipScramblerModule.prepareAndroidVpn()
+ val authorizeVpnIntent = orbotServiceSupervisor.prepareAndroidVpn()
if (authorizeVpnIntent == null) {
localStateRepository.emitStartVpnDisclaimer(null)
@@ -190,17 +166,8 @@ class IpScramblingStateUseCase(
}
fun startIpScrambling() {
- localStateRepository.internetPrivacyMode.value = HIDE_IP_LOADING
- ipScramblerModule.start(enableNotification = isStandaloneBuild)
- }
-
- private fun map(status: IpScramblerModule.Status): InternetPrivacyMode {
- return when (status) {
- IpScramblerModule.Status.OFF -> REAL_IP
- IpScramblerModule.Status.ON -> HIDE_IP
- IpScramblerModule.Status.STARTING -> HIDE_IP_LOADING
- IpScramblerModule.Status.STOPPING,
- IpScramblerModule.Status.START_DISABLED -> REAL_IP_LOADING
- }
+ localStateRepository.internetPrivacyMode.value = FeatureServiceState.STARTING
+ orbotServiceSupervisor.setDNSFilter((trackersServiceSupervisor.dnsFilterForIpScrambling))
+ orbotServiceSupervisor.start(enableNotification = isStandaloneBuild)
}
}