Enable voice search in omnibox

  Bug: 6601348

Bring back voice search. Also fixes focus bug for fixed titlebar.

Change-Id: I83c8ad96957b62c23ad0bbb35685712687d11213
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 8a00ba3..e901d45 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -52,6 +52,7 @@
 import android.provider.BrowserContract.Images;
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.Intents.Insert;
+import android.speech.RecognizerIntent;
 import android.text.TextUtils;
 import android.util.Log;
 import android.util.Patterns;
@@ -129,6 +130,7 @@
     final static int PREFERENCES_PAGE = 3;
     final static int FILE_SELECTED = 4;
     final static int AUTOFILL_SETUP = 5;
+    final static int VOICE_RESULT = 6;
 
     private final static int WAKELOCK_TIMEOUT = 5 * 60 * 1000; // 5 minutes
 
@@ -216,6 +218,8 @@
 
     private boolean mBlockEvents;
 
+    private String mVoiceResult;
+
     public Controller(Activity browser) {
         mActivity = browser;
         mSettings = BrowserSettings.getInstance();
@@ -700,6 +704,10 @@
         mNetworkHandler.onResume();
         WebView.enablePlatformNotifications();
         NfcHandler.register(mActivity, this);
+        if (mVoiceResult != null) {
+            mUi.onVoiceResult(mVoiceResult);
+            mVoiceResult = null;
+        }
     }
 
     private void releaseWakeLock() {
@@ -1190,6 +1198,15 @@
                     }
                 }
                 break;
+            case VOICE_RESULT:
+                if (resultCode == Activity.RESULT_OK && intent != null) {
+                    ArrayList<String> results = intent.getStringArrayListExtra(
+                            RecognizerIntent.EXTRA_RESULTS);
+                    if (results.size() >= 1) {
+                        mVoiceResult = results.get(0);
+                    }
+                }
+                break;
             default:
                 break;
         }
@@ -2804,6 +2821,23 @@
     }
 
     @Override
+    public boolean supportsVoice() {
+        PackageManager pm = mActivity.getPackageManager();
+        List activities = pm.queryIntentActivities(new Intent(
+                RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
+        return activities.size() != 0;
+    }
+
+    @Override
+    public void startVoiceRecognizer() {
+        Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
+        voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
+                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
+        voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
+        mActivity.startActivityForResult(voice, VOICE_RESULT);
+    }
+
+    @Override
     public void setBlockEvents(boolean block) {
         mBlockEvents = block;
     }