2019-01-15 02:42:58 +01:00
|
|
|
/* 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/. */
|
2019-01-29 20:20:29 +01:00
|
|
|
|
2019-01-15 02:42:58 +01:00
|
|
|
package org.mozilla.fenix.search
|
|
|
|
|
2019-02-28 20:55:39 +01:00
|
|
|
import android.content.Context
|
2019-01-15 02:42:58 +01:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-02-08 00:37:52 +01:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2019-01-15 02:42:58 +01:00
|
|
|
import androidx.fragment.app.Fragment
|
2019-03-25 22:13:22 +01:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search.*
|
2019-01-29 20:20:29 +01:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search.view.*
|
2019-04-17 18:14:29 +02:00
|
|
|
import mozilla.components.browser.search.SearchEngine
|
2019-04-19 23:12:42 +02:00
|
|
|
import mozilla.components.feature.qr.QrFeature
|
2019-02-28 20:55:39 +01:00
|
|
|
import mozilla.components.feature.search.SearchUseCases
|
|
|
|
import mozilla.components.feature.session.SessionUseCases
|
2019-04-19 23:12:42 +02:00
|
|
|
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
|
2019-03-19 00:09:27 +01:00
|
|
|
import mozilla.components.support.ktx.kotlin.isUrl
|
2019-03-06 23:53:49 +01:00
|
|
|
import org.mozilla.fenix.BrowserDirection
|
2019-02-15 18:31:03 +01:00
|
|
|
import org.mozilla.fenix.HomeActivity
|
2019-01-15 02:42:58 +01:00
|
|
|
import org.mozilla.fenix.R
|
2019-03-19 00:09:27 +01:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2019-02-17 02:04:32 +01:00
|
|
|
import org.mozilla.fenix.components.toolbar.SearchAction
|
2019-03-29 21:49:50 +01:00
|
|
|
import org.mozilla.fenix.components.toolbar.SearchChange
|
2019-02-17 02:04:32 +01:00
|
|
|
import org.mozilla.fenix.components.toolbar.SearchState
|
|
|
|
import org.mozilla.fenix.components.toolbar.ToolbarComponent
|
|
|
|
import org.mozilla.fenix.components.toolbar.ToolbarUIView
|
2019-02-28 20:55:39 +01:00
|
|
|
import org.mozilla.fenix.ext.components
|
2019-02-20 17:58:42 +01:00
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2019-01-29 20:20:29 +01:00
|
|
|
import org.mozilla.fenix.mvi.ActionBusFactory
|
2019-02-17 02:04:32 +01:00
|
|
|
import org.mozilla.fenix.mvi.getAutoDisposeObservable
|
2019-02-08 19:35:48 +01:00
|
|
|
import org.mozilla.fenix.mvi.getManagedEmitter
|
2019-01-31 00:51:49 +01:00
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarAction
|
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarChange
|
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarComponent
|
2019-03-29 21:49:50 +01:00
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarUIView
|
2019-01-15 02:42:58 +01:00
|
|
|
|
|
|
|
class SearchFragment : Fragment() {
|
2019-01-31 00:51:49 +01:00
|
|
|
private lateinit var toolbarComponent: ToolbarComponent
|
|
|
|
private lateinit var awesomeBarComponent: AwesomeBarComponent
|
2019-02-28 20:55:39 +01:00
|
|
|
private var sessionId: String? = null
|
|
|
|
private var isPrivate = false
|
2019-04-19 23:12:42 +02:00
|
|
|
private val qrFeature = ViewBoundFeatureWrapper<QrFeature>()
|
2019-01-24 21:50:30 +01:00
|
|
|
|
2019-01-15 02:42:58 +01:00
|
|
|
override fun onCreateView(
|
2019-01-30 17:36:14 +01:00
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
2019-01-15 02:42:58 +01:00
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
2019-02-28 20:55:39 +01:00
|
|
|
sessionId = SearchFragmentArgs.fromBundle(arguments!!).sessionId
|
|
|
|
isPrivate = (activity as HomeActivity).browsingModeManager.isPrivate
|
2019-01-29 20:20:29 +01:00
|
|
|
val view = inflater.inflate(R.layout.fragment_search, container, false)
|
2019-02-20 17:58:42 +01:00
|
|
|
val url = sessionId?.let {
|
2019-03-01 07:43:52 +01:00
|
|
|
requireComponents.core.sessionManager.findSessionById(it)?.let { session ->
|
|
|
|
session.url
|
2019-02-20 17:58:42 +01:00
|
|
|
}
|
|
|
|
} ?: ""
|
|
|
|
|
2019-02-05 00:41:26 +01:00
|
|
|
toolbarComponent = ToolbarComponent(
|
2019-03-25 22:13:22 +01:00
|
|
|
view.toolbar_component_wrapper,
|
2019-02-05 00:41:26 +01:00
|
|
|
ActionBusFactory.get(this),
|
2019-02-08 19:35:48 +01:00
|
|
|
sessionId,
|
2019-02-15 18:31:03 +01:00
|
|
|
isPrivate,
|
2019-03-29 21:49:50 +01:00
|
|
|
SearchState(url, isEditing = true),
|
|
|
|
view.search_engine_icon
|
2019-02-05 00:41:26 +01:00
|
|
|
)
|
2019-03-25 22:13:22 +01:00
|
|
|
|
2019-02-28 20:55:39 +01:00
|
|
|
awesomeBarComponent = AwesomeBarComponent(view.search_layout, ActionBusFactory.get(this))
|
2019-01-31 07:49:41 +01:00
|
|
|
ActionBusFactory.get(this).logMergedObservables()
|
2019-01-29 20:20:29 +01:00
|
|
|
return view
|
2019-01-15 02:42:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2019-01-23 22:39:53 +01:00
|
|
|
|
2019-01-29 20:20:29 +01:00
|
|
|
layoutComponents(view.search_layout)
|
|
|
|
|
2019-04-19 23:12:42 +02:00
|
|
|
qrFeature.set(
|
|
|
|
QrFeature(
|
|
|
|
requireContext(),
|
|
|
|
fragmentManager = requireFragmentManager(),
|
|
|
|
onNeedToRequestPermissions = { permissions ->
|
|
|
|
requestPermissions(permissions, REQUEST_CODE_CAMERA_PERMISSIONS)
|
|
|
|
},
|
|
|
|
onScanResult = { result ->
|
|
|
|
(activity as HomeActivity)
|
|
|
|
.openToBrowserAndLoad(result, from = BrowserDirection.FromSearch)
|
|
|
|
// TODO add metrics, also should we have confirmation before going to a URL?
|
|
|
|
}),
|
|
|
|
owner = this,
|
|
|
|
view = view
|
|
|
|
)
|
|
|
|
|
|
|
|
view.search_scan_button.setOnClickListener {
|
|
|
|
qrFeature.get()?.scan(R.id.container)
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:51:49 +01:00
|
|
|
lifecycle.addObserver((toolbarComponent.uiView as ToolbarUIView).toolbarIntegration)
|
2019-01-23 22:39:53 +01:00
|
|
|
|
2019-01-31 00:51:49 +01:00
|
|
|
view.toolbar_wrapper.clipToOutline = false
|
2019-03-25 22:13:22 +01:00
|
|
|
|
2019-03-29 21:49:50 +01:00
|
|
|
search_shortcuts_button.setOnClickListener {
|
2019-03-26 22:45:23 +01:00
|
|
|
val isOpen = (awesomeBarComponent.uiView as AwesomeBarUIView).state?.showShortcutEnginePicker ?: false
|
|
|
|
|
|
|
|
getManagedEmitter<AwesomeBarChange>().onNext(AwesomeBarChange.SearchShortcutEnginePicker(!isOpen))
|
|
|
|
|
|
|
|
if (isOpen) {
|
|
|
|
requireComponents.analytics.metrics.track(Event.SearchShortcutMenuClosed)
|
|
|
|
} else {
|
|
|
|
requireComponents.analytics.metrics.track(Event.SearchShortcutMenuOpened)
|
|
|
|
}
|
2019-03-25 22:13:22 +01:00
|
|
|
}
|
2019-02-18 22:18:55 +01:00
|
|
|
}
|
2019-01-24 21:10:16 +01:00
|
|
|
|
2019-02-25 20:37:20 +01:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
(activity as AppCompatActivity).supportActionBar?.hide()
|
|
|
|
}
|
|
|
|
|
2019-02-18 22:18:55 +01:00
|
|
|
override fun onStart() {
|
|
|
|
super.onStart()
|
2019-03-29 21:49:50 +01:00
|
|
|
subscribeToSearchActions()
|
|
|
|
subscribeToAwesomeBarActions()
|
|
|
|
}
|
2019-03-13 19:12:58 +01:00
|
|
|
|
2019-03-29 21:49:50 +01:00
|
|
|
private fun subscribeToSearchActions() {
|
2019-02-17 02:04:32 +01:00
|
|
|
getAutoDisposeObservable<SearchAction>()
|
2019-01-31 00:51:49 +01:00
|
|
|
.subscribe {
|
|
|
|
when (it) {
|
2019-02-20 23:16:59 +01:00
|
|
|
is SearchAction.UrlCommitted -> {
|
2019-02-24 13:28:10 +01:00
|
|
|
if (it.url.isNotBlank()) {
|
2019-04-19 23:12:42 +02:00
|
|
|
(activity as HomeActivity).openToBrowserAndLoad(
|
|
|
|
it.url, it.session, it.engine,
|
|
|
|
BrowserDirection.FromSearch
|
|
|
|
)
|
2019-03-19 00:09:27 +01:00
|
|
|
|
|
|
|
val event = if (it.url.isUrl()) {
|
|
|
|
Event.EnteredUrl(false)
|
|
|
|
} else {
|
2019-04-19 23:12:42 +02:00
|
|
|
if (it.engine == null) {
|
|
|
|
return@subscribe
|
|
|
|
}
|
2019-04-17 01:31:35 +02:00
|
|
|
|
|
|
|
createSearchEvent(it.engine, false)
|
2019-03-19 00:09:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
requireComponents.analytics.metrics.track(event)
|
2019-02-24 13:28:10 +01:00
|
|
|
}
|
2019-02-20 23:16:59 +01:00
|
|
|
}
|
2019-01-31 07:49:41 +01:00
|
|
|
is SearchAction.TextChanged -> {
|
2019-02-08 19:35:48 +01:00
|
|
|
getManagedEmitter<AwesomeBarChange>().onNext(AwesomeBarChange.UpdateQuery(it.query))
|
2019-01-31 07:49:41 +01:00
|
|
|
}
|
2019-03-13 19:12:58 +01:00
|
|
|
is SearchAction.EditingCanceled -> {
|
|
|
|
activity?.onBackPressed()
|
|
|
|
}
|
2019-01-31 00:51:49 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-29 21:49:50 +01:00
|
|
|
}
|
2019-01-31 00:51:49 +01:00
|
|
|
|
2019-03-29 21:49:50 +01:00
|
|
|
private fun subscribeToAwesomeBarActions() {
|
2019-02-17 02:04:32 +01:00
|
|
|
getAutoDisposeObservable<AwesomeBarAction>()
|
2019-01-31 00:51:49 +01:00
|
|
|
.subscribe {
|
|
|
|
when (it) {
|
2019-02-28 20:55:39 +01:00
|
|
|
is AwesomeBarAction.URLTapped -> {
|
|
|
|
getSessionUseCase(requireContext(), sessionId == null).invoke(it.url)
|
2019-03-06 23:53:49 +01:00
|
|
|
(activity as HomeActivity).openToBrowser(sessionId, BrowserDirection.FromSearch)
|
2019-03-19 00:09:27 +01:00
|
|
|
requireComponents.analytics.metrics.track(Event.EnteredUrl(false))
|
2019-02-28 20:55:39 +01:00
|
|
|
}
|
|
|
|
is AwesomeBarAction.SearchTermsTapped -> {
|
2019-03-12 16:49:22 +01:00
|
|
|
getSearchUseCase(requireContext(), sessionId == null)
|
|
|
|
.invoke(it.searchTerms, it.engine)
|
2019-03-06 23:53:49 +01:00
|
|
|
(activity as HomeActivity).openToBrowser(sessionId, BrowserDirection.FromSearch)
|
2019-03-26 22:45:23 +01:00
|
|
|
|
2019-04-19 23:12:42 +02:00
|
|
|
if (it.engine == null) {
|
|
|
|
return@subscribe
|
|
|
|
}
|
2019-04-17 01:31:35 +02:00
|
|
|
val event = createSearchEvent(it.engine, true)
|
|
|
|
|
|
|
|
requireComponents.analytics.metrics.track(event)
|
2019-02-28 18:25:37 +01:00
|
|
|
}
|
2019-03-29 21:49:50 +01:00
|
|
|
is AwesomeBarAction.SearchShortcutEngineSelected -> {
|
|
|
|
getManagedEmitter<AwesomeBarChange>()
|
|
|
|
.onNext(AwesomeBarChange.SearchShortcutEngineSelected(it.engine))
|
|
|
|
getManagedEmitter<SearchChange>()
|
|
|
|
.onNext(SearchChange.SearchShortcutEngineSelected(it.engine))
|
2019-03-26 22:45:23 +01:00
|
|
|
|
|
|
|
requireComponents.analytics.metrics.track(Event.SearchShortcutSelected(it.engine.name))
|
2019-03-29 21:49:50 +01:00
|
|
|
}
|
2019-01-31 00:51:49 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-24 21:10:16 +01:00
|
|
|
}
|
2019-02-28 20:55:39 +01:00
|
|
|
|
2019-04-17 01:31:35 +02:00
|
|
|
private fun createSearchEvent(engine: SearchEngine, isSuggestion: Boolean): Event.PerformedSearch {
|
|
|
|
val isShortcut = engine != requireComponents.search.searchEngineManager.defaultSearchEngine
|
|
|
|
|
|
|
|
val engineSource =
|
|
|
|
if (isShortcut) Event.PerformedSearch.EngineSource.Shortcut(engine)
|
|
|
|
else Event.PerformedSearch.EngineSource.Default(engine)
|
|
|
|
|
|
|
|
val source =
|
|
|
|
if (isSuggestion) Event.PerformedSearch.EventSource.Suggestion(engineSource)
|
|
|
|
else Event.PerformedSearch.EventSource.Action(engineSource)
|
|
|
|
|
|
|
|
return Event.PerformedSearch(source)
|
|
|
|
}
|
|
|
|
|
2019-02-28 20:55:39 +01:00
|
|
|
private fun getSearchUseCase(context: Context, useNewTab: Boolean): SearchUseCases.SearchUseCase {
|
|
|
|
if (!useNewTab) {
|
|
|
|
return context.components.useCases.searchUseCases.defaultSearch
|
|
|
|
}
|
|
|
|
|
|
|
|
return when (isPrivate) {
|
|
|
|
true -> context.components.useCases.searchUseCases.newPrivateTabSearch
|
|
|
|
false -> context.components.useCases.searchUseCases.newTabSearch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getSessionUseCase(context: Context, useNewTab: Boolean): SessionUseCases.LoadUrlUseCase {
|
|
|
|
if (!useNewTab) {
|
|
|
|
return context.components.useCases.sessionUseCases.loadUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
return when (isPrivate) {
|
|
|
|
true -> context.components.useCases.tabsUseCases.addPrivateTab
|
|
|
|
false -> context.components.useCases.tabsUseCases.addTab
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 23:12:42 +02:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val REQUEST_CODE_CAMERA_PERMISSIONS = 1
|
|
|
|
}
|
2019-01-15 02:42:58 +01:00
|
|
|
}
|