diff options
4 files changed, 167 insertions, 4 deletions
diff --git a/core/java/com/android/internal/accessibility/util/AccessibilityUtils.java b/core/java/com/android/internal/accessibility/util/AccessibilityUtils.java index 3a8f427d54bc..4f9fc39300ae 100644 --- a/core/java/com/android/internal/accessibility/util/AccessibilityUtils.java +++ b/core/java/com/android/internal/accessibility/util/AccessibilityUtils.java @@ -32,6 +32,9 @@ import android.content.pm.ResolveInfo; import android.os.Build; import android.os.UserHandle; import android.provider.Settings; +import android.telecom.TelecomManager; +import android.telephony.Annotation; +import android.telephony.TelephonyManager; import android.text.ParcelableSpan; import android.text.Spanned; import android.text.TextUtils; @@ -204,6 +207,32 @@ public final class AccessibilityUtils { } /** + * Intercepts the {@link AccessibilityService#GLOBAL_ACTION_KEYCODE_HEADSETHOOK} action + * by directly interacting with TelecomManager if a call is incoming or in progress. + * + * <p> + * Provided here in shared utils to be used by both the legacy and modern (SysUI) + * system action implementations. + * </p> + * + * @return True if the action was propagated to TelecomManager, otherwise false. + */ + public static boolean interceptHeadsetHookForActiveCall(Context context) { + final TelecomManager telecomManager = context.getSystemService(TelecomManager.class); + @Annotation.CallState final int callState = + telecomManager != null ? telecomManager.getCallState() + : TelephonyManager.CALL_STATE_IDLE; + if (callState == TelephonyManager.CALL_STATE_RINGING) { + telecomManager.acceptRingingCall(); + return true; + } else if (callState == TelephonyManager.CALL_STATE_OFFHOOK) { + telecomManager.endCall(); + return true; + } + return false; + } + + /** * Indicates whether the current user has completed setup via the setup wizard. * {@link android.provider.Settings.Secure#USER_SETUP_COMPLETE} * diff --git a/packages/SystemUI/src/com/android/systemui/accessibility/SystemActions.java b/packages/SystemUI/src/com/android/systemui/accessibility/SystemActions.java index f3c71da63594..4158390ec953 100644 --- a/packages/SystemUI/src/com/android/systemui/accessibility/SystemActions.java +++ b/packages/SystemUI/src/com/android/systemui/accessibility/SystemActions.java @@ -45,6 +45,8 @@ import android.view.accessibility.AccessibilityManager; import com.android.internal.R; import com.android.internal.accessibility.dialog.AccessibilityButtonChooserActivity; +import com.android.internal.accessibility.util.AccessibilityUtils; +import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.ScreenshotHelper; import com.android.systemui.CoreStartable; import com.android.systemui.dagger.SysUISingleton; @@ -520,8 +522,11 @@ public class SystemActions implements CoreStartable { SCREENSHOT_ACCESSIBILITY_ACTIONS, new Handler(Looper.getMainLooper()), null); } - private void handleHeadsetHook() { - sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HEADSETHOOK); + @VisibleForTesting + void handleHeadsetHook() { + if (!AccessibilityUtils.interceptHeadsetHookForActiveCall(mContext)) { + sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HEADSETHOOK); + } } private void handleAccessibilityButton() { diff --git a/packages/SystemUI/tests/src/com/android/systemui/accessibility/SystemActionsTest.java b/packages/SystemUI/tests/src/com/android/systemui/accessibility/SystemActionsTest.java new file mode 100644 index 000000000000..025c88c36203 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/accessibility/SystemActionsTest.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.accessibility; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.hardware.input.InputManager; +import android.os.RemoteException; +import android.telecom.TelecomManager; +import android.telephony.TelephonyManager; +import android.testing.AndroidTestingRunner; +import android.testing.TestableLooper; +import android.view.KeyEvent; + +import androidx.test.filters.SmallTest; + +import com.android.systemui.SysuiTestCase; +import com.android.systemui.recents.Recents; +import com.android.systemui.settings.FakeDisplayTracker; +import com.android.systemui.settings.UserTracker; +import com.android.systemui.shade.ShadeController; +import com.android.systemui.statusbar.NotificationShadeWindowController; +import com.android.systemui.statusbar.phone.CentralSurfaces; + +import dagger.Lazy; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@TestableLooper.RunWithLooper +@SmallTest +@RunWith(AndroidTestingRunner.class) +public class SystemActionsTest extends SysuiTestCase { + @Mock + private UserTracker mUserTracker; + @Mock + private NotificationShadeWindowController mNotificationShadeController; + @Mock + private ShadeController mShadeController; + @Mock + private Lazy<Optional<CentralSurfaces>> mCentralSurfacesOptionalLazy; + @Mock + private Optional<Recents> mRecentsOptional; + @Mock + private TelecomManager mTelecomManager; + @Mock + private InputManager mInputManager; + private final FakeDisplayTracker mDisplayTracker = new FakeDisplayTracker(mContext); + + private SystemActions mSystemActions; + + @Before + public void setUp() throws RemoteException { + MockitoAnnotations.initMocks(this); + mContext.addMockSystemService(TelecomManager.class, mTelecomManager); + mContext.addMockSystemService(InputManager.class, mInputManager); + mSystemActions = new SystemActions(mContext, mUserTracker, mNotificationShadeController, + mShadeController, mCentralSurfacesOptionalLazy, mRecentsOptional, mDisplayTracker); + } + + @Test + public void handleHeadsetHook_callStateIdle_injectsKeyEvents() { + when(mTelecomManager.getCallState()).thenReturn(TelephonyManager.CALL_STATE_IDLE); + // Use a custom doAnswer captor that copies the KeyEvent before storing it, because the + // method under test modifies the event object after injecting it which prevents + // reliably asserting on the event properties. + final List<KeyEvent> keyEvents = new ArrayList<>(); + doAnswer(invocation -> { + keyEvents.add(new KeyEvent(invocation.getArgument(0))); + return null; + }).when(mInputManager).injectInputEvent(any(), anyInt()); + + mSystemActions.handleHeadsetHook(); + + assertThat(keyEvents.size()).isEqualTo(2); + assertThat(keyEvents.get(0).getKeyCode()).isEqualTo(KeyEvent.KEYCODE_HEADSETHOOK); + assertThat(keyEvents.get(0).getAction()).isEqualTo(KeyEvent.ACTION_DOWN); + assertThat(keyEvents.get(1).getKeyCode()).isEqualTo(KeyEvent.KEYCODE_HEADSETHOOK); + assertThat(keyEvents.get(1).getAction()).isEqualTo(KeyEvent.ACTION_UP); + } + + @Test + public void handleHeadsetHook_callStateRinging_answersCall() { + when(mTelecomManager.getCallState()).thenReturn(TelephonyManager.CALL_STATE_RINGING); + + mSystemActions.handleHeadsetHook(); + + verify(mTelecomManager).acceptRingingCall(); + } + + @Test + public void handleHeadsetHook_callStateOffhook_endsCall() { + when(mTelecomManager.getCallState()).thenReturn(TelephonyManager.CALL_STATE_OFFHOOK); + + mSystemActions.handleHeadsetHook(); + + verify(mTelecomManager).endCall(); + } +} diff --git a/services/accessibility/java/com/android/server/accessibility/SystemActionPerformer.java b/services/accessibility/java/com/android/server/accessibility/SystemActionPerformer.java index a13df475d25d..9747579fa231 100644 --- a/services/accessibility/java/com/android/server/accessibility/SystemActionPerformer.java +++ b/services/accessibility/java/com/android/server/accessibility/SystemActionPerformer.java @@ -36,6 +36,7 @@ import android.view.WindowManager; import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; import com.android.internal.R; +import com.android.internal.accessibility.util.AccessibilityUtils; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.ScreenshotHelper; @@ -302,8 +303,10 @@ public class SystemActionPerformer { case AccessibilityService.GLOBAL_ACTION_TAKE_SCREENSHOT: return takeScreenshot(); case AccessibilityService.GLOBAL_ACTION_KEYCODE_HEADSETHOOK: - sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HEADSETHOOK, - InputDevice.SOURCE_KEYBOARD); + if (!AccessibilityUtils.interceptHeadsetHookForActiveCall(mContext)) { + sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HEADSETHOOK, + InputDevice.SOURCE_KEYBOARD); + } return true; case AccessibilityService.GLOBAL_ACTION_DPAD_UP: sendDownAndUpKeyEvents(KeyEvent.KEYCODE_DPAD_UP, |