Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.car; |
| 17 | |
kanant | 49fc8d4 | 2020-10-19 16:34:01 -0700 | [diff] [blame] | 18 | import static android.car.CarOccupantZoneManager.DisplayTypeEnum; |
Pavel Maltsev | 338cef8 | 2016-07-26 16:04:15 -0700 | [diff] [blame] | 19 | import static android.hardware.input.InputManager.INJECT_INPUT_EVENT_MODE_ASYNC; |
Nicholas Sauer | 3e2b20c | 2018-06-28 18:55:15 -0700 | [diff] [blame] | 20 | import static android.service.voice.VoiceInteractionSession.SHOW_SOURCE_PUSH_TO_TALK; |
Pavel Maltsev | 338cef8 | 2016-07-26 16:04:15 -0700 | [diff] [blame] | 21 | |
Antonio Kantek | 3a2c5a2 | 2021-06-04 16:02:24 -0700 | [diff] [blame] | 22 | import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.DUMP_INFO; |
| 23 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 25 | import android.annotation.UserIdInt; |
Nicholas Sauer | dcb3caa | 2018-06-19 15:04:04 -0700 | [diff] [blame] | 26 | import android.app.ActivityManager; |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 27 | import android.bluetooth.BluetoothAdapter; |
| 28 | import android.bluetooth.BluetoothDevice; |
| 29 | import android.bluetooth.BluetoothHeadsetClient; |
| 30 | import android.bluetooth.BluetoothProfile; |
kanant | 49fc8d4 | 2020-10-19 16:34:01 -0700 | [diff] [blame] | 31 | import android.car.CarOccupantZoneManager; |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 32 | import android.car.CarProjectionManager; |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 33 | import android.car.input.CarInputManager; |
kanant | ed82e29 | 2020-07-23 14:10:17 -0700 | [diff] [blame] | 34 | import android.car.input.CustomInputEvent; |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 35 | import android.car.input.ICarInput; |
| 36 | import android.car.input.ICarInputCallback; |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 37 | import android.car.input.RotaryEvent; |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 38 | import android.car.user.CarUserManager; |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 39 | import android.content.ContentResolver; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 40 | import android.content.Context; |
| 41 | import android.content.Intent; |
kanant | 20c2f3d | 2020-10-01 11:39:12 -0700 | [diff] [blame] | 42 | import android.content.pm.PackageManager; |
Babak Bostan | a2fc911 | 2020-02-12 11:44:47 -0800 | [diff] [blame] | 43 | import android.content.res.Resources; |
Pavel Maltsev | 338cef8 | 2016-07-26 16:04:15 -0700 | [diff] [blame] | 44 | import android.hardware.input.InputManager; |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 45 | import android.net.Uri; |
Antonio Kantek | f386191 | 2021-05-13 18:01:34 -0700 | [diff] [blame] | 46 | import android.os.Binder; |
Vitalii Tomkiv | 2bd0692 | 2016-07-26 11:07:48 -0700 | [diff] [blame] | 47 | import android.os.Bundle; |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 48 | import android.os.Handler; |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 49 | import android.os.Looper; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 50 | import android.os.UserHandle; |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 51 | import android.provider.CallLog.Calls; |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 52 | import android.provider.Settings; |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 53 | import android.telecom.TelecomManager; |
Vitalii Tomkiv | 2bd0692 | 2016-07-26 11:07:48 -0700 | [diff] [blame] | 54 | import android.text.TextUtils; |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 55 | import android.util.IndentingPrintWriter; |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 56 | import android.view.InputDevice; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 57 | import android.view.KeyEvent; |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 58 | import android.view.ViewConfiguration; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 59 | |
| 60 | import com.android.car.hal.InputHalService; |
Antonio Kantek | 3a2c5a2 | 2021-06-04 16:02:24 -0700 | [diff] [blame] | 61 | import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport; |
Mayank Garg | 801ea6a | 2020-09-29 15:43:49 -0700 | [diff] [blame] | 62 | import com.android.car.internal.common.UserHelperLite; |
Gaurav Bhola | 53fef5c | 2021-07-07 19:50:34 +0000 | [diff] [blame] | 63 | import com.android.car.pm.CarSafetyAccessibilityService; |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 64 | import com.android.car.user.CarUserService; |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 65 | import com.android.internal.annotations.GuardedBy; |
| 66 | import com.android.internal.annotations.VisibleForTesting; |
Nicholas Sauer | dcb3caa | 2018-06-19 15:04:04 -0700 | [diff] [blame] | 67 | import com.android.internal.app.AssistUtils; |
| 68 | import com.android.internal.app.IVoiceInteractionSessionShowCallback; |
Antonio Kantek | aa8a7cd | 2021-03-11 11:23:00 -0800 | [diff] [blame] | 69 | import com.android.internal.os.BackgroundThread; |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 70 | import com.android.server.utils.Slogf; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 71 | |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 72 | import java.util.ArrayList; |
Gaurav Bhola | 53fef5c | 2021-07-07 19:50:34 +0000 | [diff] [blame] | 73 | import java.util.Arrays; |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 74 | import java.util.BitSet; |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 75 | import java.util.Collections; |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 76 | import java.util.List; |
Alexander Mishkovets | de539cd | 2020-11-10 15:41:58 +0100 | [diff] [blame] | 77 | import java.util.function.BooleanSupplier; |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 78 | import java.util.function.IntSupplier; |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 79 | import java.util.function.Supplier; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 80 | |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 81 | /** |
| 82 | * CarInputService monitors and handles input event through vehicle HAL. |
| 83 | */ |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 84 | public class CarInputService extends ICarInput.Stub |
| 85 | implements CarServiceBase, InputHalService.InputListener { |
Gaurav Bhola | 53fef5c | 2021-07-07 19:50:34 +0000 | [diff] [blame] | 86 | public static final String ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR = ":"; |
| 87 | private static final int MAX_RETRIES_FOR_ENABLING_ACCESSIBILITY_SERVICES = 5; |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 88 | private static final String TAG = CarLog.TAG_INPUT; |
| 89 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 90 | /** An interface to receive {@link KeyEvent}s as they occur. */ |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 91 | public interface KeyEventListener { |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 92 | /** Called when a key event occurs. */ |
| 93 | void onKeyEvent(KeyEvent event); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 96 | private final class KeyPressTimer { |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 97 | private final Runnable mLongPressRunnable; |
| 98 | private final Runnable mCallback = this::onTimerExpired; |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 99 | private final IntSupplier mLongPressDelaySupplier; |
| 100 | |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 101 | @GuardedBy("CarInputService.this.mLock") |
| 102 | private final Handler mHandler; |
| 103 | @GuardedBy("CarInputService.this.mLock") |
| 104 | private boolean mDown; |
| 105 | @GuardedBy("CarInputService.this.mLock") |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 106 | private boolean mLongPress = false; |
Enrico Granata | 755dc57 | 2017-11-15 11:49:08 -0800 | [diff] [blame] | 107 | |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 108 | KeyPressTimer( |
| 109 | Handler handler, IntSupplier longPressDelaySupplier, Runnable longPressRunnable) { |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 110 | mHandler = handler; |
| 111 | mLongPressRunnable = longPressRunnable; |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 112 | mLongPressDelaySupplier = longPressDelaySupplier; |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** Marks that a key was pressed, and starts the long-press timer. */ |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 116 | void keyDown() { |
| 117 | synchronized (mLock) { |
| 118 | mDown = true; |
| 119 | mLongPress = false; |
| 120 | mHandler.removeCallbacks(mCallback); |
| 121 | mHandler.postDelayed(mCallback, mLongPressDelaySupplier.getAsInt()); |
| 122 | } |
Enrico Granata | 755dc57 | 2017-11-15 11:49:08 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 125 | /** |
| 126 | * Marks that a key was released, and stops the long-press timer. |
| 127 | * |
| 128 | * Returns true if the press was a long-press. |
| 129 | */ |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 130 | boolean keyUp() { |
| 131 | synchronized (mLock) { |
| 132 | mHandler.removeCallbacks(mCallback); |
| 133 | mDown = false; |
| 134 | return mLongPress; |
| 135 | } |
Enrico Granata | 755dc57 | 2017-11-15 11:49:08 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 138 | private void onTimerExpired() { |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 139 | synchronized (mLock) { |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 140 | // If the timer expires after key-up, don't retroactively make the press long. |
| 141 | if (!mDown) { |
| 142 | return; |
| 143 | } |
| 144 | mLongPress = true; |
Enrico Granata | 755dc57 | 2017-11-15 11:49:08 -0800 | [diff] [blame] | 145 | } |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 146 | mLongPressRunnable.run(); |
Enrico Granata | 755dc57 | 2017-11-15 11:49:08 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 150 | private final IVoiceInteractionSessionShowCallback mShowCallback = |
Nicholas Sauer | dcb3caa | 2018-06-19 15:04:04 -0700 | [diff] [blame] | 151 | new IVoiceInteractionSessionShowCallback.Stub() { |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 152 | @Override |
| 153 | public void onFailed() { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 154 | Slogf.w(TAG, "Failed to show VoiceInteractionSession"); |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 155 | } |
Nicholas Sauer | dcb3caa | 2018-06-19 15:04:04 -0700 | [diff] [blame] | 156 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 157 | @Override |
| 158 | public void onShown() { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 159 | Slogf.d(TAG, "IVoiceInteractionSessionShowCallback onShown()"); |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 160 | } |
| 161 | }; |
Nicholas Sauer | dcb3caa | 2018-06-19 15:04:04 -0700 | [diff] [blame] | 162 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 163 | @VisibleForTesting |
| 164 | static final String EXTRA_CAR_PUSH_TO_TALK = |
Nicholas Sauer | dcb3caa | 2018-06-19 15:04:04 -0700 | [diff] [blame] | 165 | "com.android.car.input.EXTRA_CAR_PUSH_TO_TALK"; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 166 | |
| 167 | private final Context mContext; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 168 | private final InputHalService mInputHalService; |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 169 | private final CarUserService mUserService; |
kanant | 20c2f3d | 2020-10-01 11:39:12 -0700 | [diff] [blame] | 170 | private final CarOccupantZoneService mCarOccupantZoneService; |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 171 | private final TelecomManager mTelecomManager; |
Nicholas Sauer | dcb3caa | 2018-06-19 15:04:04 -0700 | [diff] [blame] | 172 | private final AssistUtils mAssistUtils; |
kanant | 36553a0 | 2020-07-16 13:51:44 -0700 | [diff] [blame] | 173 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 174 | // The default handler for main-display input events. By default, injects the events into |
| 175 | // the input queue via InputManager, but can be overridden for testing. |
| 176 | private final KeyEventListener mMainDisplayHandler; |
| 177 | // The supplier for the last-called number. By default, gets the number from the call log. |
| 178 | // May be overridden for testing. |
| 179 | private final Supplier<String> mLastCalledNumberSupplier; |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 180 | // The supplier for the system long-press delay, in milliseconds. By default, gets the value |
| 181 | // from Settings.Secure for the current user, falling back to the system-wide default |
| 182 | // long-press delay defined in ViewConfiguration. May be overridden for testing. |
| 183 | private final IntSupplier mLongPressDelaySupplier; |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 184 | // ComponentName of the RotaryService. |
| 185 | private final String mRotaryServiceComponentName; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 186 | |
Alexander Mishkovets | de539cd | 2020-11-10 15:41:58 +0100 | [diff] [blame] | 187 | private final BooleanSupplier mShouldCallButtonEndOngoingCallSupplier; |
| 188 | |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 189 | private final Object mLock = new Object(); |
| 190 | |
| 191 | @GuardedBy("mLock") |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 192 | private CarProjectionManager.ProjectionKeyEventHandler mProjectionKeyEventHandler; |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 193 | |
| 194 | @GuardedBy("mLock") |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 195 | private final BitSet mProjectionKeyEventsSubscribed = new BitSet(); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 196 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 197 | private final KeyPressTimer mVoiceKeyTimer; |
| 198 | private final KeyPressTimer mCallKeyTimer; |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 199 | |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 200 | @GuardedBy("mLock") |
Pavel Maltsev | e11ad3a | 2016-03-25 15:40:24 -0700 | [diff] [blame] | 201 | private KeyEventListener mInstrumentClusterKeyListener; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 202 | |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 203 | private final InputCaptureClientController mCaptureController; |
| 204 | |
Antonio Kantek | 3d6174b | 2021-03-24 20:08:27 -0700 | [diff] [blame] | 205 | private final BluetoothAdapter mBluetoothAdapter; |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 206 | |
| 207 | // BluetoothHeadsetClient set through mBluetoothProfileServiceListener, and used by |
| 208 | // launchBluetoothVoiceRecognition(). |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 209 | @GuardedBy("mLock") |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 210 | private BluetoothHeadsetClient mBluetoothHeadsetClient; |
| 211 | |
| 212 | private final BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 213 | new BluetoothProfile.ServiceListener() { |
| 214 | @Override |
| 215 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
| 216 | if (profile == BluetoothProfile.HEADSET_CLIENT) { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 217 | Slogf.d(TAG, "Bluetooth proxy connected for HEADSET_CLIENT profile"); |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 218 | synchronized (mLock) { |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 219 | mBluetoothHeadsetClient = (BluetoothHeadsetClient) proxy; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void onServiceDisconnected(int profile) { |
| 226 | if (profile == BluetoothProfile.HEADSET_CLIENT) { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 227 | Slogf.d(TAG, "Bluetooth proxy disconnected for HEADSET_CLIENT profile"); |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 228 | synchronized (mLock) { |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 229 | mBluetoothHeadsetClient = null; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | }; |
| 234 | |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 235 | private final CarUserManager.UserLifecycleListener mUserLifecycleListener = event -> { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 236 | Slogf.d(TAG, "CarInputService.onEvent(%s)", event); |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 237 | if (CarUserManager.USER_LIFECYCLE_EVENT_TYPE_SWITCHING == event.getEventType()) { |
Gaurav Bhola | 53fef5c | 2021-07-07 19:50:34 +0000 | [diff] [blame] | 238 | updateCarAccessibilityServicesSettings(event.getUserId()); |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 239 | } |
| 240 | }; |
| 241 | |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 242 | private static int getViewLongPressDelay(ContentResolver cr) { |
| 243 | return Settings.Secure.getIntForUser( |
| 244 | cr, |
| 245 | Settings.Secure.LONG_PRESS_TIMEOUT, |
| 246 | ViewConfiguration.getLongPressTimeout(), |
| 247 | UserHandle.USER_CURRENT); |
| 248 | } |
| 249 | |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 250 | public CarInputService(Context context, InputHalService inputHalService, |
kanant | 20c2f3d | 2020-10-01 11:39:12 -0700 | [diff] [blame] | 251 | CarUserService userService, CarOccupantZoneService occupantZoneService) { |
| 252 | this(context, inputHalService, userService, occupantZoneService, |
| 253 | new Handler(Looper.getMainLooper()), |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 254 | context.getSystemService(TelecomManager.class), new AssistUtils(context), |
| 255 | event -> |
| 256 | context.getSystemService(InputManager.class) |
| 257 | .injectInputEvent(event, INJECT_INPUT_EVENT_MODE_ASYNC), |
| 258 | () -> Calls.getLastOutgoingCall(context), |
kanant | d3f3659 | 2020-07-30 16:52:23 -0700 | [diff] [blame] | 259 | () -> getViewLongPressDelay(context.getContentResolver()), |
Alexander Mishkovets | de539cd | 2020-11-10 15:41:58 +0100 | [diff] [blame] | 260 | () -> context.getResources().getBoolean(R.bool.config_callButtonEndsOngoingCall), |
Antonio Kantek | 3d6174b | 2021-03-24 20:08:27 -0700 | [diff] [blame] | 261 | new InputCaptureClientController(context), |
| 262 | BluetoothAdapter.getDefaultAdapter()); |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @VisibleForTesting |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 266 | CarInputService(Context context, InputHalService inputHalService, CarUserService userService, |
kanant | 20c2f3d | 2020-10-01 11:39:12 -0700 | [diff] [blame] | 267 | CarOccupantZoneService occupantZoneService, Handler handler, |
| 268 | TelecomManager telecomManager, AssistUtils assistUtils, |
| 269 | KeyEventListener mainDisplayHandler, |
| 270 | Supplier<String> lastCalledNumberSupplier, IntSupplier longPressDelaySupplier, |
Alexander Mishkovets | de539cd | 2020-11-10 15:41:58 +0100 | [diff] [blame] | 271 | BooleanSupplier shouldCallButtonEndOngoingCallSupplier, |
Antonio Kantek | 3d6174b | 2021-03-24 20:08:27 -0700 | [diff] [blame] | 272 | InputCaptureClientController captureController, BluetoothAdapter bluetoothAdapter) { |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 273 | mContext = context; |
kanant | d3f3659 | 2020-07-30 16:52:23 -0700 | [diff] [blame] | 274 | mCaptureController = captureController; |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 275 | mInputHalService = inputHalService; |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 276 | mUserService = userService; |
kanant | 20c2f3d | 2020-10-01 11:39:12 -0700 | [diff] [blame] | 277 | mCarOccupantZoneService = occupantZoneService; |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 278 | mTelecomManager = telecomManager; |
| 279 | mAssistUtils = assistUtils; |
| 280 | mMainDisplayHandler = mainDisplayHandler; |
| 281 | mLastCalledNumberSupplier = lastCalledNumberSupplier; |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 282 | mLongPressDelaySupplier = longPressDelaySupplier; |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 283 | |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 284 | mVoiceKeyTimer = |
| 285 | new KeyPressTimer( |
| 286 | handler, longPressDelaySupplier, this::handleVoiceAssistLongPress); |
| 287 | mCallKeyTimer = |
| 288 | new KeyPressTimer(handler, longPressDelaySupplier, this::handleCallLongPress); |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 289 | |
| 290 | mRotaryServiceComponentName = mContext.getString(R.string.rotaryService); |
Alexander Mishkovets | de539cd | 2020-11-10 15:41:58 +0100 | [diff] [blame] | 291 | mShouldCallButtonEndOngoingCallSupplier = shouldCallButtonEndOngoingCallSupplier; |
Antonio Kantek | 3d6174b | 2021-03-24 20:08:27 -0700 | [diff] [blame] | 292 | mBluetoothAdapter = bluetoothAdapter; |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 295 | /** |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 296 | * Set projection key event listener. If null, unregister listener. |
Vitalii Tomkiv | 61e2c23 | 2016-03-08 10:48:51 -0800 | [diff] [blame] | 297 | */ |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 298 | public void setProjectionKeyEventHandler( |
| 299 | @Nullable CarProjectionManager.ProjectionKeyEventHandler listener, |
| 300 | @Nullable BitSet events) { |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 301 | synchronized (mLock) { |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 302 | mProjectionKeyEventHandler = listener; |
| 303 | mProjectionKeyEventsSubscribed.clear(); |
| 304 | if (events != null) { |
| 305 | mProjectionKeyEventsSubscribed.or(events); |
| 306 | } |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Antonio Kantek | 040c28f | 2020-07-08 10:35:41 -0700 | [diff] [blame] | 310 | /** |
| 311 | * Sets the instrument cluster key event listener. |
| 312 | */ |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 313 | public void setInstrumentClusterKeyListener(KeyEventListener listener) { |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 314 | synchronized (mLock) { |
Pavel Maltsev | e11ad3a | 2016-03-25 15:40:24 -0700 | [diff] [blame] | 315 | mInstrumentClusterKeyListener = listener; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public void init() { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 321 | if (!mInputHalService.isKeyInputSupported()) { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 322 | Slogf.w(TAG, "Hal does not support key input."); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 323 | return; |
| 324 | } |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 325 | Slogf.d(TAG, "Hal supports key input."); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 326 | mInputHalService.setInputListener(this); |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 327 | if (mBluetoothAdapter != null) { |
Antonio Kantek | aa8a7cd | 2021-03-11 11:23:00 -0800 | [diff] [blame] | 328 | BackgroundThread.getHandler().post(() -> { |
| 329 | mBluetoothAdapter.getProfileProxy(mContext, |
| 330 | mBluetoothProfileServiceListener, BluetoothProfile.HEADSET_CLIENT); |
| 331 | }); |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 332 | } |
Gaurav Bhola | 53fef5c | 2021-07-07 19:50:34 +0000 | [diff] [blame] | 333 | mUserService.addUserLifecycleListener(mUserLifecycleListener); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public void release() { |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 338 | synchronized (mLock) { |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 339 | mProjectionKeyEventHandler = null; |
| 340 | mProjectionKeyEventsSubscribed.clear(); |
Pavel Maltsev | e11ad3a | 2016-03-25 15:40:24 -0700 | [diff] [blame] | 341 | mInstrumentClusterKeyListener = null; |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 342 | if (mBluetoothHeadsetClient != null) { |
| 343 | mBluetoothAdapter.closeProfileProxy( |
| 344 | BluetoothProfile.HEADSET_CLIENT, mBluetoothHeadsetClient); |
| 345 | mBluetoothHeadsetClient = null; |
| 346 | } |
| 347 | } |
Gaurav Bhola | 53fef5c | 2021-07-07 19:50:34 +0000 | [diff] [blame] | 348 | mUserService.removeUserLifecycleListener(mUserLifecycleListener); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Override |
Antonio Kantek | 65b9bc3 | 2021-03-16 12:01:25 -0700 | [diff] [blame] | 352 | public void onKeyEvent(KeyEvent event, @DisplayTypeEnum int targetDisplayType) { |
Scott Randolph | 64e8882 | 2018-03-15 11:00:05 -0700 | [diff] [blame] | 353 | // Special case key code that have special "long press" handling for automotive |
Vitalii Tomkiv | 2bd0692 | 2016-07-26 11:07:48 -0700 | [diff] [blame] | 354 | switch (event.getKeyCode()) { |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 355 | case KeyEvent.KEYCODE_VOICE_ASSIST: |
| 356 | handleVoiceAssistKey(event); |
| 357 | return; |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 358 | case KeyEvent.KEYCODE_CALL: |
| 359 | handleCallKey(event); |
| 360 | return; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 361 | default: |
| 362 | break; |
| 363 | } |
Scott Randolph | 64e8882 | 2018-03-15 11:00:05 -0700 | [diff] [blame] | 364 | |
Antonio Kantek | 65b9bc3 | 2021-03-16 12:01:25 -0700 | [diff] [blame] | 365 | assignDisplayId(event, targetDisplayType); |
| 366 | |
Scott Randolph | 64e8882 | 2018-03-15 11:00:05 -0700 | [diff] [blame] | 367 | // Allow specifically targeted keys to be routed to the cluster |
Antonio Kantek | 65b9bc3 | 2021-03-16 12:01:25 -0700 | [diff] [blame] | 368 | if (targetDisplayType == CarOccupantZoneManager.DISPLAY_TYPE_INSTRUMENT_CLUSTER |
Yuncheol Heo | 34ba7dd | 2021-03-12 20:27:19 -0800 | [diff] [blame] | 369 | && handleInstrumentClusterKey(event)) { |
| 370 | return; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 371 | } |
Antonio Kantek | 65b9bc3 | 2021-03-16 12:01:25 -0700 | [diff] [blame] | 372 | if (mCaptureController.onKeyEvent(targetDisplayType, event)) { |
Yuncheol Heo | 34ba7dd | 2021-03-12 20:27:19 -0800 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | mMainDisplayHandler.onKeyEvent(event); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Antonio Kantek | 65b9bc3 | 2021-03-16 12:01:25 -0700 | [diff] [blame] | 378 | private void assignDisplayId(KeyEvent event, @DisplayTypeEnum int targetDisplayType) { |
| 379 | // Setting display id for driver user id (currently MAIN and CLUSTER display types are |
| 380 | // linked to driver user only) |
| 381 | int newDisplayId = mCarOccupantZoneService.getDisplayIdForDriver(targetDisplayType); |
| 382 | |
| 383 | // Display id is overridden even if already set. |
| 384 | event.setDisplayId(newDisplayId); |
| 385 | } |
| 386 | |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 387 | @Override |
kanant | 49fc8d4 | 2020-10-19 16:34:01 -0700 | [diff] [blame] | 388 | public void onRotaryEvent(RotaryEvent event, @DisplayTypeEnum int targetDisplay) { |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 389 | if (!mCaptureController.onRotaryEvent(targetDisplay, event)) { |
| 390 | List<KeyEvent> keyEvents = rotaryEventToKeyEvents(event); |
| 391 | for (KeyEvent keyEvent : keyEvents) { |
| 392 | onKeyEvent(keyEvent, targetDisplay); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
kanant | ed82e29 | 2020-07-23 14:10:17 -0700 | [diff] [blame] | 397 | @Override |
| 398 | public void onCustomInputEvent(CustomInputEvent event) { |
| 399 | if (!mCaptureController.onCustomInputEvent(event)) { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 400 | Slogf.w(TAG, "Failed to propagate (%s)", event); |
kanant | ed82e29 | 2020-07-23 14:10:17 -0700 | [diff] [blame] | 401 | return; |
| 402 | } |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 403 | Slogf.d(TAG, "Succeed injecting (%s)", event); |
kanant | ed82e29 | 2020-07-23 14:10:17 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 406 | private static List<KeyEvent> rotaryEventToKeyEvents(RotaryEvent event) { |
| 407 | int numClicks = event.getNumberOfClicks(); |
| 408 | int numEvents = numClicks * 2; // up / down per each click |
| 409 | boolean clockwise = event.isClockwise(); |
| 410 | int keyCode; |
| 411 | switch (event.getInputType()) { |
| 412 | case CarInputManager.INPUT_TYPE_ROTARY_NAVIGATION: |
| 413 | keyCode = clockwise |
| 414 | ? KeyEvent.KEYCODE_NAVIGATE_NEXT |
| 415 | : KeyEvent.KEYCODE_NAVIGATE_PREVIOUS; |
| 416 | break; |
| 417 | case CarInputManager.INPUT_TYPE_ROTARY_VOLUME: |
| 418 | keyCode = clockwise |
| 419 | ? KeyEvent.KEYCODE_VOLUME_UP |
| 420 | : KeyEvent.KEYCODE_VOLUME_DOWN; |
| 421 | break; |
| 422 | default: |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 423 | Slogf.e(TAG, "Unknown rotary input type: %d", event.getInputType()); |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 424 | return Collections.EMPTY_LIST; |
| 425 | } |
| 426 | ArrayList<KeyEvent> keyEvents = new ArrayList<>(numEvents); |
| 427 | for (int i = 0; i < numClicks; i++) { |
| 428 | long uptime = event.getUptimeMillisForClick(i); |
| 429 | KeyEvent downEvent = createKeyEvent(/* down= */ true, uptime, uptime, keyCode); |
| 430 | KeyEvent upEvent = createKeyEvent(/* down= */ false, uptime, uptime, keyCode); |
| 431 | keyEvents.add(downEvent); |
| 432 | keyEvents.add(upEvent); |
| 433 | } |
| 434 | return keyEvents; |
| 435 | } |
| 436 | |
| 437 | private static KeyEvent createKeyEvent(boolean down, long downTime, long eventTime, |
| 438 | int keyCode) { |
| 439 | return new KeyEvent( |
| 440 | downTime, |
| 441 | eventTime, |
| 442 | /* action= */ down ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP, |
| 443 | keyCode, |
| 444 | /* repeat= */ 0, |
| 445 | /* metaState= */ 0, |
| 446 | /* deviceId= */ 0, |
| 447 | /* scancode= */ 0, |
| 448 | /* flags= */ 0, |
| 449 | InputDevice.SOURCE_CLASS_BUTTON); |
| 450 | } |
| 451 | |
Antonio Kantek | 48e16af | 2021-08-05 22:50:43 -0700 | [diff] [blame] | 452 | /** |
| 453 | * Requests capturing of input event for the specified display for all requested input types. |
| 454 | * |
| 455 | * Currently this method requires {@code android.car.permission.CAR_MONITOR_INPUT} or |
| 456 | * {@code android.permission.MONITOR_INPUT} permissions (any of them will be acceptable). |
| 457 | */ |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 458 | @Override |
kanant | 49fc8d4 | 2020-10-19 16:34:01 -0700 | [diff] [blame] | 459 | public int requestInputEventCapture(ICarInputCallback callback, |
| 460 | @DisplayTypeEnum int targetDisplayType, |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 461 | int[] inputTypes, int requestFlags) { |
| 462 | return mCaptureController.requestInputEventCapture(callback, targetDisplayType, inputTypes, |
| 463 | requestFlags); |
| 464 | } |
| 465 | |
Antonio Kantek | 48e16af | 2021-08-05 22:50:43 -0700 | [diff] [blame] | 466 | /** |
| 467 | * Overloads #requestInputEventCapture(int, int[], int, CarInputCaptureCallback) by providing |
| 468 | * a {@link java.util.concurrent.Executor} to be used when invoking the callback argument. |
| 469 | * |
| 470 | * Currently this method requires {@code android.car.permission.CAR_MONITOR_INPUT} or |
| 471 | * {@code android.permission.MONITOR_INPUT} permissions (any of them will be acceptable). |
| 472 | */ |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 473 | @Override |
kanant | 49fc8d4 | 2020-10-19 16:34:01 -0700 | [diff] [blame] | 474 | public void releaseInputEventCapture(ICarInputCallback callback, |
| 475 | @DisplayTypeEnum int targetDisplayType) { |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 476 | mCaptureController.releaseInputEventCapture(callback, targetDisplayType); |
| 477 | } |
| 478 | |
kanant | 20c2f3d | 2020-10-01 11:39:12 -0700 | [diff] [blame] | 479 | /** |
| 480 | * Injects the {@link KeyEvent} passed as parameter against Car Input API. |
| 481 | * <p> |
| 482 | * The event's display id will be overridden accordingly to the display type (it will be |
| 483 | * retrieved from {@link CarOccupantZoneService}). |
| 484 | * |
kanant | 49fc8d4 | 2020-10-19 16:34:01 -0700 | [diff] [blame] | 485 | * @param event the event to inject |
kanant | 20c2f3d | 2020-10-01 11:39:12 -0700 | [diff] [blame] | 486 | * @param targetDisplayType the display type associated with the event |
| 487 | * @throws SecurityException when caller doesn't have INJECT_EVENTS permission granted |
| 488 | */ |
| 489 | @Override |
kanant | 49fc8d4 | 2020-10-19 16:34:01 -0700 | [diff] [blame] | 490 | public void injectKeyEvent(KeyEvent event, @DisplayTypeEnum int targetDisplayType) { |
kanant | 20c2f3d | 2020-10-01 11:39:12 -0700 | [diff] [blame] | 491 | // Permission check |
| 492 | if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 493 | android.Manifest.permission.INJECT_EVENTS)) { |
| 494 | throw new SecurityException("Injecting KeyEvent requires INJECT_EVENTS permission"); |
| 495 | } |
| 496 | |
Antonio Kantek | f386191 | 2021-05-13 18:01:34 -0700 | [diff] [blame] | 497 | long token = Binder.clearCallingIdentity(); |
| 498 | try { |
| 499 | // Redirect event to onKeyEvent |
| 500 | onKeyEvent(event, targetDisplayType); |
| 501 | } finally { |
| 502 | Binder.restoreCallingIdentity(token); |
| 503 | } |
kanant | 20c2f3d | 2020-10-01 11:39:12 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 506 | private void handleVoiceAssistKey(KeyEvent event) { |
| 507 | int action = event.getAction(); |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 508 | if (action == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { |
Enrico Granata | 755dc57 | 2017-11-15 11:49:08 -0800 | [diff] [blame] | 509 | mVoiceKeyTimer.keyDown(); |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 510 | dispatchProjectionKeyEvent(CarProjectionManager.KEY_EVENT_VOICE_SEARCH_KEY_DOWN); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 511 | } else if (action == KeyEvent.ACTION_UP) { |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 512 | if (mVoiceKeyTimer.keyUp()) { |
| 513 | // Long press already handled by handleVoiceAssistLongPress(), nothing more to do. |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 514 | // Hand it off to projection, if it's interested, otherwise we're done. |
| 515 | dispatchProjectionKeyEvent( |
| 516 | CarProjectionManager.KEY_EVENT_VOICE_SEARCH_LONG_PRESS_KEY_UP); |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 517 | return; |
| 518 | } |
Enrico Granata | 755dc57 | 2017-11-15 11:49:08 -0800 | [diff] [blame] | 519 | |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 520 | if (dispatchProjectionKeyEvent( |
| 521 | CarProjectionManager.KEY_EVENT_VOICE_SEARCH_SHORT_PRESS_KEY_UP)) { |
| 522 | return; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 523 | } |
Enrico Granata | 755dc57 | 2017-11-15 11:49:08 -0800 | [diff] [blame] | 524 | |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 525 | launchDefaultVoiceAssistantHandler(); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 529 | private void handleVoiceAssistLongPress() { |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 530 | // If projection wants this event, let it take it. |
| 531 | if (dispatchProjectionKeyEvent( |
| 532 | CarProjectionManager.KEY_EVENT_VOICE_SEARCH_LONG_PRESS_KEY_DOWN)) { |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 533 | return; |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 534 | } |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 535 | // Otherwise, try to launch voice recognition on a BT device. |
| 536 | if (launchBluetoothVoiceRecognition()) { |
| 537 | return; |
| 538 | } |
| 539 | // Finally, fallback to the default voice assist handling. |
| 540 | launchDefaultVoiceAssistantHandler(); |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 543 | private void handleCallKey(KeyEvent event) { |
| 544 | int action = event.getAction(); |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 545 | if (action == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { |
Enrico Granata | 755dc57 | 2017-11-15 11:49:08 -0800 | [diff] [blame] | 546 | mCallKeyTimer.keyDown(); |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 547 | dispatchProjectionKeyEvent(CarProjectionManager.KEY_EVENT_CALL_KEY_DOWN); |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 548 | } else if (action == KeyEvent.ACTION_UP) { |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 549 | if (mCallKeyTimer.keyUp()) { |
| 550 | // Long press already handled by handleCallLongPress(), nothing more to do. |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 551 | // Hand it off to projection, if it's interested, otherwise we're done. |
| 552 | dispatchProjectionKeyEvent(CarProjectionManager.KEY_EVENT_CALL_LONG_PRESS_KEY_UP); |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 553 | return; |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 554 | } |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 555 | |
| 556 | if (acceptCallIfRinging()) { |
| 557 | // Ringing call answered, nothing more to do. |
| 558 | return; |
| 559 | } |
| 560 | |
Alexander Mishkovets | de539cd | 2020-11-10 15:41:58 +0100 | [diff] [blame] | 561 | if (mShouldCallButtonEndOngoingCallSupplier.getAsBoolean() && endCall()) { |
| 562 | // On-going call ended, nothing more to do. |
| 563 | return; |
| 564 | } |
| 565 | |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 566 | if (dispatchProjectionKeyEvent( |
| 567 | CarProjectionManager.KEY_EVENT_CALL_SHORT_PRESS_KEY_UP)) { |
| 568 | return; |
| 569 | } |
| 570 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 571 | launchDialerHandler(); |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 575 | private void handleCallLongPress() { |
| 576 | // Long-press answers call if ringing, same as short-press. |
| 577 | if (acceptCallIfRinging()) { |
| 578 | return; |
| 579 | } |
| 580 | |
Alexander Mishkovets | de539cd | 2020-11-10 15:41:58 +0100 | [diff] [blame] | 581 | if (mShouldCallButtonEndOngoingCallSupplier.getAsBoolean() && endCall()) { |
| 582 | return; |
| 583 | } |
| 584 | |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 585 | if (dispatchProjectionKeyEvent(CarProjectionManager.KEY_EVENT_CALL_LONG_PRESS_KEY_DOWN)) { |
| 586 | return; |
| 587 | } |
| 588 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 589 | dialLastCallHandler(); |
| 590 | } |
| 591 | |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 592 | private boolean dispatchProjectionKeyEvent(@CarProjectionManager.KeyEventNum int event) { |
| 593 | CarProjectionManager.ProjectionKeyEventHandler projectionKeyEventHandler; |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 594 | synchronized (mLock) { |
Justin Paupore | a057d05 | 2019-03-29 15:04:00 -0700 | [diff] [blame] | 595 | projectionKeyEventHandler = mProjectionKeyEventHandler; |
| 596 | if (projectionKeyEventHandler == null || !mProjectionKeyEventsSubscribed.get(event)) { |
| 597 | // No event handler, or event handler doesn't want this event - we're done. |
| 598 | return false; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | projectionKeyEventHandler.onKeyEvent(event); |
| 603 | return true; |
| 604 | } |
| 605 | |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 606 | private void launchDialerHandler() { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 607 | Slogf.i(TAG, "call key, launch dialer intent"); |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 608 | Intent dialerIntent = new Intent(Intent.ACTION_DIAL); |
| 609 | mContext.startActivityAsUser(dialerIntent, null, UserHandle.CURRENT_OR_SELF); |
| 610 | } |
| 611 | |
| 612 | private void dialLastCallHandler() { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 613 | Slogf.i(TAG, "call key, dialing last call"); |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 614 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 615 | String lastNumber = mLastCalledNumberSupplier.get(); |
| 616 | if (!TextUtils.isEmpty(lastNumber)) { |
Vitalii Tomkiv | e2142e5 | 2016-04-29 11:35:26 -0700 | [diff] [blame] | 617 | Intent callLastNumberIntent = new Intent(Intent.ACTION_CALL) |
| 618 | .setData(Uri.fromParts("tel", lastNumber, null)) |
| 619 | .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 620 | mContext.startActivityAsUser(callLastNumberIntent, null, UserHandle.CURRENT_OR_SELF); |
| 621 | } |
| 622 | } |
| 623 | |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 624 | private boolean acceptCallIfRinging() { |
| 625 | if (mTelecomManager != null && mTelecomManager.isRinging()) { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 626 | Slogf.i(TAG, "call key while ringing. Answer the call!"); |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 627 | mTelecomManager.acceptRingingCall(); |
| 628 | return true; |
| 629 | } |
Justin Paupore | fcd39fe | 2019-01-30 17:13:15 -0800 | [diff] [blame] | 630 | return false; |
| 631 | } |
| 632 | |
Alexander Mishkovets | de539cd | 2020-11-10 15:41:58 +0100 | [diff] [blame] | 633 | private boolean endCall() { |
| 634 | if (mTelecomManager != null && mTelecomManager.isInCall()) { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 635 | Slogf.i(TAG, "End the call!"); |
Alexander Mishkovets | de539cd | 2020-11-10 15:41:58 +0100 | [diff] [blame] | 636 | mTelecomManager.endCall(); |
| 637 | return true; |
| 638 | } |
| 639 | return false; |
| 640 | } |
| 641 | |
Babak Bostan | a2fc911 | 2020-02-12 11:44:47 -0800 | [diff] [blame] | 642 | private boolean isBluetoothVoiceRecognitionEnabled() { |
| 643 | Resources res = mContext.getResources(); |
| 644 | return res.getBoolean(R.bool.enableLongPressBluetoothVoiceRecognition); |
| 645 | } |
| 646 | |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 647 | private boolean launchBluetoothVoiceRecognition() { |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 648 | synchronized (mLock) { |
Babak Bostan | a2fc911 | 2020-02-12 11:44:47 -0800 | [diff] [blame] | 649 | if (mBluetoothHeadsetClient == null || !isBluetoothVoiceRecognitionEnabled()) { |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | // getConnectedDevices() does not make any guarantees about the order of the returned |
| 653 | // list. As of 2019-02-26, this code is only triggered through a long-press of the |
| 654 | // voice recognition key, so handling of multiple connected devices that support voice |
| 655 | // recognition is not expected to be a primary use case. |
| 656 | List<BluetoothDevice> devices = mBluetoothHeadsetClient.getConnectedDevices(); |
| 657 | if (devices != null) { |
| 658 | for (BluetoothDevice device : devices) { |
| 659 | Bundle bundle = mBluetoothHeadsetClient.getCurrentAgFeatures(device); |
| 660 | if (bundle == null || !bundle.getBoolean( |
| 661 | BluetoothHeadsetClient.EXTRA_AG_FEATURE_VOICE_RECOGNITION)) { |
| 662 | continue; |
| 663 | } |
| 664 | if (mBluetoothHeadsetClient.startVoiceRecognition(device)) { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 665 | Slogf.d(TAG, "started voice recognition on BT device at (%s)", |
| 666 | device.getAddress()); |
George Lu | abb9a96 | 2019-02-15 14:45:52 -0800 | [diff] [blame] | 667 | return true; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | return false; |
| 673 | } |
| 674 | |
Pavel Maltsev | e11ad3a | 2016-03-25 15:40:24 -0700 | [diff] [blame] | 675 | private void launchDefaultVoiceAssistantHandler() { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 676 | Slogf.i(TAG, "voice key, invoke AssistUtils"); |
Nicholas Sauer | dcb3caa | 2018-06-19 15:04:04 -0700 | [diff] [blame] | 677 | |
| 678 | if (mAssistUtils.getAssistComponentForUser(ActivityManager.getCurrentUser()) == null) { |
Antonio Kantek | abf133e | 2021-04-21 10:24:51 -0700 | [diff] [blame] | 679 | Slogf.w(TAG, "Unable to retrieve assist component for current user"); |
Nicholas Sauer | dcb3caa | 2018-06-19 15:04:04 -0700 | [diff] [blame] | 680 | return; |
| 681 | } |
| 682 | |
| 683 | final Bundle args = new Bundle(); |
| 684 | args.putBoolean(EXTRA_CAR_PUSH_TO_TALK, true); |
| 685 | |
| 686 | mAssistUtils.showSessionForActiveService(args, |
Nicholas Sauer | 3e2b20c | 2018-06-28 18:55:15 -0700 | [diff] [blame] | 687 | SHOW_SOURCE_PUSH_TO_TALK, mShowCallback, null /*activityToken*/); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Yuncheol Heo | 34ba7dd | 2021-03-12 20:27:19 -0800 | [diff] [blame] | 690 | /** |
| 691 | * @return false if the KeyEvent isn't consumed because there is no |
| 692 | * InstrumentClusterKeyListener. |
| 693 | */ |
| 694 | private boolean handleInstrumentClusterKey(KeyEvent event) { |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 695 | KeyEventListener listener = null; |
Antonio Kantek | 8239431 | 2019-12-03 17:00:13 -0800 | [diff] [blame] | 696 | synchronized (mLock) { |
Pavel Maltsev | e11ad3a | 2016-03-25 15:40:24 -0700 | [diff] [blame] | 697 | listener = mInstrumentClusterKeyListener; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 698 | } |
| 699 | if (listener == null) { |
Yuncheol Heo | 34ba7dd | 2021-03-12 20:27:19 -0800 | [diff] [blame] | 700 | return false; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 701 | } |
| 702 | listener.onKeyEvent(event); |
Yuncheol Heo | 34ba7dd | 2021-03-12 20:27:19 -0800 | [diff] [blame] | 703 | return true; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Gaurav Bhola | 53fef5c | 2021-07-07 19:50:34 +0000 | [diff] [blame] | 706 | private List<String> getAccessibilityServicesToBeEnabled() { |
| 707 | String carSafetyAccessibilityServiceComponentName = mContext.getPackageName() |
| 708 | + "/" |
| 709 | + CarSafetyAccessibilityService.class.getName(); |
| 710 | ArrayList<String> accessibilityServicesToBeEnabled = new ArrayList<>(); |
| 711 | accessibilityServicesToBeEnabled.add(carSafetyAccessibilityServiceComponentName); |
| 712 | if (!TextUtils.isEmpty(mRotaryServiceComponentName)) { |
| 713 | accessibilityServicesToBeEnabled.add(mRotaryServiceComponentName); |
| 714 | } |
| 715 | return accessibilityServicesToBeEnabled; |
| 716 | } |
| 717 | |
| 718 | private static List<String> createServiceListFromSettingsString( |
| 719 | String accessibilityServicesString) { |
| 720 | return TextUtils.isEmpty(accessibilityServicesString) |
| 721 | ? new ArrayList<>() |
| 722 | : Arrays.asList(accessibilityServicesString.split( |
| 723 | ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR)); |
| 724 | } |
| 725 | |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 726 | @Override |
Antonio Kantek | 3a2c5a2 | 2021-06-04 16:02:24 -0700 | [diff] [blame] | 727 | @ExcludeFromCodeCoverageGeneratedReport(reason = DUMP_INFO) |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 728 | public void dump(IndentingPrintWriter writer) { |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 729 | writer.println("*Input Service*"); |
Justin Paupore | d6c8e70 | 2019-04-23 18:22:08 -0700 | [diff] [blame] | 730 | writer.println("Long-press delay: " + mLongPressDelaySupplier.getAsInt() + "ms"); |
Alexander Mishkovets | de539cd | 2020-11-10 15:41:58 +0100 | [diff] [blame] | 731 | writer.println("Call button ends ongoing call: " |
| 732 | + mShouldCallButtonEndOngoingCallSupplier.getAsBoolean()); |
Keun young Park | 401479c | 2020-02-19 14:15:51 -0800 | [diff] [blame] | 733 | mCaptureController.dump(writer); |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 734 | } |
| 735 | |
Gaurav Bhola | 53fef5c | 2021-07-07 19:50:34 +0000 | [diff] [blame] | 736 | private void updateCarAccessibilityServicesSettings(@UserIdInt int userId) { |
Mayank Garg | 94f3eb9 | 2020-08-12 12:38:58 -0700 | [diff] [blame] | 737 | if (UserHelperLite.isHeadlessSystemUser(userId)) { |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 738 | return; |
| 739 | } |
Gaurav Bhola | 53fef5c | 2021-07-07 19:50:34 +0000 | [diff] [blame] | 740 | List<String> accessibilityServicesToBeEnabled = getAccessibilityServicesToBeEnabled(); |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 741 | ContentResolver contentResolver = mContext.getContentResolver(); |
Gaurav Bhola | 53fef5c | 2021-07-07 19:50:34 +0000 | [diff] [blame] | 742 | List<String> alreadyEnabledServices = createServiceListFromSettingsString( |
| 743 | Settings.Secure.getStringForUser(contentResolver, |
| 744 | Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, |
| 745 | userId)); |
| 746 | |
| 747 | int retry = 0; |
| 748 | while (!alreadyEnabledServices.containsAll(accessibilityServicesToBeEnabled) |
| 749 | && retry <= MAX_RETRIES_FOR_ENABLING_ACCESSIBILITY_SERVICES) { |
| 750 | ArrayList<String> enabledServicesList = new ArrayList<>(alreadyEnabledServices); |
| 751 | int numAccessibilityServicesToBeEnabled = accessibilityServicesToBeEnabled.size(); |
| 752 | for (int i = 0; i < numAccessibilityServicesToBeEnabled; i++) { |
| 753 | String serviceToBeEnabled = accessibilityServicesToBeEnabled.get(i); |
| 754 | if (!enabledServicesList.contains(serviceToBeEnabled)) { |
| 755 | enabledServicesList.add(serviceToBeEnabled); |
| 756 | } |
| 757 | } |
| 758 | Settings.Secure.putStringForUser(contentResolver, |
| 759 | Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, |
| 760 | String.join(ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR, enabledServicesList), |
| 761 | userId); |
| 762 | // Read again to account for any race condition with other parts of the code that might |
| 763 | // be enabling other accessibility services. |
| 764 | alreadyEnabledServices = createServiceListFromSettingsString( |
| 765 | Settings.Secure.getStringForUser(contentResolver, |
| 766 | Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, |
| 767 | userId)); |
| 768 | retry++; |
| 769 | } |
| 770 | if (!alreadyEnabledServices.containsAll(accessibilityServicesToBeEnabled)) { |
| 771 | Slogf.e(TAG, "Failed to enable accessibility services"); |
| 772 | } |
| 773 | |
Yabin Huang | 9cbc758 | 2020-05-27 13:04:15 -0700 | [diff] [blame] | 774 | Settings.Secure.putStringForUser(contentResolver, |
| 775 | Settings.Secure.ACCESSIBILITY_ENABLED, |
| 776 | "1", |
| 777 | userId); |
| 778 | } |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 779 | } |