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
|
2020-05-14 19:47:13 +02:00
|
|
|
import android.app.Activity.RESULT_OK
|
2019-02-28 20:55:39 +01:00
|
|
|
import android.content.Context
|
2019-04-28 08:00:55 +02:00
|
|
|
import android.content.DialogInterface
|
2020-05-14 19:47:13 +02:00
|
|
|
import android.content.Intent
|
2019-04-28 08:00:55 +02:00
|
|
|
import android.graphics.Typeface.BOLD
|
|
|
|
import android.graphics.Typeface.ITALIC
|
2019-01-15 02:42:58 +01:00
|
|
|
import android.os.Bundle
|
2020-05-14 19:47:13 +02:00
|
|
|
import android.speech.RecognizerIntent
|
|
|
|
import android.speech.RecognizerIntent.EXTRA_RESULTS
|
2019-04-28 08:00:55 +02:00
|
|
|
import android.text.style.StyleSpan
|
2020-05-13 20:20:54 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.view.ViewStub
|
2019-04-28 08:00:55 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
2020-05-14 19:47:13 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
2019-11-15 17:01:34 +01:00
|
|
|
import androidx.core.view.isVisible
|
2020-05-20 00:35:04 +02:00
|
|
|
import androidx.core.widget.NestedScrollView
|
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
|
2020-02-06 00:33:11 +01:00
|
|
|
import androidx.navigation.fragment.navArgs
|
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-11-15 17:01:34 +01:00
|
|
|
import kotlinx.android.synthetic.main.search_suggestions_onboarding.view.*
|
2019-08-07 23:00:53 +02:00
|
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
2020-05-14 19:47:13 +02:00
|
|
|
import mozilla.components.browser.toolbar.BrowserToolbar
|
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-11-28 00:02:47 +01:00
|
|
|
import mozilla.components.support.base.feature.UserInteractionHandler
|
2019-04-19 23:12:42 +02:00
|
|
|
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
|
2020-03-30 18:31:29 +02:00
|
|
|
import mozilla.components.support.ktx.android.content.getColorFromAttr
|
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
|
2020-07-07 22:36:29 +02:00
|
|
|
import mozilla.components.support.ktx.android.content.res.getSpanned
|
2020-05-13 19:36:32 +02:00
|
|
|
import mozilla.components.support.ktx.android.view.hideKeyboard
|
2019-11-26 13:41:17 +01:00
|
|
|
import mozilla.components.ui.autocomplete.InlineAutocompleteEditText
|
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
|
2020-05-13 13:50:45 +02:00
|
|
|
import org.mozilla.fenix.components.searchengine.CustomSearchEngineStore
|
2020-05-13 16:48:02 +02:00
|
|
|
import org.mozilla.fenix.components.searchengine.FenixSearchEngineProvider
|
2019-12-02 19:05:55 +01:00
|
|
|
import org.mozilla.fenix.ext.components
|
2019-11-25 21:36:47 +01:00
|
|
|
import org.mozilla.fenix.ext.hideToolbar
|
2019-02-20 17:58:42 +01:00
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
2019-12-02 19:05:55 +01:00
|
|
|
import org.mozilla.fenix.ext.settings
|
2019-07-13 01:32:00 +02:00
|
|
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarView
|
2020-08-08 00:24:31 +02:00
|
|
|
import org.mozilla.fenix.search.ext.areShortcutsAvailable
|
2019-07-13 01:32:00 +02:00
|
|
|
import org.mozilla.fenix.search.toolbar.ToolbarView
|
2019-11-15 17:01:34 +01:00
|
|
|
import org.mozilla.fenix.settings.SupportUtils
|
2020-05-13 13:50:45 +02:00
|
|
|
import org.mozilla.fenix.settings.registerOnSharedPreferenceChangeListener
|
2020-05-14 19:47:13 +02:00
|
|
|
import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.SPEECH_REQUEST_CODE
|
2019-07-13 01:32:00 +02:00
|
|
|
|
|
|
|
@Suppress("TooManyFunctions", "LargeClass")
|
2019-11-28 00:02:47 +01:00
|
|
|
class SearchFragment : Fragment(), UserInteractionHandler {
|
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-08-30 15:21:34 +02:00
|
|
|
private lateinit var searchStore: SearchFragmentStore
|
2019-07-13 01:32:00 +02:00
|
|
|
private lateinit var searchInteractor: SearchInteractor
|
2019-01-24 21:50:30 +01:00
|
|
|
|
2020-05-28 23:34:44 +02:00
|
|
|
private val speechIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
|
|
|
|
|
2020-05-13 20:20:54 +02:00
|
|
|
@Suppress("LongMethod")
|
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? {
|
2020-05-26 20:06:01 +02:00
|
|
|
val activity = activity as HomeActivity
|
2020-07-21 19:58:28 +02:00
|
|
|
val settings = activity.settings()
|
2020-05-26 20:06:01 +02:00
|
|
|
val args by navArgs<SearchFragmentArgs>()
|
2020-07-02 11:43:40 +02:00
|
|
|
|
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
|
|
|
|
2020-05-26 20:06:01 +02:00
|
|
|
val isPrivate = activity.browsingModeManager.mode.isPrivate
|
2020-03-03 21:27:18 +01:00
|
|
|
|
2020-03-05 22:54:54 +01:00
|
|
|
requireComponents.analytics.metrics.track(Event.InteractWithSearchURLArea)
|
|
|
|
|
2019-07-16 20:29:57 +02:00
|
|
|
searchStore = StoreProvider.get(this) {
|
2019-08-30 15:21:34 +02:00
|
|
|
SearchFragmentStore(
|
2020-08-08 00:24:31 +02:00
|
|
|
createInitialSearchFragmentState(
|
|
|
|
activity,
|
|
|
|
requireComponents,
|
|
|
|
tabId = args.sessionId,
|
2020-05-26 20:06:01 +02:00
|
|
|
pastedText = args.pastedText,
|
|
|
|
searchAccessPoint = args.searchAccessPoint
|
2019-07-16 20:29:57 +02:00
|
|
|
)
|
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-08-20 18:07:00 +02:00
|
|
|
val searchController = DefaultSearchController(
|
2020-05-26 20:06:01 +02:00
|
|
|
activity = activity,
|
2020-07-21 19:58:28 +02:00
|
|
|
sessionManager = requireComponents.core.sessionManager,
|
2020-03-05 21:29:23 +01:00
|
|
|
store = searchStore,
|
|
|
|
navController = findNavController(),
|
2020-07-21 19:58:28 +02:00
|
|
|
settings = settings,
|
|
|
|
metrics = requireComponents.analytics.metrics,
|
2020-03-05 21:29:23 +01:00
|
|
|
clearToolbarFocus = ::clearToolbarFocus
|
2019-08-20 18:07:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
searchInteractor = SearchInteractor(
|
|
|
|
searchController
|
2019-07-13 01:32:00 +02:00
|
|
|
)
|
|
|
|
|
2020-08-05 21:40:43 +02:00
|
|
|
awesomeBarView = AwesomeBarView(requireContext(), searchInteractor,
|
2020-06-21 23:56:49 +02:00
|
|
|
view.findViewById(R.id.awesomeBar))
|
2020-05-13 16:48:02 +02:00
|
|
|
setShortcutsChangedListener(CustomSearchEngineStore.PREF_FILE_SEARCH_ENGINES)
|
|
|
|
setShortcutsChangedListener(FenixSearchEngineProvider.PREF_FILE_SEARCH_ENGINES)
|
2020-05-13 13:50:45 +02:00
|
|
|
|
2020-05-20 00:35:04 +02:00
|
|
|
view.scrollView.setOnScrollChangeListener {
|
|
|
|
_: NestedScrollView, _: Int, _: Int, _: Int, _: Int ->
|
|
|
|
view.hideKeyboard()
|
2020-05-13 19:36:32 +02:00
|
|
|
}
|
2020-05-20 00:35:04 +02:00
|
|
|
|
2019-07-30 00:00:48 +02:00
|
|
|
toolbarView = ToolbarView(
|
2020-06-21 23:56:49 +02:00
|
|
|
requireContext(),
|
2019-07-30 00:00:48 +02:00
|
|
|
searchInteractor,
|
|
|
|
historyStorageProvider(),
|
2020-03-03 21:27:18 +01:00
|
|
|
isPrivate,
|
2020-06-21 23:56:49 +02:00
|
|
|
view.toolbar,
|
2020-03-02 22:21:56 +01:00
|
|
|
requireComponents.core.engine
|
2019-07-30 00:00:48 +02:00
|
|
|
)
|
2019-07-13 01:32:00 +02:00
|
|
|
|
2020-05-14 19:47:13 +02:00
|
|
|
toolbarView.view.addEditAction(
|
|
|
|
BrowserToolbar.Button(
|
|
|
|
ContextCompat.getDrawable(requireContext(), R.drawable.ic_microphone)!!,
|
|
|
|
requireContext().getString(R.string.voice_search_content_description),
|
2020-07-23 20:24:37 +02:00
|
|
|
visible = {
|
2020-08-08 00:24:31 +02:00
|
|
|
searchStore.state.searchEngineSource.searchEngine.identifier.contains("google") &&
|
2020-07-23 20:24:37 +02:00
|
|
|
speechIsAvailable() &&
|
2020-07-21 19:58:28 +02:00
|
|
|
settings.shouldShowVoiceSearch
|
2020-07-23 20:24:37 +02:00
|
|
|
},
|
2020-05-14 19:47:13 +02:00
|
|
|
listener = ::launchVoiceSearch
|
|
|
|
)
|
|
|
|
)
|
2020-05-18 19:43:03 +02:00
|
|
|
|
2020-08-06 18:02:43 +02:00
|
|
|
awesomeBarView.view.setOnEditSuggestionListener(toolbarView.view::setSearchTerms)
|
|
|
|
|
2019-11-26 13:41:17 +01:00
|
|
|
val urlView = toolbarView.view
|
|
|
|
.findViewById<InlineAutocompleteEditText>(R.id.mozac_browser_toolbar_edit_url_view)
|
|
|
|
urlView?.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
|
|
|
|
|
2020-03-03 21:27:18 +01:00
|
|
|
requireComponents.core.engine.speculativeCreateSession(isPrivate)
|
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
|
|
|
}
|
|
|
|
|
2020-05-28 23:34:44 +02:00
|
|
|
private fun speechIsAvailable(): Boolean {
|
|
|
|
return (speechIntent.resolveActivity(requireContext().packageManager) != null)
|
|
|
|
}
|
|
|
|
|
2020-05-13 16:48:02 +02:00
|
|
|
private fun setShortcutsChangedListener(preferenceFileName: String) {
|
2020-05-13 13:50:45 +02:00
|
|
|
requireContext().getSharedPreferences(
|
2020-05-13 16:48:02 +02:00
|
|
|
preferenceFileName,
|
2020-05-13 13:50:45 +02:00
|
|
|
Context.MODE_PRIVATE
|
2020-05-13 16:48:02 +02:00
|
|
|
).registerOnSharedPreferenceChangeListener(viewLifecycleOwner) { _, _ ->
|
|
|
|
awesomeBarView.update(searchStore.state)
|
2020-05-13 13:50:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 19:47:13 +02:00
|
|
|
private fun launchVoiceSearch() {
|
2020-05-28 23:34:44 +02:00
|
|
|
// Note if a user disables speech while the app is on the search fragment
|
|
|
|
// the voice button will still be available and *will* cause a crash if tapped,
|
|
|
|
// since the `visible` call is only checked on create. In order to avoid extra complexity
|
|
|
|
// around such a small edge case, we make the button have no functionality in this case.
|
|
|
|
if (!speechIsAvailable()) { return }
|
|
|
|
|
2020-05-20 23:35:57 +02:00
|
|
|
requireComponents.analytics.metrics.track(Event.VoiceSearchTapped)
|
2020-05-28 23:34:44 +02:00
|
|
|
speechIntent.apply {
|
2020-05-14 19:47:13 +02:00
|
|
|
putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
|
|
|
|
putExtra(RecognizerIntent.EXTRA_PROMPT, requireContext().getString(R.string.voice_search_explainer))
|
|
|
|
}
|
|
|
|
startActivityForResult(speechIntent, SPEECH_REQUEST_CODE)
|
|
|
|
}
|
|
|
|
|
2020-03-05 21:29:23 +01:00
|
|
|
private fun clearToolbarFocus() {
|
2020-06-24 02:20:48 +02:00
|
|
|
toolbarView.view.hideKeyboard()
|
2020-03-05 21:29:23 +01:00
|
|
|
toolbarView.view.clearFocus()
|
|
|
|
}
|
|
|
|
|
2019-08-07 23:00:53 +02:00
|
|
|
@ExperimentalCoroutinesApi
|
2019-09-16 17:58:19 +02:00
|
|
|
@SuppressWarnings("LongMethod")
|
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-12-23 17:15:34 +01:00
|
|
|
search_scan_button.visibility = if (context?.hasCamera() == true) View.VISIBLE else View.GONE
|
2019-01-29 20:20:29 +01:00
|
|
|
|
2019-04-19 23:12:42 +02:00
|
|
|
qrFeature.set(
|
|
|
|
QrFeature(
|
|
|
|
requireContext(),
|
2019-09-26 20:04:42 +02:00
|
|
|
fragmentManager = parentFragmentManager,
|
2019-04-19 23:12:42 +02:00
|
|
|
onNeedToRequestPermissions = { permissions ->
|
|
|
|
requestPermissions(permissions, REQUEST_CODE_CAMERA_PERMISSIONS)
|
|
|
|
},
|
|
|
|
onScanResult = { result ->
|
2019-12-23 17:15:34 +01:00
|
|
|
search_scan_button.isChecked = false
|
2019-04-28 08:00:55 +02:00
|
|
|
activity?.let {
|
2019-07-12 18:44:36 +02:00
|
|
|
AlertDialog.Builder(it).apply {
|
2020-07-07 22:36:29 +02:00
|
|
|
val spannable = resources.getSpanned(
|
2019-04-28 08:00:55 +02:00
|
|
|
R.string.qr_scanner_confirmation_dialog_message,
|
2020-07-07 22:36:29 +02:00
|
|
|
getString(R.string.app_name) to StyleSpan(BOLD),
|
|
|
|
result to StyleSpan(ITALIC)
|
2019-04-28 08:00:55 +02:00
|
|
|
)
|
|
|
|
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,
|
2020-07-02 11:43:40 +02:00
|
|
|
newTab = searchStore.state.tabId == 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-12-23 17:15:34 +01:00
|
|
|
view.search_scan_button.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)
|
|
|
|
}
|
|
|
|
|
2020-07-28 00:19:31 +02:00
|
|
|
view.search_engines_shortcut_button.setOnClickListener {
|
2020-03-30 18:31:29 +02:00
|
|
|
searchInteractor.onSearchShortcutsButtonClicked()
|
|
|
|
}
|
|
|
|
|
2019-11-15 17:01:34 +01:00
|
|
|
val stubListener = ViewStub.OnInflateListener { _, inflated ->
|
|
|
|
inflated.learn_more.setOnClickListener {
|
|
|
|
(activity as HomeActivity)
|
|
|
|
.openToBrowserAndLoad(
|
|
|
|
searchTermOrURL = SupportUtils.getGenericSumoURLForTopic(
|
2019-12-02 19:05:55 +01:00
|
|
|
SupportUtils.SumoTopic.SEARCH_SUGGESTION
|
|
|
|
),
|
2020-07-02 11:43:40 +02:00
|
|
|
newTab = searchStore.state.tabId == null,
|
2019-11-15 17:01:34 +01:00
|
|
|
from = BrowserDirection.FromSearch
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
inflated.allow.setOnClickListener {
|
|
|
|
inflated.visibility = View.GONE
|
|
|
|
context?.settings()?.shouldShowSearchSuggestionsInPrivate = true
|
|
|
|
context?.settings()?.showSearchSuggestionsInPrivateOnboardingFinished = true
|
2020-02-21 18:37:35 +01:00
|
|
|
searchStore.dispatch(SearchFragmentAction.SetShowSearchSuggestions(true))
|
|
|
|
searchStore.dispatch(SearchFragmentAction.AllowSearchSuggestionsInPrivateModePrompt(false))
|
2019-11-22 09:29:14 +01:00
|
|
|
requireComponents.analytics.metrics.track(Event.PrivateBrowsingShowSearchSuggestions)
|
2019-11-15 17:01:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inflated.dismiss.setOnClickListener {
|
|
|
|
inflated.visibility = View.GONE
|
|
|
|
context?.settings()?.shouldShowSearchSuggestionsInPrivate = false
|
|
|
|
context?.settings()?.showSearchSuggestionsInPrivateOnboardingFinished = true
|
|
|
|
}
|
2019-12-02 19:05:55 +01:00
|
|
|
|
|
|
|
inflated.text.text =
|
|
|
|
getString(R.string.search_suggestions_onboarding_text, getString(R.string.app_name))
|
2019-12-05 18:37:20 +01:00
|
|
|
|
|
|
|
inflated.title.text =
|
|
|
|
getString(R.string.search_suggestions_onboarding_title)
|
2019-11-15 17:01:34 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 19:05:55 +01:00
|
|
|
view.search_suggestions_onboarding.setOnInflateListener((stubListener))
|
2019-11-15 17:01:34 +01:00
|
|
|
|
2019-09-16 17:58:19 +02:00
|
|
|
fill_link_from_clipboard.setOnClickListener {
|
|
|
|
(activity as HomeActivity)
|
|
|
|
.openToBrowserAndLoad(
|
2019-09-18 20:23:32 +02:00
|
|
|
searchTermOrURL = requireContext().components.clipboardHandler.url ?: "",
|
2020-07-02 11:43:40 +02:00
|
|
|
newTab = searchStore.state.tabId == null,
|
2019-09-16 17:58:19 +02:00
|
|
|
from = BrowserDirection.FromSearch
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-25 16:32:32 +02:00
|
|
|
consumeFrom(searchStore) {
|
|
|
|
awesomeBarView.update(it)
|
2020-03-30 18:31:29 +02:00
|
|
|
updateSearchShortcutsIcon(it)
|
2019-07-25 16:32:32 +02:00
|
|
|
toolbarView.update(it)
|
|
|
|
updateSearchWithLabel(it)
|
2019-09-18 20:23:32 +02:00
|
|
|
updateClipboardSuggestion(it, requireContext().components.clipboardHandler.url)
|
2019-11-15 17:01:34 +01:00
|
|
|
updateSearchSuggestionsHintVisibility(it)
|
2020-07-17 23:22:13 +02:00
|
|
|
updateToolbarContentDescription(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
|
|
|
|
2020-07-17 23:22:13 +02:00
|
|
|
private fun updateToolbarContentDescription(searchState: SearchFragmentState) {
|
|
|
|
val urlView = toolbarView.view
|
|
|
|
.findViewById<InlineAutocompleteEditText>(R.id.mozac_browser_toolbar_edit_url_view)
|
|
|
|
toolbarView.view.contentDescription =
|
|
|
|
searchState.searchEngineSource.searchEngine.name + ", " + urlView.hint
|
|
|
|
urlView?.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
|
|
|
|
}
|
|
|
|
|
2019-02-25 20:37:20 +01:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2019-06-12 17:08:39 +02:00
|
|
|
|
2020-08-08 00:24:31 +02:00
|
|
|
val provider = requireComponents.search.provider
|
|
|
|
|
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.
|
2020-08-08 00:24:31 +02:00
|
|
|
val currentDefaultEngine = provider.getDefaultEngine(requireContext())
|
2019-08-30 20:26:38 +02:00
|
|
|
|
2019-08-01 16:48:38 +02:00
|
|
|
if (searchStore.state.defaultEngineSource.searchEngine != currentDefaultEngine) {
|
|
|
|
searchStore.dispatch(
|
2019-08-30 15:21:34 +02:00
|
|
|
SearchFragmentAction.SelectNewDefaultSearchEngine
|
2019-08-01 16:48:38 +02:00
|
|
|
(currentDefaultEngine)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:18:37 +02:00
|
|
|
// Users can from this fragment go to install/uninstall search engines and then return.
|
2020-08-08 00:24:31 +02:00
|
|
|
val areShortcutsAvailable = provider.areShortcutsAvailable(requireContext())
|
2020-07-02 11:18:37 +02:00
|
|
|
if (searchStore.state.areShortcutsAvailable != areShortcutsAvailable) {
|
|
|
|
searchStore.dispatch(SearchFragmentAction.UpdateShortcutsAvailability(areShortcutsAvailable))
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:12:15 +02:00
|
|
|
if (!permissionDidUpdate) {
|
2020-05-14 19:47:13 +02:00
|
|
|
toolbarView.view.edit.focus()
|
2019-04-26 17:12:15 +02:00
|
|
|
}
|
2019-07-13 01:32:00 +02:00
|
|
|
|
2019-12-02 19:05:55 +01:00
|
|
|
updateClipboardSuggestion(
|
|
|
|
searchStore.state,
|
2020-08-08 00:24:31 +02:00
|
|
|
requireComponents.clipboardHandler.url
|
2019-12-02 19:05:55 +01:00
|
|
|
)
|
2019-09-16 17:58:19 +02:00
|
|
|
|
2019-04-26 17:12:15 +02:00
|
|
|
permissionDidUpdate = false
|
2019-11-25 21:36:47 +01:00
|
|
|
hideToolbar()
|
2019-02-25 20:37:20 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 19:47:13 +02:00
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
|
2020-05-18 22:37:46 +02:00
|
|
|
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
|
2020-05-14 19:47:13 +02:00
|
|
|
intent?.getStringArrayListExtra(EXTRA_RESULTS)?.first()?.also {
|
|
|
|
toolbarView.view.edit.updateUrl(url = it, shouldHighlight = true)
|
|
|
|
searchInteractor.onTextChanged(it)
|
|
|
|
toolbarView.view.edit.focus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() -> {
|
2020-05-14 19:47:13 +02:00
|
|
|
toolbarView.view.edit.focus()
|
2019-12-23 17:15:34 +01:00
|
|
|
view?.search_scan_button?.isChecked = false
|
2019-07-13 01:32:00 +02:00
|
|
|
toolbarView.view.requestFocus()
|
2020-06-24 02:20:48 +02:00
|
|
|
true
|
2019-04-25 21:31:17 +02:00
|
|
|
}
|
2020-06-24 02:20:48 +02:00
|
|
|
else -> false
|
2019-04-25 21:31:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 15:21:34 +02:00
|
|
|
private fun updateSearchWithLabel(searchState: SearchFragmentState) {
|
2020-07-28 00:19:31 +02:00
|
|
|
search_engine_shortcut.visibility =
|
2019-10-03 17:53:16 +02:00
|
|
|
if (searchState.showSearchShortcuts) View.VISIBLE else View.GONE
|
2019-01-24 21:10:16 +01:00
|
|
|
}
|
2019-02-28 20:55:39 +01:00
|
|
|
|
2019-09-16 17:58:19 +02:00
|
|
|
private fun updateClipboardSuggestion(searchState: SearchFragmentState, clipboardUrl: String?) {
|
2019-10-03 17:53:16 +02:00
|
|
|
val visibility =
|
2019-09-16 17:58:19 +02:00
|
|
|
if (searchState.showClipboardSuggestions && searchState.query.isEmpty() && !clipboardUrl.isNullOrEmpty())
|
2019-12-02 19:05:55 +01:00
|
|
|
View.VISIBLE else View.GONE
|
2019-09-16 17:58:19 +02:00
|
|
|
|
2019-10-03 17:53:16 +02:00
|
|
|
fill_link_from_clipboard.visibility = visibility
|
|
|
|
divider_line.visibility = visibility
|
2019-09-16 17:58:19 +02:00
|
|
|
clipboard_url.text = clipboardUrl
|
2020-03-02 22:21:56 +01:00
|
|
|
|
|
|
|
if (clipboardUrl != null && !((activity as HomeActivity).browsingModeManager.mode.isPrivate)) {
|
|
|
|
requireComponents.core.engine.speculativeConnect(clipboardUrl)
|
|
|
|
}
|
2019-09-16 17:58:19 +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-12-23 17:15:34 +01:00
|
|
|
view?.search_scan_button?.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? {
|
2019-09-24 19:33:46 +02:00
|
|
|
return if (requireContext().settings().shouldShowHistorySuggestions) {
|
2019-07-13 01:32:00 +02:00
|
|
|
requireComponents.core.historyStorage
|
|
|
|
} else null
|
|
|
|
}
|
|
|
|
|
2019-11-15 17:01:34 +01:00
|
|
|
private fun updateSearchSuggestionsHintVisibility(state: SearchFragmentState) {
|
2019-12-05 18:37:20 +01:00
|
|
|
view?.apply {
|
|
|
|
findViewById<View>(R.id.search_suggestions_onboarding)?.isVisible = state.showSearchSuggestionsHint
|
|
|
|
|
|
|
|
search_suggestions_onboarding_divider?.isVisible =
|
2020-07-28 00:19:31 +02:00
|
|
|
search_engine_shortcut.isVisible && state.showSearchSuggestionsHint
|
2019-12-05 18:37:20 +01:00
|
|
|
}
|
2019-11-15 17:01:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 18:31:29 +02:00
|
|
|
private fun updateSearchShortcutsIcon(searchState: SearchFragmentState) {
|
|
|
|
view?.apply {
|
2020-07-28 00:19:31 +02:00
|
|
|
search_engines_shortcut_button.isVisible = searchState.areShortcutsAvailable
|
2020-07-02 11:18:37 +02:00
|
|
|
|
2020-03-30 18:31:29 +02:00
|
|
|
val showShortcuts = searchState.showSearchShortcuts
|
2020-07-28 00:19:31 +02:00
|
|
|
search_engines_shortcut_button.isChecked = showShortcuts
|
2020-03-30 18:31:29 +02:00
|
|
|
|
|
|
|
val color = if (showShortcuts) R.attr.contrastText else R.attr.primaryText
|
2020-07-28 00:19:31 +02:00
|
|
|
search_engines_shortcut_button.compoundDrawables[0]?.setTint(
|
2020-03-30 18:31:29 +02:00
|
|
|
requireContext().getColorFromAttr(color)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|