[fenix] For https://github.com/mozilla-mobile/fenix/issues/24211: Remove wrapper...
[gecko.git] / mobile / android / fenix / app / src / main / java / org / mozilla / fenix / home / intent / SpeechProcessingIntentProcessor.kt
blob6aff6ffb4a003c7cb1ad095d4d800a8cabde20b0
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
21 /**
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.
24  */
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 {
32         if (
33             !intent.hasExtra(SPEECH_PROCESSING) ||
34             intent.extras?.getBoolean(HomeActivity.OPEN_TO_BROWSER_AND_LOAD) != true
35         ) {
36             return false
37         }
39         out.putExtra(HomeActivity.OPEN_TO_BROWSER_AND_LOAD, false)
41         store.waitForSelectedOrDefaultSearchEngine { searchEngine ->
42             if (searchEngine != null) {
43                 launchToBrowser(
44                     searchEngine,
45                     intent.getStringExtra(SPEECH_PROCESSING).orEmpty()
46                 )
47             }
48         }
50         return true
51     }
53     private fun launchToBrowser(searchEngine: SearchEngine, text: String) {
54         activity.components.strictMode.resetAfter(StrictMode.allowThreadDiskReads()) {
55             MetricsUtils.recordSearchMetrics(
56                 searchEngine,
57                 searchEngine == store.state.search.selectedOrDefaultSearchEngine,
58                 MetricsUtils.Source.WIDGET
59             )
60         }
62         activity.openToBrowserAndLoad(
63             searchTermOrURL = text,
64             newTab = true,
65             from = BrowserDirection.FromGlobal,
66             engine = searchEngine,
67             forceSearch = true
68         )
69     }