Add short voice search button handler to Input service.
- CarInputService allows to set listener for short VOICE_ASSISTANT key
press along with handler for long press events.
Bug: 19730682
Change-Id: Ia5bac58859f31e291595e58ae27adbed8ef9ddce
diff --git a/service/src/com/android/car/CarInputService.java b/service/src/com/android/car/CarInputService.java
index fbea87a..8b0bd3d 100644
--- a/service/src/com/android/car/CarInputService.java
+++ b/service/src/com/android/car/CarInputService.java
@@ -42,6 +42,7 @@
private final Context mContext;
+ private KeyEventListener mVoiceAssitantKeyListener;
private KeyEventListener mLongVoiceAssitantKeyListener;
private long mLastVoiceKeyDownTime = 0;
@@ -56,10 +57,21 @@
}
/**
+ * Set listener for listening voice assistant key event. Setting to null stops listening.
+ * If listener is not set, default behavior will be done for short press.
+ * If listener is set, short key press will lead into calling the listener.
+ * @param listener
+ */
+ public void setVoiceAssitantKeyListener(KeyEventListener listener) {
+ synchronized (this) {
+ mVoiceAssitantKeyListener = listener;
+ }
+ }
+
+ /**
* Set listener for listening long voice assistant key event. Setting to null stops listening.
- * If listener is not set, default behavior will be done for both short and long press.
- * If listener is set, short key press will still lead into default behavior but long press
- * will lead into calling the listener.
+ * If listener is not set, default behavior will be done for long press.
+ * If listener is set, short long press will lead into calling the listener.
* @param listener
*/
public void setLongVoiceAssitantKeyListener(KeyEventListener listener) {
@@ -100,6 +112,7 @@
@Override
public void release() {
synchronized (this) {
+ mVoiceAssitantKeyListener = null;
mLongVoiceAssitantKeyListener = null;
mInstumentClusterKeyListener = null;
if (mInjectionDeviceFd != null) {
@@ -143,16 +156,21 @@
} else if (action == KeyEvent.ACTION_UP) {
// if no listener, do not handle long press
KeyEventListener listener = null;
+ KeyEventListener shortPressListener = null;
+ KeyEventListener longPressListener = null;
long downTime;
synchronized (this) {
- listener = mLongVoiceAssitantKeyListener;
+ shortPressListener = mVoiceAssitantKeyListener;
+ longPressListener = mLongVoiceAssitantKeyListener;
downTime = mLastVoiceKeyDownTime;
}
- if (listener == null) {
+ if (shortPressListener == null && longPressListener == null) {
launchDefaultVoiceAssitantHandler();
} else {
long duration = SystemClock.elapsedRealtime() - downTime;
- if (duration > VOICE_LONG_PRESS_TIME_MS) {
+ listener = (duration > VOICE_LONG_PRESS_TIME_MS
+ ? longPressListener : shortPressListener);
+ if (listener != null) {
listener.onKeyEvent(event);
} else {
launchDefaultVoiceAssitantHandler();