diff options
Diffstat (limited to 'app/src/main/java/foundation/e/privacycentralapp/domain')
3 files changed, 122 insertions, 2 deletions
diff --git a/app/src/main/java/foundation/e/privacycentralapp/domain/entities/InternetPrivacyMode.kt b/app/src/main/java/foundation/e/privacycentralapp/domain/entities/InternetPrivacyMode.kt index 879c435..534bb2f 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/domain/entities/InternetPrivacyMode.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/domain/entities/InternetPrivacyMode.kt @@ -18,5 +18,8 @@ package foundation.e.privacycentralapp.domain.entities enum class InternetPrivacyMode { - REAL_IP, HIDE_IP + REAL_IP, + HIDE_IP, + HIDE_IP_LOADING, + REAL_IP_LOADING } diff --git a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/GetQuickPrivacyStateUseCase.kt b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/GetQuickPrivacyStateUseCase.kt index 20ac0d9..db6f312 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/GetQuickPrivacyStateUseCase.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/GetQuickPrivacyStateUseCase.kt @@ -20,7 +20,8 @@ package foundation.e.privacycentralapp.domain.usecases import foundation.e.privacycentralapp.data.repositories.LocalStateRepository class GetQuickPrivacyStateUseCase(private val localStateRepository: LocalStateRepository) { - val isQuickPrivacyEnabled = localStateRepository.isQuickPrivacyEnabled + val quickPrivacyEnabledFlow = localStateRepository.quickPrivacyEnabledFlow + val isQuickPrivacyEnabled get() = localStateRepository.isQuickPrivacyEnabled fun toggle(): Boolean { val newState = !localStateRepository.isQuickPrivacyEnabled diff --git a/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/IpScramblingStateUseCase.kt b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/IpScramblingStateUseCase.kt new file mode 100644 index 0000000..a6bf50b --- /dev/null +++ b/app/src/main/java/foundation/e/privacycentralapp/domain/usecases/IpScramblingStateUseCase.kt @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2021 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.domain.usecases + +import android.content.Intent +import android.util.Log +import foundation.e.privacycentralapp.data.repositories.LocalStateRepository +import foundation.e.privacycentralapp.domain.entities.InternetPrivacyMode +import foundation.e.privacymodules.ipscramblermodule.IIpScramblerModule +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.callbackFlow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.launch + +class IpScramblingStateUseCase( + private val ipScramblerModule: IIpScramblerModule, + private val localStateRepository: LocalStateRepository, + private val coroutineScope: CoroutineScope +) { + + // private val internetPrivacyModeMutableFlow = MutableStateFlow(InternetPrivacyMode.REAL_IP) + val internetPrivacyMode: StateFlow<InternetPrivacyMode> = callbackFlow { + val listener = object : IIpScramblerModule.Listener { + override fun onStatusChanged(newStatus: IIpScramblerModule.Status) { + offer(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 = InternetPrivacyMode.REAL_IP + ) + + init { + coroutineScope.launch { + localStateRepository.quickPrivacyEnabledFlow.collect { + Log.d("testQPFlow", "QuickPrivacy enabled: $it") + applySettings(it, localStateRepository.isIpScramblingEnabled) + } + } + } + + fun toggle(hideIp: Boolean): Intent? { + if (!localStateRepository.isQuickPrivacyEnabled) return null + + localStateRepository.isIpScramblingEnabled = hideIp + return applySettings(true, hideIp) + } + + private fun applySettings(isQuickPrivacyEnabled: Boolean, isIpScramblingEnabled: Boolean): Intent? { + when { + isQuickPrivacyEnabled && isIpScramblingEnabled -> when (internetPrivacyMode.value) { + InternetPrivacyMode.REAL_IP, InternetPrivacyMode.REAL_IP_LOADING -> { + val intent = ipScramblerModule.prepareAndroidVpn() + if (intent != null) { + return intent + } else { + ipScramblerModule.start() + } + } + else -> { + Log.d("testQPFlow", "Not starting tor, already in started state") + } + } + else -> when (internetPrivacyMode.value) { + InternetPrivacyMode.HIDE_IP, InternetPrivacyMode.HIDE_IP_LOADING -> ipScramblerModule.stop() + + else -> { + Log.d("testQPFlow", "Not stoping tor, already in stop or stoping state") + } + } + } + return null + } + + private fun map(status: IIpScramblerModule.Status): InternetPrivacyMode { + return when (status) { + IIpScramblerModule.Status.OFF -> InternetPrivacyMode.REAL_IP + IIpScramblerModule.Status.ON -> InternetPrivacyMode.HIDE_IP + IIpScramblerModule.Status.STARTING -> InternetPrivacyMode.HIDE_IP_LOADING + IIpScramblerModule.Status.STOPPING, + IIpScramblerModule.Status.START_DISABLED -> InternetPrivacyMode.REAL_IP_LOADING + } + } +} |
