summaryrefslogtreecommitdiff
path: root/app/src/main/java/foundation/e/advancedprivacy/data/repositories/LocalStateRepository.kt
blob: fd309a6aa77aff590660024cf0901980b69e6b85 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (C) 2023 MURENA SAS
 * 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.ApplicationDescription
import foundation.e.advancedprivacy.domain.entities.FeatureState
import foundation.e.advancedprivacy.domain.entities.LocationMode
import foundation.e.advancedprivacy.domain.entities.MainFeatures
import foundation.e.advancedprivacy.domain.repositories.LocalStateRepository
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 LocalStateRepositoryImpl(context: Context) : LocalStateRepository {
    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_ALTITUDE = "fakeAltitude"
        private const val KEY_FAKE_SPEED = "fakeSpeed"
        private const val KEY_FAKE_JITTER = "fakeJitter"
        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))

    override val blockTrackers = _blockTrackers.asStateFlow()

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

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

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

    override val fakeLocationEnabled = _fakeLocationEnabled.asStateFlow()

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

    override var fakeAltitude: Float
        get() = sharedPref.getFloat(KEY_FAKE_ALTITUDE, 3.0f)
        set(value) {
            sharedPref.edit()
                .putFloat(KEY_FAKE_ALTITUDE, value)
                .apply()
        }

    override var fakeSpeed: Float
        get() = sharedPref.getFloat(KEY_FAKE_SPEED, 1.0f)
        set(value) {
            sharedPref.edit()
                .putFloat(KEY_FAKE_SPEED, value)
                .apply()
        }

    override var fakeJitter: Float
        get() = sharedPref.getFloat(KEY_FAKE_JITTER, 3.0f)
        set(value) {
            sharedPref.edit()
                .putFloat(KEY_FAKE_JITTER, value)
                .apply()
        }

    override 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()
        }

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

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

    override val ipScramblingSetting = _ipScramblingSetting.asStateFlow()

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

    override val internetPrivacyMode: MutableStateFlow<FeatureState> = MutableStateFlow(FeatureState.OFF)

    private val _startVpnDisclaimer = MutableSharedFlow<MainFeatures>()

    override suspend fun emitStartVpnDisclaimer(feature: MainFeatures) {
        _startVpnDisclaimer.emit(feature)
    }

    override val startVpnDisclaimer: SharedFlow<MainFeatures> = _startVpnDisclaimer

    private val _otherVpnRunning = MutableSharedFlow<ApplicationDescription>()

    override suspend fun emitOtherVpnRunning(appDesc: ApplicationDescription) {
        _otherVpnRunning.emit(appDesc)
    }

    override val otherVpnRunning: SharedFlow<ApplicationDescription> = _otherVpnRunning

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

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

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

    override 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()
    }
}