Mode.kt 3.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/* 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.home

import android.content.Context
import mozilla.components.concept.sync.AccountObserver
import mozilla.components.concept.sync.AuthType
import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.concept.sync.Profile
import mozilla.components.service.fxa.sharing.ShareableAccount
13
14
15
import org.mozilla.fenix.tor.TorController
import org.mozilla.fenix.tor.TorEvents
import org.mozilla.fenix.tor.bootstrap.TorQuickStart
16
17
18
19
20
21
22
23
24
25
26
27
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.onboarding.FenixOnboarding

/**
 * Describes various states of the home fragment UI.
 */
sealed class Mode {
    object Normal : Mode()
    object Private : Mode()
    data class Onboarding(val state: OnboardingState) : Mode()
28
    object Bootstrap : Mode()
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

    companion object {
        fun fromBrowsingMode(browsingMode: BrowsingMode) = when (browsingMode) {
            BrowsingMode.Normal -> Normal
            BrowsingMode.Private -> Private
        }
    }
}

/**
 * Describes various onboarding states.
 */
sealed class OnboardingState {
    // Signed out, without an option to auto-login using a shared FxA account.
    object SignedOutNoAutoSignIn : OnboardingState()
    // Signed out, with an option to auto-login into a shared FxA account.
    data class SignedOutCanAutoSignIn(val withAccount: ShareableAccount) : OnboardingState()
    // Signed in.
    object SignedIn : OnboardingState()
}

50
@SuppressWarnings("LongParameterList", "TooManyFunctions")
51
52
53
class CurrentMode(
    private val context: Context,
    private val onboarding: FenixOnboarding,
54
55
56
    private val torQuickStart: TorQuickStart,
    private val shouldStartTor: Boolean,
    private val torController: TorController,
57
    private val browsingModeManager: BrowsingModeManager,
58
    private val dispatchModeChanges: (mode: Mode) -> Unit
59
) : AccountObserver, TorEvents {
60

61
    private val accountManager by lazy { context.components.backgroundServices.accountManager }
62

63
64
65
66
67
68
69
    init {
        torController.registerTorListener(this)
    }

    fun getCurrentMode() = if (shouldStartTor && (!torQuickStart.quickStartTor() && !torController.isBootstrapped)) {
        Mode.Bootstrap
    } else if (onboarding.userHasBeenOnboarded()) {
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        Mode.fromBrowsingMode(browsingModeManager.mode)
    } else {
        val account = accountManager.authenticatedAccount()
        if (account != null) {
            Mode.Onboarding(OnboardingState.SignedIn)
        } else {
            val availableAccounts = accountManager.shareableAccounts(context)
            if (availableAccounts.isEmpty()) {
                Mode.Onboarding(OnboardingState.SignedOutNoAutoSignIn)
            } else {
                Mode.Onboarding(OnboardingState.SignedOutCanAutoSignIn(availableAccounts[0]))
            }
        }
    }

    fun emitModeChanges() {
86
        dispatchModeChanges(getCurrentMode())
87
88
    }

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    @SuppressWarnings("EmptyFunctionBlock")
    override fun onTorConnecting() {
    }

    override fun onTorConnected() {
        dispatchModeChanges(getCurrentMode())
    }

    override fun onTorStopped() {
        dispatchModeChanges(getCurrentMode())
    }

    @SuppressWarnings("EmptyFunctionBlock")
    override fun onTorStatusUpdate(entry: String?, status: String?) {
    }

    fun unregisterTorListener() {
        torController.unregisterTorListener(this)
    }

109
110
111
112
113
    override fun onAuthenticated(account: OAuthAccount, authType: AuthType) = emitModeChanges()
    override fun onAuthenticationProblems() = emitModeChanges()
    override fun onLoggedOut() = emitModeChanges()
    override fun onProfileUpdated(profile: Profile) = emitModeChanges()
}