Merge branch 'mozilla_main' into fork
commit
8d84ec8ef5
@ -1,252 +0,0 @@
|
|||||||
/* 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.search
|
|
||||||
|
|
||||||
import android.content.DialogInterface
|
|
||||||
import android.content.Intent
|
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Build
|
|
||||||
import android.text.SpannableString
|
|
||||||
import androidx.annotation.VisibleForTesting
|
|
||||||
import androidx.appcompat.app.AlertDialog
|
|
||||||
import androidx.navigation.NavController
|
|
||||||
import mozilla.components.browser.search.SearchEngine
|
|
||||||
import mozilla.components.browser.session.Session
|
|
||||||
import mozilla.components.browser.session.SessionManager
|
|
||||||
import mozilla.components.support.ktx.kotlin.isUrl
|
|
||||||
import org.mozilla.fenix.BrowserDirection
|
|
||||||
import org.mozilla.fenix.HomeActivity
|
|
||||||
import org.mozilla.fenix.R
|
|
||||||
import org.mozilla.fenix.components.metrics.Event
|
|
||||||
import org.mozilla.fenix.components.metrics.Event.PerformedSearch.SearchAccessPoint.ACTION
|
|
||||||
import org.mozilla.fenix.components.metrics.Event.PerformedSearch.SearchAccessPoint.NONE
|
|
||||||
import org.mozilla.fenix.components.metrics.Event.PerformedSearch.SearchAccessPoint.SUGGESTION
|
|
||||||
import org.mozilla.fenix.components.metrics.MetricController
|
|
||||||
import org.mozilla.fenix.components.metrics.MetricsUtils
|
|
||||||
import org.mozilla.fenix.components.searchengine.CustomSearchEngineStore
|
|
||||||
import org.mozilla.fenix.crashes.CrashListActivity
|
|
||||||
import org.mozilla.fenix.ext.navigateSafe
|
|
||||||
import org.mozilla.fenix.settings.SupportUtils
|
|
||||||
import org.mozilla.fenix.settings.SupportUtils.MozillaPage.MANIFESTO
|
|
||||||
import org.mozilla.fenix.utils.Settings
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An interface that handles the view manipulation of the Search, triggered by the Interactor
|
|
||||||
*/
|
|
||||||
@Suppress("TooManyFunctions")
|
|
||||||
interface SearchController {
|
|
||||||
fun handleUrlCommitted(url: String)
|
|
||||||
fun handleEditingCancelled()
|
|
||||||
fun handleTextChanged(text: String)
|
|
||||||
fun handleUrlTapped(url: String)
|
|
||||||
fun handleSearchTermsTapped(searchTerms: String)
|
|
||||||
fun handleSearchShortcutEngineSelected(searchEngine: SearchEngine)
|
|
||||||
fun handleClickSearchEngineSettings()
|
|
||||||
fun handleExistingSessionSelected(session: Session)
|
|
||||||
fun handleExistingSessionSelected(tabId: String)
|
|
||||||
fun handleSearchShortcutsButtonClicked()
|
|
||||||
fun handleCameraPermissionsNeeded()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Suppress("TooManyFunctions", "LongParameterList")
|
|
||||||
class DefaultSearchController(
|
|
||||||
private val activity: HomeActivity,
|
|
||||||
private val sessionManager: SessionManager,
|
|
||||||
private val store: SearchFragmentStore,
|
|
||||||
private val navController: NavController,
|
|
||||||
private val settings: Settings,
|
|
||||||
private val metrics: MetricController,
|
|
||||||
private val clearToolbarFocus: () -> Unit
|
|
||||||
) : SearchController {
|
|
||||||
|
|
||||||
override fun handleUrlCommitted(url: String) {
|
|
||||||
when (url) {
|
|
||||||
"about:crashes" -> {
|
|
||||||
// The list of past crashes can be accessed via "settings > about", but desktop and
|
|
||||||
// fennec users may be used to navigating to "about:crashes". So we intercept this here
|
|
||||||
// and open the crash list activity instead.
|
|
||||||
activity.startActivity(Intent(activity, CrashListActivity::class.java))
|
|
||||||
}
|
|
||||||
"about:addons" -> {
|
|
||||||
val directions = SearchFragmentDirections.actionGlobalAddonsManagementFragment()
|
|
||||||
navController.navigateSafe(R.id.searchFragment, directions)
|
|
||||||
}
|
|
||||||
"moz://a" -> openSearchOrUrl(SupportUtils.getMozillaPageUrl(MANIFESTO))
|
|
||||||
else -> if (url.isNotBlank()) {
|
|
||||||
openSearchOrUrl(url)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun openSearchOrUrl(url: String) {
|
|
||||||
activity.openToBrowserAndLoad(
|
|
||||||
searchTermOrURL = url,
|
|
||||||
newTab = store.state.tabId == null,
|
|
||||||
from = BrowserDirection.FromSearch,
|
|
||||||
engine = store.state.searchEngineSource.searchEngine
|
|
||||||
)
|
|
||||||
|
|
||||||
val event = if (url.isUrl()) {
|
|
||||||
Event.EnteredUrl(false)
|
|
||||||
} else {
|
|
||||||
settings.incrementActiveSearchCount()
|
|
||||||
|
|
||||||
val searchAccessPoint = when (store.state.searchAccessPoint) {
|
|
||||||
NONE -> ACTION
|
|
||||||
else -> store.state.searchAccessPoint
|
|
||||||
}
|
|
||||||
|
|
||||||
searchAccessPoint?.let { sap ->
|
|
||||||
MetricsUtils.createSearchEvent(
|
|
||||||
store.state.searchEngineSource.searchEngine,
|
|
||||||
activity,
|
|
||||||
sap
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
event?.let { metrics.track(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleEditingCancelled() {
|
|
||||||
clearToolbarFocus()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleTextChanged(text: String) {
|
|
||||||
// Display the search shortcuts on each entry of the search fragment (see #5308)
|
|
||||||
val textMatchesCurrentUrl = store.state.url == text
|
|
||||||
val textMatchesCurrentSearch = store.state.searchTerms == text
|
|
||||||
|
|
||||||
store.dispatch(SearchFragmentAction.UpdateQuery(text))
|
|
||||||
store.dispatch(
|
|
||||||
SearchFragmentAction.ShowSearchShortcutEnginePicker(
|
|
||||||
(textMatchesCurrentUrl || textMatchesCurrentSearch || text.isEmpty()) &&
|
|
||||||
settings.shouldShowSearchShortcuts
|
|
||||||
)
|
|
||||||
)
|
|
||||||
store.dispatch(
|
|
||||||
SearchFragmentAction.AllowSearchSuggestionsInPrivateModePrompt(
|
|
||||||
text.isNotEmpty() &&
|
|
||||||
activity.browsingModeManager.mode.isPrivate &&
|
|
||||||
!settings.shouldShowSearchSuggestionsInPrivate &&
|
|
||||||
!settings.showSearchSuggestionsInPrivateOnboardingFinished
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleUrlTapped(url: String) {
|
|
||||||
activity.openToBrowserAndLoad(
|
|
||||||
searchTermOrURL = url,
|
|
||||||
newTab = store.state.tabId == null,
|
|
||||||
from = BrowserDirection.FromSearch
|
|
||||||
)
|
|
||||||
|
|
||||||
metrics.track(Event.EnteredUrl(false))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleSearchTermsTapped(searchTerms: String) {
|
|
||||||
settings.incrementActiveSearchCount()
|
|
||||||
|
|
||||||
activity.openToBrowserAndLoad(
|
|
||||||
searchTermOrURL = searchTerms,
|
|
||||||
newTab = store.state.tabId == null,
|
|
||||||
from = BrowserDirection.FromSearch,
|
|
||||||
engine = store.state.searchEngineSource.searchEngine,
|
|
||||||
forceSearch = true
|
|
||||||
)
|
|
||||||
|
|
||||||
val searchAccessPoint = when (store.state.searchAccessPoint) {
|
|
||||||
NONE -> SUGGESTION
|
|
||||||
else -> store.state.searchAccessPoint
|
|
||||||
}
|
|
||||||
|
|
||||||
val event = searchAccessPoint?.let { sap ->
|
|
||||||
MetricsUtils.createSearchEvent(
|
|
||||||
store.state.searchEngineSource.searchEngine,
|
|
||||||
activity,
|
|
||||||
sap
|
|
||||||
)
|
|
||||||
}
|
|
||||||
event?.let { metrics.track(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleSearchShortcutEngineSelected(searchEngine: SearchEngine) {
|
|
||||||
store.dispatch(SearchFragmentAction.SearchShortcutEngineSelected(searchEngine))
|
|
||||||
val isCustom =
|
|
||||||
CustomSearchEngineStore.isCustomSearchEngine(activity, searchEngine.identifier)
|
|
||||||
metrics.track(Event.SearchShortcutSelected(searchEngine, isCustom))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleSearchShortcutsButtonClicked() {
|
|
||||||
val isOpen = store.state.showSearchShortcuts
|
|
||||||
store.dispatch(SearchFragmentAction.ShowSearchShortcutEnginePicker(!isOpen))
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleClickSearchEngineSettings() {
|
|
||||||
val directions = SearchFragmentDirections.actionGlobalSearchEngineFragment()
|
|
||||||
navController.navigateSafe(R.id.searchFragment, directions)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleExistingSessionSelected(session: Session) {
|
|
||||||
sessionManager.select(session)
|
|
||||||
activity.openToBrowser(
|
|
||||||
from = BrowserDirection.FromSearch
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleExistingSessionSelected(tabId: String) {
|
|
||||||
val session = sessionManager.findSessionById(tabId)
|
|
||||||
if (session != null) {
|
|
||||||
handleExistingSessionSelected(session)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates and shows an [AlertDialog] when camera permissions are needed.
|
|
||||||
*
|
|
||||||
* In versions above M, [AlertDialog.BUTTON_POSITIVE] takes the user to the app settings. This
|
|
||||||
* intent only exists in M and above. Below M, [AlertDialog.BUTTON_POSITIVE] routes to a SUMO
|
|
||||||
* help page to find the app settings.
|
|
||||||
*
|
|
||||||
* [AlertDialog.BUTTON_NEGATIVE] dismisses the dialog.
|
|
||||||
*/
|
|
||||||
override fun handleCameraPermissionsNeeded() {
|
|
||||||
val dialog = buildDialog()
|
|
||||||
dialog.show()
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
|
||||||
fun buildDialog(): AlertDialog.Builder {
|
|
||||||
return AlertDialog.Builder(activity).apply {
|
|
||||||
val spannableText = SpannableString(
|
|
||||||
activity.resources.getString(R.string.camera_permissions_needed_message)
|
|
||||||
)
|
|
||||||
setMessage(spannableText)
|
|
||||||
setNegativeButton(R.string.camera_permissions_needed_negative_button_text) {
|
|
||||||
dialog: DialogInterface, _ ->
|
|
||||||
dialog.cancel()
|
|
||||||
}
|
|
||||||
setPositiveButton(R.string.camera_permissions_needed_positive_button_text) {
|
|
||||||
dialog: DialogInterface, _ ->
|
|
||||||
val intent: Intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
||||||
Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
|
||||||
} else {
|
|
||||||
SupportUtils.createCustomTabIntent(
|
|
||||||
activity,
|
|
||||||
SupportUtils.getSumoURLForTopic(
|
|
||||||
activity,
|
|
||||||
SupportUtils.SumoTopic.QR_CAMERA_ACCESS
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
val uri = Uri.fromParts("package", activity.packageName, null)
|
|
||||||
intent.data = uri
|
|
||||||
dialog.cancel()
|
|
||||||
activity.startActivity(intent)
|
|
||||||
}
|
|
||||||
create()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,462 +0,0 @@
|
|||||||
/* 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.search
|
|
||||||
|
|
||||||
import android.Manifest
|
|
||||||
import android.app.Activity.RESULT_OK
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.DialogInterface
|
|
||||||
import android.content.Intent
|
|
||||||
import android.graphics.Typeface.BOLD
|
|
||||||
import android.graphics.Typeface.ITALIC
|
|
||||||
import android.os.Bundle
|
|
||||||
import android.speech.RecognizerIntent
|
|
||||||
import android.speech.RecognizerIntent.EXTRA_RESULTS
|
|
||||||
import android.text.style.StyleSpan
|
|
||||||
import android.view.LayoutInflater
|
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
|
||||||
import android.view.ViewStub
|
|
||||||
import androidx.appcompat.app.AlertDialog
|
|
||||||
import androidx.core.content.ContextCompat
|
|
||||||
import androidx.core.view.isVisible
|
|
||||||
import androidx.core.widget.NestedScrollView
|
|
||||||
import androidx.fragment.app.Fragment
|
|
||||||
import androidx.navigation.fragment.findNavController
|
|
||||||
import androidx.navigation.fragment.navArgs
|
|
||||||
import kotlinx.android.synthetic.main.fragment_search.*
|
|
||||||
import kotlinx.android.synthetic.main.fragment_search.view.*
|
|
||||||
import kotlinx.android.synthetic.main.search_suggestions_hint.view.*
|
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
||||||
import mozilla.components.browser.toolbar.BrowserToolbar
|
|
||||||
import mozilla.components.concept.storage.HistoryStorage
|
|
||||||
import mozilla.components.feature.qr.QrFeature
|
|
||||||
import mozilla.components.lib.state.ext.consumeFrom
|
|
||||||
import mozilla.components.support.base.feature.UserInteractionHandler
|
|
||||||
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
|
|
||||||
import mozilla.components.support.ktx.android.content.getColorFromAttr
|
|
||||||
import mozilla.components.support.ktx.android.content.hasCamera
|
|
||||||
import mozilla.components.support.ktx.android.content.isPermissionGranted
|
|
||||||
import mozilla.components.support.ktx.android.content.res.getSpanned
|
|
||||||
import mozilla.components.support.ktx.android.view.hideKeyboard
|
|
||||||
import mozilla.components.ui.autocomplete.InlineAutocompleteEditText
|
|
||||||
import org.mozilla.fenix.BrowserDirection
|
|
||||||
import org.mozilla.fenix.HomeActivity
|
|
||||||
import org.mozilla.fenix.R
|
|
||||||
import org.mozilla.fenix.components.StoreProvider
|
|
||||||
import org.mozilla.fenix.components.metrics.Event
|
|
||||||
import org.mozilla.fenix.components.searchengine.CustomSearchEngineStore
|
|
||||||
import org.mozilla.fenix.components.searchengine.FenixSearchEngineProvider
|
|
||||||
import org.mozilla.fenix.ext.components
|
|
||||||
import org.mozilla.fenix.ext.hideToolbar
|
|
||||||
import org.mozilla.fenix.ext.requireComponents
|
|
||||||
import org.mozilla.fenix.ext.settings
|
|
||||||
import org.mozilla.fenix.search.awesomebar.AwesomeBarView
|
|
||||||
import org.mozilla.fenix.search.ext.areShortcutsAvailable
|
|
||||||
import org.mozilla.fenix.search.toolbar.ToolbarView
|
|
||||||
import org.mozilla.fenix.settings.SupportUtils
|
|
||||||
import org.mozilla.fenix.settings.registerOnSharedPreferenceChangeListener
|
|
||||||
import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.SPEECH_REQUEST_CODE
|
|
||||||
|
|
||||||
@Suppress("TooManyFunctions", "LargeClass")
|
|
||||||
class SearchFragment : Fragment(), UserInteractionHandler {
|
|
||||||
private lateinit var toolbarView: ToolbarView
|
|
||||||
private lateinit var awesomeBarView: AwesomeBarView
|
|
||||||
private val qrFeature = ViewBoundFeatureWrapper<QrFeature>()
|
|
||||||
private lateinit var searchStore: SearchFragmentStore
|
|
||||||
private lateinit var searchInteractor: SearchInteractor
|
|
||||||
|
|
||||||
private val speechIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
|
|
||||||
|
|
||||||
@Suppress("LongMethod")
|
|
||||||
override fun onCreateView(
|
|
||||||
inflater: LayoutInflater,
|
|
||||||
container: ViewGroup?,
|
|
||||||
savedInstanceState: Bundle?
|
|
||||||
): View? {
|
|
||||||
val activity = activity as HomeActivity
|
|
||||||
val settings = activity.settings()
|
|
||||||
val args by navArgs<SearchFragmentArgs>()
|
|
||||||
|
|
||||||
val view = inflater.inflate(R.layout.fragment_search, container, false)
|
|
||||||
|
|
||||||
val isPrivate = activity.browsingModeManager.mode.isPrivate
|
|
||||||
|
|
||||||
requireComponents.analytics.metrics.track(Event.InteractWithSearchURLArea)
|
|
||||||
|
|
||||||
searchStore = StoreProvider.get(this) {
|
|
||||||
SearchFragmentStore(
|
|
||||||
createInitialSearchFragmentState(
|
|
||||||
activity,
|
|
||||||
requireComponents,
|
|
||||||
tabId = args.sessionId,
|
|
||||||
pastedText = args.pastedText,
|
|
||||||
searchAccessPoint = args.searchAccessPoint
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
val searchController = DefaultSearchController(
|
|
||||||
activity = activity,
|
|
||||||
sessionManager = requireComponents.core.sessionManager,
|
|
||||||
store = searchStore,
|
|
||||||
navController = findNavController(),
|
|
||||||
settings = settings,
|
|
||||||
metrics = requireComponents.analytics.metrics,
|
|
||||||
clearToolbarFocus = ::clearToolbarFocus
|
|
||||||
)
|
|
||||||
|
|
||||||
searchInteractor = SearchInteractor(
|
|
||||||
searchController
|
|
||||||
)
|
|
||||||
|
|
||||||
awesomeBarView = AwesomeBarView(
|
|
||||||
activity,
|
|
||||||
searchInteractor,
|
|
||||||
view.findViewById(R.id.awesomeBar)
|
|
||||||
)
|
|
||||||
setShortcutsChangedListener(CustomSearchEngineStore.PREF_FILE_SEARCH_ENGINES)
|
|
||||||
setShortcutsChangedListener(FenixSearchEngineProvider.PREF_FILE_SEARCH_ENGINES)
|
|
||||||
|
|
||||||
view.scrollView.setOnScrollChangeListener {
|
|
||||||
_: NestedScrollView, _: Int, _: Int, _: Int, _: Int ->
|
|
||||||
view.hideKeyboard()
|
|
||||||
}
|
|
||||||
|
|
||||||
toolbarView = ToolbarView(
|
|
||||||
requireContext(),
|
|
||||||
searchInteractor,
|
|
||||||
historyStorageProvider(),
|
|
||||||
isPrivate,
|
|
||||||
view.toolbar,
|
|
||||||
requireComponents.core.engine
|
|
||||||
)
|
|
||||||
|
|
||||||
toolbarView.view.addEditAction(
|
|
||||||
BrowserToolbar.Button(
|
|
||||||
ContextCompat.getDrawable(requireContext(), R.drawable.ic_microphone)!!,
|
|
||||||
requireContext().getString(R.string.voice_search_content_description),
|
|
||||||
visible = {
|
|
||||||
searchStore.state.searchEngineSource.searchEngine.identifier.contains("google") &&
|
|
||||||
speechIsAvailable() &&
|
|
||||||
settings.shouldShowVoiceSearch
|
|
||||||
},
|
|
||||||
listener = ::launchVoiceSearch
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
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)
|
|
||||||
startPostponedEnterTransition()
|
|
||||||
return view
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun speechIsAvailable(): Boolean {
|
|
||||||
return (speechIntent.resolveActivity(requireContext().packageManager) != null)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setShortcutsChangedListener(preferenceFileName: String) {
|
|
||||||
requireContext().getSharedPreferences(
|
|
||||||
preferenceFileName,
|
|
||||||
Context.MODE_PRIVATE
|
|
||||||
).registerOnSharedPreferenceChangeListener(viewLifecycleOwner) { _, _ ->
|
|
||||||
awesomeBarView.update(searchStore.state)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (!speechIsAvailable()) { 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, SPEECH_REQUEST_CODE)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun clearToolbarFocus() {
|
|
||||||
toolbarView.view.hideKeyboard()
|
|
||||||
toolbarView.view.clearFocus()
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
|
||||||
@SuppressWarnings("LongMethod")
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
||||||
super.onViewCreated(view, savedInstanceState)
|
|
||||||
|
|
||||||
search_scan_button.visibility = if (context?.hasCamera() == true) View.VISIBLE else View.GONE
|
|
||||||
|
|
||||||
qrFeature.set(
|
|
||||||
createQrFeature(),
|
|
||||||
owner = this,
|
|
||||||
view = view
|
|
||||||
)
|
|
||||||
|
|
||||||
view.search_scan_button.setOnClickListener {
|
|
||||||
if (requireContext().settings().shouldShowCameraPermissionPrompt) {
|
|
||||||
requireComponents.analytics.metrics.track(Event.QRScannerOpened)
|
|
||||||
qrFeature.get()?.scan(R.id.container)
|
|
||||||
} else {
|
|
||||||
if (requireContext().isPermissionGranted(Manifest.permission.CAMERA)) {
|
|
||||||
requireComponents.analytics.metrics.track(Event.QRScannerOpened)
|
|
||||||
qrFeature.get()?.scan(R.id.container)
|
|
||||||
} else {
|
|
||||||
searchInteractor.onCameraPermissionsNeeded()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
view.hideKeyboard()
|
|
||||||
search_scan_button.isChecked = false
|
|
||||||
requireContext().settings().setCameraPermissionNeededState = false
|
|
||||||
}
|
|
||||||
|
|
||||||
view.search_engines_shortcut_button.setOnClickListener {
|
|
||||||
searchInteractor.onSearchShortcutsButtonClicked()
|
|
||||||
}
|
|
||||||
|
|
||||||
val stubListener = ViewStub.OnInflateListener { _, inflated ->
|
|
||||||
inflated.learn_more.setOnClickListener {
|
|
||||||
(activity as HomeActivity)
|
|
||||||
.openToBrowserAndLoad(
|
|
||||||
searchTermOrURL = SupportUtils.getGenericSumoURLForTopic(
|
|
||||||
SupportUtils.SumoTopic.SEARCH_SUGGESTION
|
|
||||||
),
|
|
||||||
newTab = searchStore.state.tabId == null,
|
|
||||||
from = BrowserDirection.FromSearch
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
inflated.allow.setOnClickListener {
|
|
||||||
inflated.visibility = View.GONE
|
|
||||||
context?.settings()?.shouldShowSearchSuggestionsInPrivate = true
|
|
||||||
context?.settings()?.showSearchSuggestionsInPrivateOnboardingFinished = true
|
|
||||||
searchStore.dispatch(SearchFragmentAction.SetShowSearchSuggestions(true))
|
|
||||||
searchStore.dispatch(SearchFragmentAction.AllowSearchSuggestionsInPrivateModePrompt(false))
|
|
||||||
requireComponents.analytics.metrics.track(Event.PrivateBrowsingShowSearchSuggestions)
|
|
||||||
}
|
|
||||||
|
|
||||||
inflated.dismiss.setOnClickListener {
|
|
||||||
inflated.visibility = View.GONE
|
|
||||||
context?.settings()?.shouldShowSearchSuggestionsInPrivate = false
|
|
||||||
context?.settings()?.showSearchSuggestionsInPrivateOnboardingFinished = true
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
view.search_suggestions_onboarding.setOnInflateListener((stubListener))
|
|
||||||
|
|
||||||
fill_link_from_clipboard.setOnClickListener {
|
|
||||||
(activity as HomeActivity)
|
|
||||||
.openToBrowserAndLoad(
|
|
||||||
searchTermOrURL = requireContext().components.clipboardHandler.url ?: "",
|
|
||||||
newTab = searchStore.state.tabId == null,
|
|
||||||
from = BrowserDirection.FromSearch
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
consumeFrom(searchStore) {
|
|
||||||
awesomeBarView.update(it)
|
|
||||||
updateSearchShortcutsIcon(it)
|
|
||||||
toolbarView.update(it)
|
|
||||||
updateSearchWithLabel(it)
|
|
||||||
updateClipboardSuggestion(it, requireContext().components.clipboardHandler.url)
|
|
||||||
updateSearchSuggestionsHintVisibility(it)
|
|
||||||
updateToolbarContentDescription(it)
|
|
||||||
}
|
|
||||||
|
|
||||||
startPostponedEnterTransition()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createQrFeature(): QrFeature {
|
|
||||||
return QrFeature(
|
|
||||||
requireContext(),
|
|
||||||
fragmentManager = parentFragmentManager,
|
|
||||||
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(BOLD),
|
|
||||||
result to StyleSpan(ITALIC)
|
|
||||||
)
|
|
||||||
setMessage(spannable)
|
|
||||||
setNegativeButton(R.string.qr_scanner_dialog_negative) { dialog: DialogInterface, _ ->
|
|
||||||
requireComponents.analytics.metrics.track(Event.QRScannerNavigationDenied)
|
|
||||||
dialog.cancel()
|
|
||||||
resetFocus()
|
|
||||||
}
|
|
||||||
setPositiveButton(R.string.qr_scanner_dialog_positive) { dialog: DialogInterface, _ ->
|
|
||||||
requireComponents.analytics.metrics.track(Event.QRScannerNavigationAllowed)
|
|
||||||
(activity as HomeActivity)
|
|
||||||
.openToBrowserAndLoad(
|
|
||||||
searchTermOrURL = result,
|
|
||||||
newTab = searchStore.state.tabId == null,
|
|
||||||
from = BrowserDirection.FromSearch
|
|
||||||
)
|
|
||||||
dialog.dismiss()
|
|
||||||
resetFocus()
|
|
||||||
}
|
|
||||||
create()
|
|
||||||
}.show()
|
|
||||||
requireComponents.analytics.metrics.track(Event.QRScannerPromptDisplayed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onResume() {
|
|
||||||
super.onResume()
|
|
||||||
|
|
||||||
val provider = requireComponents.search.provider
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
val currentDefaultEngine = provider.getDefaultEngine(requireContext())
|
|
||||||
|
|
||||||
if (searchStore.state.defaultEngineSource.searchEngine != currentDefaultEngine) {
|
|
||||||
searchStore.dispatch(
|
|
||||||
SearchFragmentAction.SelectNewDefaultSearchEngine
|
|
||||||
(currentDefaultEngine)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Users can from this fragment go to install/uninstall search engines and then return.
|
|
||||||
val areShortcutsAvailable = provider.areShortcutsAvailable(requireContext())
|
|
||||||
if (searchStore.state.areShortcutsAvailable != areShortcutsAvailable) {
|
|
||||||
searchStore.dispatch(SearchFragmentAction.UpdateShortcutsAvailability(areShortcutsAvailable))
|
|
||||||
}
|
|
||||||
|
|
||||||
updateClipboardSuggestion(
|
|
||||||
searchStore.state,
|
|
||||||
requireComponents.clipboardHandler.url
|
|
||||||
)
|
|
||||||
|
|
||||||
hideToolbar()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
|
|
||||||
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
|
|
||||||
intent?.getStringArrayListExtra(EXTRA_RESULTS)?.first()?.also {
|
|
||||||
toolbarView.view.edit.updateUrl(url = it, shouldHighlight = true)
|
|
||||||
searchInteractor.onTextChanged(it)
|
|
||||||
toolbarView.view.edit.focus()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onPause() {
|
|
||||||
super.onPause()
|
|
||||||
toolbarView.view.clearFocus()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onBackPressed(): Boolean {
|
|
||||||
return when {
|
|
||||||
qrFeature.onBackPressed() -> {
|
|
||||||
resetFocus()
|
|
||||||
true
|
|
||||||
}
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun resetFocus() {
|
|
||||||
search_scan_button.isChecked = false
|
|
||||||
toolbarView.view.edit.focus()
|
|
||||||
toolbarView.view.requestFocus()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateSearchWithLabel(searchState: SearchFragmentState) {
|
|
||||||
search_engine_shortcut.visibility =
|
|
||||||
if (searchState.showSearchShortcuts) View.VISIBLE else View.GONE
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateClipboardSuggestion(searchState: SearchFragmentState, clipboardUrl: String?) {
|
|
||||||
val visibility =
|
|
||||||
if (searchState.showClipboardSuggestions && searchState.query.isEmpty() && !clipboardUrl.isNullOrEmpty())
|
|
||||||
View.VISIBLE else View.GONE
|
|
||||||
|
|
||||||
fill_link_from_clipboard.visibility = visibility
|
|
||||||
divider_line.visibility = visibility
|
|
||||||
clipboard_url.text = clipboardUrl
|
|
||||||
|
|
||||||
if (clipboardUrl != null && !((activity as HomeActivity).browsingModeManager.mode.isPrivate)) {
|
|
||||||
requireComponents.core.engine.speculativeConnect(clipboardUrl)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onRequestPermissionsResult(
|
|
||||||
requestCode: Int,
|
|
||||||
permissions: Array<String>,
|
|
||||||
grantResults: IntArray
|
|
||||||
) {
|
|
||||||
when (requestCode) {
|
|
||||||
REQUEST_CODE_CAMERA_PERMISSIONS -> qrFeature.withFeature {
|
|
||||||
it.onPermissionsResult(permissions, grantResults)
|
|
||||||
resetFocus()
|
|
||||||
requireContext().settings().setCameraPermissionNeededState = false
|
|
||||||
}
|
|
||||||
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun historyStorageProvider(): HistoryStorage? {
|
|
||||||
return if (requireContext().settings().shouldShowHistorySuggestions) {
|
|
||||||
requireComponents.core.historyStorage
|
|
||||||
} else null
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateSearchSuggestionsHintVisibility(state: SearchFragmentState) {
|
|
||||||
view?.apply {
|
|
||||||
findViewById<View>(R.id.search_suggestions_onboarding)?.isVisible = state.showSearchSuggestionsHint
|
|
||||||
|
|
||||||
search_suggestions_onboarding_divider?.isVisible =
|
|
||||||
search_engine_shortcut.isVisible && state.showSearchSuggestionsHint
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val REQUEST_CODE_CAMERA_PERMISSIONS = 1
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- 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/. -->
|
||||||
|
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item
|
||||||
|
android:drawable="@drawable/mozac_ic_warning"
|
||||||
|
android:bottom="14dp" />
|
||||||
|
</layer-list>
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,147 @@
|
|||||||
|
/* 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.library.bookmarks
|
||||||
|
|
||||||
|
import mozilla.components.concept.storage.BookmarkNode
|
||||||
|
import mozilla.components.concept.storage.BookmarkNodeType
|
||||||
|
import mozilla.components.support.test.robolectric.testContext
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||||
|
|
||||||
|
@RunWith(FenixRobolectricTestRunner::class)
|
||||||
|
class UtilsKtTest {
|
||||||
|
@Test
|
||||||
|
fun `friendly root titles`() {
|
||||||
|
val url = BookmarkNode(
|
||||||
|
BookmarkNodeType.ITEM,
|
||||||
|
"456",
|
||||||
|
"folder",
|
||||||
|
0,
|
||||||
|
"Mozilla",
|
||||||
|
"http://mozilla.org",
|
||||||
|
null
|
||||||
|
)
|
||||||
|
assertEquals("Mozilla", friendlyRootTitle(testContext, url))
|
||||||
|
|
||||||
|
val folder = BookmarkNode(
|
||||||
|
BookmarkNodeType.FOLDER,
|
||||||
|
"456",
|
||||||
|
"folder",
|
||||||
|
0,
|
||||||
|
"Folder",
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
assertEquals("Folder", friendlyRootTitle(testContext, folder))
|
||||||
|
|
||||||
|
val root = folder.copy(guid = "root________", title = "root")
|
||||||
|
assertEquals("Bookmarks", friendlyRootTitle(testContext, root, withMobileRoot = true))
|
||||||
|
assertEquals("Desktop Bookmarks", friendlyRootTitle(testContext, root, withMobileRoot = false))
|
||||||
|
|
||||||
|
val mobileRoot = folder.copy(guid = "mobile______", title = "mobile")
|
||||||
|
assertEquals("Bookmarks", friendlyRootTitle(testContext, mobileRoot, withMobileRoot = true))
|
||||||
|
assertEquals("mobile", friendlyRootTitle(testContext, mobileRoot, withMobileRoot = false))
|
||||||
|
|
||||||
|
val menuRoot = folder.copy(guid = "menu________", title = "menu")
|
||||||
|
assertEquals("Bookmarks Menu", friendlyRootTitle(testContext, menuRoot, withMobileRoot = true))
|
||||||
|
assertEquals("Bookmarks Menu", friendlyRootTitle(testContext, menuRoot, withMobileRoot = false))
|
||||||
|
|
||||||
|
val toolbarRoot = folder.copy(guid = "toolbar_____", title = "toolbar")
|
||||||
|
assertEquals("Bookmarks Toolbar", friendlyRootTitle(testContext, toolbarRoot, withMobileRoot = true))
|
||||||
|
assertEquals("Bookmarks Toolbar", friendlyRootTitle(testContext, toolbarRoot, withMobileRoot = false))
|
||||||
|
|
||||||
|
val unfiledRoot = folder.copy(guid = "unfiled_____", title = "unfiled")
|
||||||
|
assertEquals("Other Bookmarks", friendlyRootTitle(testContext, unfiledRoot, withMobileRoot = true))
|
||||||
|
assertEquals("Other Bookmarks", friendlyRootTitle(testContext, unfiledRoot, withMobileRoot = false))
|
||||||
|
|
||||||
|
val almostRoot = folder.copy(guid = "notRoot________", title = "root")
|
||||||
|
assertEquals("root", friendlyRootTitle(testContext, almostRoot, withMobileRoot = true))
|
||||||
|
assertEquals("root", friendlyRootTitle(testContext, almostRoot, withMobileRoot = false))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `flatNodeList various cases`() {
|
||||||
|
val url = BookmarkNode(
|
||||||
|
BookmarkNodeType.ITEM,
|
||||||
|
"456",
|
||||||
|
"folder",
|
||||||
|
0,
|
||||||
|
"Mozilla",
|
||||||
|
"http://mozilla.org",
|
||||||
|
null
|
||||||
|
)
|
||||||
|
val url2 = BookmarkNode(
|
||||||
|
BookmarkNodeType.ITEM,
|
||||||
|
"8674",
|
||||||
|
"folder2",
|
||||||
|
0,
|
||||||
|
"Mozilla",
|
||||||
|
"http://mozilla.org",
|
||||||
|
null
|
||||||
|
)
|
||||||
|
assertEquals(emptyList<BookmarkNodeWithDepth>(), url.flatNodeList(null))
|
||||||
|
|
||||||
|
val root = BookmarkNode(
|
||||||
|
BookmarkNodeType.FOLDER,
|
||||||
|
"root",
|
||||||
|
null,
|
||||||
|
0,
|
||||||
|
"root",
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
assertEquals(listOf(BookmarkNodeWithDepth(0, root, null)), root.flatNodeList(null))
|
||||||
|
assertEquals(emptyList<BookmarkNodeWithDepth>(), root.flatNodeList("root"))
|
||||||
|
|
||||||
|
val folder = BookmarkNode(
|
||||||
|
BookmarkNodeType.FOLDER,
|
||||||
|
"folder",
|
||||||
|
root.guid,
|
||||||
|
0,
|
||||||
|
"folder",
|
||||||
|
null,
|
||||||
|
listOf(url)
|
||||||
|
)
|
||||||
|
|
||||||
|
val folder3 = BookmarkNode(
|
||||||
|
BookmarkNodeType.FOLDER,
|
||||||
|
"folder3",
|
||||||
|
"folder2",
|
||||||
|
0,
|
||||||
|
"folder3",
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
|
||||||
|
val folder2 = BookmarkNode(
|
||||||
|
BookmarkNodeType.FOLDER,
|
||||||
|
"folder2",
|
||||||
|
root.guid,
|
||||||
|
0,
|
||||||
|
"folder2",
|
||||||
|
null,
|
||||||
|
listOf(folder3, url2)
|
||||||
|
)
|
||||||
|
|
||||||
|
val rootWithChildren = root.copy(children = listOf(folder, folder2))
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
BookmarkNodeWithDepth(0, rootWithChildren, null),
|
||||||
|
BookmarkNodeWithDepth(1, folder, "root"),
|
||||||
|
BookmarkNodeWithDepth(1, folder2, "root"),
|
||||||
|
BookmarkNodeWithDepth(2, folder3, "folder2")
|
||||||
|
), rootWithChildren.flatNodeList(null)
|
||||||
|
)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
listOf(
|
||||||
|
BookmarkNodeWithDepth(0, rootWithChildren, null),
|
||||||
|
BookmarkNodeWithDepth(1, folder, "root")
|
||||||
|
), rootWithChildren.flatNodeList(excludeSubtreeRoot = "folder2")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue