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

Commit 048c2bc1 authored by Casey Burkhardt's avatar Casey Burkhardt
Browse files

Initial implementation of Nav Bar Accessibility Button

This allows an AccessibilityService to set a flag in its
AccessibilityServiceInfo that triggers the navigation bar to show an
Accessibility Button and observe callbacks when the button is clicked
or there are changes in the visibility of the navigation bar.

Test: Manual (Created a sample AccessibilityService) + CTS
Bug:29231271
Change-Id: I03d653d85bc37df28ed71d8bba94b7c75fe56e43
parent cec9d8ce
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -2688,11 +2688,25 @@ package android {
package android.accessibilityservice {
  public final class AccessibilityButtonController {
    method public boolean isAccessibilityButtonAvailable();
    method public void registerAccessibilityButtonCallback(android.accessibilityservice.AccessibilityButtonController.AccessibilityButtonCallback);
    method public void registerAccessibilityButtonCallback(android.accessibilityservice.AccessibilityButtonController.AccessibilityButtonCallback, android.os.Handler);
    method public void unregisterAccessibilityButtonCallback(android.accessibilityservice.AccessibilityButtonController.AccessibilityButtonCallback);
  }
  public static abstract class AccessibilityButtonController.AccessibilityButtonCallback {
    ctor public AccessibilityButtonController.AccessibilityButtonCallback();
    method public void onAvailabilityChanged(android.accessibilityservice.AccessibilityButtonController, boolean);
    method public void onClicked(android.accessibilityservice.AccessibilityButtonController);
  }
  public abstract class AccessibilityService extends android.app.Service {
    ctor public AccessibilityService();
    method public final void disableSelf();
    method public final boolean dispatchGesture(android.accessibilityservice.GestureDescription, android.accessibilityservice.AccessibilityService.GestureResultCallback, android.os.Handler);
    method public android.view.accessibility.AccessibilityNodeInfo findFocus(int);
    method public final android.accessibilityservice.AccessibilityButtonController getAccessibilityButtonController();
    method public final android.accessibilityservice.FingerprintGestureController getFingerprintGestureController();
    method public final android.accessibilityservice.AccessibilityService.MagnificationController getMagnificationController();
    method public android.view.accessibility.AccessibilityNodeInfo getRootInActiveWindow();
@@ -2805,6 +2819,7 @@ package android.accessibilityservice {
    field public static final int FLAG_ENABLE_ACCESSIBILITY_VOLUME = 128; // 0x80
    field public static final int FLAG_INCLUDE_NOT_IMPORTANT_VIEWS = 2; // 0x2
    field public static final int FLAG_REPORT_VIEW_IDS = 16; // 0x10
    field public static final int FLAG_REQUEST_ACCESSIBILITY_BUTTON = 256; // 0x100
    field public static final int FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY = 8; // 0x8
    field public static final int FLAG_REQUEST_FILTER_KEY_EVENTS = 32; // 0x20
    field public static final int FLAG_REQUEST_TOUCH_EXPLORATION_MODE = 4; // 0x4
+15 −0
Original line number Diff line number Diff line
@@ -2807,11 +2807,25 @@ package android {
package android.accessibilityservice {
  public final class AccessibilityButtonController {
    method public boolean isAccessibilityButtonAvailable();
    method public void registerAccessibilityButtonCallback(android.accessibilityservice.AccessibilityButtonController.AccessibilityButtonCallback);
    method public void registerAccessibilityButtonCallback(android.accessibilityservice.AccessibilityButtonController.AccessibilityButtonCallback, android.os.Handler);
    method public void unregisterAccessibilityButtonCallback(android.accessibilityservice.AccessibilityButtonController.AccessibilityButtonCallback);
  }
  public static abstract class AccessibilityButtonController.AccessibilityButtonCallback {
    ctor public AccessibilityButtonController.AccessibilityButtonCallback();
    method public void onAvailabilityChanged(android.accessibilityservice.AccessibilityButtonController, boolean);
    method public void onClicked(android.accessibilityservice.AccessibilityButtonController);
  }
  public abstract class AccessibilityService extends android.app.Service {
    ctor public AccessibilityService();
    method public final void disableSelf();
    method public final boolean dispatchGesture(android.accessibilityservice.GestureDescription, android.accessibilityservice.AccessibilityService.GestureResultCallback, android.os.Handler);
    method public android.view.accessibility.AccessibilityNodeInfo findFocus(int);
    method public final android.accessibilityservice.AccessibilityButtonController getAccessibilityButtonController();
    method public final android.accessibilityservice.FingerprintGestureController getFingerprintGestureController();
    method public final android.accessibilityservice.AccessibilityService.MagnificationController getMagnificationController();
    method public android.view.accessibility.AccessibilityNodeInfo getRootInActiveWindow();
@@ -2924,6 +2938,7 @@ package android.accessibilityservice {
    field public static final int FLAG_ENABLE_ACCESSIBILITY_VOLUME = 128; // 0x80
    field public static final int FLAG_INCLUDE_NOT_IMPORTANT_VIEWS = 2; // 0x2
    field public static final int FLAG_REPORT_VIEW_IDS = 16; // 0x10
    field public static final int FLAG_REQUEST_ACCESSIBILITY_BUTTON = 256; // 0x100
    field public static final int FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY = 8; // 0x8
    field public static final int FLAG_REQUEST_FILTER_KEY_EVENTS = 32; // 0x20
    field public static final int FLAG_REQUEST_TOUCH_EXPLORATION_MODE = 4; // 0x4
+15 −0
Original line number Diff line number Diff line
@@ -2688,11 +2688,25 @@ package android {
package android.accessibilityservice {
  public final class AccessibilityButtonController {
    method public boolean isAccessibilityButtonAvailable();
    method public void registerAccessibilityButtonCallback(android.accessibilityservice.AccessibilityButtonController.AccessibilityButtonCallback);
    method public void registerAccessibilityButtonCallback(android.accessibilityservice.AccessibilityButtonController.AccessibilityButtonCallback, android.os.Handler);
    method public void unregisterAccessibilityButtonCallback(android.accessibilityservice.AccessibilityButtonController.AccessibilityButtonCallback);
  }
  public static abstract class AccessibilityButtonController.AccessibilityButtonCallback {
    ctor public AccessibilityButtonController.AccessibilityButtonCallback();
    method public void onAvailabilityChanged(android.accessibilityservice.AccessibilityButtonController, boolean);
    method public void onClicked(android.accessibilityservice.AccessibilityButtonController);
  }
  public abstract class AccessibilityService extends android.app.Service {
    ctor public AccessibilityService();
    method public final void disableSelf();
    method public final boolean dispatchGesture(android.accessibilityservice.GestureDescription, android.accessibilityservice.AccessibilityService.GestureResultCallback, android.os.Handler);
    method public android.view.accessibility.AccessibilityNodeInfo findFocus(int);
    method public final android.accessibilityservice.AccessibilityButtonController getAccessibilityButtonController();
    method public final android.accessibilityservice.FingerprintGestureController getFingerprintGestureController();
    method public final android.accessibilityservice.AccessibilityService.MagnificationController getMagnificationController();
    method public android.view.accessibility.AccessibilityNodeInfo getRootInActiveWindow();
@@ -2805,6 +2819,7 @@ package android.accessibilityservice {
    field public static final int FLAG_ENABLE_ACCESSIBILITY_VOLUME = 128; // 0x80
    field public static final int FLAG_INCLUDE_NOT_IMPORTANT_VIEWS = 2; // 0x2
    field public static final int FLAG_REPORT_VIEW_IDS = 16; // 0x10
    field public static final int FLAG_REQUEST_ACCESSIBILITY_BUTTON = 256; // 0x100
    field public static final int FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY = 8; // 0x8
    field public static final int FLAG_REQUEST_FILTER_KEY_EVENTS = 32; // 0x20
    field public static final int FLAG_REQUEST_TOUCH_EXPLORATION_MODE = 4; // 0x4
+223 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 android.accessibilityservice;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Handler;
import android.os.RemoteException;
import android.util.ArrayMap;
import android.util.Slog;

/**
 * Controller for the accessibility button within the system's navigation area
 * <p>
 * This class may be used to query the accessibility button's state and register
 * callbacks for interactions with and state changes to the accessibility button when
 * {@link AccessibilityServiceInfo#FLAG_REQUEST_ACCESSIBILITY_BUTTON} is set.
 * </p>
 * <p>
 * <strong>Note:</strong> This class and
 * {@link AccessibilityServiceInfo#FLAG_REQUEST_ACCESSIBILITY_BUTTON} should not be used as
 * the sole means for offering functionality to users via an {@link AccessibilityService}.
 * Some device implementations may choose not to provide a software-rendered system
 * navigation area, making this affordance permanently unavailable.
 * </p>
 * <p>
 * <strong>Note:</strong> On device implementations where the accessibility button is
 * supported, it may not be available at all times, such as when a foreground application uses
 * {@link android.view.View#SYSTEM_UI_FLAG_HIDE_NAVIGATION}. A user may also choose to assign
 * this button to another accessibility service or feature. In each of these cases, a
 * registered {@link AccessibilityButtonCallback}'s
 * {@link AccessibilityButtonCallback#onAvailabilityChanged(AccessibilityButtonController, boolean)}
 * method will be invoked to provide notifications of changes in the accessibility button's
 * availability to the registering service.
 * </p>
 */
public final class AccessibilityButtonController {
    private static final String LOG_TAG = "A11yButtonController";

    private final IAccessibilityServiceConnection mServiceConnection;
    private final Object mLock;
    private ArrayMap<AccessibilityButtonCallback, Handler> mCallbacks;

    AccessibilityButtonController(@NonNull IAccessibilityServiceConnection serviceConnection) {
        mServiceConnection = serviceConnection;
        mLock = new Object();
    }

    /**
     * Retrieves whether the accessibility button in the system's navigation area is
     * available to the calling service.
     * <p>
     * <strong>Note:</strong> If the service is not yet connected (e.g.
     * {@link AccessibilityService#onServiceConnected()} has not yet been called) or the
     * service has been disconnected, this method will have no effect and return {@code false}.
     * </p>
     *
     * @return {@code true} if the accessibility button in the system's navigation area is
     * available to the calling service, {@code false} otherwise
     */
    public boolean isAccessibilityButtonAvailable() {
        try {
            return mServiceConnection.isAccessibilityButtonAvailable();
        } catch (RemoteException re) {
            Slog.w(LOG_TAG, "Failed to get accessibility button availability.", re);
            re.rethrowFromSystemServer();
            return false;
        }
    }

    /**
     * Registers the provided {@link AccessibilityButtonCallback} for interaction and state
     * changes callbacks related to the accessibility button.
     *
     * @param callback the callback to add, must be non-null
     */
    public void registerAccessibilityButtonCallback(@NonNull AccessibilityButtonCallback callback) {
        registerAccessibilityButtonCallback(callback, null);
    }

    /**
     * Registers the provided {@link AccessibilityButtonCallback} for interaction and state
     * change callbacks related to the accessibility button. The callback will occur on the
     * specified {@link Handler}'s thread, or on the services's main thread if the handler is
     * {@code null}.
     *
     * @param callback the callback to add, must be non-null
     * @param handler the handler on which to callback should execute, or {@code null} to
     *                execute on the service's main thread
     */
    public void registerAccessibilityButtonCallback(@NonNull AccessibilityButtonCallback callback,
            @Nullable Handler handler) {
        synchronized (mLock) {
            if (mCallbacks == null) {
                mCallbacks = new ArrayMap<>();
            }

            mCallbacks.put(callback, handler);
        }
    }

    /**
     * Unregisters the provided {@link AccessibilityButtonCallback} for interaction and state
     * change callbacks related to the accessibility button.
     *
     * @param callback the callback to remove, must be non-null
     */
    public void unregisterAccessibilityButtonCallback(
            @NonNull AccessibilityButtonCallback callback) {
        synchronized (mLock) {
            if (mCallbacks == null) {
                return;
            }

            final int keyIndex = mCallbacks.indexOfKey(callback);
            final boolean hasKey = keyIndex >= 0;
            if (hasKey) {
                mCallbacks.removeAt(keyIndex);
            }
        }
    }

    /**
     * Dispatches the accessibility button click to any registered callbacks. This should
     * be called on the service's main thread.
     */
    void dispatchAccessibilityButtonClicked() {
        final ArrayMap<AccessibilityButtonCallback, Handler> entries;
        synchronized (mLock) {
            if (mCallbacks == null || mCallbacks.isEmpty()) {
                Slog.w(LOG_TAG, "Received accessibility button click with no callbacks!");
                return;
            }

            // Callbacks may remove themselves. Perform a shallow copy to avoid concurrent
            // modification.
            entries = new ArrayMap<>(mCallbacks);
        }

        for (int i = 0, count = entries.size(); i < count; i++) {
            final AccessibilityButtonCallback callback = entries.keyAt(i);
            final Handler handler = entries.valueAt(i);
            if (handler != null) {
                handler.post(() -> callback.onClicked(this));
            } else {
                // We're already on the main thread, just run the callback.
                callback.onClicked(this);
            }
        }
    }

    /**
     * Dispatches the accessibility button availability changes to any registered callbacks.
     * This should be called on the service's main thread.
     */
    void dispatchAccessibilityButtonAvailabilityChanged(boolean available) {
        final ArrayMap<AccessibilityButtonCallback, Handler> entries;
        synchronized (mLock) {
            if (mCallbacks == null || mCallbacks.isEmpty()) {
                Slog.w(LOG_TAG,
                        "Received accessibility button availability change with no callbacks!");
                return;
            }

            // Callbacks may remove themselves. Perform a shallow copy to avoid concurrent
            // modification.
            entries = new ArrayMap<>(mCallbacks);
        }

        for (int i = 0, count = entries.size(); i < count; i++) {
            final AccessibilityButtonCallback callback = entries.keyAt(i);
            final Handler handler = entries.valueAt(i);
            if (handler != null) {
                handler.post(() -> callback.onAvailabilityChanged(this, available));
            } else {
                // We're already on the main thread, just run the callback.
                callback.onAvailabilityChanged(this, available);
            }
        }
    }

    /**
     * Callback for interaction with and changes to state of the accessibility button
     * within the system's navigation area.
     */
    public static abstract class AccessibilityButtonCallback {

        /**
         * Called when the accessibility button in the system's navigation area is clicked.
         *
         * @param controller the controller used to register for this callback
         */
        public void onClicked(AccessibilityButtonController controller) {}

        /**
         * Called when the availability of the accessibility button in the system's
         * navigation area has changed. The accessibility button may become unavailable
         * because the device shopped showing the button, the button was assigned to another
         * service, or for other reasons.
         *
         * @param controller the controller used to register for this callback
         * @param available {@code true} if the accessibility button is available to this
         *                  service, {@code false} otherwise
         */
        public void onAvailabilityChanged(AccessibilityButtonController controller,
                boolean available) {
        }
    }
}
+79 −10
Original line number Diff line number Diff line
@@ -378,6 +378,8 @@ public abstract class AccessibilityService extends Service {
        void onPerformGestureResult(int sequence, boolean completedSuccessfully);
        void onFingerprintCapturingGesturesChanged(boolean active);
        void onFingerprintGesture(int gesture);
        void onAccessibilityButtonClicked();
        void onAccessibilityButtonAvailabilityChanged(boolean available);
    }

    /**
@@ -400,6 +402,7 @@ public abstract class AccessibilityService extends Service {

    private MagnificationController mMagnificationController;
    private SoftKeyboardController mSoftKeyboardController;
    private AccessibilityButtonController mAccessibilityButtonController;

    private int mGestureStatusCallbackSequence;

@@ -431,6 +434,9 @@ public abstract class AccessibilityService extends Service {
        if (mMagnificationController != null) {
            mMagnificationController.onServiceConnected();
        }
        if (mSoftKeyboardController != null) {
            mSoftKeyboardController.onServiceConnected();
        }

        // The client gets to handle service connection last, after we've set
        // up any state upon which their code may rely.
@@ -809,12 +815,10 @@ public abstract class AccessibilityService extends Service {
        }

        /**
         * Removes all instances of the specified change listener from the list
         * of magnification change listeners.
         * Removes the specified change listener from the list of magnification change listeners.
         *
         * @param listener the listener to remove, must be non-null
         * @return {@code true} if at least one instance of the listener was
         *         removed
         * @return {@code true} if the listener was removed, {@code false} otherwise
         */
        public boolean removeListener(@NonNull OnMagnificationChangedListener listener) {
            if (mListeners == null) {
@@ -1203,11 +1207,11 @@ public abstract class AccessibilityService extends Service {
        }

        /**
         * Removes all instances of the specified change listener from the list of magnification
         * change listeners.
         * Removes the specified change listener from the list of keyboard show mode change
         * listeners.
         *
         * @param listener the listener to remove, must be non-null
         * @return {@code true} if at least one instance of the listener was removed
         * @return {@code true} if the listener was removed, {@code false} otherwise
         */
        public boolean removeOnShowModeChangedListener(@NonNull OnShowModeChangedListener listener) {
            if (mListeners == null) {
@@ -1252,7 +1256,7 @@ public abstract class AccessibilityService extends Service {
            final ArrayMap<OnShowModeChangedListener, Handler> entries;
            synchronized (mLock) {
                if (mListeners == null || mListeners.isEmpty()) {
                    Slog.d(LOG_TAG, "Received soft keyboard show mode changed callback"
                    Slog.w(LOG_TAG, "Received soft keyboard show mode changed callback"
                            + " with no listeners registered!");
                    setSoftKeyboardCallbackEnabled(false);
                    return;
@@ -1308,9 +1312,9 @@ public abstract class AccessibilityService extends Service {
         * The lastto this method will be honored, regardless of any previous calls (including those
         * made by other AccessibilityServices).
         * <p>
         * <strong>Note:</strong> If the service is not yet conected (e.g.
         * <strong>Note:</strong> If the service is not yet connected (e.g.
         * {@link AccessibilityService#onServiceConnected()} has not yet been called) or the
         * service has been disconnected, this method will hav no effect and return {@code false}.
         * service has been disconnected, this method will have no effect and return {@code false}.
         *
         * @param showMode the new show mode for the soft keyboard
         * @return {@code true} on success
@@ -1348,6 +1352,39 @@ public abstract class AccessibilityService extends Service {
        }
    }

    /**
     * Returns the controller for the accessibility button within the system's navigation area.
     * This instance may be used to query the accessibility button's state and register listeners
     * for interactions with and state changes for the accessibility button when
     * {@link AccessibilityServiceInfo#FLAG_REQUEST_ACCESSIBILITY_BUTTON} is set.
     * <p>
     * <strong>Note:</strong> Not all devices are capable of displaying the accessibility button
     * within a navigation area, and as such, use of this class should be considered only as an
     * optional feature or shortcut on supported device implementations.
     * </p>
     *
     * @return the accessibility button controller for this {@link AccessibilityService}
     */
    @NonNull
    public final AccessibilityButtonController getAccessibilityButtonController() {
        synchronized (mLock) {
            if (mAccessibilityButtonController == null) {
                mAccessibilityButtonController = new AccessibilityButtonController(
                        AccessibilityInteractionClient.getInstance().getConnection(mConnectionId));
            }
            return mAccessibilityButtonController;
        }
    }

    private void onAccessibilityButtonClicked() {
        getAccessibilityButtonController().dispatchAccessibilityButtonClicked();
    }

    private void onAccessibilityButtonAvailabilityChanged(boolean available) {
        getAccessibilityButtonController().dispatchAccessibilityButtonAvailabilityChanged(
                available);
    }

    /**
     * Performs a global action. Such an action can be performed
     * at any moment regardless of the current application or user
@@ -1543,6 +1580,16 @@ public abstract class AccessibilityService extends Service {
            public void onFingerprintGesture(int gesture) {
                AccessibilityService.this.onFingerprintGesture(gesture);
            }

            @Override
            public void onAccessibilityButtonClicked() {
                AccessibilityService.this.onAccessibilityButtonClicked();
            }

            @Override
            public void onAccessibilityButtonAvailabilityChanged(boolean available) {
                AccessibilityService.this.onAccessibilityButtonAvailabilityChanged(available);
            }
        });
    }

@@ -1565,6 +1612,8 @@ public abstract class AccessibilityService extends Service {
        private static final int DO_GESTURE_COMPLETE = 9;
        private static final int DO_ON_FINGERPRINT_ACTIVE_CHANGED = 10;
        private static final int DO_ON_FINGERPRINT_GESTURE = 11;
        private static final int DO_ACCESSIBILITY_BUTTON_CLICKED = 12;
        private static final int DO_ACCESSIBILITY_BUTTON_AVAILABILITY_CHANGED = 13;

        private final HandlerCaller mCaller;

@@ -1645,6 +1694,17 @@ public abstract class AccessibilityService extends Service {
            mCaller.sendMessage(mCaller.obtainMessageI(DO_ON_FINGERPRINT_GESTURE, gesture));
        }

        public void onAccessibilityButtonClicked() {
            final Message message = mCaller.obtainMessage(DO_ACCESSIBILITY_BUTTON_CLICKED);
            mCaller.sendMessage(message);
        }

        public void onAccessibilityButtonAvailabilityChanged(boolean available) {
            final Message message = mCaller.obtainMessageI(
                    DO_ACCESSIBILITY_BUTTON_AVAILABILITY_CHANGED, (available ? 1 : 0));
            mCaller.sendMessage(message);
        }

        @Override
        public void executeMessage(Message message) {
            switch (message.what) {
@@ -1750,6 +1810,15 @@ public abstract class AccessibilityService extends Service {
                    mCallback.onFingerprintGesture(message.arg1);
                } return;

                case (DO_ACCESSIBILITY_BUTTON_CLICKED): {
                    mCallback.onAccessibilityButtonClicked();
                } return;

                case (DO_ACCESSIBILITY_BUTTON_AVAILABILITY_CHANGED): {
                    final boolean available = (message.arg1 != 0);
                    mCallback.onAccessibilityButtonAvailabilityChanged(available);
                } return;

                default :
                    Log.w(LOG_TAG, "Unknown message type " + message.what);
            }
Loading