public class

Accessibility

extends java.lang.Object

 java.lang.Object

↳androidx.pdf.util.Accessibility

Gradle dependencies

compile group: 'androidx.pdf', name: 'pdf-viewer', version: '1.0.0-alpha02'

  • groupId: androidx.pdf
  • artifactId: pdf-viewer
  • version: 1.0.0-alpha02

Artifact androidx.pdf:pdf-viewer:1.0.0-alpha02 it located at Google repository (https://maven.google.com/)

Overview

Helper methods for interacting with accessibility services. Could be a static utility, but instead is a singleton for easier testing.

Summary

Fields
public static final AccessibilityINSTANCE

Methods
public voidannounce(Context context, View source, int messageId)

Calls to Accessibility.announce(Context, View, String) using the text from the provided messageId.

public voidannounce(Context context, View source, java.lang.String message)

Makes an announcement to the accessibility services if accessibility is enabled.

public static final Accessibilityget()

public booleanisAccessibilityEnabled(Context context)

Returns if accessibility services are currently enabled.

public booleanisTouchExplorationEnabled(Context context)

Return if accessibility touch exploration is enabled.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final Accessibility INSTANCE

Methods

public static final Accessibility get()

public boolean isTouchExplorationEnabled(Context context)

Return if accessibility touch exploration is enabled.

public boolean isAccessibilityEnabled(Context context)

Returns if accessibility services are currently enabled.

public void announce(Context context, View source, java.lang.String message)

Makes an announcement to the accessibility services if accessibility is enabled.

Parameters:

context: The context under which to dispatch the a11y event.
source: The source of the announcement.
message: The message to be announced.

public void announce(Context context, View source, int messageId)

Calls to Accessibility.announce(Context, View, String) using the text from the provided messageId.

Source

/*
 * Copyright 2024 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 androidx.pdf.util;

import android.content.Context;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;

/**
 * Helper methods for interacting with accessibility services. Could be a static utility, but
 * instead is a singleton for easier testing.
 */
@RestrictTo(RestrictTo.Scope.LIBRARY)
@SuppressWarnings("deprecation")
public class Accessibility {
    public static final Accessibility INSTANCE = new Accessibility();

    /**
     *
     */
    @NonNull
    public static final Accessibility get() {
        return INSTANCE;
    }

    /** Return if accessibility touch exploration is enabled. */
    public boolean isTouchExplorationEnabled(@NonNull Context context) {
        return getAccessibilityManager(context).isTouchExplorationEnabled();
    }

    /** Returns if accessibility services are currently enabled. */
    public boolean isAccessibilityEnabled(@NonNull Context context) {
        return getAccessibilityManager(context).isEnabled();
    }

    /**
     * Makes an announcement to the accessibility services if accessibility is enabled.
     *
     * @param context The context under which to dispatch the a11y event.
     * @param source  The source of the announcement.
     * @param message The message to be announced.
     */
    public void announce(@NonNull Context context, @NonNull View source, @NonNull String message) {
        if (!isAccessibilityEnabled(context)) {
            return;
        }

        AccessibilityManager am = getAccessibilityManager(context);
        AccessibilityEvent evt = AccessibilityEvent.obtain();
        evt.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
        evt.setClassName(source.getClass().getName());
        evt.setPackageName(context.getPackageName());
        evt.getText().add(message);
        evt.setSource(source);
        am.sendAccessibilityEvent(evt);
    }

    /**
     * Calls to {@link #announce(Context, View, String)} using the text from the provided {@code
     * messageId}.
     */
    public void announce(@NonNull Context context, @NonNull View source, int messageId) {
        announce(context, source, context.getString(messageId));
    }

    /** @return The accessibility manager for the provided {@code context}. */
    private static AccessibilityManager getAccessibilityManager(Context context) {
        return context.getSystemService(AccessibilityManager.class);
    }

    private Accessibility() {
        // Singleton.
    }
}