From e682cb55cc422240d3d0c12a14e0a58efb0c01f4 Mon Sep 17 00:00:00 2001 From: mcarare Date: Tue, 27 Oct 2020 17:09:50 +0200 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/15430: Fix StrictMode death on opening app from widget voice search. --- .../intent/SpeechProcessingIntentProcessor.kt | 16 +++++++++------- .../mozilla/fenix/widget/VoiceSearchActivity.kt | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/home/intent/SpeechProcessingIntentProcessor.kt b/app/src/main/java/org/mozilla/fenix/home/intent/SpeechProcessingIntentProcessor.kt index ec8398fbd9..be6fe57de0 100644 --- a/app/src/main/java/org/mozilla/fenix/home/intent/SpeechProcessingIntentProcessor.kt +++ b/app/src/main/java/org/mozilla/fenix/home/intent/SpeechProcessingIntentProcessor.kt @@ -5,6 +5,7 @@ package org.mozilla.fenix.home.intent import android.content.Intent +import android.os.StrictMode import androidx.navigation.NavController import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.HomeActivity @@ -26,13 +27,14 @@ class SpeechProcessingIntentProcessor( override fun process(intent: Intent, navController: NavController, out: Intent): Boolean { return if (intent.extras?.getBoolean(HomeActivity.OPEN_TO_BROWSER_AND_LOAD) == true) { out.putExtra(HomeActivity.OPEN_TO_BROWSER_AND_LOAD, false) - - val searchEvent = MetricsUtils.createSearchEvent( - activity.components.search.provider.getDefaultEngine(activity), - activity, - Event.PerformedSearch.SearchAccessPoint.WIDGET - ) - searchEvent?.let { metrics.track(it) } + activity.components.strictMode.resetAfter(StrictMode.allowThreadDiskReads()) { + val searchEvent = MetricsUtils.createSearchEvent( + activity.components.search.provider.getDefaultEngine(activity), + activity, + Event.PerformedSearch.SearchAccessPoint.WIDGET + ) + searchEvent?.let { metrics.track(it) } + } activity.openToBrowserAndLoad( searchTermOrURL = intent.getStringExtra(SPEECH_PROCESSING).orEmpty(), diff --git a/app/src/main/java/org/mozilla/fenix/widget/VoiceSearchActivity.kt b/app/src/main/java/org/mozilla/fenix/widget/VoiceSearchActivity.kt index d76d4864d6..d46631137a 100644 --- a/app/src/main/java/org/mozilla/fenix/widget/VoiceSearchActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/widget/VoiceSearchActivity.kt @@ -7,12 +7,14 @@ package org.mozilla.fenix.widget import android.content.ComponentName import android.content.Intent import android.os.Bundle +import android.os.StrictMode import android.speech.RecognizerIntent import androidx.appcompat.app.AppCompatActivity import mozilla.components.support.locale.LocaleManager import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.IntentReceiverActivity import org.mozilla.fenix.components.metrics.Event +import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.metrics /** @@ -62,8 +64,16 @@ class VoiceSearchActivity : AppCompatActivity() { */ private fun displaySpeechRecognizer() { val intentSpeech = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply { - putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) - putExtra(RecognizerIntent.EXTRA_LANGUAGE, LocaleManager.getCurrentLocale(this@VoiceSearchActivity)) + putExtra( + RecognizerIntent.EXTRA_LANGUAGE_MODEL, + RecognizerIntent.LANGUAGE_MODEL_FREE_FORM + ) + putExtra( + RecognizerIntent.EXTRA_LANGUAGE, + components.strictMode.resetAfter(StrictMode.allowThreadDiskReads()) { + LocaleManager.getCurrentLocale(this@VoiceSearchActivity) + } + ) } metrics.track(Event.SearchWidgetVoiceSearchPressed)