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

Commit 56e82fe1 authored by Jason Hsu's avatar Jason Hsu Committed by Android (Google) Code Review
Browse files

Merge "Fix software shortcut in dialog did not update when content change." into sc-v2-dev

parents 16893da2 eba8e8da
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -180,6 +180,23 @@ public class AccessibilityDialogUtils {
        return alertDialog;
    }

    /**
     * Updates the software shortcut in edit shortcut dialog.
     *
     * @param context A valid context
     * @param editShortcutDialog Need to be a type of edit shortcut dialog
     * @return True if the update is successful
     */
    public static boolean updateSoftwareShortcutInDialog(Context context,
            Dialog editShortcutDialog) {
        final View container = editShortcutDialog.findViewById(R.id.container_layout);
        if (container != null) {
            initSoftwareShortcut(context, container);
            return true;
        }
        return false;
    }

    private static AlertDialog createDialog(Context context, int dialogType,
            CharSequence dialogTitle, DialogInterface.OnClickListener listener) {

+26 −8
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
    protected Preference mSettingsPreference;
    protected AccessibilityFooterPreferenceController mFooterPreferenceController;
    protected String mPreferenceKey;
    protected Dialog mDialog;

    protected CharSequence mSettingsTitle;
    protected Intent mSettingsIntent;
@@ -106,6 +107,7 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
    public static final int NOT_SET = -1;
    // Save user's shortcutType value when savedInstance has value (e.g. device rotated).
    protected int mSavedCheckBoxValue = NOT_SET;
    private boolean mSavedAccessibilityFloatingMenuEnabled;

    // For html description of accessibility service, must follow the rule, such as
    // <img src="R.drawable.fileName"/>, a11y settings will get the resources successfully.
@@ -127,7 +129,6 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Restore the user shortcut type.
        if (savedInstanceState != null && savedInstanceState.containsKey(
                KEY_SAVED_USER_SHORTCUT_TYPE)) {
@@ -200,6 +201,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
        mSettingsContentObserver.register(getContentResolver());
        updateShortcutPreferenceData();
        updateShortcutPreference();

        updateEditShortcutDialogIfNeeded();
    }

    @Override
@@ -208,6 +211,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
                AccessibilityManager.class);
        am.removeTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
        mSettingsContentObserver.unregister(getContentResolver());
        mSavedAccessibilityFloatingMenuEnabled = AccessibilityUtil.isFloatingMenuEnabled(
                getContext());
        super.onPause();
    }

@@ -222,24 +227,23 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference

    @Override
    public Dialog onCreateDialog(int dialogId) {
        Dialog dialog;
        switch (dialogId) {
            case DialogEnums.EDIT_SHORTCUT:
                final CharSequence dialogTitle = getPrefContext().getString(
                        R.string.accessibility_shortcut_title, mPackageName);
                final int dialogType = WizardManagerHelper.isAnySetupWizard(getIntent())
                        ? DialogType.EDIT_SHORTCUT_GENERIC_SUW : DialogType.EDIT_SHORTCUT_GENERIC;
                dialog = AccessibilityDialogUtils.showEditShortcutDialog(
                mDialog = AccessibilityDialogUtils.showEditShortcutDialog(
                        getPrefContext(), dialogType, dialogTitle,
                        this::callOnAlertDialogCheckboxClicked);
                setupEditShortcutDialog(dialog);
                return dialog;
                setupEditShortcutDialog(mDialog);
                return mDialog;
            case DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL:
                dialog = AccessibilityGestureNavigationTutorial
                mDialog = AccessibilityGestureNavigationTutorial
                        .createAccessibilityTutorialDialog(getPrefContext(),
                                getUserShortcutTypes());
                dialog.setCanceledOnTouchOutside(false);
                return dialog;
                mDialog.setCanceledOnTouchOutside(false);
                return mDialog;
            default:
                throw new IllegalArgumentException("Unsupported dialogId " + dialogId);
        }
@@ -733,6 +737,20 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
        }
    }

    private void updateEditShortcutDialogIfNeeded() {
        if (mDialog == null || !mDialog.isShowing()) {
            return;
        }

        // Content in software shortcut need to be adjusted depend on the accessibility button
        // mode status which can be changed in background.
        final boolean valueChanged = mSavedAccessibilityFloatingMenuEnabled
                != AccessibilityUtil.isFloatingMenuEnabled(getContext());
        if (valueChanged) {
            AccessibilityDialogUtils.updateSoftwareShortcutInDialog(getContext(), mDialog);
        }
    }

    @VisibleForTesting
    void saveNonEmptyUserShortcutType(int type) {
        if (type == UserShortcutType.EMPTY) {
+7 −9
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener;
import android.widget.CheckBox;

import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;

@@ -132,12 +131,11 @@ public class ToggleScreenMagnificationPreferenceFragment extends
    @Override
    public Dialog onCreateDialog(int dialogId) {
        if (mDialogDelegate != null) {
            final Dialog dialog = mDialogDelegate.onCreateDialog(dialogId);
            if (dialog != null) {
                return dialog;
            mDialog = mDialogDelegate.onCreateDialog(dialogId);
            if (mDialog != null) {
                return mDialog;
            }
        }
        final AlertDialog dialog;
        switch (dialogId) {
            case DialogEnums.GESTURE_NAVIGATION_TUTORIAL:
                return AccessibilityGestureNavigationTutorial
@@ -148,10 +146,10 @@ public class ToggleScreenMagnificationPreferenceFragment extends
                final int dialogType = WizardManagerHelper.isAnySetupWizard(getIntent())
                        ? DialogType.EDIT_SHORTCUT_MAGNIFICATION_SUW
                        : DialogType.EDIT_SHORTCUT_MAGNIFICATION;
                dialog = AccessibilityDialogUtils.showEditShortcutDialog(getPrefContext(),
                mDialog = AccessibilityDialogUtils.showEditShortcutDialog(getPrefContext(),
                        dialogType, dialogTitle, this::callOnAlertDialogCheckboxClicked);
                setupMagnificationEditShortcutDialog(dialog);
                return dialog;
                setupMagnificationEditShortcutDialog(mDialog);
                return mDialog;
            default:
                return super.onCreateDialog(dialogId);
        }
@@ -209,7 +207,7 @@ public class ToggleScreenMagnificationPreferenceFragment extends
    }

    @VisibleForTesting
    void setupMagnificationEditShortcutDialog(AlertDialog dialog) {
    void setupMagnificationEditShortcutDialog(Dialog dialog) {
        final View dialogSoftwareView = dialog.findViewById(R.id.software_shortcut);
        mSoftwareTypeCheckBox = dialogSoftwareView.findViewById(R.id.checkbox);
        setDialogTextAreaClickListener(dialogSoftwareView, mSoftwareTypeCheckBox);
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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 android.content.Context;

import androidx.appcompat.app.AlertDialog;
import androidx.test.core.app.ApplicationProvider;

import com.android.settings.R;

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

/** Tests for {@link AccessibilityDialogUtils} */
@RunWith(RobolectricTestRunner.class)
public class AccessibilityDialogUtilsTest {

    private final Context mContext = ApplicationProvider.getApplicationContext();

    @Before
    public void setUp() {
        mContext.setTheme(R.style.Theme_AppCompat);
    }

    @Test
    public void updateSoftwareShortcutInDialog_correctDialogType_success() {
        final AlertDialog dialog = AccessibilityDialogUtils.showEditShortcutDialog(
                mContext, AccessibilityDialogUtils.DialogType.EDIT_SHORTCUT_GENERIC, "Title",
                null);

        assertThat(
                AccessibilityDialogUtils.updateSoftwareShortcutInDialog(mContext, dialog)).isTrue();
    }

    @Test
    public void updateSoftwareShortcutInDialog_useNotSupportedDialog_fail() {
        final AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle("Title").show();

        assertThat(AccessibilityDialogUtils.updateSoftwareShortcutInDialog(mContext,
                dialog)).isFalse();
    }
}