Skip to content

Commit

Permalink
For mozilla-mobile#5694: Adds changing toolbar position functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sblatz committed Nov 25, 2019
1 parent 9035e4f commit ce8913d
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ toolbar_settings:
bugs:
- https://github.com/mozilla-mobile/fenix/issue/6054
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/TODO
- https://github.com/mozilla-mobile/fenix/pull/6571
notification_emails:
- [email protected]
expires: "2020-03-01"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.navigation.NavDirections
import androidx.navigation.fragment.findNavController
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.component_search.*
import kotlinx.android.synthetic.main.component_browser_toolbar.*
import kotlinx.android.synthetic.main.fragment_browser.*
import kotlinx.android.synthetic.main.fragment_browser.view.*
import kotlinx.coroutines.Dispatchers.IO
Expand Down Expand Up @@ -197,6 +198,7 @@ abstract class BaseBrowserFragment : Fragment(), BackHandler, SessionManager.Obs

browserToolbarView = BrowserToolbarView(
container = view.browserLayout,
shouldUseBottomToolbar = context.settings().shouldUseBottomToolbar,
interactor = browserInteractor,
customTabSession = customTabSessionId?.let { sessionManager.findSessionById(it) }
)
Expand Down
71 changes: 70 additions & 1 deletion app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.accessibility.AccessibilityManager
import android.widget.Button
import android.widget.RadioButton
import androidx.appcompat.widget.AppCompatImageView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.transition.TransitionInflater
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.fragment_browser.view.*
import kotlinx.android.synthetic.main.fragment_browser.*
import kotlinx.android.synthetic.main.fragment_browser.view.*
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.tracking_protection_onboarding_popup.view.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import mozilla.components.browser.session.Session
import mozilla.components.feature.contextmenu.ContextMenuCandidate
Expand Down Expand Up @@ -117,12 +125,73 @@ class BrowserFragment : BaseBrowserFragment(), BackHandler {
override fun onStart() {
super.onStart()
subscribeToTabCollections()

val toolbarSessionObserver = TrackingProtectionOverlay(
context = requireContext(),
settings = requireContext().settings()
) {
browserToolbarView.view

getSessionById()?.register(toolbarSessionObserver, this, autoPause = true)

val accessibilityManager = activity?.getSystemService(Context.ACCESSIBILITY_SERVICE) as? AccessibilityManager
accessibilityManager?.addTouchExplorationStateChangeListener {
updateToolbar()
}

updateToolbar()
}

private fun updateToolbar() {
val browserEngine = swipeRefresh.layoutParams as CoordinatorLayout.LayoutParams
val toolbarParams = browserToolbarView.view.layoutParams as CoordinatorLayout.LayoutParams

toolbarParams.gravity = if (requireContext().settings().shouldUseBottomToolbar) {
browserEngine.bottomMargin = requireContext().dimen(R.dimen.browser_toolbar_height)
Gravity.BOTTOM
} else {
browserEngine.bottomMargin = 0
Gravity.TOP
}

getSessionById()?.loading?.let {
setToolbarBehavior(it)
}
}

private fun setToolbarBehavior(loading: Boolean) {
(browserToolbarView.view.layoutParams as CoordinatorLayout.LayoutParams).apply {
// Stop toolbar from collapsing if TalkBack is enabled or page is loading
val accessibilityManager = context?.getSystemService(Context.ACCESSIBILITY_SERVICE) as? AccessibilityManager
val shouldUseBottomToolbar = requireContext().settings().shouldUseBottomToolbar

behavior = when {
loading || accessibilityManager?.isTouchExplorationEnabled == true -> {
(behavior as? BrowserToolbarTopBehavior)?.forceExpand(browserToolbarView.view)
null
}
shouldUseBottomToolbar -> {
// Do not use a dynamic behavior for the bottom toolbar
null
}
!shouldUseBottomToolbar -> {
BrowserToolbarTopBehavior(context, null)
}
else -> {
null
}
}
}
}

private val toolbarSessionObserver = object : Session.Observer {
override fun onLoadingStateChanged(session: Session, loading: Boolean) {
setToolbarBehavior(loading)
if (!loading &&
shouldShowTrackingProtectionOnboarding(session)
) {
showTrackingProtectionOnboarding()
}
}
getSessionById()?.register(toolbarSessionObserver, this, autoPause = true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ private val Event.wrapper: EventWrapper<*>?
)
is Event.ViewLoginPassword -> EventWrapper<NoExtraKeys>(
{ Logins.viewPasswordLogin.record(it) }
)
is Event.ToolbarPositionChanged -> EventWrapper(
{ ToolbarSettings.changedPosition.record(it) },
{ ToolbarSettings.changedPositionKeys.valueOf(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.browser_toolbar_popup_window.view.copy
import kotlinx.android.synthetic.main.browser_toolbar_popup_window.view.paste
import kotlinx.android.synthetic.main.browser_toolbar_popup_window.view.paste_and_go
import kotlinx.android.synthetic.main.browser_toolbar_popup_window.view.*
import mozilla.components.browser.domains.autocomplete.ShippedDomainsProvider
import mozilla.components.browser.session.Session
import mozilla.components.browser.toolbar.BrowserToolbar
Expand All @@ -41,18 +39,16 @@ interface BrowserToolbarViewInteractor {

class BrowserToolbarView(
private val container: ViewGroup,
private val shouldUseBottomToolbar: Boolean,
private val interactor: BrowserToolbarViewInteractor,
private val customTabSession: Session?
) : LayoutContainer {

override val containerView: View?
get() = container

private val urlBackground = LayoutInflater.from(container.context)
.inflate(R.layout.layout_url_background, container, false)

val view: BrowserToolbar = LayoutInflater.from(container.context)
.inflate(R.layout.component_search, container, true)
.inflate(R.layout.component_browser_toolbar, container, true)
.findViewById(R.id.toolbar)

val toolbarIntegration: ToolbarIntegration
Expand Down Expand Up @@ -180,6 +176,7 @@ class BrowserToolbarView(
readerModeStateProvider = {
sessionManager.selectedSession?.readerMode ?: false
},
shouldReverseItems = !shouldUseBottomToolbar,
onItemTapped = { interactor.onBrowserToolbarMenuItemTapped(it) },
lifecycleOwner = container.context as AppCompatActivity,
sessionManager = sessionManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import kotlinx.coroutines.launch
import mozilla.components.browser.menu.BrowserMenuBuilder
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.BrowserMenuItemToolbar
import mozilla.components.browser.menu.item.BrowserMenuImageSwitch
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.concept.storage.BookmarksStorage
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.ext.asActivity
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.theme.ThemeManager
Expand All @@ -31,6 +31,7 @@ class DefaultToolbarMenu(
private val context: Context,
private val hasAccountProblem: Boolean = false,
private val requestDesktopStateProvider: () -> Boolean = { false },
private val shouldReverseItems: Boolean,
private val onItemTapped: (ToolbarMenu.Item) -> Unit = {},
private val lifecycleOwner: LifecycleOwner,
private val bookmarksStorage: BookmarksStorage,
Expand Down Expand Up @@ -146,7 +147,7 @@ class DefaultToolbarMenu(
fun shouldShowReaderAppearance(): Boolean =
sessionManager.selectedSession?.readerMode ?: false

listOfNotNull(
val menuItems = listOfNotNull(
help,
settings,
library,
Expand All @@ -164,6 +165,8 @@ class DefaultToolbarMenu(
BrowserMenuDivider(),
menuToolbar
)

if (shouldReverseItems) { menuItems.reversed() } else { menuItems }
}

private val help = BrowserMenuImageText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.view.Gravity
import android.view.View
import androidx.core.view.isGone
import androidx.navigation.fragment.navArgs
import kotlinx.android.synthetic.main.component_search.*
import kotlinx.android.synthetic.main.component_browser_toolbar.*
import kotlinx.android.synthetic.main.fragment_browser.view.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import mozilla.components.browser.session.Session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ToolbarView(
get() = container

val view: BrowserToolbar = LayoutInflater.from(container.context)
.inflate(R.layout.component_search, container, true)
.inflate(R.layout.component_browser_toolbar, container, true)
.findViewById(R.id.toolbar)

private var isInitialized = false
Expand Down
19 changes: 10 additions & 9 deletions app/src/main/java/org/mozilla/fenix/settings/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,8 @@ class SettingsFragment : PreferenceFragmentCompat(), AccountObserver {
(activity as AppCompatActivity).supportActionBar?.show()

setSummaryAndTitleStrings()
setupPreferences()

updateAccountUIState(
context!!,
requireComponents.backgroundServices.accountManager.accountProfile()
)

updatePreferenceVisibilityForFeatureFlags()
}

private fun setSummaryAndTitleStrings() {
val trackingProtectionPreference =
findPreference<Preference>(getPreferenceKey(pref_key_tracking_protection_settings))
Expand Down Expand Up @@ -168,6 +160,15 @@ class SettingsFragment : PreferenceFragmentCompat(), AccountObserver {
isVisible =
!PrivateShortcutCreateManager.doesPrivateBrowsingPinnedShortcutExist(context)
}

setupPreferences()

updateAccountUIState(
context!!,
requireComponents.backgroundServices.accountManager.accountProfile()
)

updatePreferenceVisibilityForFeatureFlags()
}

private fun updatePreferenceVisibilityForFeatureFlags() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey

class ToolbarSettingsFragment: PreferenceFragmentCompat() {
class ToolbarSettingsFragment : PreferenceFragmentCompat() {
private lateinit var topPreference: RadioButtonPreference
private lateinit var bottomPreference: RadioButtonPreference

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/fragment_browser.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/browser_toolbar_height">
android:layout_height="match_parent">

<mozilla.components.concept.engine.EngineView
android:id="@+id/engineView"
Expand Down
2 changes: 1 addition & 1 deletion docs/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ The following metrics are added to the ping:
| sync_auth.sign_up |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |User registered a new Firefox Account, and was signed into it |[1](https://github.com/mozilla-mobile/fenix/pull/4931#issuecomment-529740300)||2020-03-01 |
| tab.media_pause |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user pressed the pause icon on a tab from the home screen |[1](https://github.com/mozilla-mobile/fenix/pull/5266)||2020-03-01 |
| tab.media_play |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user pressed the play icon on a tab from the home screen |[1](https://github.com/mozilla-mobile/fenix/pull/5266)||2020-03-01 |
| toolbar.changed_position |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |The user selected a new position for the toolbar |[1](https://github.com/mozilla-mobile/fenix/pull/TODO)|<ul><li>position: A string that indicates the new position of the toolbar TOP or BOTTOM</li></ul>|2020-03-01 |
| toolbar_settings.changed_position |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |The user selected a new position for the toolbar |[1](https://github.com/mozilla-mobile/fenix/pull/6571)|<ul><li>position: A string that indicates the new position of the toolbar TOP or BOTTOM</li></ul>|2020-03-01 |
| tracking_protection.etp_setting_changed |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user changed their tracking protection level setting to either strict or standard. |[1](https://github.com/mozilla-mobile/fenix/pull/5414#issuecomment-532847188)|<ul><li>etp_setting: The new setting for ETP: strict, standard</li></ul>|2020-03-01 |
| tracking_protection.etp_settings |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user opened tracking protection settings through settings. |[1](https://github.com/mozilla-mobile/fenix/pull/5414#issuecomment-532847188)||2020-03-01 |
| tracking_protection.etp_shield |[event](https://mozilla.github.io/glean/book/user/metrics/event.html) |A user pressed the tracking protection shield icon in toolbar. |[1](https://github.com/mozilla-mobile/fenix/pull/5414#issuecomment-532847188)||2020-03-01 |
Expand Down

0 comments on commit ce8913d

Please sign in to comment.