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

Commit 73798355 authored by Yuhan Yang's avatar Yuhan Yang Committed by Android (Google) Code Review
Browse files

Merge "Update autoclick delay summary" into main

parents b70b4780 6fd10cec
Loading
Loading
Loading
Loading
+57 −1
Original line number Diff line number Diff line
@@ -17,30 +17,75 @@
package com.android.settings.accessibility;

import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.server.accessibility.Flags;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;

/** Controller class that controls accessibility autoclick delay time settings. */
public class ToggleAutoclickDelayBeforeClickController extends BasePreferenceController {
public class ToggleAutoclickDelayBeforeClickController
        extends BasePreferenceController implements DefaultLifecycleObserver {

    public static final String TAG =
            ToggleAutoclickDelayBeforeClickController.class.getSimpleName();

    static final Uri ACCESSIBILITY_AUTOCLICK_DELAY_URI =
            Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY);

    @Nullable private FragmentManager mFragmentManager;
    @Nullable private Preference mPreference;

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

    public ToggleAutoclickDelayBeforeClickController(@NonNull Context context,
            @NonNull String preferenceKey) {
        super(context, preferenceKey);
    }

    @Override
    public void displayPreference(@NonNull PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = screen.findPreference(getPreferenceKey());
    }

    @Override
    public void onStart(@NonNull LifecycleOwner owner) {
        mContext.getContentResolver().registerContentObserver(
                ACCESSIBILITY_AUTOCLICK_DELAY_URI,
                /* notifyForDescendants= */ false,
                mSettingsObserver);
    }

    @Override
    public void onStop(@NonNull LifecycleOwner owner) {
        mContext.getContentResolver().unregisterContentObserver(mSettingsObserver);
    }

    public void setFragment(@NonNull Fragment fragment) {
        mFragmentManager = fragment.getChildFragmentManager();
    }
@@ -60,4 +105,15 @@ public class ToggleAutoclickDelayBeforeClickController extends BasePreferenceCon
    public int getAvailabilityStatus() {
        return Flags.enableAutoclickIndicator() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    }

    @Override
    public @NonNull CharSequence getSummary() {
        final int autoclickDelay = Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY,
                AccessibilityManager.AUTOCLICK_DELAY_WITH_INDICATOR_DEFAULT);
        return AutoclickUtils.getAutoclickDelaySummary(
                        mContext, R.string.accessibility_autoclick_delay_unit_second,
                        autoclickDelay);
    }

}
+21 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.os.Looper;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.testing.FragmentScenario;
@@ -50,6 +51,10 @@ public class ToggleAutoclickDelayBeforeClickControllerTest {
    private static final String PREFERENCE_KEY =
            "accessibility_control_autoclick_delay_before_click";

    private static final int TEST_AUTOCLICK_DELAY_MILLISECOND = 300;
    private static final String DEFAULT_AUTOCLICK_DELAY_SUMMARY = "1 second";
    private static final String TEST_AUTOCLICK_DELAY_SUMMARY = "0.3 seconds";

    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

@@ -108,4 +113,20 @@ public class ToggleAutoclickDelayBeforeClickControllerTest {
                .findFragmentByTag(ToggleAutoclickDelayBeforeClickController.TAG);
        assertThat(dialogFrag).isInstanceOf(AutoclickDelayDialogFragment.class);
    }

    @Test
    public void getSummary_matchesDelayTimeInSettings() {
        assertThat(mController.getSummary().toString()).isEqualTo(
                DEFAULT_AUTOCLICK_DELAY_SUMMARY);
        updateSetting(TEST_AUTOCLICK_DELAY_MILLISECOND);
        assertThat(mController.getSummary().toString()).isEqualTo(
                TEST_AUTOCLICK_DELAY_SUMMARY);
    }

    private void updateSetting(int value) {
        Settings.Secure.putInt(
                mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY,
                value);
    }
}