Add Voice Assist key.

The action taken is dependent on whether the device is in an
interactive state, allowing for interactions to work even when the
device isn't actively being used

Bug: 16292420
Change-Id: I897f4383e7e4766c0bcb6246b41514b46e03a9ac
diff --git a/api/current.txt b/api/current.txt
index 5b8ba4d..87d9f82 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -33376,6 +33376,7 @@
     field public static final int KEYCODE_U = 49; // 0x31
     field public static final int KEYCODE_UNKNOWN = 0; // 0x0
     field public static final int KEYCODE_V = 50; // 0x32
+    field public static final int KEYCODE_VOICE_ASSIST = 231; // 0xe7
     field public static final int KEYCODE_VOLUME_DOWN = 25; // 0x19
     field public static final int KEYCODE_VOLUME_MUTE = 164; // 0xa4
     field public static final int KEYCODE_VOLUME_UP = 24; // 0x18
diff --git a/core/java/android/view/KeyEvent.java b/core/java/android/view/KeyEvent.java
index 681717c..964b054 100644
--- a/core/java/android/view/KeyEvent.java
+++ b/core/java/android/view/KeyEvent.java
@@ -657,11 +657,15 @@
     /** Key code constant: TV data service key.
      * Displays data services like weather, sports. */
     public static final int KEYCODE_TV_DATA_SERVICE = 230;
+    /** Key code constant: Voice Assist key.
+     * Launches the global voice assist activity. Not delivered to applications. */
+    public static final int KEYCODE_VOICE_ASSIST = 231;
 
-    private static final int LAST_KEYCODE = KEYCODE_TV_DATA_SERVICE;
+    private static final int LAST_KEYCODE = KEYCODE_VOICE_ASSIST;
 
     // NOTE: If you add a new keycode here you must also add it to:
     //  isSystem()
+    //  isWakeKey()
     //  frameworks/native/include/android/keycodes.h
     //  frameworks/native/include/input/InputEventLabels.h
     //  frameworks/base/core/res/res/values/attrs.xml
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index 3f7c72e..9e268c3 100644
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -70,6 +70,7 @@
 import android.service.dreams.DreamManagerInternal;
 import android.service.dreams.DreamService;
 import android.service.dreams.IDreamManager;
+import android.speech.RecognizerIntent;
 import android.telecomm.TelecommManager;
 import android.util.DisplayMetrics;
 import android.util.EventLog;
@@ -2338,6 +2339,17 @@
                 }
             }
             return -1;
+        } else if (keyCode == KeyEvent.KEYCODE_VOICE_ASSIST) {
+            if (!down) {
+                Intent voiceIntent;
+                if (!keyguardOn && mPowerManager.isInteractive()) {
+                    voiceIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
+                } else {
+                    voiceIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
+                    voiceIntent.putExtra(RecognizerIntent.EXTRA_SECURE, keyguardOn);
+                }
+                mContext.startActivityAsUser(voiceIntent, UserHandle.CURRENT_OR_SELF);
+            }
         } else if (keyCode == KeyEvent.KEYCODE_SYSRQ) {
             if (down && repeatCount == 0) {
                 mHandler.post(mScreenshotRunnable);