Example usage for android.view.accessibility AccessibilityManager sendAccessibilityEvent

List of usage examples for android.view.accessibility AccessibilityManager sendAccessibilityEvent

Introduction

In this page you can find the example usage for android.view.accessibility AccessibilityManager sendAccessibilityEvent.

Prototype

public void sendAccessibilityEvent(AccessibilityEvent event) 

Source Link

Document

Sends an AccessibilityEvent .

Usage

From source file:Main.java

public static void sendAccessibilityEvent(Context context) {
    AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (am.isEnabled()) {
        am.sendAccessibilityEvent(AccessibilityEvent.obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED));
    }//from www . j  ava  2 s  . c o  m
}

From source file:Main.java

/**
 * Announce text through the AccessibilityManager for a view.
 *
 * @param text/*from  w  w  w  .j a v a  2  s. c  om*/
 * @param view
 * @param manager
 */
public static void announceText(String text, View view, AccessibilityManager manager) {
    // Only announce text if the accessibility service is enabled
    if (!manager.isEnabled()) {
        return;
    }
    AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEventCompat.TYPE_ANNOUNCEMENT);
    event.getText().add(text);
    event.setEnabled(true);
    // Tie the event to the view
    event.setClassName(view.getClass().getName());
    event.setPackageName(view.getContext().getPackageName());
    AccessibilityEventCompat.asRecord(event).setSource(view);

    // Send the announcement
    manager.sendAccessibilityEvent(event);

}

From source file:Main.java

/**
 * Try to send an {@link AccessibilityEvent}
 * for a {@link View}.//ww  w  . ja  v a  2s .co m
 *
 * @param view The View that will dispatch the AccessibilityEvent
 * @return true if the AccessibilityEvent was dispatched
 */
@SuppressWarnings("UnusedReturnValue")
public static boolean sendAccessibilityEvent(View view) {
    final AccessibilityManager accessibilityManager = (AccessibilityManager) view.getContext()
            .getSystemService(Context.ACCESSIBILITY_SERVICE);

    if (!accessibilityManager.isEnabled())
        return false;

    final AccessibilityEvent accessibilityEvent = AccessibilityEvent
            .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
    accessibilityEvent.setClassName(view.getClass().getName());
    accessibilityEvent.setPackageName(view.getContext().getPackageName());

    view.dispatchPopulateAccessibilityEvent(accessibilityEvent);
    accessibilityManager.sendAccessibilityEvent(accessibilityEvent);

    return true;
}

From source file:com.android.gallery3d.util.AccessibilityUtils.java

public static void makeAnnouncement(View view, CharSequence announcement) {
    if (view == null)
        return;/*from  ww w. ja va 2s . c o  m*/
    if (ApiHelper.HAS_ANNOUNCE_FOR_ACCESSIBILITY) {
        view.announceForAccessibility(announcement);
    } else {
        // For API 15 and earlier, we need to construct an accessibility event
        Context ctx = view.getContext();
        AccessibilityManager am = (AccessibilityManager) ctx.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (!am.isEnabled())
            return;
        AccessibilityEvent event = AccessibilityEvent
                .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
        AccessibilityRecordCompat arc = new AccessibilityRecordCompat(event);
        arc.setSource(view);
        event.setClassName(view.getClass().getName());
        event.setPackageName(view.getContext().getPackageName());
        event.setEnabled(view.isEnabled());
        event.getText().add(announcement);
        am.sendAccessibilityEvent(event);
    }
}

From source file:com.android.fastergallery.util.AccessibilityUtils.java

public static void makeAnnouncement(View view, CharSequence announcement) {
    if (view == null)
        return;//  w  ww  .  jav  a 2s  .c o  m
    if (ApiHelper.HAS_ANNOUNCE_FOR_ACCESSIBILITY) {
        view.announceForAccessibility(announcement);
    } else {
        // For API 15 and earlier, we need to construct an accessibility
        // event
        Context ctx = view.getContext();
        AccessibilityManager am = (AccessibilityManager) ctx.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (!am.isEnabled())
            return;
        AccessibilityEvent event = AccessibilityEvent
                .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
        AccessibilityRecordCompat arc = new AccessibilityRecordCompat(event);
        arc.setSource(view);
        event.setClassName(view.getClass().getName());
        event.setPackageName(view.getContext().getPackageName());
        event.setEnabled(view.isEnabled());
        event.getText().add(announcement);
        am.sendAccessibilityEvent(event);
    }
}

From source file:com.appeaser.sublimepickerlibrary.utilities.AccessibilityUtils.java

public static void makeAnnouncement(View view, CharSequence announcement) {
    if (view == null)
        return;/*from   w  w w. java2 s  .co  m*/
    if (SUtils.isApi_16_OrHigher()) {
        view.announceForAccessibility(announcement);
    } else {
        // For API 15 and earlier, we need to construct an accessibility event
        Context ctx = view.getContext();
        AccessibilityManager am = (AccessibilityManager) ctx.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (!am.isEnabled())
            return;

        AccessibilityEvent event = AccessibilityEvent
                .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
        AccessibilityRecordCompat arc = AccessibilityEventCompat.asRecord(event);
        arc.setSource(view);
        event.setClassName(view.getClass().getName());
        event.setPackageName(view.getContext().getPackageName());
        event.setEnabled(view.isEnabled());
        event.getText().add(announcement);
        am.sendAccessibilityEvent(event);
    }
}

From source file:com.android.talkback.TalkBackKeyboardShortcutPreferencesActivity.java

/**
 * Utility method for announcing text via accessibility event.
 *///from   ww  w  .  j av a  2 s. c o m
public static void announceText(String text, Context context) {
    AccessibilityManager accessibilityManager = (AccessibilityManager) context
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
        event.setContentDescription(text);
        accessibilityManager.sendAccessibilityEvent(event);
    }
}

From source file:com.aprz.easy_iosched.ui.widget.CustomRatingBar.java

@Override
public void sendAccessibilityEvent(final int eventType) {
    if (!AccessibilityUtils.isAccessibilityEnabled(getContext())) {
        return;/*w  w w  .j  a v  a2s .  c  o  m*/
    }
    super.sendAccessibilityEvent(eventType);

    AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    // Get the Talkback text.
    event.getText().add(
            getContext().getResources().getString(R.string.feedback_rating_confirmation, mRating, mMaxRating));
    event.setEnabled(true);
    AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    accessibilityManager.sendAccessibilityEvent(event);
}

From source file:com.prashant.custom.widget.crouton.Manager.java

/**
 * Generates and dispatches an SDK-specific spoken announcement.
 * <p>/*from   w w  w  .  j  av  a  2s .  co  m*/
 * For backwards compatibility, we're constructing an event from scratch
 * using the appropriate event type. If your application only targets SDK
 * 16+, you can just call View.announceForAccessibility(CharSequence).
 * </p>
 * <p/>
 * note: AccessibilityManager is only available from API lvl 4.
 * <p/>
 * Adapted from https://http://eyes-free.googlecode.com/files/accessibility_codelab_demos_v2_src.zip
 * via https://github.com/coreform/android-formidable-validation
 *
 * @param context
 *   Used to get {@link AccessibilityManager}
 * @param text
 *   The text to announce.
 */
public static void announceForAccessibilityCompat(Context context, CharSequence text) {
    if (Build.VERSION.SDK_INT >= 4) {
        AccessibilityManager accessibilityManager = (AccessibilityManager) context
                .getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (!accessibilityManager.isEnabled()) {
            return;
        }

        final int eventType;
        eventType = AccessibilityEvent.TYPE_VIEW_FOCUSED;

        // Construct an accessibility event with the minimum recommended
        // attributes. An event without a class name or package may be dropped.
        final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
        event.getText().add(text);
        event.setClassName(Manager.class.getName());
        event.setPackageName(context.getPackageName());

        // Sends the event directly through the accessibility manager. If your
        // application only targets SDK 14+, you should just call
        // getParent().requestSendAccessibilityEvent(this, event);
        accessibilityManager.sendAccessibilityEvent(event);
    }
}

From source file:ti.modules.titanium.app.AppModule.java

@Kroll.method
public void fireSystemEvent(String eventName, @Kroll.argument(optional = true) Object arg) {
    if (eventName.equals(EVENT_ACCESSIBILITY_ANNOUNCEMENT)) {

        if (!getAccessibilityEnabled()) {
            Log.w(TAG,//  w ww . ja  va2  s .  co  m
                    "Accessibility announcement ignored. Accessibility services are not enabled on this device.");
            return;
        }

        if (arg == null) {
            Log.w(TAG, "Accessibility announcement ignored. No announcement text was provided.");
            return;
        }

        AccessibilityManager accessibilityManager = TiApplication.getInstance().getAccessibilityManager();
        AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEventCompat.TYPE_ANNOUNCEMENT);
        event.setEnabled(true);
        event.getText().clear();
        event.getText().add(TiConvert.toString(arg));
        accessibilityManager.sendAccessibilityEvent(event);

    } else {
        Log.w(TAG, "Unknown system event: " + eventName);
    }
}