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

Commit 6eb3912f authored by menghanli's avatar menghanli
Browse files

Accessibility Service & Shortcut Redesign - Primary action - toggle button design

Implements DividerSwitchPreference to replace ToggleSwitch

Bug: 142528112
Test: Manual test

Change-Id: I0d80d16bfa941f7ee333a2c5680a25a26c42809d
parent da645144
Loading
Loading
Loading
Loading
+11 −10
Original line number Diff line number Diff line
@@ -18,9 +18,9 @@
    android:title="@string/accessibility_global_gesture_preference_title">

    <Preference
        android:fragment="com.android.settings.accessibility.ShortcutServicePickerFragment"
        android:key="accessibility_shortcut_service"
            android:title="@string/accessibility_shortcut_service_title"
            android:fragment="com.android.settings.accessibility.ShortcutServicePickerFragment"/>
        android:title="@string/accessibility_shortcut_service_title" />

    <SwitchPreference
        android:key="accessibility_shortcut_on_lock_screen"
@@ -28,7 +28,8 @@

    <com.android.settingslib.widget.FooterPreference
        android:key="accessibility_shortcut_footer"
        android:title="@string/accessibility_shortcut_description"
        android:selectable="false"
        android:title="@string/accessibility_shortcut_description"
        settings:searchable="false" />

</PreferenceScreen>
+36 −21
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@
 */
package com.android.settings.accessibility;

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

import android.accessibilityservice.AccessibilityServiceInfo;
import android.annotation.Nullable;
import android.app.settings.SettingsEnums;
@@ -28,15 +31,14 @@ import android.os.UserHandle;
import android.provider.Settings;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.Switch;

import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;

import com.android.internal.accessibility.AccessibilityShortcutController;
import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.SwitchBar;
import com.android.settingslib.accessibility.AccessibilityUtils;
import com.android.settingslib.search.Indexable;
import com.android.settingslib.search.SearchIndexable;
@@ -79,7 +81,7 @@ public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePrefer
        mOnLockScreenSwitchPreference.setOnPreferenceChangeListener((Preference p, Object o) -> {
            Settings.Secure.putInt(getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN,
                    ((Boolean) o) ? 1 : 0);
                    ((Boolean) o) ? ON : OFF);
            return true;
        });
    }
@@ -87,7 +89,12 @@ public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePrefer
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mShortcutPreference.setVisible(false);

        final PreferenceScreen preferenceScreen = getPreferenceScreen();
        preferenceScreen.findPreference(KEY_GENERAL_CATEGORY).setVisible(false);

        preferenceScreen.setOrderingAsAdded(false);
        mToggleServiceDividerSwitchPreference.setOrder(mServicePreference.getOrder() - 1);
    }

    @Override
@@ -111,34 +118,42 @@ public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePrefer
    }

    @Override
    protected void onInstallSwitchBarToggleSwitch() {
        super.onInstallSwitchBarToggleSwitch();
        mSwitchBar.addOnSwitchChangeListener((Switch switchView, boolean enabled) -> {
    protected void onRemoveSwitchPreferenceToggleSwitch() {
        super.onRemoveSwitchPreferenceToggleSwitch();
        mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener(null);
    }

    @Override
    protected void onInstallSwitchPreferenceToggleSwitch() {
        super.onInstallSwitchPreferenceToggleSwitch();
        mToggleServiceDividerSwitchPreference.setOnPreferenceClickListener((preference) -> {
            boolean enabled = ((SwitchPreference) preference).isChecked();
            Context context = getContext();
            if (enabled && !shortcutFeatureAvailable(context)) {
                // If no service is configured, we'll disable the shortcut shortly. Give the
                // user a chance to select a service. We'll update the preferences when we resume.
                Settings.Secure.putInt(
                        getContentResolver(), Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, 1);
                // If no service is configured, we'll disable the shortcut shortly. Give the user
                // a chance to select a service. We'll update the preferences when we resume.
                Settings.Secure.putInt(getContentResolver(),
                        Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, ON);
                mServicePreference.setEnabled(true);
                mServicePreference.performClick();
            } else {
                onPreferenceToggled(Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, enabled);
            }
            return true;
        });
    }

    @Override
    protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
        Settings.Secure.putInt(getContentResolver(), preferenceKey, enabled ? 1 : 0);
        Settings.Secure.putInt(getContentResolver(), preferenceKey, enabled ? ON : OFF);
        updatePreferences();
    }

    @Override
    protected void updateSwitchBarText(SwitchBar switchBar) {
    protected void updateToggleServiceTitle(SwitchPreference switchPreference) {
        final String switchBarText = getString(R.string.accessibility_service_master_switch_title,
                getString(R.string.accessibility_global_gesture_preference_title));
        switchBar.setSwitchBarText(switchBarText, switchBarText);
        switchPreference.setTitle(switchBarText);
    }

    private void updatePreferences() {
@@ -148,21 +163,21 @@ public class AccessibilityShortcutPreferenceFragment extends ToggleFeaturePrefer
        if (!shortcutFeatureAvailable(context)) {
            // If no service is configured, make sure the overall shortcut is turned off
            Settings.Secure.putInt(
                    getContentResolver(), Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, 0);
                    getContentResolver(), Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, OFF);
        }
        boolean isEnabled = Settings.Secure
                .getInt(cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, 1) == 1;
        mSwitchBar.setChecked(isEnabled);
                .getInt(cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ENABLED, ON) == ON;
        mToggleServiceDividerSwitchPreference.setChecked(isEnabled);
        // The shortcut is enabled by default on the lock screen as long as the user has
        // enabled the shortcut with the warning dialog
        final int dialogShown = Settings.Secure.getInt(
                cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 0);
                cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, OFF);
        final boolean enabledFromLockScreen = Settings.Secure.getInt(
                cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, dialogShown) == 1;
                cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, dialogShown) == ON;
        mOnLockScreenSwitchPreference.setChecked(enabledFromLockScreen);
        // Only enable changing the service and lock screen behavior if the shortcut is on
        mServicePreference.setEnabled(mToggleSwitch.isChecked());
        mOnLockScreenSwitchPreference.setEnabled(mToggleSwitch.isChecked());
        mServicePreference.setEnabled(mToggleServiceDividerSwitchPreference.isChecked());
        mOnLockScreenSwitchPreference.setEnabled(mToggleServiceDividerSwitchPreference.isChecked());
    }

    /**
+69 −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.Context;

import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreference;

/**
 * A switch preference that has a divider below and above. Used for Accessibility Settings use
 * service.
 */
public final class DividerSwitchPreference extends SwitchPreference {

    private Boolean mDividerAllowedAbove;
    private Boolean mDividerAllowBelow;

    public DividerSwitchPreference(Context context) {
        super(context);
        mDividerAllowedAbove = true;
        mDividerAllowBelow = true;
    }

    /**
     * Sets divider whether to show in preference above.
     *
     * @param allowed true will be drawn on above this item
     */
    public void setDividerAllowedAbove(boolean allowed) {
        if (mDividerAllowedAbove != allowed) {
            mDividerAllowedAbove = allowed;
            notifyChanged();
        }
    }

    /**
     * Sets divider whether to show in preference below.
     *
     * @param allowed true will be drawn on below this item
     */
    public void setDividerAllowedBelow(boolean allowed) {
        if (mDividerAllowedAbove != allowed) {
            mDividerAllowBelow = allowed;
            notifyChanged();
        }
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        holder.setDividerAllowedAbove(mDividerAllowedAbove);
        holder.setDividerAllowedBelow(mDividerAllowBelow);
    }
}
+10 −12
Original line number Diff line number Diff line
@@ -17,29 +17,27 @@
package com.android.settings.accessibility;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.os.Bundle;
import android.view.View;

import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.widget.SwitchBar;
import com.android.settingslib.accessibility.AccessibilityUtils;

/**
 * For accessibility services that target SDK > Q, and
 * {@link AccessibilityServiceInfo#FLAG_REQUEST_ACCESSIBILITY_BUTTON}
 * is set.
 * Fragment that does not have toggle bar to turn on service to use.
 *
 * <p>The child {@link ToggleAccessibilityServicePreferenceFragment} shows the actual UI for
 * providing basic accessibility service setup.
 *
 * <p>For accessibility services that target SDK > Q, and
 * {@link AccessibilityServiceInfo#FLAG_REQUEST_ACCESSIBILITY_BUTTON} is set.
 */
public class InvisibleToggleAccessibilityServicePreferenceFragment extends
        ToggleAccessibilityServicePreferenceFragment implements ShortcutPreference.OnClickListener{

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final SettingsActivity activity = (SettingsActivity) getActivity();
        final SwitchBar mSwitchBar = activity.getSwitchBar();
        mSwitchBar.hide();
    protected void onInstallSwitchPreferenceToggleSwitch() {
        super.onInstallSwitchPreferenceToggleSwitch();
        mToggleServiceDividerSwitchPreference.setVisible(false);
    }

    /**
+8 −2
Original line number Diff line number Diff line
@@ -25,7 +25,14 @@ import com.android.settings.R;

import com.google.common.collect.ImmutableSet;

/** For accessibility services that target SDK <= Q. */
/**
 * Fragment that only allowed hardware {@link UserShortcutType} for shortcut to open.
 *
 * <p>The child {@link ToggleAccessibilityServicePreferenceFragment} shows the actual UI for
 * providing basic accessibility service setup.
 *
 * <p>For accessibility services that target SDK <= Q.
 */
public class LegacyAccessibilityServicePreferenceFragment extends
        ToggleAccessibilityServicePreferenceFragment {

@@ -38,7 +45,6 @@ public class LegacyAccessibilityServicePreferenceFragment extends
        mShortcutPreference.setSummary(hardwareTitle);
        mShortcutPreference.setSettingsVisibility(View.GONE);

        // Only allowed hardware PreferredShortcutType in this fragment.
        setAllowedPreferredShortcutType(UserShortcutType.HARDWARE);
    }

Loading