[and] VoiceRecognition for search.

This commit is contained in:
Dmitry Kunin 2014-02-13 18:47:55 +03:00 committed by Alex Zolotarev
parent adfa57dc56
commit 54bb143302
3 changed files with 85 additions and 2 deletions

View file

@ -173,7 +173,6 @@
android:configChanges="orientation|screenLayout|screenSize"
android:label="@string/search_map"
android:windowSoftInputMode="stateVisible|adjustResize"
android:noHistory="true"
android:screenOrientation="behind" >
</activity>
<activity

View file

@ -2,6 +2,7 @@ package com.mapswithme.maps;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
@ -27,6 +28,7 @@ import android.widget.TextView;
import com.mapswithme.maps.base.MapsWithMeBaseListActivity;
import com.mapswithme.maps.location.LocationService;
import com.mapswithme.maps.search.SearchController;
import com.mapswithme.util.InputUtils;
import com.mapswithme.util.Language;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
@ -487,7 +489,7 @@ public class SearchActivity extends MapsWithMeBaseListActivity implements Locati
if (s.length() == 0) // enable voice input
{
UiUtils.invisible(mClearQueryBtn);
UiUtils.show(mVoiceInput);
UiUtils.hideIf(!InputUtils.isVoiceInputSupported(SearchActivity.this), mVoiceInput);
}
else // show clear cross
{
@ -555,6 +557,16 @@ public class SearchActivity extends MapsWithMeBaseListActivity implements Locati
{
}
});
mVoiceInput.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
final Intent vrIntent = InputUtils.createIntentForVoiceRecognition(getResources().getString(R.string.search_map));
startActivityForResult(vrIntent, RC_VOICE_RECOGNITIN);
}
});
}
@Override
@ -815,4 +827,23 @@ public class SearchActivity extends MapsWithMeBaseListActivity implements Locati
private native String getLastQuery();
private native void clearLastQuery();
// Handle voice recognition here
private final static int RC_VOICE_RECOGNITIN = 0xCA11;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.d("MwmSpeech", String.format("req=%d res=%d", requestCode, resultCode));
if ((requestCode == RC_VOICE_RECOGNITIN) && (resultCode == Activity.RESULT_OK))
{
final String result = InputUtils.getMostConfidentResult(data);
if (result != null)
mSearchBox.setText(result);
}
}
}

View file

@ -0,0 +1,53 @@
package com.mapswithme.util;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.util.Log;
public class InputUtils
{
private static Boolean mVoiceInputSupported = null;
public static boolean isVoiceInputSupported(Context context)
{
if (mVoiceInputSupported == null)
{
mVoiceInputSupported = Utils.isIntentSupported(context, new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}
return mVoiceInputSupported;
}
public static Intent createIntentForVoiceRecognition(String promptText)
{
final Intent vrIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
vrIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH)
.putExtra(RecognizerIntent.EXTRA_PROMPT, promptText);
return vrIntent;
}
/**
* @param vrIntentResult
* @return most confident result or null if nothing is available
*/
public static String getMostConfidentResult(Intent vrIntentResult)
{
final ArrayList<String> recongnizedStrings
= vrIntentResult.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (recongnizedStrings == null)
{
return null;
}
for (int i = 0; i < recongnizedStrings.size(); ++i)
Log.d("MwmSpeech", "RR: " + i + " " + recongnizedStrings.get(i));
return recongnizedStrings.isEmpty() ? null : recongnizedStrings.get(0);
}
private InputUtils() { /* static class */ }
}