You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
521 lines
21 KiB
Kotlin
521 lines
21 KiB
Kotlin
4 years ago
|
/* 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/. */
|
||
|
|
||
4 years ago
|
package org.mozilla.fenix.search
|
||
4 years ago
|
|
||
4 years ago
|
import android.Manifest
|
||
4 years ago
|
import android.app.Activity
|
||
4 years ago
|
import android.app.Dialog
|
||
4 years ago
|
import android.content.Context
|
||
4 years ago
|
import android.content.DialogInterface
|
||
4 years ago
|
import android.content.Intent
|
||
4 years ago
|
import android.graphics.Typeface
|
||
4 years ago
|
import android.os.Bundle
|
||
4 years ago
|
import android.speech.RecognizerIntent
|
||
4 years ago
|
import android.text.style.StyleSpan
|
||
4 years ago
|
import android.view.LayoutInflater
|
||
|
import android.view.View
|
||
|
import android.view.ViewGroup
|
||
4 years ago
|
import android.view.ViewStub
|
||
4 years ago
|
import android.view.WindowManager
|
||
4 years ago
|
import androidx.appcompat.app.AlertDialog
|
||
4 years ago
|
import androidx.appcompat.app.AppCompatDialogFragment
|
||
4 years ago
|
import androidx.constraintlayout.widget.ConstraintProperties.BOTTOM
|
||
|
import androidx.constraintlayout.widget.ConstraintProperties.PARENT_ID
|
||
|
import androidx.constraintlayout.widget.ConstraintProperties.TOP
|
||
|
import androidx.constraintlayout.widget.ConstraintSet
|
||
4 years ago
|
import androidx.core.content.ContextCompat
|
||
4 years ago
|
import androidx.core.view.isVisible
|
||
4 years ago
|
import androidx.navigation.fragment.findNavController
|
||
|
import androidx.navigation.fragment.navArgs
|
||
|
import kotlinx.android.synthetic.main.fragment_search_dialog.*
|
||
4 years ago
|
import kotlinx.android.synthetic.main.fragment_search_dialog.view.*
|
||
4 years ago
|
import kotlinx.android.synthetic.main.search_suggestions_hint.view.*
|
||
4 years ago
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||
4 years ago
|
import mozilla.components.browser.toolbar.BrowserToolbar
|
||
4 years ago
|
import mozilla.components.concept.storage.HistoryStorage
|
||
4 years ago
|
import mozilla.components.feature.qr.QrFeature
|
||
4 years ago
|
import mozilla.components.lib.state.ext.consumeFrom
|
||
4 years ago
|
import mozilla.components.support.base.feature.UserInteractionHandler
|
||
4 years ago
|
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
|
||
4 years ago
|
import mozilla.components.support.ktx.android.content.getColorFromAttr
|
||
4 years ago
|
import mozilla.components.support.ktx.android.content.hasCamera
|
||
4 years ago
|
import mozilla.components.support.ktx.android.content.isPermissionGranted
|
||
4 years ago
|
import mozilla.components.support.ktx.android.content.res.getSpanned
|
||
4 years ago
|
import mozilla.components.support.ktx.android.view.hideKeyboard
|
||
4 years ago
|
import mozilla.components.ui.autocomplete.InlineAutocompleteEditText
|
||
4 years ago
|
import org.mozilla.fenix.BrowserDirection
|
||
4 years ago
|
import org.mozilla.fenix.HomeActivity
|
||
4 years ago
|
import org.mozilla.fenix.R
|
||
4 years ago
|
import org.mozilla.fenix.components.metrics.Event
|
||
4 years ago
|
import org.mozilla.fenix.components.searchengine.CustomSearchEngineStore
|
||
|
import org.mozilla.fenix.components.searchengine.FenixSearchEngineProvider
|
||
4 years ago
|
import org.mozilla.fenix.components.toolbar.ToolbarPosition
|
||
4 years ago
|
import org.mozilla.fenix.ext.components
|
||
4 years ago
|
import org.mozilla.fenix.ext.isKeyboardVisible
|
||
4 years ago
|
import org.mozilla.fenix.ext.requireComponents
|
||
4 years ago
|
import org.mozilla.fenix.ext.settings
|
||
4 years ago
|
import org.mozilla.fenix.search.awesomebar.AwesomeBarView
|
||
|
import org.mozilla.fenix.search.toolbar.ToolbarView
|
||
4 years ago
|
import org.mozilla.fenix.settings.SupportUtils
|
||
4 years ago
|
import org.mozilla.fenix.settings.registerOnSharedPreferenceChangeListener
|
||
4 years ago
|
import org.mozilla.fenix.widget.VoiceSearchActivity
|
||
4 years ago
|
|
||
|
typealias SearchDialogFragmentStore = SearchFragmentStore
|
||
4 years ago
|
|
||
4 years ago
|
@SuppressWarnings("LargeClass", "TooManyFunctions")
|
||
4 years ago
|
class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler {
|
||
4 years ago
|
private lateinit var interactor: SearchDialogInteractor
|
||
|
private lateinit var store: SearchDialogFragmentStore
|
||
4 years ago
|
private lateinit var toolbarView: ToolbarView
|
||
|
private lateinit var awesomeBarView: AwesomeBarView
|
||
4 years ago
|
private var firstUpdate = true
|
||
4 years ago
|
|
||
4 years ago
|
private val qrFeature = ViewBoundFeatureWrapper<QrFeature>()
|
||
4 years ago
|
private val speechIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
|
||
4 years ago
|
|
||
4 years ago
|
private var keyboardVisible: Boolean = false
|
||
|
|
||
4 years ago
|
override fun onStart() {
|
||
|
super.onStart()
|
||
|
// https://github.com/mozilla-mobile/fenix/issues/14279
|
||
|
// To prevent GeckoView from resizing we're going to change the softInputMode to not adjust
|
||
|
// the size of the window.
|
||
|
requireActivity().window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
|
||
4 years ago
|
if (keyboardVisible) {
|
||
|
toolbarView.view.edit.focus()
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
|
override fun onStop() {
|
||
|
super.onStop()
|
||
|
// https://github.com/mozilla-mobile/fenix/issues/14279
|
||
|
// Let's reset back to the default behavior after we're done searching
|
||
|
requireActivity().window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
|
||
4 years ago
|
keyboardVisible = toolbarView.view.isKeyboardVisible()
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||
|
super.onCreate(savedInstanceState)
|
||
|
setStyle(STYLE_NO_TITLE, R.style.SearchDialogStyle)
|
||
|
}
|
||
|
|
||
4 years ago
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||
|
return object : Dialog(requireContext(), this.theme) {
|
||
|
override fun onBackPressed() {
|
||
|
this@SearchDialogFragment.onBackPressed()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
override fun onCreateView(
|
||
|
inflater: LayoutInflater,
|
||
|
container: ViewGroup?,
|
||
|
savedInstanceState: Bundle?
|
||
4 years ago
|
): View? {
|
||
4 years ago
|
val args by navArgs<SearchDialogFragmentArgs>()
|
||
4 years ago
|
val view = inflater.inflate(R.layout.fragment_search_dialog, container, false)
|
||
4 years ago
|
val activity = requireActivity() as HomeActivity
|
||
4 years ago
|
val isPrivate = activity.browsingModeManager.mode.isPrivate
|
||
4 years ago
|
|
||
4 years ago
|
requireComponents.analytics.metrics.track(Event.InteractWithSearchURLArea)
|
||
|
|
||
4 years ago
|
store = SearchDialogFragmentStore(
|
||
|
createInitialSearchFragmentState(
|
||
4 years ago
|
activity,
|
||
4 years ago
|
requireComponents,
|
||
|
tabId = args.sessionId,
|
||
|
pastedText = args.pastedText,
|
||
|
searchAccessPoint = args.searchAccessPoint
|
||
|
)
|
||
|
)
|
||
4 years ago
|
|
||
|
interactor = SearchDialogInteractor(
|
||
|
SearchDialogController(
|
||
4 years ago
|
activity = activity,
|
||
4 years ago
|
sessionManager = requireComponents.core.sessionManager,
|
||
|
store = store,
|
||
|
navController = findNavController(),
|
||
|
settings = requireContext().settings(),
|
||
|
metrics = requireComponents.analytics.metrics,
|
||
4 years ago
|
dismissDialog = { dismissAllowingStateLoss() },
|
||
4 years ago
|
clearToolbarFocus = {
|
||
4 years ago
|
toolbarView.view.hideKeyboardAndSave()
|
||
4 years ago
|
toolbarView.view.clearFocus()
|
||
|
}
|
||
|
)
|
||
|
)
|
||
4 years ago
|
|
||
|
toolbarView = ToolbarView(
|
||
|
requireContext(),
|
||
4 years ago
|
interactor,
|
||
4 years ago
|
historyStorageProvider(),
|
||
4 years ago
|
isPrivate,
|
||
4 years ago
|
view.toolbar,
|
||
|
requireComponents.core.engine
|
||
4 years ago
|
).also(::addSearchButton)
|
||
4 years ago
|
|
||
|
awesomeBarView = AwesomeBarView(
|
||
4 years ago
|
activity,
|
||
4 years ago
|
interactor,
|
||
4 years ago
|
view.awesome_bar
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
setShortcutsChangedListener(CustomSearchEngineStore.PREF_FILE_SEARCH_ENGINES)
|
||
|
setShortcutsChangedListener(FenixSearchEngineProvider.PREF_FILE_SEARCH_ENGINES)
|
||
|
|
||
4 years ago
|
view.awesome_bar.setOnTouchListener { _, _ ->
|
||
4 years ago
|
view.hideKeyboardAndSave()
|
||
4 years ago
|
false
|
||
|
}
|
||
|
|
||
4 years ago
|
awesomeBarView.view.setOnEditSuggestionListener(toolbarView.view::setSearchTerms)
|
||
|
|
||
|
val urlView = toolbarView.view
|
||
|
.findViewById<InlineAutocompleteEditText>(R.id.mozac_browser_toolbar_edit_url_view)
|
||
|
urlView?.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
|
||
|
|
||
|
requireComponents.core.engine.speculativeCreateSession(isPrivate)
|
||
|
|
||
4 years ago
|
return view
|
||
|
}
|
||
4 years ago
|
|
||
|
@ExperimentalCoroutinesApi
|
||
4 years ago
|
@SuppressWarnings("LongMethod")
|
||
4 years ago
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||
|
super.onViewCreated(view, savedInstanceState)
|
||
|
|
||
4 years ago
|
setupConstraints(view)
|
||
4 years ago
|
|
||
4 years ago
|
search_wrapper.setOnClickListener {
|
||
4 years ago
|
it.hideKeyboardAndSave()
|
||
4 years ago
|
dismissAllowingStateLoss()
|
||
|
}
|
||
|
|
||
4 years ago
|
view.search_engines_shortcut_button.setOnClickListener {
|
||
|
interactor.onSearchShortcutsButtonClicked()
|
||
|
}
|
||
|
|
||
4 years ago
|
qrFeature.set(
|
||
|
createQrFeature(),
|
||
|
owner = this,
|
||
|
view = view
|
||
|
)
|
||
|
|
||
4 years ago
|
qr_scan_button.visibility = if (context?.hasCamera() == true) View.VISIBLE else View.GONE
|
||
4 years ago
|
|
||
4 years ago
|
qr_scan_button.setOnClickListener {
|
||
4 years ago
|
if (!requireContext().hasCamera()) { return@setOnClickListener }
|
||
4 years ago
|
view.hideKeyboard()
|
||
4 years ago
|
toolbarView.view.clearFocus()
|
||
4 years ago
|
|
||
4 years ago
|
if (requireContext().settings().shouldShowCameraPermissionPrompt) {
|
||
4 years ago
|
requireComponents.analytics.metrics.track(Event.QRScannerOpened)
|
||
|
qrFeature.get()?.scan(R.id.search_wrapper)
|
||
4 years ago
|
} else {
|
||
|
if (requireContext().isPermissionGranted(Manifest.permission.CAMERA)) {
|
||
|
requireComponents.analytics.metrics.track(Event.QRScannerOpened)
|
||
|
qrFeature.get()?.scan(R.id.search_wrapper)
|
||
|
} else {
|
||
|
interactor.onCameraPermissionsNeeded()
|
||
|
resetFocus()
|
||
|
view.hideKeyboard()
|
||
|
toolbarView.view.requestFocus()
|
||
|
}
|
||
4 years ago
|
}
|
||
4 years ago
|
requireContext().settings().setCameraPermissionNeededState = false
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
fill_link_from_clipboard.setOnClickListener {
|
||
4 years ago
|
view.hideKeyboard()
|
||
|
toolbarView.view.clearFocus()
|
||
4 years ago
|
(activity as HomeActivity)
|
||
|
.openToBrowserAndLoad(
|
||
|
searchTermOrURL = requireContext().components.clipboardHandler.url ?: "",
|
||
|
newTab = store.state.tabId == null,
|
||
|
from = BrowserDirection.FromSearchDialog
|
||
|
)
|
||
|
}
|
||
|
|
||
4 years ago
|
val stubListener = ViewStub.OnInflateListener { _, inflated ->
|
||
|
inflated.learn_more.setOnClickListener {
|
||
|
(activity as HomeActivity)
|
||
|
.openToBrowserAndLoad(
|
||
|
searchTermOrURL = SupportUtils.getGenericSumoURLForTopic(
|
||
|
SupportUtils.SumoTopic.SEARCH_SUGGESTION
|
||
|
),
|
||
|
newTab = store.state.tabId == null,
|
||
4 years ago
|
from = BrowserDirection.FromSearchDialog
|
||
4 years ago
|
)
|
||
|
}
|
||
|
|
||
|
inflated.allow.setOnClickListener {
|
||
|
inflated.visibility = View.GONE
|
||
4 years ago
|
requireContext().settings().also {
|
||
|
it.shouldShowSearchSuggestionsInPrivate = true
|
||
|
it.showSearchSuggestionsInPrivateOnboardingFinished = true
|
||
|
}
|
||
4 years ago
|
store.dispatch(SearchFragmentAction.SetShowSearchSuggestions(true))
|
||
|
store.dispatch(SearchFragmentAction.AllowSearchSuggestionsInPrivateModePrompt(false))
|
||
|
requireComponents.analytics.metrics.track(Event.PrivateBrowsingShowSearchSuggestions)
|
||
|
}
|
||
|
|
||
|
inflated.dismiss.setOnClickListener {
|
||
|
inflated.visibility = View.GONE
|
||
4 years ago
|
requireContext().settings().also {
|
||
|
it.shouldShowSearchSuggestionsInPrivate = false
|
||
|
it.showSearchSuggestionsInPrivateOnboardingFinished = true
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
|
inflated.text.text =
|
||
|
getString(R.string.search_suggestions_onboarding_text, getString(R.string.app_name))
|
||
|
|
||
|
inflated.title.text =
|
||
|
getString(R.string.search_suggestions_onboarding_title)
|
||
|
}
|
||
|
|
||
4 years ago
|
view.search_suggestions_hint.setOnInflateListener((stubListener))
|
||
4 years ago
|
|
||
4 years ago
|
consumeFrom(store) {
|
||
4 years ago
|
val shouldShowAwesomebar =
|
||
|
!firstUpdate &&
|
||
4 years ago
|
it.query.isNotBlank() ||
|
||
|
it.showSearchShortcuts
|
||
|
|
||
4 years ago
|
awesome_bar?.visibility = if (shouldShowAwesomebar) View.VISIBLE else View.INVISIBLE
|
||
4 years ago
|
updateSearchSuggestionsHintVisibility(it)
|
||
4 years ago
|
updateClipboardSuggestion(it, requireContext().components.clipboardHandler.url)
|
||
4 years ago
|
updateToolbarContentDescription(it)
|
||
4 years ago
|
updateSearchShortcutsIcon(it)
|
||
4 years ago
|
toolbarView.update(it)
|
||
|
awesomeBarView.update(it)
|
||
4 years ago
|
firstUpdate = false
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
override fun onResume() {
|
||
|
super.onResume()
|
||
|
resetFocus()
|
||
|
toolbarView.view.edit.focus()
|
||
|
}
|
||
|
|
||
|
override fun onPause() {
|
||
|
super.onPause()
|
||
|
qr_scan_button.isChecked = false
|
||
|
view?.hideKeyboard()
|
||
|
toolbarView.view.requestFocus()
|
||
|
}
|
||
|
|
||
4 years ago
|
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
|
||
|
if (requestCode == VoiceSearchActivity.SPEECH_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
|
||
|
intent?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)?.first()?.also {
|
||
|
toolbarView.view.edit.updateUrl(url = it, shouldHighlight = true)
|
||
|
interactor.onTextChanged(it)
|
||
|
toolbarView.view.edit.focus()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
override fun onBackPressed(): Boolean {
|
||
4 years ago
|
return when {
|
||
|
qrFeature.onBackPressed() -> {
|
||
4 years ago
|
resetFocus()
|
||
4 years ago
|
true
|
||
|
}
|
||
|
else -> {
|
||
4 years ago
|
view?.hideKeyboardAndSave()
|
||
4 years ago
|
dismissAllowingStateLoss()
|
||
|
true
|
||
|
}
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
private fun historyStorageProvider(): HistoryStorage? {
|
||
|
return if (requireContext().settings().shouldShowHistorySuggestions) {
|
||
|
requireComponents.core.historyStorage
|
||
|
} else null
|
||
|
}
|
||
|
|
||
4 years ago
|
private fun createQrFeature(): QrFeature {
|
||
|
return QrFeature(
|
||
|
requireContext(),
|
||
|
fragmentManager = childFragmentManager,
|
||
|
onNeedToRequestPermissions = { permissions ->
|
||
|
requestPermissions(permissions, REQUEST_CODE_CAMERA_PERMISSIONS)
|
||
|
},
|
||
|
onScanResult = { result ->
|
||
|
qr_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,
|
||
4 years ago
|
from = BrowserDirection.FromSearchDialog
|
||
4 years ago
|
)
|
||
|
dialog.dismiss()
|
||
|
}
|
||
|
create()
|
||
|
}.show()
|
||
|
requireComponents.analytics.metrics.track(Event.QRScannerPromptDisplayed)
|
||
|
}
|
||
4 years ago
|
}
|
||
|
)
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
override fun onRequestPermissionsResult(
|
||
|
requestCode: Int,
|
||
|
permissions: Array<String>,
|
||
|
grantResults: IntArray
|
||
|
) {
|
||
|
when (requestCode) {
|
||
|
REQUEST_CODE_CAMERA_PERMISSIONS -> qrFeature.withFeature {
|
||
4 years ago
|
it.onPermissionsResult(permissions, grantResults)
|
||
|
resetFocus()
|
||
|
requireContext().settings().setCameraPermissionNeededState = false
|
||
4 years ago
|
}
|
||
|
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun resetFocus() {
|
||
|
qr_scan_button.isChecked = false
|
||
|
toolbarView.view.edit.focus()
|
||
|
toolbarView.view.requestFocus()
|
||
|
}
|
||
|
|
||
4 years ago
|
private fun setupConstraints(view: View) {
|
||
|
if (view.context.settings().toolbarPosition == ToolbarPosition.BOTTOM) {
|
||
|
ConstraintSet().apply {
|
||
|
clone(search_wrapper)
|
||
|
|
||
|
clear(toolbar.id, TOP)
|
||
|
connect(toolbar.id, BOTTOM, PARENT_ID, BOTTOM)
|
||
|
|
||
|
clear(pill_wrapper.id, BOTTOM)
|
||
|
connect(pill_wrapper.id, BOTTOM, toolbar.id, TOP)
|
||
|
|
||
4 years ago
|
clear(search_suggestions_hint.id, TOP)
|
||
|
connect(search_suggestions_hint.id, TOP, PARENT_ID, TOP)
|
||
|
|
||
4 years ago
|
clear(fill_link_from_clipboard.id, TOP)
|
||
|
connect(fill_link_from_clipboard.id, BOTTOM, pill_wrapper.id, TOP)
|
||
|
|
||
4 years ago
|
applyTo(search_wrapper)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
private fun updateSearchSuggestionsHintVisibility(state: SearchFragmentState) {
|
||
|
view?.apply {
|
||
4 years ago
|
val showHint = state.showSearchSuggestionsHint && !state.showSearchShortcuts
|
||
|
findViewById<View>(R.id.search_suggestions_hint)?.isVisible = showHint
|
||
|
search_suggestions_hint_divider?.isVisible = showHint
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
private fun addSearchButton(toolbarView: ToolbarView) {
|
||
|
toolbarView.view.addEditAction(
|
||
|
BrowserToolbar.Button(
|
||
|
ContextCompat.getDrawable(requireContext(), R.drawable.ic_microphone)!!,
|
||
|
requireContext().getString(R.string.voice_search_content_description),
|
||
|
visible = {
|
||
|
store.state.searchEngineSource.searchEngine.identifier.contains("google") &&
|
||
|
isSpeechAvailable() &&
|
||
|
requireContext().settings().shouldShowVoiceSearch
|
||
|
},
|
||
|
listener = ::launchVoiceSearch
|
||
|
)
|
||
|
)
|
||
|
}
|
||
|
|
||
4 years ago
|
/**
|
||
|
* Used to save keyboard status on stop/sleep, to be restored later.
|
||
|
* See #14559
|
||
|
* */
|
||
|
private fun View.hideKeyboardAndSave() {
|
||
|
keyboardVisible = false
|
||
|
this.hideKeyboard()
|
||
|
}
|
||
|
|
||
4 years ago
|
private fun launchVoiceSearch() {
|
||
|
// 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 (!isSpeechAvailable()) { return }
|
||
|
|
||
|
requireComponents.analytics.metrics.track(Event.VoiceSearchTapped)
|
||
|
speechIntent.apply {
|
||
|
putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
|
||
|
putExtra(RecognizerIntent.EXTRA_PROMPT, requireContext().getString(R.string.voice_search_explainer))
|
||
|
}
|
||
|
startActivityForResult(speechIntent, VoiceSearchActivity.SPEECH_REQUEST_CODE)
|
||
|
}
|
||
|
|
||
|
private fun isSpeechAvailable(): Boolean = speechIntent.resolveActivity(requireContext().packageManager) != null
|
||
|
|
||
4 years ago
|
private fun setShortcutsChangedListener(preferenceFileName: String) {
|
||
|
requireContext().getSharedPreferences(
|
||
|
preferenceFileName,
|
||
|
Context.MODE_PRIVATE
|
||
|
).registerOnSharedPreferenceChangeListener(viewLifecycleOwner) { _, _ ->
|
||
|
awesomeBarView.update(store.state)
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
private fun updateClipboardSuggestion(searchState: SearchFragmentState, clipboardUrl: String?) {
|
||
4 years ago
|
val shouldShowView = searchState.showClipboardSuggestions &&
|
||
4 years ago
|
searchState.query.isEmpty() &&
|
||
|
!clipboardUrl.isNullOrEmpty() &&
|
||
|
!searchState.showSearchShortcuts
|
||
4 years ago
|
|
||
4 years ago
|
fill_link_from_clipboard.visibility = if (shouldShowView) View.VISIBLE else View.GONE
|
||
4 years ago
|
clipboard_url.text = clipboardUrl
|
||
|
|
||
|
if (clipboardUrl != null && !((activity as HomeActivity).browsingModeManager.mode.isPrivate)) {
|
||
|
requireComponents.core.engine.speculativeConnect(clipboardUrl)
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
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
|
||
|
}
|
||
|
|
||
4 years ago
|
private fun updateSearchShortcutsIcon(searchState: SearchFragmentState) {
|
||
|
view?.apply {
|
||
|
search_engines_shortcut_button.isVisible = searchState.areShortcutsAvailable
|
||
|
|
||
|
val showShortcuts = searchState.showSearchShortcuts
|
||
|
search_engines_shortcut_button.isChecked = showShortcuts
|
||
|
|
||
|
val color = if (showShortcuts) R.attr.contrastText else R.attr.primaryText
|
||
|
search_engines_shortcut_button.compoundDrawables[0]?.setTint(
|
||
|
requireContext().getColorFromAttr(color)
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
companion object {
|
||
|
private const val REQUEST_CODE_CAMERA_PERMISSIONS = 1
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|