Example usage for android.view.accessibility AccessibilityEvent obtain

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

Introduction

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

Prototype

public static AccessibilityEvent obtain(AccessibilityEvent event) 

Source Link

Document

Returns a cached instance if such is available or a new one is created.

Usage

From source file:com.android.launcher2.PagedView.java

protected boolean computeScrollHelper() {
    if (mScroller.computeScrollOffset()) {
        // Don't bother scrolling if the page does not need to be moved
        if (getScrollX() != mScroller.getCurrX() || getScrollY() != mScroller.getCurrY()
                || mOverScrollX != mScroller.getCurrX()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
        }//from   w w w. j  a  v a  2  s .c  o  m
        invalidate();
        return true;
    } else if (mNextPage != INVALID_PAGE) {
        mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1));
        mNextPage = INVALID_PAGE;
        notifyPageSwitchListener();

        // Load the associated pages if necessary
        if (mDeferLoadAssociatedPagesUntilScrollCompletes) {
            loadAssociatedPages(mCurrentPage);
            mDeferLoadAssociatedPagesUntilScrollCompletes = false;
        }

        // We don't want to trigger a page end moving unless the page has settled
        // and the user has stopped scrolling
        if (mTouchState == TOUCH_STATE_REST) {
            pageEndMoving();
        }

        // Notify the user when the page changes
        AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
                .getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (accessibilityManager.isEnabled()) {
            AccessibilityEvent ev = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
            ev.getText().add(getCurrentPageDescription());
            sendAccessibilityEventUnchecked(ev);
        }
        return true;
    }
    return false;
}

From source file:com.llf.android.launcher3.Folder.java

private void sendCustomAccessibilityEvent(int type, String text) {
    AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain(type);
        // XXX onInitializeAccessibilityEvent(event);
        event.getText().add(text);//from ww w .  j a  v a  2s .  c om
        accessibilityManager.sendAccessibilityEvent(event);
    }
}

From source file:com.googlecode.eyesfree.testing.BaseAccessibilityInstrumentationTestCase.java

protected void onEventReceived(AccessibilityEvent event) {
    synchronized (mAccessibilityEventLock) {
        mLastEventTime = SystemClock.uptimeMillis();
    }/*from w w  w.ja v a2  s. c  o  m*/

    synchronized (mEventCache) {
        if (mRecordingEvents) {
            mEventCache.add(AccessibilityEvent.obtain(event));
            mEventCache.notifyAll();
        }
    }
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static void announceForAccessibilityCompat(final Context context, final View view,
        final CharSequence text, final Class<?> cls) {
    final AccessibilityManager accessibilityManager = (AccessibilityManager) context
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (!accessibilityManager.isEnabled())
        return;/*www . ja  va 2s. c o  m*/
    // Prior to SDK 16, announcements could only be made through FOCUSED
    // events. Jelly Bean (SDK 16) added support for speaking text verbatim
    // using the ANNOUNCEMENT event type.
    final int eventType;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        eventType = AccessibilityEvent.TYPE_VIEW_FOCUSED;
    } else {
        eventType = AccessibilityEventCompat.TYPE_ANNOUNCEMENT;
    }

    // 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(cls.getName());
    event.setPackageName(context.getPackageName());
    event.setSource(view);

    // 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:cc.flydev.launcher.Page.java

private void sendScrollAccessibilityEvent() {
    AccessibilityManager am = (AccessibilityManager) getContext()
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (am.isEnabled()) {
        AccessibilityEvent ev = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
        ev.setItemCount(getChildCount());
        ev.setFromIndex(mCurrentPage);/* w w  w  . j  a  v a  2  s.  c om*/

        final int action;
        if (getNextPage() >= mCurrentPage) {
            action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD;
        } else {
            action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
        }

        ev.setAction(action);
        sendAccessibilityEventUnchecked(ev);
    }
}

From source file:com.coreform.open.android.formidablevalidation.ValidationManager.java

/**
  * Generates and dispatches an SDK-specific spoken announcement.
  * <p>/*w ww .j a  v a2  s  .  c  o  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>
  * 
  * Adapted from https://http://eyes-free.googlecode.com/files/accessibility_codelab_demos_v2_src.zip
  *
  * @param text The text to announce.
  */
public static void announceForAccessibilityCompat(CharSequence text) {
    if (!mAccessibilityManager.isEnabled()) {
        return;
    }

    // Prior to SDK 16, announcements could only be made through FOCUSED
    // events. Jelly Bean (SDK 16) added support for speaking text verbatim
    // using the ANNOUNCEMENT event type.
    final int eventType;
    if (Build.VERSION.SDK_INT < 16) {
        eventType = AccessibilityEvent.TYPE_VIEW_FOCUSED;
    } else {
        eventType = AccessibilityEventCompat.TYPE_ANNOUNCEMENT;
    }

    // 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(SetErrorHandler.class.getName());
    event.setPackageName(mContext.getPackageName());

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

From source file:com.klinker.deskclock.widget.multiwaveview.GlowPadView.java

private void handleMove(MotionEvent event) {
    int activeTarget = -1;
    final int historySize = event.getHistorySize();
    ArrayList<TargetDrawable> targets = mTargetDrawables;
    int ntargets = targets.size();
    float x = 0.0f;
    float y = 0.0f;
    int actionIndex = event.findPointerIndex(mPointerId);

    if (actionIndex == -1) {
        return; // no data for this pointer
    }//from  www  .  ja  va 2 s. c o m

    for (int k = 0; k < historySize + 1; k++) {
        float eventX = k < historySize ? event.getHistoricalX(actionIndex, k) : event.getX(actionIndex);
        float eventY = k < historySize ? event.getHistoricalY(actionIndex, k) : event.getY(actionIndex);
        // tx and ty are relative to wave center
        float tx = eventX - mWaveCenterX;
        float ty = eventY - mWaveCenterY;
        float touchRadius = (float) Math.sqrt(dist2(tx, ty));
        final float scale = touchRadius > mOuterRadius ? mOuterRadius / touchRadius : 1.0f;
        float limitX = tx * scale;
        float limitY = ty * scale;
        double angleRad = Math.atan2(-ty, tx);

        if (!mDragging) {
            trySwitchToFirstTouchState(eventX, eventY);
        }

        if (mDragging) {
            // For multiple targets, snap to the one that matches
            final float snapRadius = mOuterRadius - mSnapMargin;
            final float snapDistance2 = snapRadius * snapRadius;
            // Find first target in range
            for (int i = 0; i < ntargets; i++) {
                TargetDrawable target = targets.get(i);

                double targetMinRad = (i - 0.5) * 2 * Math.PI / ntargets;
                double targetMaxRad = (i + 0.5) * 2 * Math.PI / ntargets;
                if (target.isEnabled()) {
                    boolean angleMatches = (angleRad > targetMinRad && angleRad <= targetMaxRad)
                            || (angleRad + 2 * Math.PI > targetMinRad
                                    && angleRad + 2 * Math.PI <= targetMaxRad);
                    if (angleMatches && (dist2(tx, ty) > snapDistance2)) {
                        activeTarget = i;
                    }
                }
            }
        }
        x = limitX;
        y = limitY;
    }

    if (!mDragging) {
        return;
    }

    if (activeTarget != -1) {
        switchToState(STATE_SNAP, x, y);
        updateGlowPosition(x, y);
    } else {
        switchToState(STATE_TRACKING, x, y);
        updateGlowPosition(x, y);
    }

    if (mActiveTarget != activeTarget) {
        // Defocus the old target
        if (mActiveTarget != -1) {
            TargetDrawable target = targets.get(mActiveTarget);
            target.setState(TargetDrawable.STATE_INACTIVE);
        }
        // Focus the new target
        if (activeTarget != -1) {
            TargetDrawable target = targets.get(activeTarget);
            target.setState(TargetDrawable.STATE_FOCUSED);
            final AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
                    .getSystemService(Context.ACCESSIBILITY_SERVICE);
            if (accessibilityManager.isEnabled()) {
                String targetContentDescription = getTargetDescription(activeTarget);
                if (Build.VERSION.SDK_INT >= 16) {
                    announceForAccessibility(targetContentDescription);
                } else {
                    AccessibilityEvent acc_event = AccessibilityEvent
                            .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
                    AccessibilityRecordCompat arc = new AccessibilityRecordCompat(acc_event);
                    arc.setSource(this);
                    acc_event.setClassName(this.getClass().getName());
                    acc_event.setPackageName(this.getContext().getPackageName());
                    acc_event.setEnabled(this.isEnabled());
                    acc_event.getText().add(targetContentDescription);
                    accessibilityManager.sendAccessibilityEvent(acc_event);
                }
            }
        }
    }
    mActiveTarget = activeTarget;
}

From source file:com.klinker.deskclock.widget.multiwaveview.GlowPadView.java

private void announceTargets() {
    StringBuilder utterance = new StringBuilder();
    final int targetCount = mTargetDrawables.size();
    for (int i = 0; i < targetCount; i++) {
        String targetDescription = getTargetDescription(i);
        String directionDescription = getDirectionDescription(i);
        if (!TextUtils.isEmpty(targetDescription) && !TextUtils.isEmpty(directionDescription)) {
            String text = String.format(directionDescription, targetDescription);
            utterance.append(text);/*from w  w  w  . j  av  a2  s.  c  om*/
        }
    }
    if (utterance.length() > 0) {
        final AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
                .getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (accessibilityManager.isEnabled()) {
            if (Build.VERSION.SDK_INT >= 16) {
                announceForAccessibility(utterance.toString());
            } else {
                AccessibilityEvent acc_event = AccessibilityEvent
                        .obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
                AccessibilityRecordCompat arc = new AccessibilityRecordCompat(acc_event);
                arc.setSource(this);
                acc_event.setClassName(this.getClass().getName());
                acc_event.setPackageName(this.getContext().getPackageName());
                acc_event.setEnabled(this.isEnabled());
                acc_event.getText().add(utterance.toString());
                accessibilityManager.sendAccessibilityEvent(acc_event);
            }
        }
    }
}

From source file:com.aliyun.homeshell.Folder.java

public void sendCustomAccessibilityEvent(int type, String text) {
    AccessibilityManager accessibilityManager = (AccessibilityManager) getContext()
            .getSystemService(Context.ACCESSIBILITY_SERVICE);
    if (accessibilityManager.isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain(type);
        onInitializeAccessibilityEvent(event);
        event.getText().add(text);//from w w  w  . jav  a 2 s  . c  om
        accessibilityManager.sendAccessibilityEvent(event);
    }
}

From source file:com.android.incallui.CallCardFragment.java

@Override
public void sendAccessibilityAnnouncement() {
    mHandler.postDelayed(new Runnable() {
        @Override/*ww  w. j a  v  a2  s  . c  o  m*/
        public void run() {
            if (getView() != null && getView().getParent() != null && isAccessibilityEnabled(getContext())) {
                AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT);
                dispatchPopulateAccessibilityEvent(event);
                getView().getParent().requestSendAccessibilityEvent(getView(), event);
            }
        }

        private boolean isAccessibilityEnabled(Context context) {
            AccessibilityManager accessibilityManager = (AccessibilityManager) context
                    .getSystemService(Context.ACCESSIBILITY_SERVICE);
            return accessibilityManager != null && accessibilityManager.isEnabled();

        }
    }, ACCESSIBILITY_ANNOUNCEMENT_DELAY_MS);
}