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

Commit 85ad1abb authored by Chun-Ku Lin's avatar Chun-Ku Lin
Browse files

Create a PreferenceController for ShortcutPreference, and apply it to

ToggleAutoclickPreferenceFragment

Bug: 406052931
Test: atest
Test: Toggle shortcut on Auto click screen, verify shortcuts
Test: Toggle shortcut settings, modify shortcuts in EditshortcutsScreen,
verify shortcuts shown correctly on Auto click screen
Flag: EXEMPT refactor - risk taken

Change-Id: I463272071a900159b8087ee10ac7673ebb8ffedf
parent eebbf6d6
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -38,7 +38,8 @@
    <com.android.settings.accessibility.ShortcutPreference
        android:key="autoclick_shortcut_preference"
        android:title="@string/accessibility_autoclick_shortcut_title"
        android:persistent="false"/>
        android:persistent="false"
        settings:controller="com.android.settings.accessibility.ToggleAutoclickShortcutPreferenceController"/>

    <!-- Items in a list should not be searchable. -->
    <com.android.settingslib.widget.SelectorWithWidgetPreference
+69 −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.content.ComponentName
import android.content.Context
import androidx.preference.Preference
import com.android.settings.accessibility.shortcuts.EditShortcutsPreferenceFragment
import com.google.android.setupcompat.util.WizardManagerHelper

/**
 * Base class for Fragment that holds a [ShortcutPreference]
 */
abstract class ShortcutFragment : BaseSupportFragment() {

    abstract fun getShortcutLabel(): CharSequence
    abstract fun getFeatureComponentName(): ComponentName
    open fun getShortcutPreferenceController(): ToggleShortcutPreferenceController {
        return use<ToggleShortcutPreferenceController>(ToggleShortcutPreferenceController::class.java)
    }

    override fun onDisplayPreferenceDialog(preference: Preference) {
        if (preference is ShortcutPreference) {
            val isChecked = preference.isChecked
            val prefController = getShortcutPreferenceController()
            if (isChecked) {
                AccessibilityShortcutsTutorial.DialogFragment.showDialog(
                    getChildFragmentManager(),
                    prefController.getUserPreferredShortcutTypes(getFeatureComponentName()),
                    getShortcutLabel(),
                    WizardManagerHelper.isAnySetupWizard(getIntent())
                )
            }
            return
        }

        super.onDisplayPreferenceDialog(preference)
    }

    override fun onPreferenceTreeClick(preference: Preference): Boolean {
        if (preference is ShortcutPreference) {
            EditShortcutsPreferenceFragment.showEditShortcutScreen(
                requireContext(), getMetricsCategory(), preference.title,
                getFeatureComponentName(), getIntent()
            )
            return true
        }
        return super.onPreferenceTreeClick(preference)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        getShortcutPreferenceController().initialize(getFeatureComponentName())
    }
}
+13 −58
Original line number Diff line number Diff line
@@ -21,49 +21,38 @@ import static com.android.internal.accessibility.AccessibilityShortcutController
import android.app.settings.SettingsEnums;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.android.internal.annotations.VisibleForTesting;
import com.android.server.accessibility.Flags;
import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;

import java.util.List;

/**
 * Fragment for preference screen for settings related to Automatically click after mouse stops
 * feature.
 */
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class ToggleAutoclickPreferenceFragment
        extends AccessibilityShortcutPreferenceFragment {
public class ToggleAutoclickPreferenceFragment extends ShortcutFragment {

    private static final String TAG = "AutoclickPrefFragment";

    @VisibleForTesting
    static final String KEY_AUTOCLICK_SHORTCUT_PREFERENCE = "autoclick_shortcut_preference";

    /**
     * Autoclick settings do not need to set any restriction key for pin protected.
     */
    public ToggleAutoclickPreferenceFragment() {
        super(/* restrictionKey= */ null);
    @NonNull
    @Override
    public CharSequence getShortcutLabel() {
        return requireContext().getString(R.string.accessibility_autoclick_shortcut_title);
    }

    @NonNull
    @Override
    protected CharSequence getLabelName() {
        return getContext().getString(R.string.accessibility_autoclick_shortcut_title);
    public ToggleShortcutPreferenceController getShortcutPreferenceController() {
        return use(ToggleAutoclickShortcutPreferenceController.class);
    }

    @NonNull
    @Override
    protected boolean showGeneralCategory() {
        return false;
    public ComponentName getFeatureComponentName() {
        return AUTOCLICK_COMPONENT_NAME;
    }

    @Override
@@ -86,47 +75,13 @@ public class ToggleAutoclickPreferenceFragment
        return R.xml.accessibility_autoclick_settings;
    }

    @Override
    protected ComponentName getComponentName() {
        return AUTOCLICK_COMPONENT_NAME;
    }

    @Override
    protected CharSequence getShortcutTitle() {
        return getString(R.string.accessibility_autoclick_shortcut_title);
    }

    @Override
    protected String getShortcutPreferenceKey() {
        return KEY_AUTOCLICK_SHORTCUT_PREFERENCE;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        use(ToggleAutoclickDelayBeforeClickController.class).setFragment(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (!Flags.enableAutoclickIndicator()) {
            getPreferenceScreen().removePreference(mShortcutPreference);
        }
        return view;
    }

    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider(R.xml.accessibility_autoclick_settings) {
                @Override
                public List<String> getNonIndexableKeys(Context context) {
                    List<String> niks = super.getNonIndexableKeys(context);
            new BaseSearchIndexProvider(R.xml.accessibility_autoclick_settings);

                    if (!Flags.enableAutoclickIndicator()) {
                        niks.add(KEY_AUTOCLICK_SHORTCUT_PREFERENCE);
                    }
                    return niks;
                }
            };
}
+38 −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 com.android.server.accessibility.Flags

import android.content.Context

/**
 * ShortcutPreference's controller on [ToggleAutoclickPreferenceFragment].
 */
class ToggleAutoclickShortcutPreferenceController(context: Context, preferenceKey: String) :
    ToggleShortcutPreferenceController(context, preferenceKey) {

        override fun getAvailabilityStatus(): Int {
        // Note: when the flag is shipped, remove this class and replace its reference
        // in accessibility_autoclick_settings xml
        return if (Flags.enableAutoclickIndicator()) {
            AVAILABLE
        } else {
            CONDITIONALLY_UNAVAILABLE
        }
    }
}
+30 −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.content.Context
import com.android.internal.accessibility.common.ShortcutConstants

/**
 * The preference controller for the ShortcutPreference on the Magnification screen.
 * The only difference is that we allow more shortcut options for Magnifications.
 */
class ToggleMagnificationShortcutPreferenceController(context: Context, key: String) :
    ToggleShortcutPreferenceController(context, key) {

    override val shortcutSettingsKey = ShortcutConstants.MAGNIFICATION_SHORTCUT_SETTINGS.toList()
}
 No newline at end of file
Loading