summaryrefslogtreecommitdiffstats
path: root/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/home/HomeMenuView.kt
blob: 7a94a7837a084ba9ee95cf64301c80b8ce77ac6a (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* 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 android.view.View
import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.Companion.PRIVATE
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import mozilla.appservices.fxaclient.FxaServer
import mozilla.appservices.fxaclient.contentUrl
import mozilla.appservices.places.BookmarkRoot
import mozilla.components.browser.menu.view.MenuButton
import mozilla.components.concept.sync.FxAEntryPoint
import mozilla.components.service.glean.private.NoExtras
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.HomeScreen
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.components.accounts.AccountState
import org.mozilla.fenix.components.accounts.FenixFxAEntryPoint
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.settings.deletebrowsingdata.deleteAndQuit
import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.whatsnew.WhatsNew
import java.lang.ref.WeakReference
import org.mozilla.fenix.GleanMetrics.HomeMenu as HomeMenuMetrics

/**
 * Helper class for building the [HomeMenu].
 *
 * @param view The [View] to attach the snackbar to.
 * @param context An Android [Context].
 * @param lifecycleOwner [LifecycleOwner] for the view.
 * @param homeActivity [HomeActivity] used to open URLs in a new tab.
 * @param navController [NavController] used for navigation.
 * @param menuButton The [MenuButton] that will be used to create a menu when the button is
 * clicked.
 * @param fxaEntrypoint The source entry point to FxA.
 */
class HomeMenuView(
    private val view: View,
    private val context: Context,
    private val lifecycleOwner: LifecycleOwner,
    private val homeActivity: HomeActivity,
    private val navController: NavController,
    private val menuButton: WeakReference<MenuButton>,
    private val fxaEntrypoint: FxAEntryPoint = FenixFxAEntryPoint.HomeMenu,
) {

    /**
     * Builds the [HomeMenu].
     */
    fun build() {
        HomeMenu(
            lifecycleOwner = lifecycleOwner,
            context = context,
            onItemTapped = ::onItemTapped,
            onHighlightPresent = { menuButton.get()?.setHighlight(it) },
            onMenuBuilderChanged = { menuButton.get()?.menuBuilder = it },
        )

        menuButton.get()?.setColorFilter(
            ContextCompat.getColor(
                context,
                ThemeManager.resolveAttribute(R.attr.textPrimary, context),
            ),
        )

        menuButton.get()?.register(
            object : mozilla.components.concept.menu.MenuButton.Observer {
                override fun onShow() {
                    // MenuButton used in [HomeMenuView] doesn't emit toolbar facts.
                    // A wrapper is responsible for that, but we are using the button
                    // directly, hence recording the event directly.
                    // Should investigate further: https://bugzilla.mozilla.org/show_bug.cgi?id=1868207
                    Events.toolbarMenuVisible.record(NoExtras())
                }
            },
        )
    }

    /**
     * Dismisses the menu.
     */
    fun dismissMenu() {
        menuButton.get()?.dismissMenu()
    }

    /**
     * Callback invoked when a menu item is tapped on.
     */
    @Suppress("LongMethod", "ComplexMethod")
    @VisibleForTesting(otherwise = PRIVATE)
    internal fun onItemTapped(item: HomeMenu.Item) {
        when (item) {
            HomeMenu.Item.Settings -> {
                HomeMenuMetrics.settingsItemClicked.record(NoExtras())

                navController.nav(
                    R.id.homeFragment,
                    HomeFragmentDirections.actionGlobalSettingsFragment(),
                )
            }
            HomeMenu.Item.CustomizeHome -> {
                HomeScreen.customizeHomeClicked.record(NoExtras())

                navController.nav(
                    R.id.homeFragment,
                    HomeFragmentDirections.actionGlobalHomeSettingsFragment(),
                )
            }
            is HomeMenu.Item.SyncAccount -> {
                navController.nav(
                    R.id.homeFragment,
                    when (item.accountState) {
                        AccountState.AUTHENTICATED ->
                            HomeFragmentDirections.actionGlobalAccountSettingsFragment()
                        AccountState.NEEDS_REAUTHENTICATION ->
                            HomeFragmentDirections.actionGlobalAccountProblemFragment(
                                entrypoint = fxaEntrypoint as FenixFxAEntryPoint,
                            )
                        AccountState.NO_ACCOUNT ->
                            HomeFragmentDirections.actionGlobalTurnOnSync(
                                entrypoint = fxaEntrypoint as FenixFxAEntryPoint,
                            )
                    },
                )
            }
            HomeMenu.Item.ManageAccountAndDevices -> {
                homeActivity.openToBrowserAndLoad(
                    searchTermOrURL =
                    if (context.settings().allowDomesticChinaFxaServer) {
                        FxaServer.China.contentUrl() + "/settings"
                    } else {
                        FxaServer.Release.contentUrl() + "/settings"
                    },
                    newTab = true,
                    from = BrowserDirection.FromHome,
                )
            }
            HomeMenu.Item.Bookmarks -> {
                navController.nav(
                    R.id.homeFragment,
                    HomeFragmentDirections.actionGlobalBookmarkFragment(BookmarkRoot.Mobile.id),
                )
            }
            HomeMenu.Item.History -> {
                navController.nav(
                    R.id.homeFragment,
                    HomeFragmentDirections.actionGlobalHistoryFragment(),
                )
            }
            HomeMenu.Item.Downloads -> {
                navController.nav(
                    R.id.homeFragment,
                    HomeFragmentDirections.actionGlobalDownloadsFragment(),
                )
            }
            HomeMenu.Item.Help -> {
                HomeMenuMetrics.helpTapped.record(NoExtras())
                homeActivity.openToBrowserAndLoad(
                    searchTermOrURL = SupportUtils.getSumoURLForTopic(
                        context = context,
                        topic = SupportUtils.SumoTopic.HELP,
                    ),
                    newTab = true,
                    from = BrowserDirection.FromHome,
                )
            }
            HomeMenu.Item.WhatsNew -> {
                WhatsNew.userViewedWhatsNew(context)
                Events.whatsNewTapped.record(NoExtras())

                homeActivity.openToBrowserAndLoad(
                    searchTermOrURL = SupportUtils.WHATS_NEW_URL,
                    newTab = true,
                    from = BrowserDirection.FromHome,
                )
            }
            HomeMenu.Item.Quit -> {
                // We need to show the snackbar while the browsing data is deleting (if "Delete
                // browsing data on quit" is activated). After the deletion is over, the snackbar
                // is dismissed.
                deleteAndQuit(
                    activity = homeActivity,
                    coroutineScope = lifecycleOwner.lifecycleScope,
                    snackbar = FenixSnackbar.make(
                        view = view,
                        isDisplayedWithBrowserToolbar = false,
                    ),
                )
            }
            HomeMenu.Item.ReconnectSync -> {
                navController.nav(
                    R.id.homeFragment,
                    HomeFragmentDirections.actionGlobalAccountProblemFragment(
                        entrypoint = fxaEntrypoint as FenixFxAEntryPoint,
                    ),
                )
            }
            HomeMenu.Item.Extensions -> {
                navController.nav(
                    R.id.homeFragment,
                    HomeFragmentDirections.actionGlobalAddonsManagementFragment(),
                )
            }
            is HomeMenu.Item.DesktopMode -> {
                context.settings().openNextTabInDesktopMode = item.checked
            }
        }
    }
}