2019-01-15 02:42:58 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 20:38:15 +02:00
|
|
|
* 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-04-26 17:12:15 +02:00
|
|
|
import android.Manifest
|
2019-02-28 20:55:39 +01:00
|
|
|
import android.content.Context
|
2019-04-28 08:00:55 +02:00
|
|
|
import android.content.DialogInterface
|
|
|
|
import android.graphics.Typeface.BOLD
|
|
|
|
import android.graphics.Typeface.ITALIC
|
2019-07-13 01:32:00 +02:00
|
|
|
import android.graphics.drawable.BitmapDrawable
|
2019-01-15 02:42:58 +01:00
|
|
|
import android.os.Bundle
|
2019-04-28 08:00:55 +02:00
|
|
|
import android.text.style.StyleSpan
|
2019-01-15 02:42:58 +01:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-04-28 08:00:55 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
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-07-13 01:32:00 +02:00
|
|
|
import androidx.navigation.fragment.findNavController
|
2019-08-08 00:41:52 +02:00
|
|
|
import androidx.transition.TransitionInflater
|
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-08-07 23:00:53 +02:00
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
|
|
import kotlinx.coroutines.ObsoleteCoroutinesApi
|
2019-07-13 01:32:00 +02:00
|
|
|
import mozilla.components.concept.storage.HistoryStorage
|
2019-04-19 23:12:42 +02:00
|
|
|
import mozilla.components.feature.qr.QrFeature
|
2019-07-25 16:32:32 +02:00
|
|
|
import mozilla.components.lib.state.ext.consumeFrom
|
2019-04-25 21:31:17 +02:00
|
|
|
import mozilla.components.support.base.feature.BackHandler
|
2019-04-19 23:12:42 +02:00
|
|
|
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
|
2019-06-08 02:37:43 +02:00
|
|
|
import mozilla.components.support.ktx.android.content.hasCamera
|
2019-04-26 17:12:15 +02:00
|
|
|
import mozilla.components.support.ktx.android.content.isPermissionGranted
|
2019-07-13 01:32:00 +02:00
|
|
|
import org.jetbrains.anko.backgroundDrawable
|
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-07-16 00:54:13 +02:00
|
|
|
import org.mozilla.fenix.components.StoreProvider
|
2019-03-19 00:09:27 +01:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2019-07-22 22:59:24 +02:00
|
|
|
import org.mozilla.fenix.ext.getColorFromAttr
|
2019-04-28 08:00:55 +02:00
|
|
|
import org.mozilla.fenix.ext.getSpannable
|
2019-02-20 17:58:42 +01:00
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2019-07-13 01:32:00 +02:00
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarView
|
|
|
|
import org.mozilla.fenix.search.toolbar.ToolbarView
|
|
|
|
import org.mozilla.fenix.utils.Settings
|
|
|
|
|
|
|
|
@Suppress("TooManyFunctions", "LargeClass")
|
2019-04-25 21:31:17 +02:00
|
|
|
class SearchFragment : Fragment(), BackHandler {
|
2019-07-13 01:32:00 +02:00
|
|
|
private lateinit var toolbarView: ToolbarView
|
|
|
|
private lateinit var awesomeBarView: AwesomeBarView
|
2019-04-19 23:12:42 +02:00
|
|
|
private val qrFeature = ViewBoundFeatureWrapper<QrFeature>()
|
2019-04-26 17:12:15 +02:00
|
|
|
private var permissionDidUpdate = false
|
2019-07-13 01:32:00 +02:00
|
|
|
private lateinit var searchStore: SearchStore
|
|
|
|
private lateinit var searchInteractor: SearchInteractor
|
2019-01-24 21:50:30 +01:00
|
|
|
|
2019-05-25 19:39:34 +02:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2019-08-08 00:41:52 +02:00
|
|
|
postponeEnterTransition()
|
|
|
|
sharedElementEnterTransition =
|
|
|
|
TransitionInflater.from(context).inflateTransition(android.R.transition.move)
|
|
|
|
.setDuration(
|
|
|
|
SHARED_TRANSITION_MS
|
|
|
|
)
|
2019-06-14 00:40:12 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.InteractWithSearchURLArea)
|
2019-05-25 19:39:34 +02: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-07-13 01:32:00 +02:00
|
|
|
val session = arguments
|
|
|
|
?.let(SearchFragmentArgs.Companion::fromBundle)
|
|
|
|
?.let { it.sessionId }
|
|
|
|
?.let(requireComponents.core.sessionManager::findSessionById)
|
2019-04-23 17:15:07 +02:00
|
|
|
|
2019-01-29 20:20:29 +01:00
|
|
|
val view = inflater.inflate(R.layout.fragment_search, container, false)
|
2019-04-23 17:15:07 +02:00
|
|
|
val url = session?.url ?: ""
|
2019-08-01 16:48:38 +02:00
|
|
|
val currentSearchEngine = SearchEngineSource.Default(
|
|
|
|
requireComponents.search.searchEngineManager.getDefaultSearchEngine(requireContext())
|
|
|
|
)
|
2019-02-20 17:58:42 +01:00
|
|
|
|
2019-07-16 20:29:57 +02:00
|
|
|
searchStore = StoreProvider.get(this) {
|
2019-07-17 22:10:24 +02:00
|
|
|
SearchStore(
|
2019-07-16 00:54:13 +02:00
|
|
|
SearchState(
|
2019-07-16 20:29:57 +02:00
|
|
|
query = url,
|
2019-08-01 16:48:38 +02:00
|
|
|
showShortcutEnginePicker = false,
|
|
|
|
searchEngineSource = currentSearchEngine,
|
|
|
|
defaultEngineSource = currentSearchEngine,
|
2019-07-16 20:29:57 +02:00
|
|
|
showSuggestions = Settings.getInstance(requireContext()).showSearchSuggestions,
|
|
|
|
showVisitedSitesBookmarks = Settings.getInstance(requireContext()).shouldShowVisitedSitesBookmarks,
|
|
|
|
session = session
|
|
|
|
)
|
2019-07-13 01:32:00 +02:00
|
|
|
)
|
2019-07-16 20:29:57 +02:00
|
|
|
}
|
2019-03-25 22:13:22 +01:00
|
|
|
|
2019-07-13 01:32:00 +02:00
|
|
|
searchInteractor = SearchInteractor(
|
|
|
|
activity as HomeActivity,
|
|
|
|
findNavController(),
|
|
|
|
searchStore
|
|
|
|
)
|
|
|
|
|
|
|
|
awesomeBarView = AwesomeBarView(view.search_layout, searchInteractor)
|
2019-07-30 00:00:48 +02:00
|
|
|
toolbarView = ToolbarView(
|
|
|
|
view.toolbar_component_wrapper,
|
|
|
|
searchInteractor,
|
|
|
|
historyStorageProvider(),
|
2019-08-07 22:02:08 +02:00
|
|
|
(activity as HomeActivity).browsingModeManager.mode.isPrivate
|
2019-07-30 00:00:48 +02:00
|
|
|
)
|
2019-07-13 01:32:00 +02:00
|
|
|
|
2019-08-08 00:41:52 +02:00
|
|
|
startPostponedEnterTransition()
|
2019-01-29 20:20:29 +01:00
|
|
|
return view
|
2019-01-15 02:42:58 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 23:00:53 +02:00
|
|
|
@ObsoleteCoroutinesApi
|
|
|
|
@ExperimentalCoroutinesApi
|
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-08-03 10:14:35 +02:00
|
|
|
searchScanButton.visibility = if (context?.hasCamera() == true) View.VISIBLE else View.GONE
|
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 ->
|
2019-08-03 10:14:35 +02:00
|
|
|
searchScanButton.isChecked = false
|
2019-04-28 08:00:55 +02:00
|
|
|
activity?.let {
|
2019-07-12 18:44:36 +02:00
|
|
|
AlertDialog.Builder(it).apply {
|
2019-04-28 08:00:55 +02:00
|
|
|
val spannable = resources.getSpannable(
|
|
|
|
R.string.qr_scanner_confirmation_dialog_message,
|
|
|
|
listOf(
|
|
|
|
getString(R.string.app_name) to listOf(StyleSpan(BOLD)),
|
|
|
|
result to listOf(StyleSpan(ITALIC))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
setMessage(spannable)
|
2019-05-21 01:08:50 +02:00
|
|
|
setNegativeButton(R.string.qr_scanner_dialog_negative) { dialog: DialogInterface, _ ->
|
2019-05-15 19:01:26 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.QRScannerNavigationDenied)
|
2019-04-28 08:00:55 +02:00
|
|
|
dialog.cancel()
|
|
|
|
}
|
2019-05-21 01:08:50 +02:00
|
|
|
setPositiveButton(R.string.qr_scanner_dialog_positive) { dialog: DialogInterface, _ ->
|
2019-05-15 19:01:26 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.QRScannerNavigationAllowed)
|
2019-04-28 08:00:55 +02:00
|
|
|
(activity as HomeActivity)
|
2019-04-29 21:32:30 +02:00
|
|
|
.openToBrowserAndLoad(
|
|
|
|
searchTermOrURL = result,
|
2019-07-13 01:32:00 +02:00
|
|
|
newTab = searchStore.state.session == null,
|
2019-04-29 21:32:30 +02:00
|
|
|
from = BrowserDirection.FromSearch
|
|
|
|
)
|
2019-04-28 08:00:55 +02:00
|
|
|
dialog.dismiss()
|
|
|
|
}
|
|
|
|
create()
|
|
|
|
}.show()
|
2019-05-15 19:01:26 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.QRScannerPromptDisplayed)
|
2019-04-28 08:00:55 +02:00
|
|
|
}
|
2019-04-19 23:12:42 +02:00
|
|
|
}),
|
|
|
|
owner = this,
|
|
|
|
view = view
|
|
|
|
)
|
|
|
|
|
2019-08-03 10:14:35 +02:00
|
|
|
view.searchScanButton.setOnClickListener {
|
2019-07-13 01:32:00 +02:00
|
|
|
toolbarView.view.clearFocus()
|
2019-05-15 19:01:26 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.QRScannerOpened)
|
2019-04-19 23:12:42 +02:00
|
|
|
qrFeature.get()?.scan(R.id.container)
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:51:49 +01:00
|
|
|
view.toolbar_wrapper.clipToOutline = false
|
2019-03-25 22:13:22 +01:00
|
|
|
|
2019-08-03 10:14:35 +02:00
|
|
|
searchShortcutsButton.setOnClickListener {
|
2019-07-13 01:32:00 +02:00
|
|
|
val isOpen = searchStore.state.showShortcutEnginePicker
|
2019-07-15 18:39:56 +02:00
|
|
|
searchStore.dispatch(SearchAction.ShowSearchShortcutEnginePicker(!isOpen))
|
2019-03-26 22:45:23 +01:00
|
|
|
|
|
|
|
if (isOpen) {
|
|
|
|
requireComponents.analytics.metrics.track(Event.SearchShortcutMenuClosed)
|
|
|
|
} else {
|
|
|
|
requireComponents.analytics.metrics.track(Event.SearchShortcutMenuOpened)
|
|
|
|
}
|
2019-07-11 19:24:58 +02:00
|
|
|
|
|
|
|
searchInteractor.turnOnStartedTyping()
|
2019-03-25 22:13:22 +01:00
|
|
|
}
|
2019-05-25 19:39:34 +02:00
|
|
|
|
2019-07-25 16:32:32 +02:00
|
|
|
consumeFrom(searchStore) {
|
|
|
|
awesomeBarView.update(it)
|
|
|
|
toolbarView.update(it)
|
|
|
|
updateSearchEngineIcon(it)
|
|
|
|
updateSearchShortuctsIcon(it)
|
|
|
|
updateSearchWithLabel(it)
|
2019-07-13 01:32:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-25 19:39:34 +02:00
|
|
|
startPostponedEnterTransition()
|
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()
|
2019-06-12 17:08:39 +02:00
|
|
|
|
2019-08-01 16:48:38 +02:00
|
|
|
// The user has the option to go to 'Shortcuts' -> 'Search engine settings' to modify the default search engine.
|
|
|
|
// When returning from that settings screen we need to update it to account for any changes.
|
2019-08-08 00:41:52 +02:00
|
|
|
val currentDefaultEngine =
|
|
|
|
requireComponents.search.searchEngineManager.getDefaultSearchEngine(requireContext())
|
2019-08-01 16:48:38 +02:00
|
|
|
if (searchStore.state.defaultEngineSource.searchEngine != currentDefaultEngine) {
|
|
|
|
searchStore.dispatch(
|
|
|
|
SearchAction.SelectNewDefaultSearchEngine
|
|
|
|
(currentDefaultEngine)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:12:15 +02:00
|
|
|
if (!permissionDidUpdate) {
|
2019-07-13 01:32:00 +02:00
|
|
|
toolbarView.view.requestFocus()
|
2019-04-26 17:12:15 +02:00
|
|
|
}
|
2019-07-13 01:32:00 +02:00
|
|
|
|
2019-04-26 17:12:15 +02:00
|
|
|
permissionDidUpdate = false
|
2019-02-25 20:37:20 +01:00
|
|
|
(activity as AppCompatActivity).supportActionBar?.hide()
|
|
|
|
}
|
|
|
|
|
2019-04-25 21:31:17 +02:00
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
2019-07-13 01:32:00 +02:00
|
|
|
toolbarView.view.clearFocus()
|
2019-04-25 21:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBackPressed(): Boolean {
|
|
|
|
return when {
|
|
|
|
qrFeature.onBackPressed() -> {
|
2019-08-03 10:14:35 +02:00
|
|
|
view?.searchScanButton?.isChecked = false
|
2019-07-13 01:32:00 +02:00
|
|
|
toolbarView.view.requestFocus()
|
2019-04-25 21:31:17 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-13 01:32:00 +02:00
|
|
|
private fun updateSearchEngineIcon(searchState: SearchState) {
|
|
|
|
val searchIcon = searchState.searchEngineSource.searchEngine.icon
|
|
|
|
val draw = BitmapDrawable(resources, searchIcon)
|
|
|
|
val iconSize = resources.getDimension(R.dimen.preference_icon_drawable_size).toInt()
|
|
|
|
draw.setBounds(0, 0, iconSize, iconSize)
|
2019-08-03 10:14:35 +02:00
|
|
|
searchEngineIcon?.backgroundDrawable = draw
|
2019-03-29 21:49:50 +01:00
|
|
|
}
|
2019-01-31 00:51:49 +01:00
|
|
|
|
2019-07-13 01:32:00 +02:00
|
|
|
private fun updateSearchWithLabel(searchState: SearchState) {
|
2019-08-08 00:41:52 +02:00
|
|
|
searchWithShortcuts.visibility =
|
|
|
|
if (searchState.showShortcutEnginePicker) View.VISIBLE else View.GONE
|
2019-01-24 21:10:16 +01:00
|
|
|
}
|
2019-02-28 20:55:39 +01:00
|
|
|
|
2019-07-13 01:32:00 +02:00
|
|
|
private fun updateSearchShortuctsIcon(searchState: SearchState) {
|
|
|
|
with(requireContext()) {
|
|
|
|
val showShortcuts = searchState.showShortcutEnginePicker
|
2019-08-03 10:14:35 +02:00
|
|
|
searchShortcutsButton?.isChecked = showShortcuts
|
2019-04-17 01:31:35 +02:00
|
|
|
|
2019-07-23 18:35:24 +02:00
|
|
|
val color = if (showShortcuts) R.attr.contrastText else R.attr.primaryText
|
2019-04-17 01:31:35 +02:00
|
|
|
|
2019-08-03 10:14:35 +02:00
|
|
|
searchShortcutsButton.compoundDrawables[0]?.setTint(getColorFromAttr(color))
|
2019-07-13 01:32:00 +02:00
|
|
|
}
|
2019-04-17 01:31:35 +02:00
|
|
|
}
|
|
|
|
|
2019-08-08 00:41:52 +02:00
|
|
|
override fun onRequestPermissionsResult(
|
|
|
|
requestCode: Int,
|
|
|
|
permissions: Array<String>,
|
|
|
|
grantResults: IntArray
|
|
|
|
) {
|
2019-04-22 02:00:16 +02:00
|
|
|
when (requestCode) {
|
|
|
|
REQUEST_CODE_CAMERA_PERMISSIONS -> qrFeature.withFeature {
|
|
|
|
it.onPermissionsResult(permissions, grantResults)
|
2019-04-26 17:12:15 +02:00
|
|
|
|
2019-04-27 07:02:50 +02:00
|
|
|
context?.let { context: Context ->
|
|
|
|
if (context.isPermissionGranted(Manifest.permission.CAMERA)) {
|
|
|
|
permissionDidUpdate = true
|
2019-05-31 00:35:05 +02:00
|
|
|
} else {
|
2019-08-03 10:14:35 +02:00
|
|
|
view?.searchScanButton?.isChecked = false
|
2019-04-27 07:02:50 +02:00
|
|
|
}
|
2019-04-26 17:12:15 +02:00
|
|
|
}
|
2019-04-22 02:00:16 +02:00
|
|
|
}
|
|
|
|
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-13 01:32:00 +02:00
|
|
|
private fun historyStorageProvider(): HistoryStorage? {
|
|
|
|
return if (Settings.getInstance(requireContext()).shouldShowVisitedSitesBookmarks) {
|
|
|
|
requireComponents.core.historyStorage
|
|
|
|
} else null
|
|
|
|
}
|
|
|
|
|
2019-04-19 23:12:42 +02:00
|
|
|
companion object {
|
2019-08-08 00:41:52 +02:00
|
|
|
private const val SHARED_TRANSITION_MS = 200L
|
2019-04-19 23:12:42 +02:00
|
|
|
private const val REQUEST_CODE_CAMERA_PERMISSIONS = 1
|
|
|
|
}
|
2019-01-15 02:42:58 +01:00
|
|
|
}
|