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

Commit e133913e authored by Yalan Yiue's avatar Yalan Yiue Committed by Android (Google) Code Review
Browse files

Merge "[3FT] Migrate current Settings page using standard sPreference" into main

parents 381e4cc7 c84b4495
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -90,3 +90,10 @@ flag {
    purpose: PURPOSE_BUGFIX
  }
}

flag {
  name: "touchpad_settings_design_update"
  namespace: "android_settings"
  description: "All touchpad settings pages should either migrate to non-custom implementations or be updated"
  bug: "400647661"
}
+44 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:key="input_touchpad_three_finger_tap"
    android:persistent="false"
    android:title="@string/three_finger_tap_preference_title">

    <com.android.settingslib.widget.SelectorWithWidgetPreference
        android:key="middle_click"
        android:title="@string/three_finger_tap_middle_click"
        settings:controller="com.android.settings.inputmethod.TouchpadThreeFingerTapActionPreferenceController"/>
    <com.android.settingslib.widget.SelectorWithWidgetPreference
        android:key="launch_gemini"
        android:title="@string/three_finger_tap_launch_gemini"
        settings:controller="com.android.settings.inputmethod.TouchpadThreeFingerTapActionPreferenceController"/>
    <com.android.settingslib.widget.SelectorWithWidgetPreference
        android:key="go_home"
        android:title="@string/three_finger_tap_go_home"
        settings:controller="com.android.settings.inputmethod.TouchpadThreeFingerTapActionPreferenceController"/>
    <com.android.settingslib.widget.SelectorWithWidgetPreference
        android:key="go_back"
        android:title="@string/three_finger_tap_go_back"
        settings:controller="com.android.settings.inputmethod.TouchpadThreeFingerTapActionPreferenceController"/>
    <com.android.settingslib.widget.SelectorWithWidgetPreference
        android:key="recent_apps"
        android:title="@string/three_finger_tap_recent_apps"
        settings:controller="com.android.settings.inputmethod.TouchpadThreeFingerTapActionPreferenceController"/>
</PreferenceScreen>
+145 −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 android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
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 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;
import com.android.settingslib.widget.SelectorWithWidgetPreference;

/**
 * Preference controller that updates different the three finger tap action.
 * When clicking on the top level Three Tinger Tap Preference (handled by
 * {@link TouchpadThreeFingerTapPreferenceController}) on the Touchpad page, it loads a
 * page of action Preferences.
 */
public class TouchpadThreeFingerTapActionPreferenceController extends BasePreferenceController
        implements LifecycleEventObserver, SelectorWithWidgetPreference.OnClickListener {

    private final InputManager mInputManager;
    private final ContentResolver mContentResolver;

    @Nullable
    private SelectorWithWidgetPreference mPreference;


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

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

    @VisibleForTesting
    TouchpadThreeFingerTapActionPreferenceController(@NonNull Context context,
            @NonNull String key,
            ContentObserver contentObserver) {
        this(context, key);
        mObserver = contentObserver;
    }

    @Override
    public int getAvailabilityStatus() {
        boolean isTouchpad = InputPeripheralsSettingsUtils.isTouchpad();
        return (InputSettings.isTouchpadThreeFingerTapShortcutFeatureFlagEnabled() && isTouchpad)
                ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    }

    @Override
    public void displayPreference(@NonNull PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = screen.findPreference(mPreferenceKey);
        if (mPreference != null) {
            mPreference.setOnClickListener(this);
        }
    }

    @Override
    public void onRadioButtonClicked(@NonNull SelectorWithWidgetPreference preference) {
        final int gestureType = TouchpadThreeFingerTapUtils.getGestureTypeByPrefKey(mPreferenceKey);
        setGesture(gestureType);
    }

    @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);

        int prefValue = TouchpadThreeFingerTapUtils.getGestureTypeByPrefKey(mPreferenceKey);
        int currentValue =
                TouchpadThreeFingerTapUtils.getCurrentGestureType(mContentResolver);
        if (mPreference != null) {
            mPreference.setChecked(prefValue == currentValue);
        }
    }

    private void setGesture(int customGestureType) {
        boolean isUnspecified = customGestureType == KeyGestureEvent.KEY_GESTURE_TYPE_UNSPECIFIED;
        InputGestureData gestureData = isUnspecified ? null : new InputGestureData.Builder()
                .setTrigger(TouchpadThreeFingerTapUtils.TRIGGER)
                .setKeyGestureType(customGestureType)
                .build();
        mInputManager.removeAllCustomInputGestures(InputGestureData.Filter.TOUCHPAD);
        if (!isUnspecified) {
            mInputManager.addCustomInputGesture(gestureData);
        }
        TouchpadThreeFingerTapUtils.setGestureType(mContentResolver, customGestureType);
    }
}
+7 −2
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.inputmethod;

import static com.android.settings.inputmethod.InputPeripheralsSettingsUtils.isTouchpad;
import static com.android.settings.flags.Flags.touchpadSettingsDesignUpdate;

import android.app.settings.SettingsEnums;
import android.content.Context;
@@ -31,6 +32,10 @@ public class TouchpadThreeFingerTapFragment extends InputDeviceDashboardFragment

    private static final String TAG = "TouchpadThreeFingerTapFragment";

    private static final int RES = touchpadSettingsDesignUpdate()
            ? R.xml.input_touchpad_three_finger_tap_action :
            R.xml.input_touchpad_three_finger_tap_customization;

    @Override
    public int getMetricsCategory() {
        return SettingsEnums.TOUCHPAD_THREE_FINGER_TAP;
@@ -38,7 +43,7 @@ public class TouchpadThreeFingerTapFragment extends InputDeviceDashboardFragment

    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.input_touchpad_three_finger_tap_customization;
        return RES;
    }

    @Override
@@ -47,7 +52,7 @@ public class TouchpadThreeFingerTapFragment extends InputDeviceDashboardFragment
    }

    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider(R.xml.input_touchpad_three_finger_tap_customization) {
            new BaseSearchIndexProvider(RES) {
                @Override
                protected boolean isPageSearchEnabled(Context context) {
                    return isTouchpad();
+1 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;

import java.util.Map;

/** The top-level preference controller that handles the three finger tap behaviour. */
public class TouchpadThreeFingerTapPreferenceController extends BasePreferenceController
        implements LifecycleEventObserver {

Loading