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

Commit 48b546ec authored by jasonwshsu's avatar jasonwshsu Committed by Jason Hsu
Browse files

Extract UserShortcutType functions from ToggleFeaturePreferenceFragment

* Simplify StringJoiner to '+' since it only have one case
* Simplify Collections usage to use removeIf() and findFirst() directly
* Change class name SharedPreferenceUtils to PreferredShortcuts

Bug: 158540780
Test: atest ToggleFeaturePreferenceFragmentTest
Test: atest PreferredShortcutsTest
Change-Id: I02f03ccba09b6d7edaa5c0c8223ab3561a5e976b
parent 26f1b5f6
Loading
Loading
Loading
Loading
+102 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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 android.content.SharedPreferences;

import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;

import java.util.HashSet;
import java.util.Set;

/** Static utility methods relating to {@link PreferredShortcut} */
public final class PreferredShortcuts {

    private static final String ACCESSIBILITY_PERF = "accessibility_prefs";
    private static final String USER_SHORTCUT_TYPE = "user_shortcut_type";

    /**
     * Retrieves {@link UserShortcutType} for the given {@code componentName} from
     * SharedPreferences.
     *
     * @param context       {@link Context} to access the {@link SharedPreferences}
     * @param componentName Name of the service or activity, should be the format of {@link
     *                      ComponentName#flattenToString()}.
     * @param defaultType   See {@link UserShortcutType}
     * @return {@link UserShortcutType}
     */
    public static int retrieveUserShortcutType(Context context, String componentName,
            int defaultType) {
        if (componentName == null) {
            return defaultType;
        }

        // Create a mutable set to modify
        final Set<String> info = new HashSet<>(getFromSharedPreferences(context));
        info.removeIf(str -> !str.contains(componentName));

        if (info.isEmpty()) {
            return defaultType;
        }

        final String str = info.stream().findFirst().get();
        final PreferredShortcut shortcut = PreferredShortcut.fromString(str);
        return shortcut.getType();
    }

    /**
     * Saves a {@link PreferredShortcut} which containing {@link ComponentName#flattenToString()}
     * and {@link UserShortcutType} in SharedPreferences.
     *
     * @param context  {@link Context} to access the {@link SharedPreferences}
     * @param shortcut Contains {@link ComponentName#flattenToString()} and {@link UserShortcutType}
     */
    public static void saveUserShortcutType(Context context, PreferredShortcut shortcut) {
        final String componentName = shortcut.getComponentName();
        if (componentName == null) {
            return;
        }

        // Create a mutable set to modify
        final Set<String> info = new HashSet<>(getFromSharedPreferences(context));
        info.removeIf(str -> str.contains(componentName));
        info.add(shortcut.toString());
        saveToSharedPreferences(context, info);
    }

    /**
     * Returns a immutable set of {@link PreferredShortcut#toString()} list from
     * SharedPreferences.
     */
    private static Set<String> getFromSharedPreferences(Context context) {
        return getSharedPreferences(context).getStringSet(USER_SHORTCUT_TYPE, Set.of());
    }

    /** Sets a set of {@link PreferredShortcut#toString()} list into SharedPreferences. */
    private static void saveToSharedPreferences(Context context, Set<String> data) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putStringSet(USER_SHORTCUT_TYPE, data).apply();
    }

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(ACCESSIBILITY_PERF, Context.MODE_PRIVATE);
    }

    private PreferredShortcuts() {}
}
+0 −49
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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 android.content.SharedPreferences;

import com.google.common.collect.ImmutableSet;

import java.util.Set;

/** Utility class for SharedPreferences. */
public final class SharedPreferenceUtils {

    private static final String ACCESSIBILITY_PERF = "accessibility_prefs";
    private static final String USER_SHORTCUT_TYPE = "user_shortcut_type";
    private SharedPreferenceUtils() { }

    private static SharedPreferences getSharedPreferences(Context context, String fileName) {
        return context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
    }

    /** Returns a set of user shortcuts list to determine user preferred service shortcut. */
    public static Set<String> getUserShortcutTypes(Context context) {
        return getSharedPreferences(context, ACCESSIBILITY_PERF)
                .getStringSet(USER_SHORTCUT_TYPE, ImmutableSet.of());
    }

    /** Sets a set of user shortcuts list to determine user preferred service shortcut. */
    public static void setUserShortcutType(Context context, Set<String> data) {
        SharedPreferences.Editor editor = getSharedPreferences(context, ACCESSIBILITY_PERF).edit();
        editor.remove(USER_SHORTCUT_TYPE).apply();
        editor.putStringSet(USER_SHORTCUT_TYPE, data).apply();
    }
}
+7 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.accessibility;

import static com.android.settings.accessibility.AccessibilityStatsLogUtils.logAccessibilityServiceEnabled;
import static com.android.settings.accessibility.PreferredShortcuts.retrieveUserShortcutType;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Activity;
@@ -292,7 +293,8 @@ public class ToggleAccessibilityServicePreferenceFragment extends

    @Override
    public void onToggleClicked(ShortcutPreference preference) {
        final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
        final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
                mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
        if (preference.isChecked()) {
            if (!mToggleServiceDividerSwitchPreference.isChecked()) {
                preference.setChecked(false);
@@ -313,7 +315,8 @@ public class ToggleAccessibilityServicePreferenceFragment extends
    public void onSettingsClicked(ShortcutPreference preference) {
        // Do not restore shortcut in shortcut chooser dialog when shortcutPreference is turned off.
        mUserShortcutTypesCache = mShortcutPreference.isChecked()
                ? getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE)
                ? retrieveUserShortcutType(getPrefContext(), mComponentName.flattenToString(),
                UserShortcutType.SOFTWARE)
                : UserShortcutType.EMPTY;

        final boolean isServiceOnOrShortcutAdded = mShortcutPreference.isChecked()
@@ -415,7 +418,8 @@ public class ToggleAccessibilityServicePreferenceFragment extends
    private void onAllowButtonFromShortcutToggleClicked() {
        mShortcutPreference.setChecked(true);

        final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
        final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
                mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
        AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes, mComponentName);

        mIsDialogShown.set(false);
+21 −53
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.settings.accessibility;

import static com.android.settings.accessibility.AccessibilityUtil.getScreenHeightPixels;
import static com.android.settings.accessibility.AccessibilityUtil.getScreenWidthPixels;
import static com.android.settings.accessibility.PreferredShortcuts.retrieveUserShortcutType;
import static com.android.settings.accessibility.PreferredShortcuts.saveUserShortcutType;

import android.app.Dialog;
import android.app.settings.SettingsEnums;
@@ -44,6 +46,7 @@ import android.view.accessibility.AccessibilityManager.TouchExplorationStateChan
import android.widget.CheckBox;
import android.widget.ImageView;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
@@ -57,16 +60,11 @@ import com.android.settings.widget.SwitchBar;
import com.android.settingslib.accessibility.AccessibilityUtils;
import com.android.settingslib.widget.FooterPreference;

import com.google.common.annotations.VisibleForTesting;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Base class for accessibility fragments with toggle, shortcut, some helper functions
@@ -525,32 +523,14 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
        if (saveChanges) {
            final boolean isChanged = (mUserShortcutTypesCache != UserShortcutType.EMPTY);
            if (isChanged) {
                setUserShortcutType(getPrefContext(), mUserShortcutTypesCache);
                final PreferredShortcut shortcut = new PreferredShortcut(
                        mComponentName.flattenToString(), mUserShortcutTypesCache);
                saveUserShortcutType(getPrefContext(), shortcut);
            }
            mUserShortcutTypes = mUserShortcutTypesCache;
        }
    }

    private void setUserShortcutType(Context context, int type) {
        if (mComponentName == null) {
            return;
        }

        Set<String> info = SharedPreferenceUtils.getUserShortcutTypes(context);
        final String componentName = mComponentName.flattenToString();
        if (info.isEmpty()) {
            info = new HashSet<>();
        } else {
            final Set<String> filtered = info.stream()
                    .filter(str -> str.contains(componentName))
                    .collect(Collectors.toSet());
            info.removeAll(filtered);
        }
        final PreferredShortcut shortcut = new PreferredShortcut(componentName, type);
        info.add(shortcut.toString());
        SharedPreferenceUtils.setUserShortcutType(context, info);
    }

    protected CharSequence getShortcutTypeSummary(Context context) {
        if (!mShortcutPreference.isSettingsEditable()) {
            return context.getText(R.string.accessibility_shortcut_edit_dialog_title_hardware);
@@ -560,7 +540,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
            return context.getText(R.string.switch_off_text);
        }

        final int shortcutTypes = getUserShortcutTypes(context, UserShortcutType.SOFTWARE);
        final int shortcutTypes = retrieveUserShortcutType(context,
                mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
        int resId = R.string.accessibility_shortcut_edit_summary_software;
        if (AccessibilityUtil.isGestureNavigateEnabled(context)) {
            resId = AccessibilityUtil.isTouchExploreEnabled(context)
@@ -589,25 +570,6 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
                null, joinStrings);
    }

    protected int getUserShortcutTypes(Context context, @UserShortcutType int defaultValue) {
        if (mComponentName == null) {
            return defaultValue;
        }

        final Set<String> info = SharedPreferenceUtils.getUserShortcutTypes(context);
        final String componentName = mComponentName.flattenToString();
        final Set<String> filtered = info.stream()
                .filter(str -> str.contains(componentName))
                .collect(Collectors.toSet());
        if (filtered.isEmpty()) {
            return defaultValue;
        }

        final String str = (String) filtered.toArray()[0];
        final PreferredShortcut shortcut = PreferredShortcut.fromString(str);
        return shortcut.getType();
    }

    /**
     * This method will be invoked when a button in the edit shortcut dialog is clicked.
     *
@@ -638,10 +600,13 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
        mUserShortcutTypes = AccessibilityUtil.getUserShortcutTypesFromSettings(getPrefContext(),
                mComponentName);
        if (mUserShortcutTypes != UserShortcutType.EMPTY) {
            setUserShortcutType(getPrefContext(), mUserShortcutTypes);
            final PreferredShortcut shortcut = new PreferredShortcut(
                    mComponentName.flattenToString(), mUserShortcutTypes);
            saveUserShortcutType(getPrefContext(), shortcut);
        } else {
            //  Get the user shortcut type from shared_prefs if cannot get from settings provider.
            mUserShortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
            mUserShortcutTypes = retrieveUserShortcutType(getPrefContext(),
                    mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
        }
    }

@@ -664,7 +629,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
            return;
        }

        final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
        final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
                mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
        mShortcutPreference.setChecked(
                AccessibilityUtil.hasValuesInSettings(getPrefContext(), shortcutTypes,
                        mComponentName));
@@ -681,7 +647,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
            return;
        }

        final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
        final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
                mComponentName.flattenToString(), UserShortcutType.SOFTWARE);
        if (preference.isChecked()) {
            AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes,
                    mComponentName);
@@ -697,7 +664,8 @@ public abstract class ToggleFeaturePreferenceFragment extends SettingsPreference
    public void onSettingsClicked(ShortcutPreference preference) {
        // Do not restore shortcut in shortcut chooser dialog when shortcutPreference is turned off.
        mUserShortcutTypesCache = mShortcutPreference.isChecked()
                ? getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE)
                ? retrieveUserShortcutType(getPrefContext(),
                mComponentName.flattenToString(), UserShortcutType.SOFTWARE)
                : UserShortcutType.EMPTY;
        showDialog(DialogEnums.EDIT_SHORTCUT);
    }
+18 −41
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ package com.android.settings.accessibility;
import static com.android.internal.accessibility.AccessibilityShortcutController.MAGNIFICATION_CONTROLLER_NAME;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.android.settings.accessibility.PreferredShortcuts.retrieveUserShortcutType;
import static com.android.settings.accessibility.PreferredShortcuts.saveUserShortcutType;

import android.app.Dialog;
import android.app.settings.SettingsEnums;
@@ -46,12 +48,9 @@ import com.android.settings.R;
import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * Fragment that shows the actual UI for providing basic magnification accessibility service setup
@@ -210,35 +209,22 @@ public class ToggleScreenMagnificationPreferenceFragment extends
        if (saveChanges) {
            final boolean isChanged = (mUserShortcutTypesCache != UserShortcutType.EMPTY);
            if (isChanged) {
                setUserShortcutType(getPrefContext(), mUserShortcutTypesCache);
                final PreferredShortcut shortcut = new PreferredShortcut(
                        MAGNIFICATION_CONTROLLER_NAME, mUserShortcutTypesCache);
                saveUserShortcutType(getPrefContext(), shortcut);
            }
            mUserShortcutTypes = mUserShortcutTypesCache;
        }
    }

    private void setUserShortcutType(Context context, int type) {
        Set<String> info = SharedPreferenceUtils.getUserShortcutTypes(context);
        if (info.isEmpty()) {
            info = new HashSet<>();
        } else {
            final Set<String> filtered = info.stream().filter(
                    str -> str.contains(MAGNIFICATION_CONTROLLER_NAME)).collect(
                    Collectors.toSet());
            info.removeAll(filtered);
        }
        final PreferredShortcut shortcut = new PreferredShortcut(MAGNIFICATION_CONTROLLER_NAME,
                type);
        info.add(shortcut.toString());
        SharedPreferenceUtils.setUserShortcutType(context, info);
    }

    @Override
    protected CharSequence getShortcutTypeSummary(Context context) {
        if (!mShortcutPreference.isChecked()) {
            return context.getText(R.string.switch_off_text);
        }

        final int shortcutType = getUserShortcutTypes(context, UserShortcutType.EMPTY);
        final int shortcutType = retrieveUserShortcutType(context, MAGNIFICATION_CONTROLLER_NAME,
                UserShortcutType.EMPTY);
        int resId = R.string.accessibility_shortcut_edit_summary_software;
        if (AccessibilityUtil.isGestureNavigateEnabled(context)) {
            resId = AccessibilityUtil.isTouchExploreEnabled(context)
@@ -273,21 +259,6 @@ public class ToggleScreenMagnificationPreferenceFragment extends
                null, joinStrings);
    }

    @Override
    protected int getUserShortcutTypes(Context context, @UserShortcutType int defaultValue) {
        final Set<String> info = SharedPreferenceUtils.getUserShortcutTypes(context);
        final Set<String> filtered = info.stream().filter(
                str -> str.contains(MAGNIFICATION_CONTROLLER_NAME)).collect(
                Collectors.toSet());
        if (filtered.isEmpty()) {
            return defaultValue;
        }

        final String str = (String) filtered.toArray()[0];
        final PreferredShortcut shortcut = PreferredShortcut.fromString(str);
        return shortcut.getType();
    }

    @Override
    protected void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
        updateUserShortcutType(/* saveChanges= */ true);
@@ -341,7 +312,8 @@ public class ToggleScreenMagnificationPreferenceFragment extends

    @Override
    public void onToggleClicked(ShortcutPreference preference) {
        final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
        final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
                MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE);
        if (preference.isChecked()) {
            optInAllMagnificationValuesToSettings(getPrefContext(), shortcutTypes);
            showDialog(DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL);
@@ -355,7 +327,8 @@ public class ToggleScreenMagnificationPreferenceFragment extends
    public void onSettingsClicked(ShortcutPreference preference) {
        // Do not restore shortcut in shortcut chooser dialog when shortcutPreference is turned off.
        mUserShortcutTypesCache = mShortcutPreference.isChecked()
                ? getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE)
                ? retrieveUserShortcutType(getPrefContext(),
                MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE)
                : UserShortcutType.EMPTY;
        showDialog(DialogEnums.MAGNIFICATION_EDIT_SHORTCUT);
    }
@@ -365,10 +338,13 @@ public class ToggleScreenMagnificationPreferenceFragment extends
        // Get the user shortcut type from settings provider.
        mUserShortcutTypes = getUserShortcutTypeFromSettings(getPrefContext());
        if (mUserShortcutTypes != UserShortcutType.EMPTY) {
            setUserShortcutType(getPrefContext(), mUserShortcutTypes);
            final PreferredShortcut shortcut = new PreferredShortcut(
                    MAGNIFICATION_CONTROLLER_NAME, mUserShortcutTypes);
            saveUserShortcutType(getPrefContext(), shortcut);
        } else {
            //  Get the user shortcut type from shared_prefs if cannot get from settings provider.
            mUserShortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
            mUserShortcutTypes = retrieveUserShortcutType(getPrefContext(),
                    MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE);
        }
    }

@@ -389,7 +365,8 @@ public class ToggleScreenMagnificationPreferenceFragment extends

    @Override
    protected void updateShortcutPreference() {
        final int shortcutTypes = getUserShortcutTypes(getPrefContext(), UserShortcutType.SOFTWARE);
        final int shortcutTypes = retrieveUserShortcutType(getPrefContext(),
                MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE);
        mShortcutPreference.setChecked(
                hasMagnificationValuesInSettings(getPrefContext(), shortcutTypes));
        mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
Loading