Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1dcf7281 authored by Daniel Norman's avatar Daniel Norman
Browse files

Updates the HEADSETHOOK system action to directly answer/hangup calls.

Updated in both the legacy system (SystemActionPerformer) as well as
the new system (SystemActions in SysUI), in case a device does not use
the new SysUI style.

This preserves the HEADSETHOOK accessibility system action behavior
while allowing the telecom team the ability to change the listener
behavior for these key events.

- If a call is incoming: answer the call
- If a call is in progress: hang up the call
- Otherwise: send the KeyEvent so that media is played/paused

Bug: 278177899
Test: Unit test with mocks:
        atest SystemActionsTest
Test: CTS within the Telecom test suite:
        atest android.telecom.cts.AccessibilitySystemActionTest
Test: manual with a service that sends this system action
Change-Id: I735b3a14cefb6322ed1535ded83c4a08df4ada1a
parent 9188aa60
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -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;
@@ -203,6 +206,32 @@ public final class AccessibilityUtils {
        return false;
    }

    /**
     * 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}
+7 −2
Original line number Diff line number Diff line
@@ -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,9 +522,12 @@ public class SystemActions implements CoreStartable {
                SCREENSHOT_ACCESSIBILITY_ACTIONS, new Handler(Looper.getMainLooper()), null);
    }

    private void handleHeadsetHook() {
    @VisibleForTesting
    void handleHeadsetHook() {
        if (!AccessibilityUtils.interceptHeadsetHookForActiveCall(mContext)) {
            sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HEADSETHOOK);
        }
    }

    private void handleAccessibilityButton() {
        AccessibilityManager.getInstance(mContext).notifyAccessibilityButtonClicked(
+126 −0
Original line number Diff line number Diff line
/*
 * 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();
    }
}
+5 −2
Original line number Diff line number Diff line
@@ -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:
                    if (!AccessibilityUtils.interceptHeadsetHookForActiveCall(mContext)) {
                        sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HEADSETHOOK,
                                InputDevice.SOURCE_KEYBOARD);
                    }
                    return true;
                case AccessibilityService.GLOBAL_ACTION_DPAD_UP:
                    sendDownAndUpKeyEvents(KeyEvent.KEYCODE_DPAD_UP,