Example usage for android.view.accessibility AccessibilityEvent setContentDescription

List of usage examples for android.view.accessibility AccessibilityEvent setContentDescription

Introduction

In this page you can find the example usage for android.view.accessibility AccessibilityEvent setContentDescription.

Prototype

public void setContentDescription(CharSequence contentDescription) 

Source Link

Document

Sets the description of the source.

Usage

From source file:com.android.mail.utils.ViewUtils.java

/**
 * Convenience method for sending a {@link android.view.accessibility.AccessibilityEvent#TYPE_ANNOUNCEMENT}
 * {@link android.view.accessibility.AccessibilityEvent} to make an announcement which is related to some
 * sort of a context change for which none of the events representing UI transitions
 * is a good fit. For example, announcing a new page in a book. If accessibility
 * is not enabled this method does nothing.
 *
 * @param view view to perform the accessibility announcement
 * @param text The announcement text.//from w ww  .  j a v  a2s. c  om
 */
public static void announceForAccessibility(View view, CharSequence text) {
    final AccessibilityManager accessibilityManager = (AccessibilityManager) view.getContext()
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    final ViewParent parent = view.getParent();
    if (accessibilityManager.isEnabled() && parent != null) {
        AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
        view.onInitializeAccessibilityEvent(event);
        event.getText().add(text);
        event.setContentDescription(null);
        parent.requestSendAccessibilityEvent(view, event);
    }
}

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

/**
 * Utility method for announcing text via accessibility event.
 *///from  w w w .j  ava  2 s  . c om
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.mytouch.launcher3.accessibility.DragAndDropAccessibilityDelegate.java

@Override
protected void onPopulateEventForVirtualView(int id, AccessibilityEvent event) {
    if (id == INVALID_ID) {
        throw new IllegalArgumentException("Invalid virtual view id");
    }/* w w w .j  a v a 2 s  .  c  o m*/
    event.setContentDescription(mContext.getString(com.mytouch.launcher3.R.string.action_move_here));
}

From source file:com.android.launcher3.accessibility.DragAndDropAccessibilityDelegate.java

@Override
protected void onPopulateEventForVirtualView(int id, AccessibilityEvent event) {
    if (id == INVALID_ID) {
        throw new IllegalArgumentException("Invalid virtual view id");
    }//from   w  ww  . j a  va  2 s  . c  o  m
    event.setContentDescription(mContext.getString(R.string.action_move_here));
}

From source file:org.connectbot.views.CheckableMenuItem.java

public CheckableMenuItem(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    mRootView = inflate(context, R.layout.view_checkablemenuitem, this);

    mTitle = (TextView) mRootView.findViewById(R.id.title);
    mSummary = (TextView) mRootView.findViewById(R.id.summary);
    mSwitch = (SwitchCompat) findViewById(R.id.checkbox_switch);

    setFocusable(true);/*  w  w w.  j ava  2  s  .co  m*/

    mAccessHelper = new ExploreByTouchHelper(this) {
        private final Rect mTmpRect = new Rect();

        @Override
        protected int getVirtualViewAt(float x, float y) {
            return HOST_ID;
        }

        @Override
        protected void getVisibleVirtualViews(List<Integer> virtualViewIds) {
        }

        @Override
        protected void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) {
            if (virtualViewId != HOST_ID) {
                // TODO(kroot): remove this when the bug is fixed.
                event.setContentDescription(PLACEHOLDER_STRING);
                return;
            }

            event.setContentDescription(mTitle.getText() + " " + mSummary.getText());
            event.setClassName(ACCESSIBILITY_EVENT_CLASS_NAME);
            event.setChecked(isChecked());
        }

        @Override
        protected void onPopulateNodeForVirtualView(int virtualViewId, AccessibilityNodeInfoCompat node) {
            if (virtualViewId != HOST_ID) {
                // TODO(kroot): remove this when the bug is fixed.
                node.setBoundsInParent(mPlaceHolderRect);
                node.setContentDescription(PLACEHOLDER_STRING);
                return;
            }

            mTmpRect.set(0, 0, CheckableMenuItem.this.getWidth(), CheckableMenuItem.this.getHeight());
            node.setBoundsInParent(mTmpRect);

            node.addAction(AccessibilityNodeInfoCompat.ACTION_CLICK);
            node.setClassName(ACCESSIBILITY_EVENT_CLASS_NAME);
            node.setCheckable(true);
            node.setChecked(isChecked());

            node.addChild(mTitle);
            node.addChild(mSummary);
        }

        @Override
        protected boolean onPerformActionForVirtualView(int virtualViewId, int action, Bundle arguments) {
            if (virtualViewId != HOST_ID) {
                return false;
            }

            if (action == AccessibilityNodeInfoCompat.ACTION_CLICK) {
                mSwitch.toggle();
                sendAccessibilityEvent(mRootView, AccessibilityEventCompat.CONTENT_CHANGE_TYPE_UNDEFINED);
                return true;
            }

            return false;
        }
    };
    ViewCompat.setAccessibilityDelegate(mRootView, mAccessHelper);

    setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mSwitch.toggle();
        }
    });

    if (attrs != null) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CheckableMenuItem, 0, 0);

        @DrawableRes
        int iconRes = typedArray.getResourceId(R.styleable.CheckableMenuItem_android_icon, 0);
        @StringRes
        int titleRes = typedArray.getResourceId(R.styleable.CheckableMenuItem_android_title, 0);
        @StringRes
        int summaryRes = typedArray.getResourceId(R.styleable.CheckableMenuItem_summary, 0);

        typedArray.recycle();

        ImageView icon = (ImageView) mRootView.findViewById(R.id.icon);
        mTitle.setText(titleRes);
        if (iconRes != 0) {
            Resources resources = context.getResources();
            Resources.Theme theme = context.getTheme();
            Drawable iconDrawable = VectorDrawableCompat.create(resources, iconRes, theme);

            icon.setImageDrawable(iconDrawable);
        }
        if (summaryRes != 0) {
            mSummary.setText(summaryRes);
        }
    }
}

From source file:com.facebook.litho.ComponentAccessibilityDelegate.java

@Override
protected void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) {
    // TODO (T10543861): ExploreByTouchHelper enforces subclasses to set a content description
    // or text on new events but components don't provide APIs to do so yet.
    event.setContentDescription("");
}

From source file:com.onyx.latinime.accessibility.AccessibilityEntityProvider.java

/**
 * Creates and populates an {@link AccessibilityEvent} for the specified key
 * and event type.//from w w  w  .  jav  a2  s.  c o  m
 *
 * @param key A key on the host keyboard view.
 * @param eventType The event type to create.
 * @return A populated {@link AccessibilityEvent} for the key.
 * @see AccessibilityEvent
 */
public AccessibilityEvent createAccessibilityEvent(final Key key, final int eventType) {
    final int virtualViewId = generateVirtualViewIdForKey(key);
    final String keyDescription = getKeyDescription(key);
    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    event.setPackageName(mKeyboardView.getContext().getPackageName());
    event.setClassName(key.getClass().getName());
    event.setContentDescription(keyDescription);
    event.setEnabled(true);
    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    record.setSource(mKeyboardView, virtualViewId);
    return event;
}

From source file:com.android.inputmethod.accessibility.KeyboardAccessibilityNodeProvider.java

/**
 * Creates and populates an {@link AccessibilityEvent} for the specified key
 * and event type.//from w w  w  . j av  a  2s. c  o  m
 *
 * @param key A key on the host keyboard view.
 * @param eventType The event type to create.
 * @return A populated {@link AccessibilityEvent} for the key.
 * @see AccessibilityEvent
 */
public AccessibilityEvent createAccessibilityEvent(final Key key, final int eventType) {
    final int virtualViewId = getVirtualViewIdOf(key);
    final String keyDescription = getKeyDescription(key);
    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    event.setPackageName(mKeyboardView.getContext().getPackageName());
    event.setClassName(key.getClass().getName());
    event.setContentDescription(keyDescription);
    event.setEnabled(true);
    final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event);
    record.setSource(mKeyboardView, virtualViewId);
    return event;
}

From source file:com.android.hareime.accessibility.AccessibilityEntityProvider.java

/**
 * Creates and populates an {@link AccessibilityEvent} for the specified key
 * and event type.//from ww w.  jav a2s .c o m
 *
 * @param key A key on the host keyboard view.
 * @param eventType The event type to create.
 * @return A populated {@link AccessibilityEvent} for the key.
 * @see AccessibilityEvent
 */
public AccessibilityEvent createAccessibilityEvent(Key key, int eventType) {
    final int virtualViewId = generateVirtualViewIdForKey(key);
    final String keyDescription = getKeyDescription(key);

    final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
    event.setPackageName(mKeyboardView.getContext().getPackageName());
    event.setClassName(key.getClass().getName());
    event.setContentDescription(keyDescription);
    event.setEnabled(true);

    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    record.setSource(mKeyboardView, virtualViewId);

    return event;
}

From source file:com.android.inputmethod.accessibility.KeyboardAccessibilityDelegate.java

/**
 * Sends a window state change event with the specified text.
 *
 * @param text The text to send with the event.
 *//*  ww w  .j  a  va  2 s.co  m*/
protected void sendWindowStateChanged(final String text) {
    final AccessibilityEvent stateChange = AccessibilityEvent
            .obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
    mKeyboardView.onInitializeAccessibilityEvent(stateChange);
    stateChange.getText().add(text);
    stateChange.setContentDescription(null);

    final ViewParent parent = mKeyboardView.getParent();
    if (parent != null) {
        parent.requestSendAccessibilityEvent(mKeyboardView, stateChange);
    }
}