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

Commit 1e7c306f authored by yyalan's avatar yyalan
Browse files

[3FT] Launch a selected app when three finger tap is triggered

When clicking on a radio button, we use its key to restore the AppLaunchData, and set the gesture to launch this app.

Demo in comment.

Bug: 399645334
Flag: com.android.settings.flags.touchpad_settings_design_update
Flag: com.android.settings.flags.three_finger_tap_app_launch
Test: TouchpadThreeFingerTapAppSelectionPreferenceControllerTest
Change-Id: I7c3f2da61b4faca85e600e014782a54716e80995
parent 904b3755
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -131,13 +131,12 @@ public class TouchpadThreeFingerTapActionPreferenceController extends BasePrefer
    }

    private void setGesture(int customGestureType) {
        boolean isUnspecified = customGestureType == KeyGestureEvent.KEY_GESTURE_TYPE_UNSPECIFIED;
        InputGestureData gestureData = isUnspecified ? null : new InputGestureData.Builder()
        mInputManager.removeAllCustomInputGestures(InputGestureData.Filter.TOUCHPAD);
        if (customGestureType != KeyGestureEvent.KEY_GESTURE_TYPE_UNSPECIFIED) {
            InputGestureData gestureData = new InputGestureData.Builder()
                    .setTrigger(TouchpadThreeFingerTapUtils.TRIGGER)
                    .setKeyGestureType(customGestureType)
                    .build();
        mInputManager.removeAllCustomInputGestures(InputGestureData.Filter.TOUCHPAD);
        if (!isUnspecified) {
            mInputManager.addCustomInputGesture(gestureData);
        }
        TouchpadThreeFingerTapUtils.setGestureType(mContentResolver, customGestureType);
+153 −8
Original line number Diff line number Diff line
@@ -16,16 +16,35 @@

package com.android.settings.inputmethod;

import static android.content.ComponentName.unflattenFromString;
import static android.hardware.input.AppLaunchData.createLaunchDataForComponent;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.database.ContentObserver;
import android.hardware.input.AppLaunchData;
import android.hardware.input.AppLaunchData.ComponentData;
import android.hardware.input.InputGestureData;
import android.hardware.input.InputManager;
import android.hardware.input.InputSettings;
import android.hardware.input.KeyGestureEvent;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.util.DisplayMetrics;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.core.BasePreferenceController;
@@ -39,16 +58,113 @@ import java.util.List;
 * This screen is a sub-page of {@link TouchpadThreeFingerTapPreferenceController}) allowing three
 * finger tap gesture to open the selected app.
 */
public class TouchpadThreeFingerTapAppSelectionPreferenceController
        extends BasePreferenceController {
public class TouchpadThreeFingerTapAppSelectionPreferenceController extends BasePreferenceController
        implements LifecycleEventObserver, SelectorWithWidgetPreference.OnClickListener {

    private final ContentResolver mContentResolver;
    private LauncherApps mLauncherApps;
    private InputManager mInputManager;

    private ContentObserver mObserver =
            new ContentObserver(new Handler(Looper.getMainLooper())) {
                @Override
                public void onChange(boolean selfChange, @Nullable Uri uri) {
                    if (uri == null || mPreferenceScreen == null) {
                        return;
                    }
                    if (uri.equals(TouchpadThreeFingerTapUtils.TARGET_ACTION_URI)) {
                        updateState(mPreferenceScreen);
                    }
                }
            };

    private final LauncherApps mLauncherApps;
    @Nullable private PreferenceScreen mPreferenceScreen;

    public TouchpadThreeFingerTapAppSelectionPreferenceController(@NonNull Context context,
            @NonNull String key) {
        super(context, key);
        mLauncherApps = mContext.getSystemService(LauncherApps.class);
        mLauncherApps = context.getSystemService(LauncherApps.class);
        mInputManager = context.getSystemService(InputManager.class);
        mContentResolver = context.getContentResolver();
    }

    @VisibleForTesting
    TouchpadThreeFingerTapAppSelectionPreferenceController(@NonNull Context context,
            @NonNull String key,
            LauncherApps launcherApps,
            InputManager inputManager,
            ContentObserver contentObserver) {
        this(context, key);
        mLauncherApps = launcherApps;
        mInputManager = inputManager;
        mObserver = contentObserver;
    }

    @Override
    public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner,
            @NonNull Lifecycle.Event event) {
        if (event == Lifecycle.Event.ON_START) {
            mContentResolver.registerContentObserver(
                    TouchpadThreeFingerTapUtils.TARGET_ACTION_URI,
                    /* notifyForDescendants = */ true, mObserver);
        } else if (event == Lifecycle.Event.ON_STOP) {
            mContentResolver.unregisterContentObserver(mObserver);
        }
    }

    @Override
    public void updateState(@NonNull Preference preference) {
        super.updateState(preference);
        updateAppListSelection();
    }

    private void updateAppListSelection() {
        int current = TouchpadThreeFingerTapUtils.getCurrentGestureType(mContentResolver);

        // When the current gesture state is not app launching, the key is set to null so that no
        // app Preference will be selected
        String matchingKey =
                current == KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION
                        ? parsePreferenceKey() : null;
        updateCheckStatus(matchingKey);
    }

    private void updateCheckStatus(@Nullable String matchingKey) {
        if (mPreferenceScreen == null) {
            return;
        }
        int count = mPreferenceScreen.getPreferenceCount();
        for (int i = 0; i < count; i++) {
            Preference pref = mPreferenceScreen.getPreference(i);
            if (pref instanceof SelectorWithWidgetPreference appPreference) {
                boolean isMatched = matchingKey != null
                        && matchingKey.equals(appPreference.getKey());
                appPreference.setChecked(isMatched);
            }
        }
    }

    @Nullable
    private String parsePreferenceKey() {
        InputGestureData gestureData =
                mInputManager.getInputGesture(TouchpadThreeFingerTapUtils.TRIGGER);
        if (gestureData != null) {
            ComponentData componentData = (ComponentData) gestureData.getAction().appLaunchData();
            if (componentData != null) {
                ComponentName component = new ComponentName(
                        componentData.getPackageName(), componentData.getClassName());
                return parsePreferenceKeyFromComponent(component);
            }
        }
        return null;
    }

    @NonNull
    private String parsePreferenceKeyFromComponent(@NonNull ComponentName componentName) {
        // flattenToString contains the component's package name and its class name. This way, when
        // given a Preference, we can restore its corresponding component using its key.
        // unflattenFromString is called in onRadioButtonClicked
        return componentName.flattenToString();
    }

    @Override
@@ -62,10 +178,10 @@ public class TouchpadThreeFingerTapAppSelectionPreferenceController
    public void displayPreference(@NonNull PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreferenceScreen = screen;
        updateApps();
        populateApps();
    }

    private void updateApps() {
    private void populateApps() {
        if (mPreferenceScreen == null) {
            return;
        }
@@ -81,11 +197,40 @@ public class TouchpadThreeFingerTapAppSelectionPreferenceController
        }
    }

    private SelectorWithWidgetPreference createPreference(LauncherActivityInfo appInfo) {
    @NonNull
    private SelectorWithWidgetPreference createPreference(@NonNull LauncherActivityInfo appInfo) {
        SelectorWithWidgetPreference preference = new SelectorWithWidgetPreference(mContext);
        preference.setKey(appInfo.getComponentName().flattenToString());
        ComponentName component = appInfo.getComponentName();
        preference.setKey(parsePreferenceKeyFromComponent(component));
        preference.setTitle(appInfo.getLabel());
        preference.setIcon(appInfo.getIcon(DisplayMetrics.DENSITY_DEVICE_STABLE));
        preference.setOnClickListener(this);
        return preference;
    }

    @Override
    public void onRadioButtonClicked(@NonNull SelectorWithWidgetPreference preference) {
        String key = preference.getKey();

        // The key stores the component's information for each Preference
        // See comments in parsePreferenceKeyFromComponent()
        ComponentName component = unflattenFromString(key);
        if (component != null) {
            AppLaunchData appLaunchData = createLaunchDataForComponent(
                    component.getPackageName(), component.getClassName());
            setLaunchingApp(appLaunchData);
        }
        updateAppListSelection();
    }

    private void setLaunchingApp(@NonNull AppLaunchData appLaunchData) {
        InputGestureData gestureData =
                new InputGestureData.Builder()
                .setTrigger(TouchpadThreeFingerTapUtils.TRIGGER)
                .setAppLaunchData(appLaunchData)
                .build();
        mInputManager.removeAllCustomInputGestures(InputGestureData.Filter.TOUCHPAD);
        mInputManager.addCustomInputGesture(gestureData);
        TouchpadThreeFingerTapUtils.setLaunchAppAsGestureType(mContentResolver);
    }
}
+12 −6
Original line number Diff line number Diff line
@@ -18,11 +18,11 @@ package com.android.settings.inputmethod;
import static android.hardware.input.InputGestureData.TOUCHPAD_GESTURE_TYPE_THREE_FINGER_TAP;
import static android.hardware.input.InputGestureData.createTouchpadTrigger;

import android.app.ActivityManager;
import android.content.ContentResolver;
import android.hardware.input.InputGestureData;
import android.hardware.input.KeyGestureEvent;
import android.net.Uri;
import android.os.UserHandle;
import android.provider.Settings;

import androidx.annotation.NonNull;
@@ -57,7 +57,7 @@ public final class TouchpadThreeFingerTapUtils {
                resolver,
                TARGET_ACTION,
                KeyGestureEvent.KEY_GESTURE_TYPE_UNSPECIFIED,
                UserHandle.USER_CURRENT);
                ActivityManager.getCurrentUser());
    }

    /**
@@ -76,9 +76,15 @@ public final class TouchpadThreeFingerTapUtils {
     */
    public static void setGestureType(@NonNull ContentResolver resolver, int gestureType) {
        Settings.System.putIntForUser(
                resolver,
                TouchpadThreeFingerTapUtils.TARGET_ACTION,
                gestureType,
                UserHandle.USER_CURRENT);
                resolver, TARGET_ACTION, gestureType, ActivityManager.getCurrentUser());
    }

    /**
     * Set KEY_GESTURE_TYPE_LAUNCH_APPLICATION as the gesture type
     * @param resolver ContentResolver
     */
    public static void setLaunchAppAsGestureType(@NonNull ContentResolver resolver) {
        setGestureType(
                resolver, /* gestureType = */ KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION);
    }
}
+259 −0
Original line number Diff line number Diff line
/*
 * Copyright 2025 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.settings.inputmethod;

import static android.hardware.input.AppLaunchData.createLaunchDataForComponent;
import static android.hardware.input.InputManager.CUSTOM_INPUT_GESTURE_RESULT_SUCCESS;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.database.ContentObserver;
import android.graphics.drawable.Drawable;
import android.hardware.input.AppLaunchData;
import android.hardware.input.AppLaunchData.ComponentData;
import android.hardware.input.InputGestureData;
import android.hardware.input.InputManager;
import android.hardware.input.KeyGestureEvent;
import android.os.UserHandle;

import androidx.preference.PreferenceScreen;
import androidx.preference.TwoStatePreference;

import com.android.settings.testutils.shadow.ShadowSystemSettings;
import com.android.settingslib.widget.SelectorWithWidgetPreference;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.List;

/** Tests for {@link TouchpadThreeFingerTapActionPreferenceController} */
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {
        ShadowSystemSettings.class,
})
public class TouchpadThreeFingerTapAppSelectionPreferenceControllerTest {

    private static final String PREF_KEY = "testScreen";
    private static final String TEST_TITLE_PREFIX = "testTitle";
    private static final String TEST_PACKAGE_PREFIX = "testPackage";
    private static final String TEST_CLASS_PREFIX = "testClass";
    private static final int GO_HOME_GESTURE = KeyGestureEvent.KEY_GESTURE_TYPE_HOME;
    private static final InputGestureData.Filter TOUCHPAD_FILTER = InputGestureData.Filter.TOUCHPAD;

    @Rule
    public MockitoRule mMockitoRule = MockitoJUnit.rule();

    @Mock
    private PreferenceScreen mMockPreferenceScreen;

    @Mock
    private ContentObserver mMockContentObserver;

    @Mock
    private LauncherApps mMockLauncherApps;
    @Mock
    private LauncherActivityInfo mMockActivityInfo1;
    @Mock
    private LauncherActivityInfo mMockActivityInfo2;
    @Mock
    private Drawable mMockDrawable;

    @Mock
    private InputManager mMockInputManager;

    private final Context mContext = RuntimeEnvironment.application;
    private ContentResolver mContentResolver;
    private TouchpadThreeFingerTapAppSelectionPreferenceController mController;
    private InputGestureData mCustomInputGesture = null;

    @Before
    public void setup() {
        mContentResolver = mContext.getContentResolver();
        mController = new TouchpadThreeFingerTapAppSelectionPreferenceController(
                mContext, PREF_KEY, mMockLauncherApps, mMockInputManager, mMockContentObserver);
        setupMockLauncherApps();
        setupMockInputManager();
    }

    private void setupMockInputManager() {
        doAnswer(
                invocation -> {
                    mCustomInputGesture = null;
                    return null;
                }
        ).when(mMockInputManager).removeAllCustomInputGestures(eq(TOUCHPAD_FILTER));

        doAnswer(
                invocation -> {
                    mCustomInputGesture = invocation.getArgument(0);
                    return CUSTOM_INPUT_GESTURE_RESULT_SUCCESS;
                }
        ).when(mMockInputManager).addCustomInputGesture(any(InputGestureData.class));

        doAnswer(
                invocation -> {
                    return mCustomInputGesture;
                }
        ).when(mMockInputManager).getInputGesture(eq(TouchpadThreeFingerTapUtils.TRIGGER));
    }

    private void setupMockLauncherApps() {
        List<LauncherActivityInfo> activityInfos = new ArrayList<>();
        activityInfos.add(mMockActivityInfo1);
        activityInfos.add(mMockActivityInfo2);
        when(mMockLauncherApps.getActivityList(isNull(), any(UserHandle.class)))
                .thenReturn(activityInfos);

        for (int i = 0; i < activityInfos.size(); i++) {
            setupMockActivityInfo(activityInfos.get(i), i);
        }
    }

    private void setupMockActivityInfo(LauncherActivityInfo activityInfo, int suffix) {
        when(activityInfo.getLabel()).thenReturn(TEST_TITLE_PREFIX + suffix);
        when(activityInfo.getComponentName()).thenReturn(new ComponentName(
                TEST_PACKAGE_PREFIX + suffix, TEST_CLASS_PREFIX + suffix));
        when(activityInfo.getIcon(anyInt())).thenReturn(mMockDrawable);
    }

    @Test
    public void displayPreference_populateAllApps() {
        ArgumentCaptor<SelectorWithWidgetPreference> captor =
                ArgumentCaptor.forClass(SelectorWithWidgetPreference.class);

        mController.displayPreference(mMockPreferenceScreen);

        verify(mMockPreferenceScreen).removeAll();
        verify(mMockPreferenceScreen, times(2)).addPreference(captor.capture());

        List<SelectorWithWidgetPreference> prefs = captor.getAllValues();
        assertThat(prefs).hasSize(2);

        for (int i = 0; i < prefs.size(); i++) {
            SelectorWithWidgetPreference pref = prefs.get(i);
            assertTrue((TEST_TITLE_PREFIX + i).contentEquals(pref.getTitle()));
            assertThat(pref.getIcon()).isEqualTo(mMockDrawable);

            ComponentName component = new ComponentName(
                    TEST_PACKAGE_PREFIX + i, TEST_CLASS_PREFIX + i);
            String key = component.flattenToString();
            assertThat(pref.getKey()).isEqualTo(key);
        }
    }

    @Test
    public void updateState_whenActionIsLaunchApp_correspondingAppChecked() {
        ArgumentCaptor<SelectorWithWidgetPreference> captor = capturePrefs();

        setupLaunchingApp(/* matchingIndex = */ 1);
        mController.updateState(mMockPreferenceScreen);

        List<SelectorWithWidgetPreference> prefs = captor.getAllValues();
        assertThat(prefs.get(0).isChecked()).isFalse();
        assertThat(prefs.get(1).isChecked()).isTrue();
    }

    @Test
    public void updateState_whenActionIsGoHome_nothingChecked() {
        ArgumentCaptor<SelectorWithWidgetPreference> captor = capturePrefs();

        TouchpadThreeFingerTapUtils.setGestureType(mContentResolver, GO_HOME_GESTURE);
        mController.updateState(mMockPreferenceScreen);

        List<SelectorWithWidgetPreference> prefs = captor.getAllValues();
        assertTrue(prefs.stream().noneMatch(TwoStatePreference::isChecked));
    }

    @Test
    public void onRadioButtonClick_gestureAndTargetAppUpdated() {
        ArgumentCaptor<SelectorWithWidgetPreference> captor = capturePrefs();

        int clickingIndex = 0;
        mController.onRadioButtonClicked(captor.getAllValues().get(clickingIndex));

        // Settings key is updated
        int gesture = TouchpadThreeFingerTapUtils.getCurrentGestureType(mContentResolver);
        assertThat(gesture).isEqualTo(KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION);

        // InputManager gesture is updated
        assertThat(mCustomInputGesture).isNotNull();
        ComponentData componentData =
                (ComponentData) mCustomInputGesture.getAction().appLaunchData();
        assertThat(componentData).isNotNull();
        assertThat(componentData.getPackageName()).isEqualTo(TEST_PACKAGE_PREFIX + clickingIndex);
        assertThat(componentData.getClassName()).isEqualTo(TEST_CLASS_PREFIX + clickingIndex);

        // The pref list is updated accordingly
        List<SelectorWithWidgetPreference> prefs = captor.getAllValues();
        assertThat(prefs.get(0).isChecked()).isTrue();
        assertThat(prefs.get(1).isChecked()).isFalse();
    }

    private ArgumentCaptor<SelectorWithWidgetPreference> capturePrefs() {
        ArgumentCaptor<SelectorWithWidgetPreference> captor =
                ArgumentCaptor.forClass(SelectorWithWidgetPreference.class);
        mController.displayPreference(mMockPreferenceScreen);
        verify(mMockPreferenceScreen, times(2)).addPreference(captor.capture());
        when(mMockPreferenceScreen.getPreferenceCount()).thenReturn(captor.getAllValues().size());
        when(mMockPreferenceScreen.getPreference(eq(0)))
                .thenReturn(captor.getAllValues().get(0));
        when(mMockPreferenceScreen.getPreference(eq(1)))
                .thenReturn(captor.getAllValues().get(1));
        return captor;
    }

    private void setupLaunchingApp(int matchingIndex) {
        AppLaunchData appLaunchData = createLaunchDataForComponent(
                TEST_PACKAGE_PREFIX + matchingIndex, TEST_CLASS_PREFIX + matchingIndex);

        mCustomInputGesture = createGestureForApp(appLaunchData);
        TouchpadThreeFingerTapUtils.setLaunchAppAsGestureType(mContentResolver);
    }

    private InputGestureData createGestureForApp(AppLaunchData appLaunchData) {
        return new InputGestureData.Builder()
                .setTrigger(TouchpadThreeFingerTapUtils.TRIGGER)
                .setAppLaunchData(appLaunchData)
                .build();
    }
}