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

Commit 2182a3b6 authored by Svetoslav Ganov's avatar Svetoslav Ganov Committed by Android (Google) Code Review
Browse files

Merge "TwoStatePreference does not fire click events."

parents 5db7af96 5c3ea06a
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -61,8 +61,8 @@ public class CheckBoxPreference extends TwoStatePreference {
        View checkboxView = view.findViewById(com.android.internal.R.id.checkbox);
        if (checkboxView != null && checkboxView instanceof Checkable) {
            ((Checkable) checkboxView).setChecked(mChecked);

            sendAccessibilityEventForView(checkboxView);
            // Post this so this view is bound and attached when firing the event.
            postSendAccessibilityEventForView(checkboxView);
        }

        syncSummaryView(view);
+2 −2
Original line number Diff line number Diff line
@@ -102,8 +102,8 @@ public class SwitchPreference extends TwoStatePreference {
        View checkableView = view.findViewById(com.android.internal.R.id.switchWidget);
        if (checkableView != null && checkableView instanceof Checkable) {
            ((Checkable) checkableView).setChecked(mChecked);

            sendAccessibilityEventForView(checkableView);
            // Post this so this view is bound and attached when firing the event.
            postSendAccessibilityEventForView(checkableView);

            if (checkableView instanceof Switch) {
                final Switch switchView = (Switch) checkableView;
+24 −21
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package android.preference;

import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
@@ -39,28 +38,20 @@ public abstract class TwoStatePreference extends Preference {
    private CharSequence mSummaryOff;
    boolean mChecked;
    private boolean mSendAccessibilityEventViewClickedType;
    private AccessibilityManager mAccessibilityManager;
    private boolean mDisableDependentsState;

    private SendAccessibilityEventTypeViewClicked mSendAccessibilityEventTypeViewClicked;

    public TwoStatePreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mAccessibilityManager =
                (AccessibilityManager) getContext().getSystemService(Service.ACCESSIBILITY_SERVICE);
    }

    public TwoStatePreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        mAccessibilityManager =
                (AccessibilityManager) getContext().getSystemService(Service.ACCESSIBILITY_SERVICE);
        this(context, attrs, 0);
    }

    public TwoStatePreference(Context context) {
        super(context);

        mAccessibilityManager =
                (AccessibilityManager) getContext().getSystemService(Service.ACCESSIBILITY_SERVICE);
        this(context, null);
    }

    @Override
@@ -198,20 +189,23 @@ public abstract class TwoStatePreference extends Preference {
    }

    /**
     * Send an accessibility event for the given view if appropriate
     * Post send an accessibility event for the given view if appropriate.
     *
     * @param view View that should send the event
     */
    void sendAccessibilityEventForView(View view) {
    void postSendAccessibilityEventForView(View view) {
        // send an event to announce the value change of the state. It is done here
        // because clicking a preference does not immediately change the checked state
        // for example when enabling the WiFi
        if (mSendAccessibilityEventViewClickedType &&
                mAccessibilityManager.isEnabled() &&
                view.isEnabled()) {
        if (mSendAccessibilityEventViewClickedType
                && AccessibilityManager.getInstance(getContext()).isEnabled()
                && view.isEnabled()) {
            mSendAccessibilityEventViewClickedType = false;

            int eventType = AccessibilityEvent.TYPE_VIEW_CLICKED;
            view.sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
            if (mSendAccessibilityEventTypeViewClicked == null) {
                mSendAccessibilityEventTypeViewClicked = new SendAccessibilityEventTypeViewClicked();
            }
            mSendAccessibilityEventTypeViewClicked.mView = view;
            view.post(mSendAccessibilityEventTypeViewClicked);
        }
    }

@@ -306,4 +300,13 @@ public abstract class TwoStatePreference extends Preference {
            }
        };
    }

    private final class SendAccessibilityEventTypeViewClicked implements Runnable {
        private View mView;

        @Override
        public void run() {
            mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
        }
    }
}