From 5e7d12ef1effb30f8f3cef918e3f15aec2eb521c Mon Sep 17 00:00:00 2001 From: calderwoodra Date: Fri, 26 Jan 2018 11:21:44 -0800 Subject: Add voice search to NUI. Bug: 72525718 Test: MainActivityIntegrationTest PiperOrigin-RevId: 183418364 Change-Id: I30a4b7fe3e8a82c1b5cddcd747bd01ebd127b624 --- .../com/android/dialer/main/impl/MainActivity.java | 11 +++++++ .../dialer/main/impl/MainSearchController.java | 36 ++++++++++++++++++++-- .../dialer/main/impl/res/values/strings.xml | 3 ++ 3 files changed, 48 insertions(+), 2 deletions(-) (limited to 'java') diff --git a/java/com/android/dialer/main/impl/MainActivity.java b/java/com/android/dialer/main/impl/MainActivity.java index 168589f13..242bf2d48 100644 --- a/java/com/android/dialer/main/impl/MainActivity.java +++ b/java/com/android/dialer/main/impl/MainActivity.java @@ -28,6 +28,7 @@ import android.widget.ImageView; import com.android.dialer.calllog.ui.NewCallLogFragment; import com.android.dialer.common.LogUtil; import com.android.dialer.compat.CompatUtils; +import com.android.dialer.constants.ActivityRequestCodes; import com.android.dialer.contactsfragment.ContactsFragment; import com.android.dialer.contactsfragment.ContactsFragment.Header; import com.android.dialer.contactsfragment.ContactsFragment.OnContactSelectedListener; @@ -116,6 +117,16 @@ public final class MainActivity extends AppCompatActivity searchController.onSaveInstanceState(bundle); } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == ActivityRequestCodes.DIALTACTS_VOICE_SEARCH) { + searchController.onVoiceResults(resultCode, data); + } else { + LogUtil.e("MainActivity.onActivityResult", "Unknown request code: " + requestCode); + } + } + @Override public void onContactSelected(ImageView photo, Uri contactUri, long contactId) { // TODO(calderwoodra): Add impression logging diff --git a/java/com/android/dialer/main/impl/MainSearchController.java b/java/com/android/dialer/main/impl/MainSearchController.java index 041e32ed7..b01f80de6 100644 --- a/java/com/android/dialer/main/impl/MainSearchController.java +++ b/java/com/android/dialer/main/impl/MainSearchController.java @@ -17,16 +17,22 @@ package com.android.dialer.main.impl; import android.app.FragmentTransaction; +import android.content.ActivityNotFoundException; +import android.content.Intent; import android.os.Bundle; +import android.speech.RecognizerIntent; import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; +import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; +import android.widget.Toast; import com.android.dialer.callintent.CallInitiationType; import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; +import com.android.dialer.constants.ActivityRequestCodes; import com.android.dialer.dialpadview.DialpadFragment; import com.android.dialer.dialpadview.DialpadFragment.DialpadListener; import com.android.dialer.dialpadview.DialpadFragment.OnDialpadQueryChangedListener; @@ -36,6 +42,7 @@ import com.android.dialer.searchfragment.list.NewSearchFragment; import com.android.dialer.searchfragment.list.NewSearchFragment.SearchFragmentListener; import com.android.dialer.util.ViewUtil; import com.google.common.base.Optional; +import java.util.ArrayList; /** * Search controller for handling all the logic related to entering and exiting the search UI. @@ -255,8 +262,12 @@ final class MainSearchController implements SearchBarListener { */ @Override public void onSearchBarClicked() { + openSearch(Optional.absent()); + } + + private void openSearch(Optional query) { fab.hide(); - toolbar.expand(/* animate=*/ true, Optional.absent()); + toolbar.expand(/* animate=*/ true, query); toolbar.showKeyboard(); hideBottomNav(); @@ -294,7 +305,28 @@ final class MainSearchController implements SearchBarListener { } @Override - public void onVoiceButtonClicked(VoiceSearchResultCallback voiceSearchResultCallback) {} + public void onVoiceButtonClicked(VoiceSearchResultCallback voiceSearchResultCallback) { + try { + Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); + mainActivity.startActivityForResult(voiceIntent, ActivityRequestCodes.DIALTACTS_VOICE_SEARCH); + } catch (ActivityNotFoundException e) { + Toast.makeText(mainActivity, R.string.voice_search_not_available, Toast.LENGTH_SHORT).show(); + } + } + + public void onVoiceResults(int resultCode, Intent data) { + if (resultCode == AppCompatActivity.RESULT_OK) { + ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); + if (matches.size() > 0) { + LogUtil.i("MainSearchController.onVoiceResults", "voice search - match found"); + openSearch(Optional.of(matches.get(0))); + } else { + LogUtil.i("MainSearchController.onVoiceResults", "voice search - nothing heard"); + } + } else { + LogUtil.e("MainSearchController.onVoiceResults", "voice search failed"); + } + } @Override public void openSettings() {} diff --git a/java/com/android/dialer/main/impl/res/values/strings.xml b/java/com/android/dialer/main/impl/res/values/strings.xml index f530fa2bf..0fc1246d6 100644 --- a/java/com/android/dialer/main/impl/res/values/strings.xml +++ b/java/com/android/dialer/main/impl/res/values/strings.xml @@ -46,4 +46,7 @@ Voicemail Contacts + + + Voice search not available -- cgit v1.2.3