TorQuickStart.kt 1.65 KB
Newer Older
1
2
3
4
5
6
7
8
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.tor.bootstrap

import android.content.Context
import android.content.SharedPreferences
9
import android.os.StrictMode
10
11
12
import androidx.annotation.VisibleForTesting
import mozilla.components.support.ktx.android.content.PreferencesHolder
import mozilla.components.support.ktx.android.content.booleanPreference
13
import org.mozilla.fenix.ext.resetPoliciesAfter
14
15
16

class TorQuickStart(context: Context) : PreferencesHolder {

17
18
19
20
21
22
    override val preferences: SharedPreferences = StrictMode.allowThreadDiskReads().resetPoliciesAfter {
        context.getSharedPreferences(
            PREF_NAME_TOR_BOOTSTRAP_KEY,
            Context.MODE_PRIVATE
        )
    }
23
24
25

    private var torQuickStart by booleanPreference(TOR_QUICK_START, default = false)

26
    fun quickStartTor() = StrictMode.allowThreadDiskReads().resetPoliciesAfter { torQuickStart }
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

    fun enableQuickStartTor() {
        torQuickStart = true
    }
    fun disableQuickStartTor() {
        torQuickStart = false
    }
    fun setQuickStartTor(enabled: Boolean) = if (enabled) {
        enableQuickStartTor()
    } else {
        disableQuickStartTor()
    }

    companion object {
        /**
         * Name of the shared preferences file.
         */
        private const val PREF_NAME_TOR_BOOTSTRAP_KEY = "tor.bootstrap"

        /**
         * Key for [quickStartTor].
         */
        @VisibleForTesting
        internal const val TOR_QUICK_START = "tor.bootstrap.quick_start_enabled"
    }
}