summaryrefslogtreecommitdiff
path: root/app/src/main/java/foundation/e/advancedprivacy/data/repositories/LocalStateRepository.kt
blob: 3f73c78900f6221d7d0b28aa5d6097ff59c86855 (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
/*
 * 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.advancedprivacy.data.repositories

import android.content.Context
import foundation.e.advancedprivacy.domain.entities.InternetPrivacyMode
import foundation.e.advancedprivacy.domain.entities.LocationMode
import foundation.e.privacymodules.permissions.data.ApplicationDescription
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

class LocalStateRepository(context: Context) {
    companion object {
        private const val SHARED_PREFS_FILE = "localState"
        private const val KEY_BLOCK_TRACKERS = "blockTrackers"
        private const val KEY_IP_SCRAMBLING = "ipScrambling"
        private const val KEY_FAKE_LOCATION = "fakeLocation"
        private const val KEY_FAKE_LATITUDE = "fakeLatitude"
        private const val KEY_FAKE_LONGITUDE = "fakeLongitude"
        private const val KEY_FIRST_BOOT = "firstBoot"
        private const val KEY_HIDE_WARNING_TRACKERS = "hide_warning_trackers"
        private const val KEY_HIDE_WARNING_LOCATION = "hide_warning_location"
        private const val KEY_HIDE_WARNING_IPSCRAMBLING = "hide_warning_ipscrambling"
    }

    private val sharedPref = context.getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE)

    private val _blockTrackers = MutableStateFlow(sharedPref.getBoolean(KEY_BLOCK_TRACKERS, true))
    val blockTrackers = _blockTrackers.asStateFlow()

    fun setBlockTrackers(enabled: Boolean) {
        set(KEY_BLOCK_TRACKERS, enabled)
        _blockTrackers.update { enabled }
    }

    val areAllTrackersBlocked: MutableStateFlow<Boolean> = MutableStateFlow(false)

    private val _fakeLocationEnabled = MutableStateFlow(sharedPref.getBoolean(KEY_FAKE_LOCATION, false))

    val fakeLocationEnabled = _fakeLocationEnabled.asStateFlow()

    fun setFakeLocationEnabled(enabled: Boolean) {
        set(KEY_FAKE_LOCATION, enabled)
        _fakeLocationEnabled.update { enabled }
    }

    var fakeLocation: Pair<Float, Float>
        get() = Pair(
            // Initial default value is Quezon City
            sharedPref.getFloat(KEY_FAKE_LATITUDE, 14.6760f),
            sharedPref.getFloat(KEY_FAKE_LONGITUDE, 121.0437f)
        )

        set(value) {
            sharedPref.edit()
                .putFloat(KEY_FAKE_LATITUDE, value.first)
                .putFloat(KEY_FAKE_LONGITUDE, value.second)
                .apply()
        }

    val locationMode: MutableStateFlow<LocationMode> = MutableStateFlow(LocationMode.REAL_LOCATION)

    private val _ipScramblingSetting = MutableStateFlow(sharedPref.getBoolean(KEY_IP_SCRAMBLING, false))
    val ipScramblingSetting = _ipScramblingSetting.asStateFlow()

    fun setIpScramblingSetting(enabled: Boolean) {
        set(KEY_IP_SCRAMBLING, enabled)
        _ipScramblingSetting.update { enabled }
    }

    val internetPrivacyMode: MutableStateFlow<InternetPrivacyMode> = MutableStateFlow(InternetPrivacyMode.REAL_IP)

    private val _otherVpnRunning = MutableSharedFlow<ApplicationDescription>()
    suspend fun emitOtherVpnRunning(appDesc: ApplicationDescription) {
        _otherVpnRunning.emit(appDesc)
    }
    val otherVpnRunning: SharedFlow<ApplicationDescription> = _otherVpnRunning

    var firstBoot: Boolean
        get() = sharedPref.getBoolean(KEY_FIRST_BOOT, true)
        set(value) = set(KEY_FIRST_BOOT, value)

    var hideWarningTrackers: Boolean
        get() = sharedPref.getBoolean(KEY_HIDE_WARNING_TRACKERS, false)
        set(value) = set(KEY_HIDE_WARNING_TRACKERS, value)

    var hideWarningLocation: Boolean
        get() = sharedPref.getBoolean(KEY_HIDE_WARNING_LOCATION, false)
        set(value) = set(KEY_HIDE_WARNING_LOCATION, value)

    var hideWarningIpScrambling: Boolean
        get() = sharedPref.getBoolean(KEY_HIDE_WARNING_IPSCRAMBLING, false)
        set(value) = set(KEY_HIDE_WARNING_IPSCRAMBLING, value)

    private fun set(key: String, value: Boolean) {
        sharedPref.edit().putBoolean(key, value).apply()
    }
}