1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.fenix.home.intent
7 import android.content.Intent
8 import android.os.StrictMode
9 import androidx.navigation.NavController
10 import mozilla.components.browser.state.search.SearchEngine
11 import mozilla.components.browser.state.state.selectedOrDefaultSearchEngine
12 import mozilla.components.browser.state.store.BrowserStore
13 import mozilla.components.feature.search.ext.waitForSelectedOrDefaultSearchEngine
14 import org.mozilla.fenix.BrowserDirection
15 import org.mozilla.fenix.HomeActivity
16 import org.mozilla.fenix.components.metrics.MetricController
17 import org.mozilla.fenix.components.metrics.MetricsUtils
18 import org.mozilla.fenix.ext.components
19 import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.SPEECH_PROCESSING
22 * The search widget has a microphone button to let users search with their voice.
23 * Once the search is complete then a new search should be started.
25 class SpeechProcessingIntentProcessor(
26 private val activity: HomeActivity,
27 private val store: BrowserStore,
28 private val metrics: MetricController
29 ) : HomeIntentProcessor {
31 override fun process(intent: Intent, navController: NavController, out: Intent): Boolean {
33 !intent.hasExtra(SPEECH_PROCESSING) ||
34 intent.extras?.getBoolean(HomeActivity.OPEN_TO_BROWSER_AND_LOAD) != true
39 out.putExtra(HomeActivity.OPEN_TO_BROWSER_AND_LOAD, false)
41 store.waitForSelectedOrDefaultSearchEngine { searchEngine ->
42 if (searchEngine != null) {
45 intent.getStringExtra(SPEECH_PROCESSING).orEmpty()
53 private fun launchToBrowser(searchEngine: SearchEngine, text: String) {
54 activity.components.strictMode.resetAfter(StrictMode.allowThreadDiskReads()) {
55 MetricsUtils.recordSearchMetrics(
57 searchEngine == store.state.search.selectedOrDefaultSearchEngine,
58 MetricsUtils.Source.WIDGET
62 activity.openToBrowserAndLoad(
63 searchTermOrURL = text,
65 from = BrowserDirection.FromGlobal,
66 engine = searchEngine,