2020-08-04 19:35:08 +02: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/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix.searchdialog
|
|
|
|
|
2020-08-07 02:21:52 +02:00
|
|
|
import android.app.Dialog
|
2020-08-17 20:31:09 +02:00
|
|
|
import android.content.DialogInterface
|
|
|
|
import android.graphics.Typeface
|
2020-08-04 19:35:08 +02:00
|
|
|
import android.os.Bundle
|
2020-08-17 20:31:09 +02:00
|
|
|
import android.text.style.StyleSpan
|
2020-08-04 19:35:08 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2020-08-17 20:31:09 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
2020-08-04 19:35:08 +02:00
|
|
|
import androidx.appcompat.app.AppCompatDialogFragment
|
2020-08-07 00:49:01 +02:00
|
|
|
import androidx.constraintlayout.widget.ConstraintProperties.BOTTOM
|
|
|
|
import androidx.constraintlayout.widget.ConstraintProperties.PARENT_ID
|
|
|
|
import androidx.constraintlayout.widget.ConstraintProperties.TOP
|
|
|
|
import androidx.constraintlayout.widget.ConstraintSet
|
2020-08-06 01:22:07 +02:00
|
|
|
import androidx.navigation.fragment.findNavController
|
|
|
|
import androidx.navigation.fragment.navArgs
|
2020-08-17 20:31:09 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search.*
|
|
|
|
import kotlinx.android.synthetic.main.fragment_search.view.*
|
2020-08-06 01:22:07 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search_dialog.*
|
2020-08-17 20:14:17 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search_dialog.pill_wrapper
|
2020-08-17 20:31:09 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search_dialog.search_scan_button
|
2020-08-17 20:14:17 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search_dialog.toolbar
|
|
|
|
import kotlinx.android.synthetic.main.fragment_search_dialog.view.*
|
2020-08-17 20:31:09 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_search_dialog.view.search_scan_button
|
|
|
|
import kotlinx.android.synthetic.main.fragment_search_dialog.view.toolbar
|
2020-08-06 01:22:07 +02:00
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
2020-08-17 20:31:09 +02:00
|
|
|
import mozilla.components.browser.state.selector.findTab
|
|
|
|
import mozilla.components.feature.qr.QrFeature
|
2020-08-06 01:22:07 +02:00
|
|
|
import mozilla.components.lib.state.ext.consumeFrom
|
2020-08-07 02:21:52 +02:00
|
|
|
import mozilla.components.support.base.feature.UserInteractionHandler
|
2020-08-17 20:31:09 +02:00
|
|
|
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
|
|
|
|
import mozilla.components.support.ktx.android.content.res.getSpanned
|
2020-08-06 01:22:07 +02:00
|
|
|
import mozilla.components.support.ktx.android.view.hideKeyboard
|
2020-08-17 20:31:09 +02:00
|
|
|
import org.mozilla.fenix.BrowserDirection
|
2020-08-06 01:22:07 +02:00
|
|
|
import org.mozilla.fenix.HomeActivity
|
2020-08-04 19:35:08 +02:00
|
|
|
import org.mozilla.fenix.R
|
2020-08-17 20:31:09 +02:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2020-08-07 00:49:01 +02:00
|
|
|
import org.mozilla.fenix.components.toolbar.ToolbarPosition
|
2020-08-05 21:40:43 +02:00
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2020-08-06 01:22:07 +02:00
|
|
|
import org.mozilla.fenix.ext.settings
|
2020-08-17 20:31:09 +02:00
|
|
|
import org.mozilla.fenix.search.SearchEngineSource
|
|
|
|
import org.mozilla.fenix.search.SearchFragment
|
|
|
|
import org.mozilla.fenix.search.SearchFragmentState
|
2020-08-06 01:22:07 +02:00
|
|
|
import org.mozilla.fenix.search.SearchFragmentStore
|
|
|
|
import org.mozilla.fenix.search.SearchInteractor
|
2020-08-05 21:40:43 +02:00
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarView
|
2020-08-08 00:24:31 +02:00
|
|
|
import org.mozilla.fenix.search.createInitialSearchFragmentState
|
2020-08-05 21:40:43 +02:00
|
|
|
import org.mozilla.fenix.search.toolbar.ToolbarView
|
2020-08-06 01:22:07 +02:00
|
|
|
|
|
|
|
typealias SearchDialogFragmentStore = SearchFragmentStore
|
|
|
|
typealias SearchDialogInteractor = SearchInteractor
|
2020-08-04 19:35:08 +02:00
|
|
|
|
2020-08-07 02:21:52 +02:00
|
|
|
class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler {
|
2020-08-05 21:40:43 +02:00
|
|
|
|
2020-08-06 01:22:07 +02:00
|
|
|
private lateinit var interactor: SearchDialogInteractor
|
|
|
|
private lateinit var store: SearchDialogFragmentStore
|
2020-08-05 21:40:43 +02:00
|
|
|
private lateinit var toolbarView: ToolbarView
|
|
|
|
private lateinit var awesomeBarView: AwesomeBarView
|
|
|
|
|
2020-08-17 20:31:09 +02:00
|
|
|
private val qrFeature = ViewBoundFeatureWrapper<QrFeature>()
|
|
|
|
|
2020-08-05 21:40:43 +02:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setStyle(STYLE_NO_TITLE, R.style.SearchDialogStyle)
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:21:52 +02:00
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
|
|
return object : Dialog(requireContext(), this.theme) {
|
|
|
|
override fun onBackPressed() {
|
|
|
|
this@SearchDialogFragment.onBackPressed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 19:35:08 +02:00
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
2020-08-05 21:40:43 +02:00
|
|
|
): View? {
|
2020-08-08 00:24:31 +02:00
|
|
|
val args by navArgs<SearchDialogFragmentArgs>()
|
2020-08-05 21:40:43 +02:00
|
|
|
val view = inflater.inflate(R.layout.fragment_search_dialog, container, false)
|
2020-08-08 00:24:31 +02:00
|
|
|
|
|
|
|
store = SearchDialogFragmentStore(
|
|
|
|
createInitialSearchFragmentState(
|
|
|
|
activity as HomeActivity,
|
|
|
|
requireComponents,
|
|
|
|
tabId = args.sessionId,
|
|
|
|
pastedText = args.pastedText,
|
|
|
|
searchAccessPoint = args.searchAccessPoint
|
|
|
|
)
|
|
|
|
)
|
2020-08-06 01:22:07 +02:00
|
|
|
|
|
|
|
interactor = SearchDialogInteractor(
|
|
|
|
SearchDialogController(
|
|
|
|
activity = requireActivity() as HomeActivity,
|
|
|
|
sessionManager = requireComponents.core.sessionManager,
|
|
|
|
store = store,
|
|
|
|
navController = findNavController(),
|
|
|
|
settings = requireContext().settings(),
|
|
|
|
metrics = requireComponents.analytics.metrics,
|
|
|
|
clearToolbarFocus = {
|
|
|
|
toolbarView.view.hideKeyboard()
|
|
|
|
toolbarView.view.clearFocus()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2020-08-05 21:40:43 +02:00
|
|
|
|
|
|
|
toolbarView = ToolbarView(
|
|
|
|
requireContext(),
|
2020-08-06 01:22:07 +02:00
|
|
|
interactor,
|
2020-08-05 21:40:43 +02:00
|
|
|
null,
|
|
|
|
false,
|
|
|
|
view.toolbar,
|
|
|
|
requireComponents.core.engine
|
|
|
|
)
|
|
|
|
|
|
|
|
awesomeBarView = AwesomeBarView(
|
|
|
|
requireContext(),
|
2020-08-06 01:22:07 +02:00
|
|
|
interactor,
|
2020-08-17 20:14:17 +02:00
|
|
|
view.awesome_bar
|
2020-08-05 21:40:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
return view
|
|
|
|
}
|
2020-08-06 01:22:07 +02:00
|
|
|
|
|
|
|
@ExperimentalCoroutinesApi
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
2020-08-07 00:49:01 +02:00
|
|
|
if (view.context.settings().toolbarPosition == ToolbarPosition.BOTTOM) {
|
|
|
|
ConstraintSet().apply {
|
|
|
|
clone(search_wrapper)
|
|
|
|
|
|
|
|
clear(toolbar.id, TOP)
|
|
|
|
connect(toolbar.id, BOTTOM, PARENT_ID, BOTTOM)
|
|
|
|
|
2020-08-17 20:14:17 +02:00
|
|
|
clear(awesome_bar.id, TOP)
|
|
|
|
clear(pill_wrapper.id, BOTTOM)
|
|
|
|
connect(awesome_bar.id, TOP, PARENT_ID, TOP)
|
|
|
|
connect(pill_wrapper.id, BOTTOM, toolbar.id, TOP)
|
2020-08-07 00:49:01 +02:00
|
|
|
|
|
|
|
applyTo(search_wrapper)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:21:52 +02:00
|
|
|
search_wrapper.setOnClickListener {
|
|
|
|
it.hideKeyboard()
|
|
|
|
dismissAllowingStateLoss()
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:31:09 +02:00
|
|
|
search_scan_button.setOnClickListener {
|
|
|
|
toolbarView.view.clearFocus()
|
|
|
|
requireComponents.analytics.metrics.track(Event.QRScannerOpened)
|
|
|
|
qrFeature.get()?.scan(R.id.search_wrapper)
|
|
|
|
}
|
|
|
|
|
|
|
|
qrFeature.set(
|
|
|
|
QrFeature(
|
|
|
|
requireContext(),
|
|
|
|
fragmentManager = childFragmentManager,
|
|
|
|
onNeedToRequestPermissions = { permissions ->
|
|
|
|
requestPermissions(permissions, REQUEST_CODE_CAMERA_PERMISSIONS)
|
|
|
|
},
|
|
|
|
onScanResult = { result ->
|
|
|
|
search_scan_button.isChecked = false
|
|
|
|
activity?.let {
|
|
|
|
AlertDialog.Builder(it).apply {
|
|
|
|
val spannable = resources.getSpanned(
|
|
|
|
R.string.qr_scanner_confirmation_dialog_message,
|
|
|
|
getString(R.string.app_name) to StyleSpan(Typeface.BOLD),
|
|
|
|
result to StyleSpan(Typeface.ITALIC)
|
|
|
|
)
|
|
|
|
setMessage(spannable)
|
|
|
|
setNegativeButton(R.string.qr_scanner_dialog_negative) { dialog: DialogInterface, _ ->
|
|
|
|
requireComponents.analytics.metrics.track(Event.QRScannerNavigationDenied)
|
|
|
|
dialog.cancel()
|
|
|
|
}
|
|
|
|
setPositiveButton(R.string.qr_scanner_dialog_positive) { dialog: DialogInterface, _ ->
|
|
|
|
requireComponents.analytics.metrics.track(Event.QRScannerNavigationAllowed)
|
|
|
|
(activity as HomeActivity)
|
|
|
|
.openToBrowserAndLoad(
|
|
|
|
searchTermOrURL = result,
|
|
|
|
newTab = store.state.tabId == null,
|
|
|
|
from = BrowserDirection.FromSearch
|
|
|
|
)
|
|
|
|
dialog.dismiss()
|
|
|
|
}
|
|
|
|
create()
|
|
|
|
}.show()
|
|
|
|
requireComponents.analytics.metrics.track(Event.QRScannerPromptDisplayed)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
owner = this,
|
|
|
|
view = view
|
|
|
|
)
|
|
|
|
|
2020-08-06 01:22:07 +02:00
|
|
|
consumeFrom(store) {
|
2020-08-17 20:14:17 +02:00
|
|
|
awesome_bar?.visibility = if (it.query.isEmpty()) View.INVISIBLE else View.VISIBLE
|
2020-08-06 01:22:07 +02:00
|
|
|
toolbarView.update(it)
|
|
|
|
awesomeBarView.update(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:21:52 +02:00
|
|
|
override fun onBackPressed(): Boolean {
|
2020-08-17 20:31:09 +02:00
|
|
|
return when {
|
|
|
|
qrFeature.onBackPressed() -> {
|
|
|
|
toolbarView.view.edit.focus()
|
|
|
|
view?.search_scan_button?.isChecked = false
|
|
|
|
toolbarView.view.requestFocus()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
view?.hideKeyboard()
|
|
|
|
dismissAllowingStateLoss()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 02:21:52 +02:00
|
|
|
|
2020-08-17 20:31:09 +02:00
|
|
|
companion object {
|
|
|
|
private const val REQUEST_CODE_CAMERA_PERMISSIONS = 1
|
2020-08-07 02:21:52 +02:00
|
|
|
}
|
2020-08-04 19:35:08 +02:00
|
|
|
}
|