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

Commit 72eb18f6 authored by Longbo Wei's avatar Longbo Wei Committed by Android (Google) Code Review
Browse files

Merge "autoclick: Alert user to use 3-button navigation" into main

parents ffd45878 26f0526e
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -5922,6 +5922,12 @@
    <!-- Title for the switch setting to toggle on/off the revert to left click after action. [CHAR LIMIT=NONE] -->
    <string name="autoclick_revert_to_left_click_title">Revert to left click after action</string>
    <!-- Title for preference screen for configuring vibrations. [CHAR LIMIT=NONE] -->
    <!-- TODO(b/394683600): Update string to translatable once approved by UXW. -->
    <!-- Title for autoclick navigation recommendation dialog [CHAR LIMIT=NONE] -->
    <string name="accessibility_autoclick_navigation_title">Autoclick works best with 3-button navigation</string>
    <!-- TODO(b/394683600): Update string to translatable once approved by UXW. -->
    <!-- Message for autoclick navigation recommendation dialog [CHAR LIMIT=NONE] -->
    <string name="accessibility_autoclick_navigation_message">Enable 3-button navigation in settings so that you can use autoclick to go home, go back, and open recent apps.</string>
    <string name="accessibility_vibration_settings_title">Vibration &amp; haptics</string>
    <!-- Summary for preference screen for configuring vibrations. [CHAR LIMIT=NONE] -->
    <string name="accessibility_vibration_settings_summary">Control the vibration strength for different usages</string>
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 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.accessibility;

import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.android.settings.R;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settings.gestures.SystemNavigationGestureSettings;

/**
 * Fragment for creating a dialog suggesting 3-button navigation mode for Autoclick.
 */
public class AutoclickNavigationSuggestionDialogFragment extends InstrumentedDialogFragment {

    private static final String TAG =
            AutoclickNavigationSuggestionDialogFragment.class.getSimpleName();

    /** Create a new instance of the dialog fragment. */
    public static @NonNull AutoclickNavigationSuggestionDialogFragment newInstance() {
        return new AutoclickNavigationSuggestionDialogFragment();
    }

    @Override
    public int getMetricsCategory() {
        return SettingsEnums.ACCESSIBILITY_TOGGLE_AUTOCLICK;
    }

    @Override
    public @NonNull Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        // Create button click listeners.
        final DialogInterface.OnClickListener cancelListener =
                (dialog, which) -> dialog.dismiss();
        final DialogInterface.OnClickListener settingsListener =
                (dialog, which) -> {
                    new SubSettingLauncher(getContext())
                            .setDestination(SystemNavigationGestureSettings.class.getName())
                            .setSourceMetricsCategory(getMetricsCategory())
                            .launch();
                    dialog.dismiss();
                };

        // Create dialog using standard title and message methods
        return new AlertDialog.Builder(getContext())
                .setTitle(R.string.accessibility_autoclick_navigation_title)
                .setMessage(R.string.accessibility_autoclick_navigation_message)
                .setNegativeButton(R.string.cancel, cancelListener)
                .setPositiveButton(R.string.settings_label, settingsListener)
                .create();
    }
}
+41 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.settings.accessibility;

import static android.provider.Settings.Secure.NAVIGATION_MODE;

import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;

@@ -30,6 +32,8 @@ import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
@@ -47,6 +51,7 @@ public class ToggleAutoclickMainSwitchPreferenceController
            Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED);
    private final ContentResolver mContentResolver;
    private @Nullable Preference mPreference;
    private @Nullable FragmentManager mFragmentManager;

    @VisibleForTesting
    final ContentObserver mSettingsObserver =
@@ -66,6 +71,10 @@ public class ToggleAutoclickMainSwitchPreferenceController
        mContentResolver = context.getContentResolver();
    }

    public void setFragment(@NonNull Fragment fragment) {
        mFragmentManager = fragment.getChildFragmentManager();
    }

    @Override
    public int getAvailabilityStatus() {
        return Flags.enableAutoclickIndicator() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
@@ -89,6 +98,11 @@ public class ToggleAutoclickMainSwitchPreferenceController
                Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED,
                isChecked ? ON : OFF);

        // Show navigation suggestion dialog when enabling.
        if (isChecked && isGestureNavigationEnabled()) {
            showNavigationSuggestion();
        }

        return true;
    }

@@ -109,4 +123,31 @@ public class ToggleAutoclickMainSwitchPreferenceController
    public int getSliceHighlightMenuRes() {
        return R.string.menu_key_system;
    }

    /**
     * Shows navigation suggestion dialog when autoclick is enabled.
     */
    private void showNavigationSuggestion() {
        if (mFragmentManager == null) {
            return;
        }

        // Show the navigation suggestion dialog.
        AutoclickNavigationSuggestionDialogFragment.newInstance()
                .show(mFragmentManager,
                        AutoclickNavigationSuggestionDialogFragment.class.getName());
    }

    /*
     * Navigation bar modes:
     *  0 - 3-button navigation
     *  1 - 2-button navigation (deprecated on newer Android versions)
     *  2 - Gesture navigation (no visible buttons)
     *
     * Returns true if the current navigation mode is gesture-based.
     */
    private boolean isGestureNavigationEnabled() {
        int navigationMode = Settings.Secure.getInt(mContentResolver, NAVIGATION_MODE, -1);
        return navigationMode == 2;
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -78,7 +78,12 @@ public class ToggleAutoclickPreferenceFragment extends ShortcutFragment {
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        // Set up delay controller.
        use(ToggleAutoclickDelayBeforeClickController.class).setFragment(this);

        // Set up the main switch controller.
        use(ToggleAutoclickMainSwitchPreferenceController.class).setFragment(this);
    }

    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+110 −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.accessibility;

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

import static org.robolectric.Shadows.shadowOf;

import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.testing.FragmentScenario;
import androidx.lifecycle.Lifecycle;

import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SubSettings;
import com.android.settings.gestures.SystemNavigationGestureSettings;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowLooper;

@RunWith(RobolectricTestRunner.class)
public class AutoclickNavigationSuggestionDialogFragmentTest {

    private FragmentScenario<AutoclickNavigationSuggestionDialogFragment> mFragmentScenario;

    @Before
    public void setUp() {
        // Set up the fragment.
        mFragmentScenario = FragmentScenario.launch(
                AutoclickNavigationSuggestionDialogFragment.class,
                new Bundle(),
                R.style.Theme_AlertDialog_SettingsLib,
                Lifecycle.State.RESUMED);
    }

    @Test
    public void onCreateDialog_returnsAlertDialogWithButtons() {
        mFragmentScenario.onFragment(fragment -> {
            // Verify dialog is created.
            assertThat(fragment.getDialog()).isNotNull();
            assertThat(fragment.requireDialog()).isInstanceOf(AlertDialog.class);

            AlertDialog dialog = (AlertDialog) fragment.requireDialog();

            // Verify buttons exist.
            assertThat(dialog.getButton(AlertDialog.BUTTON_NEGATIVE)).isNotNull();
            assertThat(dialog.getButton(AlertDialog.BUTTON_POSITIVE)).isNotNull();
        });
    }

    @Test
    public void performClickOnCancel_dialogDismisses() {
        mFragmentScenario.onFragment(fragment -> {
            AlertDialog dialog = (AlertDialog) fragment.requireDialog();
            assertThat(dialog.isShowing()).isTrue();

            // Click the negative button (Cancel).
            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).performClick();
            ShadowLooper.idleMainLooper();

            // Verify dialog dismisses.
            assertThat(dialog.isShowing()).isFalse();
        });
    }

    @Test
    public void performClickOnSettings_goToSystemNavigationGestureSettings() {
        mFragmentScenario.onFragment(fragment -> {
            AlertDialog dialog = (AlertDialog) fragment.requireDialog();
            assertThat(dialog.isShowing()).isTrue();

            // Click the positive button (Settings).
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
            ShadowLooper.idleMainLooper();

            // Verify dialog dismisses.
            assertThat(dialog.isShowing()).isFalse();

            // Verify the correct intent was launched.
            Intent intent = shadowOf(
                    (ContextWrapper) dialog.getContext()).peekNextStartedActivity();
            assertThat(intent).isNotNull();
            assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName());
            assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
                    .isEqualTo(SystemNavigationGestureSettings.class.getName());

        });
    }
}