parent
3f41a4e9c6
commit
88aa519210
@ -0,0 +1,100 @@
|
|||||||
|
/* 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.widget
|
||||||
|
|
||||||
|
import android.content.ComponentName
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.speech.RecognizerIntent
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import org.mozilla.fenix.HomeActivity
|
||||||
|
import org.mozilla.fenix.IntentReceiverActivity
|
||||||
|
import org.mozilla.fenix.components.metrics.Event
|
||||||
|
import org.mozilla.fenix.ext.metrics
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Launches voice recognition then uses it to start a new web search.
|
||||||
|
*/
|
||||||
|
class VoiceSearchActivity : AppCompatActivity() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds the intent that initially started this activity
|
||||||
|
* so that it can persist through the speech activity.
|
||||||
|
*/
|
||||||
|
private var previousIntent: Intent? = null
|
||||||
|
|
||||||
|
override fun onSaveInstanceState(outState: Bundle) {
|
||||||
|
super.onSaveInstanceState(outState)
|
||||||
|
outState.putParcelable(PREVIOUS_INTENT, previousIntent)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
// Retrieve the previous intent from the saved state
|
||||||
|
previousIntent = savedInstanceState?.get(PREVIOUS_INTENT) as Intent?
|
||||||
|
if (previousIntent.isForSpeechProcessing()) {
|
||||||
|
// Don't reopen the speech recognizer
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// The intent property is nullable, but the rest of the code below assumes it is not.
|
||||||
|
val intent = intent?.let { Intent(intent) } ?: Intent()
|
||||||
|
|
||||||
|
if (intent.isForSpeechProcessing()) {
|
||||||
|
previousIntent = intent
|
||||||
|
displaySpeechRecognizer()
|
||||||
|
} else {
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays a speech recognizer popup that listens for input from the user.
|
||||||
|
*/
|
||||||
|
private fun displaySpeechRecognizer() {
|
||||||
|
val intentSpeech = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
|
||||||
|
putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
|
||||||
|
}
|
||||||
|
metrics.track(Event.SearchWidgetVoiceSearchPressed)
|
||||||
|
|
||||||
|
startActivityForResult(intentSpeech, SPEECH_REQUEST_CODE)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
|
||||||
|
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
|
||||||
|
val spokenText = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)?.first()
|
||||||
|
val context = this
|
||||||
|
|
||||||
|
previousIntent?.apply {
|
||||||
|
component = ComponentName(context, IntentReceiverActivity::class.java)
|
||||||
|
putExtra(SPEECH_PROCESSING, spokenText)
|
||||||
|
putExtra(HomeActivity.OPEN_TO_BROWSER_AND_LOAD, true)
|
||||||
|
startActivity(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the [SPEECH_PROCESSING] extra is present and set to true.
|
||||||
|
* Returns false if the intent is null.
|
||||||
|
*/
|
||||||
|
private fun Intent?.isForSpeechProcessing(): Boolean =
|
||||||
|
this?.getBooleanExtra(SPEECH_PROCESSING, false) == true
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
internal const val SPEECH_REQUEST_CODE = 0
|
||||||
|
internal const val PREVIOUS_INTENT = "org.mozilla.fenix.previous_intent"
|
||||||
|
/**
|
||||||
|
* In [VoiceSearchActivity] activity, used to store if the speech processing should start.
|
||||||
|
* In [IntentReceiverActivity] activity, used to store the search terms.
|
||||||
|
*/
|
||||||
|
const val SPEECH_PROCESSING = "speech_processing"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,115 @@
|
|||||||
|
/* 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.widget
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.ComponentName
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.speech.RecognizerIntent.ACTION_RECOGNIZE_SPEECH
|
||||||
|
import android.speech.RecognizerIntent.EXTRA_LANGUAGE_MODEL
|
||||||
|
import android.speech.RecognizerIntent.EXTRA_RESULTS
|
||||||
|
import android.speech.RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
|
||||||
|
import androidx.appcompat.app.AppCompatActivity.RESULT_OK
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.ObsoleteCoroutinesApi
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertNull
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mozilla.fenix.HomeActivity.Companion.OPEN_TO_BROWSER_AND_LOAD
|
||||||
|
import org.mozilla.fenix.IntentReceiverActivity
|
||||||
|
import org.mozilla.fenix.TestApplication
|
||||||
|
import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.PREVIOUS_INTENT
|
||||||
|
import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.SPEECH_PROCESSING
|
||||||
|
import org.mozilla.fenix.widget.VoiceSearchActivity.Companion.SPEECH_REQUEST_CODE
|
||||||
|
import org.robolectric.Robolectric
|
||||||
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.Shadows
|
||||||
|
import org.robolectric.android.controller.ActivityController
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
import org.robolectric.shadows.ShadowActivity
|
||||||
|
|
||||||
|
@ObsoleteCoroutinesApi
|
||||||
|
@ExperimentalCoroutinesApi
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
@Config(application = TestApplication::class)
|
||||||
|
class VoiceSearchActivityTest {
|
||||||
|
|
||||||
|
private lateinit var controller: ActivityController<VoiceSearchActivity>
|
||||||
|
private lateinit var activity: Activity
|
||||||
|
private lateinit var shadow: ShadowActivity
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
val intent = Intent()
|
||||||
|
intent.putExtra(SPEECH_PROCESSING, true)
|
||||||
|
|
||||||
|
controller = Robolectric.buildActivity(VoiceSearchActivity::class.java, intent)
|
||||||
|
activity = controller.get()
|
||||||
|
shadow = Shadows.shadowOf(activity)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `process intent with speech processing set to true`() {
|
||||||
|
controller.create()
|
||||||
|
|
||||||
|
val intentForResult = shadow.peekNextStartedActivityForResult()
|
||||||
|
assertEquals(SPEECH_REQUEST_CODE, intentForResult.requestCode)
|
||||||
|
assertEquals(ACTION_RECOGNIZE_SPEECH, intentForResult.intent.action)
|
||||||
|
assertEquals(LANGUAGE_MODEL_FREE_FORM, intentForResult.intent.getStringExtra(EXTRA_LANGUAGE_MODEL))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `process intent with speech processing set to false`() {
|
||||||
|
val intent = Intent()
|
||||||
|
intent.putExtra(SPEECH_PROCESSING, false)
|
||||||
|
|
||||||
|
val controller = Robolectric.buildActivity(VoiceSearchActivity::class.java, intent)
|
||||||
|
val activity = controller.get()
|
||||||
|
|
||||||
|
controller.create()
|
||||||
|
|
||||||
|
assertTrue(activity.isFinishing)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `process intent with speech processing in previous intent set to true`() {
|
||||||
|
val savedInstanceState = Bundle()
|
||||||
|
val previousIntent = Intent().apply {
|
||||||
|
putExtra(SPEECH_PROCESSING, true)
|
||||||
|
}
|
||||||
|
savedInstanceState.putParcelable(PREVIOUS_INTENT, previousIntent)
|
||||||
|
|
||||||
|
controller.create(savedInstanceState)
|
||||||
|
|
||||||
|
assertFalse(activity.isFinishing)
|
||||||
|
assertNull(shadow.peekNextStartedActivityForResult())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `handle speech result`() {
|
||||||
|
controller.create()
|
||||||
|
|
||||||
|
val resultIntent = Intent().apply {
|
||||||
|
putStringArrayListExtra(EXTRA_RESULTS, arrayListOf("hello world"))
|
||||||
|
}
|
||||||
|
shadow.receiveResult(
|
||||||
|
shadow.peekNextStartedActivityForResult().intent,
|
||||||
|
RESULT_OK,
|
||||||
|
resultIntent
|
||||||
|
)
|
||||||
|
|
||||||
|
val browserIntent = shadow.peekNextStartedActivity()
|
||||||
|
|
||||||
|
assertTrue(activity.isFinishing)
|
||||||
|
assertEquals(ComponentName(activity, IntentReceiverActivity::class.java), browserIntent.component)
|
||||||
|
assertEquals("hello world", browserIntent.getStringExtra(SPEECH_PROCESSING))
|
||||||
|
assertTrue(browserIntent.getBooleanExtra(OPEN_TO_BROWSER_AND_LOAD, false))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue