You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iceraven-browser/app/src/main/java/org/mozilla/fenix/home/HomeMenu.kt

300 lines
11 KiB
Kotlin

/* 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 androidx.core.content.ContextCompat.getColor
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.BrowserMenuHighlight
import mozilla.components.browser.menu.BrowserMenuItem
import mozilla.components.browser.menu.ext.getHighlight
import mozilla.components.browser.menu.item.BrowserMenuDivider
import mozilla.components.browser.menu.item.BrowserMenuHighlightableItem
import mozilla.components.browser.menu.item.BrowserMenuImageSwitch
import mozilla.components.browser.menu.item.BrowserMenuImageText
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import mozilla.components.concept.sync.AccountObserver
import mozilla.components.concept.sync.AuthType
import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.support.ktx.android.content.getColorFromAttr
import org.mozilla.fenix.Config
import org.mozilla.fenix.R
import org.mozilla.fenix.components.accounts.AccountState
import org.mozilla.fenix.components.accounts.FenixAccountManager
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.nimbus.FxNimbus
import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.whatsnew.WhatsNew
@Suppress("LargeClass", "LongMethod")
class HomeMenu(
private val lifecycleOwner: LifecycleOwner,
private val context: Context,
private val onItemTapped: (Item) -> Unit = {},
private val onMenuBuilderChanged: (BrowserMenuBuilder) -> Unit = {},
private val onHighlightPresent: (BrowserMenuHighlight) -> Unit = {},
) {
sealed class Item {
object Bookmarks : Item()
object History : Item()
object Downloads : Item()
object Extensions : Item()
data class SyncAccount(val accountState: AccountState) : Item()
/**
* A button item to open up the settings page of FxA, shown up in mozilla online builds.
*/
object ManageAccountAndDevices : Item()
object WhatsNew : Item()
object Help : Item()
object CustomizeHome : Item()
object Settings : Item()
object Quit : Item()
object ReconnectSync : Item()
data class DesktopMode(val checked: Boolean) : Item()
}
private val primaryTextColor = ThemeManager.resolveAttribute(R.attr.textPrimary, context)
private val syncDisconnectedColor =
ThemeManager.resolveAttribute(R.attr.syncDisconnected, context)
private val syncDisconnectedBackgroundColor =
context.getColorFromAttr(R.attr.syncDisconnectedBackground)
private val accountManager = FenixAccountManager(context)
// 'Reconnect' and 'Quit' items aren't needed most of the time, so we'll only create the if necessary.
private val reconnectToSyncItem by lazy {
BrowserMenuHighlightableItem(
context.getString(R.string.sync_reconnect),
R.drawable.ic_sync_disconnected,
iconTintColorResource = syncDisconnectedColor,
textColorResource = primaryTextColor,
highlight = BrowserMenuHighlight.HighPriority(
backgroundTint = syncDisconnectedBackgroundColor,
canPropagate = false,
),
isHighlighted = { true },
) {
onItemTapped.invoke(Item.ReconnectSync)
}
}
private val quitItem by lazy {
BrowserMenuImageText(
context.getString(R.string.delete_browsing_data_on_quit_action),
R.drawable.mozac_ic_quit,
primaryTextColor,
) {
onItemTapped.invoke(Item.Quit)
}
}
private fun syncSignInMenuItem(): BrowserMenuImageText? {
val syncItemTitle =
if (context.components.backgroundServices.accountManagerAvailableQueue.isReady()) {
if (context.settings().showDisplayNameInsteadofEmail) {
accountManager.accountProfileDisplayName
?: context.getString(R.string.sync_menu_sync_and_save_data)
} else {
accountManager.accountProfileEmail
?: context.getString(R.string.sync_menu_sync_and_save_data)
}
} else {
null
}
return when (syncItemTitle) {
null -> null
else -> {
BrowserMenuImageText(
syncItemTitle,
R.drawable.ic_signed_out,
primaryTextColor,
) {
onItemTapped.invoke(Item.SyncAccount(accountManager.accountState))
}
}
}
}
val desktopItem = BrowserMenuImageSwitch(
For #23350 - Revert changes from removing duplicate icons that already exists in ui-icons (#23527) * Revert "For #23121 - Override @color/mozac_ui_icons_fill with ?primaryText attribute" This reverts commit 12347c99999c340e7f7072ab2749cb13c5fd86c9. * Revert "For #23121 - Replace @drawble/ic_share with @drawable/mozac_ic_share" This reverts commit bbf6ce3f0c52e58493423ce2e2c62e3fb08f21b2. * Revert "For #23121 - Replace @drawble/ic_storage_enabled with @drawable/mozac_ic_storage" This reverts commit 930c7bf3b3076cf6b56be85fd733dd9f5cf2d1bb. * Revert "For #23121 - Replace @drawble/ic_notifications_enabled with @drawable/mozac_ic_notification" This reverts commit 9069b57c2440b517357a180e7c74b70ab86501a8. * Revert "For #23121 - Replace @drawble/ic_microphone_enabled with @drawable/mozac_ic_microphone" This reverts commit 53216f3f4a1fc6982aee8ab111cb6e92ef54f92a. * Revert "For #23121 - Replace @drawble/ic_location_enabled with @drawable/mozac_ic_location" This reverts commit 9ee9aafd879a884814f1d4b826db75f24ebdacec. * Revert "For #23121 - Replace @drawble/ic_autoplay_disabled with @drawable/mozac_ic_autoplay_blocked" This reverts commit b045a5e20317516bac422ed44549d5871a2cb96a. * Revert "For #23121 - Replace @drawble/ic_camera_enabled with @drawable/mozac_ic_video" This reverts commit 62842db131fee3aa0646c586f075f85105d32cb7. * Revert "For #23121 - Replace @drawble/mozac_ic_extensions_black with @drawable/mozac_ic_extensions" This reverts commit c020a9da10e6a49ce097b48fe2d40b7b21c578fe. * Revert "For #23121 - Replace @drawble/ic_top_sites with @drawable/mozac_ic_pin" This reverts commit ca67b0a752f9582a85ef5d22ff9cfba5f95fd1a0. * Revert "For #23121 - Replace @drawble/ic_search with @drawable/mozac_ic_search" This reverts commit 02d9197945ca9305cf3d2017954a48dd762a8a09. * Revert "For #23121 - Replace @drawble/ic_readermode with @drawable/mozac_ic_reader_mode" This reverts commit cf8592c709d26cf34a187175a2a0b4275a53a576. * Revert "For #23121 - Replace @drawble/ic_menu with @drawable/mozac_ic_menu" This reverts commit a1ac0190242ae416472442e3838318d210a6e419. * Revert "For #23121 - Replace @drawble/ic_login with @drawable/mozac_ic_login" This reverts commit 541c56b5894436c4384c7fdcb6ad3a57a566833a. * Revert "For #23121 - Replace @drawble/ic_internet with @drawable/mozac_ic_globe" This reverts commit 4d8adce85e07edce1969bd188698963c7452cac7. * Revert "For #23121 - Replace @drawble/ic_download with @drawable/mozac_ic_download" This reverts commit ef026d3ec290614053001a0709b800480ac59bf5. * Revert "For #23121 - Replace @drawble/ic_lock with @drawable/mozac_ic_lock" This reverts commit 18591d5dd848af6a11f1ebf4d37efc602a0f36e7. * Revert "For #23121 - Replace @drawble/ic_desktop with @drawable/mozac_ic_device_desktop" This reverts commit ad33e3c1e148bf20264a4cc08c524814da15409a. * Revert "For #23121 - Replace @drawble/ic_close with @drawable/mozac_ic_close" This reverts commit a9f0fefac21e63260a7e8547431a9a5af780faf1. * Revert "For #23121 - Replace @drawble/ic_delete with @drawable/mozac_ic_delete" This reverts commit 33dc752ef2864e8662eebe63756ea37360c4cf61. * Revert "For #23121 - Replace @drawble/ic_chevron_up with @drawable/mozac_ic_arrowhead_up" This reverts commit 5bf937cfd37e2fec2258d8e03e3f31d35952d9cc. * Revert "For #23121 - Replace @drawble/ic_chevron_down with @drawable/mozac_ic_arrowhead_down" This reverts commit 0fadd68112fc313f20be14c13ee75bb9ce67c472. * Revert "For #23121 - Replace @drawble/ic_back_button with @drawable/mozac_ic_back" This reverts commit bea766e7858e6a0bf38a8d1a74d79c8da68d204a. * Revert "For #23121 - Replace @drawable/ic_arrowhead_right with @drawable/mozac_ic_arrowhead_right" This reverts commit 5a6f349ea857cd2a5fdeb3b6001dcf52e8463025. * Revert "For #23121 - Replace @drawable/ic_new with @drawable/mozac_ic_new" This reverts commit ae38410106d7ed0eb65f3eda43ef45ecb7fecc73. * Revert "For #23121 - Replace @drawable/ic_addons_extensions with @drawable/mozac_ic_extensions" This reverts commit 9352946afce4d7be135bfe9b1ffc83895eb20b40.
2 years ago
imageResource = R.drawable.ic_desktop,
label = context.getString(R.string.browser_menu_desktop_site),
initialState = { context.settings().openNextTabInDesktopMode },
) { checked ->
onItemTapped.invoke(Item.DesktopMode(checked))
}
@Suppress("ComplexMethod")
private fun coreMenuItems(): List<BrowserMenuItem> {
val settings = context.components.settings
val bookmarksItem = BrowserMenuImageText(
context.getString(R.string.library_bookmarks),
R.drawable.ic_bookmark_list,
primaryTextColor,
) {
onItemTapped.invoke(Item.Bookmarks)
}
val historyItem = BrowserMenuImageText(
context.getString(R.string.library_history),
R.drawable.ic_history,
primaryTextColor,
) {
onItemTapped.invoke(Item.History)
}
val downloadsItem = BrowserMenuImageText(
context.getString(R.string.library_downloads),
For #23350 - Revert changes from removing duplicate icons that already exists in ui-icons (#23527) * Revert "For #23121 - Override @color/mozac_ui_icons_fill with ?primaryText attribute" This reverts commit 12347c99999c340e7f7072ab2749cb13c5fd86c9. * Revert "For #23121 - Replace @drawble/ic_share with @drawable/mozac_ic_share" This reverts commit bbf6ce3f0c52e58493423ce2e2c62e3fb08f21b2. * Revert "For #23121 - Replace @drawble/ic_storage_enabled with @drawable/mozac_ic_storage" This reverts commit 930c7bf3b3076cf6b56be85fd733dd9f5cf2d1bb. * Revert "For #23121 - Replace @drawble/ic_notifications_enabled with @drawable/mozac_ic_notification" This reverts commit 9069b57c2440b517357a180e7c74b70ab86501a8. * Revert "For #23121 - Replace @drawble/ic_microphone_enabled with @drawable/mozac_ic_microphone" This reverts commit 53216f3f4a1fc6982aee8ab111cb6e92ef54f92a. * Revert "For #23121 - Replace @drawble/ic_location_enabled with @drawable/mozac_ic_location" This reverts commit 9ee9aafd879a884814f1d4b826db75f24ebdacec. * Revert "For #23121 - Replace @drawble/ic_autoplay_disabled with @drawable/mozac_ic_autoplay_blocked" This reverts commit b045a5e20317516bac422ed44549d5871a2cb96a. * Revert "For #23121 - Replace @drawble/ic_camera_enabled with @drawable/mozac_ic_video" This reverts commit 62842db131fee3aa0646c586f075f85105d32cb7. * Revert "For #23121 - Replace @drawble/mozac_ic_extensions_black with @drawable/mozac_ic_extensions" This reverts commit c020a9da10e6a49ce097b48fe2d40b7b21c578fe. * Revert "For #23121 - Replace @drawble/ic_top_sites with @drawable/mozac_ic_pin" This reverts commit ca67b0a752f9582a85ef5d22ff9cfba5f95fd1a0. * Revert "For #23121 - Replace @drawble/ic_search with @drawable/mozac_ic_search" This reverts commit 02d9197945ca9305cf3d2017954a48dd762a8a09. * Revert "For #23121 - Replace @drawble/ic_readermode with @drawable/mozac_ic_reader_mode" This reverts commit cf8592c709d26cf34a187175a2a0b4275a53a576. * Revert "For #23121 - Replace @drawble/ic_menu with @drawable/mozac_ic_menu" This reverts commit a1ac0190242ae416472442e3838318d210a6e419. * Revert "For #23121 - Replace @drawble/ic_login with @drawable/mozac_ic_login" This reverts commit 541c56b5894436c4384c7fdcb6ad3a57a566833a. * Revert "For #23121 - Replace @drawble/ic_internet with @drawable/mozac_ic_globe" This reverts commit 4d8adce85e07edce1969bd188698963c7452cac7. * Revert "For #23121 - Replace @drawble/ic_download with @drawable/mozac_ic_download" This reverts commit ef026d3ec290614053001a0709b800480ac59bf5. * Revert "For #23121 - Replace @drawble/ic_lock with @drawable/mozac_ic_lock" This reverts commit 18591d5dd848af6a11f1ebf4d37efc602a0f36e7. * Revert "For #23121 - Replace @drawble/ic_desktop with @drawable/mozac_ic_device_desktop" This reverts commit ad33e3c1e148bf20264a4cc08c524814da15409a. * Revert "For #23121 - Replace @drawble/ic_close with @drawable/mozac_ic_close" This reverts commit a9f0fefac21e63260a7e8547431a9a5af780faf1. * Revert "For #23121 - Replace @drawble/ic_delete with @drawable/mozac_ic_delete" This reverts commit 33dc752ef2864e8662eebe63756ea37360c4cf61. * Revert "For #23121 - Replace @drawble/ic_chevron_up with @drawable/mozac_ic_arrowhead_up" This reverts commit 5bf937cfd37e2fec2258d8e03e3f31d35952d9cc. * Revert "For #23121 - Replace @drawble/ic_chevron_down with @drawable/mozac_ic_arrowhead_down" This reverts commit 0fadd68112fc313f20be14c13ee75bb9ce67c472. * Revert "For #23121 - Replace @drawble/ic_back_button with @drawable/mozac_ic_back" This reverts commit bea766e7858e6a0bf38a8d1a74d79c8da68d204a. * Revert "For #23121 - Replace @drawable/ic_arrowhead_right with @drawable/mozac_ic_arrowhead_right" This reverts commit 5a6f349ea857cd2a5fdeb3b6001dcf52e8463025. * Revert "For #23121 - Replace @drawable/ic_new with @drawable/mozac_ic_new" This reverts commit ae38410106d7ed0eb65f3eda43ef45ecb7fecc73. * Revert "For #23121 - Replace @drawable/ic_addons_extensions with @drawable/mozac_ic_extensions" This reverts commit 9352946afce4d7be135bfe9b1ffc83895eb20b40.
2 years ago
R.drawable.ic_download,
primaryTextColor,
) {
onItemTapped.invoke(Item.Downloads)
}
val extensionsItem = BrowserMenuImageText(
context.getString(R.string.browser_menu_add_ons),
For #23350 - Revert changes from removing duplicate icons that already exists in ui-icons (#23527) * Revert "For #23121 - Override @color/mozac_ui_icons_fill with ?primaryText attribute" This reverts commit 12347c99999c340e7f7072ab2749cb13c5fd86c9. * Revert "For #23121 - Replace @drawble/ic_share with @drawable/mozac_ic_share" This reverts commit bbf6ce3f0c52e58493423ce2e2c62e3fb08f21b2. * Revert "For #23121 - Replace @drawble/ic_storage_enabled with @drawable/mozac_ic_storage" This reverts commit 930c7bf3b3076cf6b56be85fd733dd9f5cf2d1bb. * Revert "For #23121 - Replace @drawble/ic_notifications_enabled with @drawable/mozac_ic_notification" This reverts commit 9069b57c2440b517357a180e7c74b70ab86501a8. * Revert "For #23121 - Replace @drawble/ic_microphone_enabled with @drawable/mozac_ic_microphone" This reverts commit 53216f3f4a1fc6982aee8ab111cb6e92ef54f92a. * Revert "For #23121 - Replace @drawble/ic_location_enabled with @drawable/mozac_ic_location" This reverts commit 9ee9aafd879a884814f1d4b826db75f24ebdacec. * Revert "For #23121 - Replace @drawble/ic_autoplay_disabled with @drawable/mozac_ic_autoplay_blocked" This reverts commit b045a5e20317516bac422ed44549d5871a2cb96a. * Revert "For #23121 - Replace @drawble/ic_camera_enabled with @drawable/mozac_ic_video" This reverts commit 62842db131fee3aa0646c586f075f85105d32cb7. * Revert "For #23121 - Replace @drawble/mozac_ic_extensions_black with @drawable/mozac_ic_extensions" This reverts commit c020a9da10e6a49ce097b48fe2d40b7b21c578fe. * Revert "For #23121 - Replace @drawble/ic_top_sites with @drawable/mozac_ic_pin" This reverts commit ca67b0a752f9582a85ef5d22ff9cfba5f95fd1a0. * Revert "For #23121 - Replace @drawble/ic_search with @drawable/mozac_ic_search" This reverts commit 02d9197945ca9305cf3d2017954a48dd762a8a09. * Revert "For #23121 - Replace @drawble/ic_readermode with @drawable/mozac_ic_reader_mode" This reverts commit cf8592c709d26cf34a187175a2a0b4275a53a576. * Revert "For #23121 - Replace @drawble/ic_menu with @drawable/mozac_ic_menu" This reverts commit a1ac0190242ae416472442e3838318d210a6e419. * Revert "For #23121 - Replace @drawble/ic_login with @drawable/mozac_ic_login" This reverts commit 541c56b5894436c4384c7fdcb6ad3a57a566833a. * Revert "For #23121 - Replace @drawble/ic_internet with @drawable/mozac_ic_globe" This reverts commit 4d8adce85e07edce1969bd188698963c7452cac7. * Revert "For #23121 - Replace @drawble/ic_download with @drawable/mozac_ic_download" This reverts commit ef026d3ec290614053001a0709b800480ac59bf5. * Revert "For #23121 - Replace @drawble/ic_lock with @drawable/mozac_ic_lock" This reverts commit 18591d5dd848af6a11f1ebf4d37efc602a0f36e7. * Revert "For #23121 - Replace @drawble/ic_desktop with @drawable/mozac_ic_device_desktop" This reverts commit ad33e3c1e148bf20264a4cc08c524814da15409a. * Revert "For #23121 - Replace @drawble/ic_close with @drawable/mozac_ic_close" This reverts commit a9f0fefac21e63260a7e8547431a9a5af780faf1. * Revert "For #23121 - Replace @drawble/ic_delete with @drawable/mozac_ic_delete" This reverts commit 33dc752ef2864e8662eebe63756ea37360c4cf61. * Revert "For #23121 - Replace @drawble/ic_chevron_up with @drawable/mozac_ic_arrowhead_up" This reverts commit 5bf937cfd37e2fec2258d8e03e3f31d35952d9cc. * Revert "For #23121 - Replace @drawble/ic_chevron_down with @drawable/mozac_ic_arrowhead_down" This reverts commit 0fadd68112fc313f20be14c13ee75bb9ce67c472. * Revert "For #23121 - Replace @drawble/ic_back_button with @drawable/mozac_ic_back" This reverts commit bea766e7858e6a0bf38a8d1a74d79c8da68d204a. * Revert "For #23121 - Replace @drawable/ic_arrowhead_right with @drawable/mozac_ic_arrowhead_right" This reverts commit 5a6f349ea857cd2a5fdeb3b6001dcf52e8463025. * Revert "For #23121 - Replace @drawable/ic_new with @drawable/mozac_ic_new" This reverts commit ae38410106d7ed0eb65f3eda43ef45ecb7fecc73. * Revert "For #23121 - Replace @drawable/ic_addons_extensions with @drawable/mozac_ic_extensions" This reverts commit 9352946afce4d7be135bfe9b1ffc83895eb20b40.
2 years ago
R.drawable.ic_addons_extensions,
primaryTextColor,
) {
onItemTapped.invoke(Item.Extensions)
}
val manageAccountAndDevicesItem = SimpleBrowserMenuItem(
context.getString(R.string.browser_menu_manage_account_and_devices),
textColorResource = primaryTextColor,
) {
onItemTapped.invoke(Item.ManageAccountAndDevices)
}
val whatsNewItem = BrowserMenuHighlightableItem(
context.getString(R.string.browser_menu_whats_new),
R.drawable.ic_whats_new,
iconTintColorResource = primaryTextColor,
highlight = BrowserMenuHighlight.LowPriority(
notificationTint = getColor(context, R.color.fx_mobile_icon_color_information),
),
isHighlighted = { WhatsNew.shouldHighlightWhatsNew(context) },
) {
onItemTapped.invoke(Item.WhatsNew)
}
val helpItem = BrowserMenuImageText(
context.getString(R.string.browser_menu_help),
R.drawable.mozac_ic_help,
primaryTextColor,
) {
onItemTapped.invoke(Item.Help)
}
val customizeHomeItem = BrowserMenuImageText(
context.getString(R.string.browser_menu_customize_home_1),
R.drawable.ic_customize,
primaryTextColor,
) {
onItemTapped.invoke(Item.CustomizeHome)
}
// Use nimbus to set the icon and title.
val nimbusValidation = FxNimbus.features.nimbusValidation.value()
val settingsItem = BrowserMenuImageText(
nimbusValidation.settingsTitle,
R.drawable.mozac_ic_settings,
primaryTextColor,
) {
onItemTapped.invoke(Item.Settings)
}
// Only query account manager if it has been initialized.
// We don't want to cause its initialization just for this check.
val accountAuthItem =
if (context.components.backgroundServices.accountManagerAvailableQueue.isReady() &&
context.components.backgroundServices.accountManager.accountNeedsReauth()
) {
reconnectToSyncItem
} else {
null
}
val menuItems = listOfNotNull(
bookmarksItem,
historyItem,
downloadsItem,
extensionsItem,
syncSignInMenuItem(),
accountAuthItem,
if (Config.channel.isMozillaOnline) manageAccountAndDevicesItem else null,
BrowserMenuDivider(),
desktopItem,
BrowserMenuDivider(),
whatsNewItem,
helpItem,
customizeHomeItem,
settingsItem,
if (settings.shouldDeleteBrowsingDataOnQuit) quitItem else null,
).also { items ->
items.getHighlight()?.let { onHighlightPresent(it) }
}
return menuItems
}
init {
val menuItems = coreMenuItems()
// Report initial state.
onMenuBuilderChanged(BrowserMenuBuilder(menuItems))
// Observe account state changes, and update menu item builder with a new set of items.
context.components.backgroundServices.accountManagerAvailableQueue.runIfReadyOrQueue {
// This task isn't relevant if our parent fragment isn't around anymore.
if (lifecycleOwner.lifecycle.currentState == Lifecycle.State.DESTROYED) {
return@runIfReadyOrQueue
}
context.components.backgroundServices.accountManager.register(
object : AccountObserver {
override fun onAuthenticationProblems() {
lifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
onMenuBuilderChanged(
BrowserMenuBuilder(
menuItems,
),
)
}
}
override fun onAuthenticated(account: OAuthAccount, authType: AuthType) {
lifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
onMenuBuilderChanged(
BrowserMenuBuilder(
menuItems,
),
)
}
}
override fun onLoggedOut() {
lifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
onMenuBuilderChanged(
BrowserMenuBuilder(
menuItems,
),
)
}
}
},
lifecycleOwner,
)
}
}
}