Example usage for android.support.v4.view.accessibility AccessibilityRecordCompat AccessibilityRecordCompat

List of usage examples for android.support.v4.view.accessibility AccessibilityRecordCompat AccessibilityRecordCompat

Introduction

In this page you can find the example usage for android.support.v4.view.accessibility AccessibilityRecordCompat AccessibilityRecordCompat.

Prototype

public AccessibilityRecordCompat(Object record) 

Source Link

Usage

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

public static void makeAnnouncement(View view, CharSequence announcement) {
    if (view == null)
        return;//from  w  w w .  j  a  va2 s.c om
    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 w  w .  j  a  va  2s.co  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.google.android.marvin.mytalkback.formatter.ImageViewFormatter.java

@Override
public boolean format(AccessibilityEvent event, TalkBackService context, Utterance utterance) {
    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    final AccessibilityNodeInfoCompat source = record.getSource();
    final CharSequence text = ruleNonTextViews.format(context, source, event);
    utterance.addSpoken(text);//from  ww w  .j  a v a  2 s.  c om
    return true;
}

From source file:com.google.android.marvin.mytalkback.formatter.ProgressBarFormatter.java

private boolean shouldDropEvent(AccessibilityEvent event) {
    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    final AccessibilityNodeInfoCompat source = record.getSource();

    // Don't drop if we're on pre-ICS or the event was generated (e.g.
    // missing a node).
    if (source == null) {
        return false;
    }//from   w  ww .jav a2 s. c o  m

    // Don't drop if the node is currently focused or accessibility focused.
    if (source.isFocused() || source.isAccessibilityFocused()) {
        return false;
    }

    // Don't drop if the node was recently explored.
    if (source.equals(sRecentlyExplored)) {
        return false;
    }

    return true;
}

From source file:com.google.android.marvin.mytalkback.ProcessorWebContent.java

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    // Only announce relevant events
    if (!AccessibilityEventUtils.eventMatchesAnyType(event, MASK_ACCEPTED_EVENT_TYPES)) {
        return;// w w w. j av a 2 s  .com
    }

    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    final AccessibilityNodeInfoCompat source = record.getSource();

    // Drop consecutive qualifying events from the same node.
    if (mLastNode != null && mLastNode.equals(source)) {
        return;
    } else {
        if (mLastNode != null) {
            mLastNode.recycle();
        }
        mLastNode = source;
    }

    // Only announce nodes that have web content.
    if (!WebInterfaceUtils.hasWebContent(source)) {
        return;
    }

    if (WebInterfaceUtils.isScriptInjectionEnabled(mService)) {
        // Instruct accessibility script to announce the page title as long
        // as continuous reading isn't active.
        if (mFullScreenReadController.getReadingState() != AutomaticReadingState.ENTERING_WEB_CONTENT) {
            WebInterfaceUtils.performSpecialAction(source, WebInterfaceUtils.ACTION_READ_PAGE_TITLE_ELEMENT);
        } else {
            // Reset the state for full screen reading now that we've moved
            // into web content.
            mFullScreenReadController.interrupt();
        }
    } else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        // On versions that include a script injection preference, inform
        // the user that script injection is disabled.
        final String preferenceName = AutomationUtils.getPackageString(mService, PACKAGE_SETTINGS,
                RES_NAME_SCRIPT_INJECTION_TITLE);
        if (preferenceName != null) {
            final CharSequence announcement = mService.getString(R.string.hint_script_injection,
                    preferenceName);
            mSpeechController.speak(announcement, SpeechController.QUEUE_MODE_INTERRUPT, 0, null);
        }
    }
}

From source file:com.google.android.marvin.mytalkback.formatter.ScrollFormatter.java

/**
 * Returns a floating point value representing the scroll position of an
 * {@link AccessibilityEvent}. This value may be outside the range {0..1}.
 * If there's no valid way to obtain a position, this method returns 0.5.
 *
 * @param event The event from which to obtain the scroll position.
 * @return A floating point value representing the scroll position.
 *//*  ww  w  .  j a v  a2 s . c om*/
private float getScrollPosition(AccessibilityEvent event) {
    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    final int itemCount = event.getItemCount();
    final int fromIndex = event.getFromIndex();

    // First, attempt to use (fromIndex / itemCount).
    if ((fromIndex >= 0) && (itemCount > 0)) {
        return (fromIndex / (float) itemCount);
    }

    final int scrollY = record.getScrollY();
    final int maxScrollY = record.getMaxScrollY();

    // Next, attempt to use (scrollY / maxScrollY). This will fail if the
    // getMaxScrollX() method is not available.
    if ((scrollY >= 0) && (maxScrollY > 0)) {
        return (scrollY / (float) maxScrollY);
    }

    // Finally, attempt to use (scrollY / itemCount).
    // TODO(alanv): Hack from previous versions -- is it still needed?
    if ((scrollY >= 0) && (itemCount > 0) && (scrollY <= itemCount)) {
        return (scrollY / (float) itemCount);
    }

    return 0.5f;
}

From source file:com.google.android.marvin.mytalkback.ProcessorLongHover.java

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    final int eventType = event.getEventType();

    if ((eventType == AccessibilityEvent.TYPE_VIEW_HOVER_ENTER)
            || (eventType == AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START)) {
        mIsTouchExploring = true;//from ww  w  .  java 2 s .  c  o  m
    }

    if (eventType == AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END) {
        mIsTouchExploring = false;
        cacheEnteredNode(null);
        cancelLongHover();
        return;
    }

    if (!mIsTouchExploring
            || ((eventType != TRIGGER_ACTION) && (eventType != AccessibilityEvent.TYPE_VIEW_HOVER_EXIT))) {
        return;
    }

    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    final AccessibilityNodeInfoCompat source = record.getSource();

    if (source == null) {
        return;
    }

    if (eventType == TRIGGER_ACTION) {
        cacheEnteredNode(source);
        postLongHoverRunnable(event);
    } else if (eventType == AccessibilityEventCompat.TYPE_VIEW_HOVER_EXIT) {
        if (source.equals(mWaitingForExit)) {
            cancelLongHover();
        }
    }

    source.recycle();
}

From source file:com.google.android.marvin.mytalkback.formatter.TouchExplorationFormatter.java

/**
 * Formatter that returns an utterance to announce touch exploration.
 *///from  w w w .java  2 s.com
@Override
public boolean format(AccessibilityEvent event, TalkBackService context, Utterance utterance) {
    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    final AccessibilityNodeInfoCompat sourceNode = record.getSource();
    final AccessibilityNodeInfoCompat focusedNode = getFocusedNode(event.getEventType(), sourceNode);

    // Drop the event if the source node was non-null, but the focus
    // algorithm decided to drop the event by returning null.
    if ((sourceNode != null) && (focusedNode == null)) {
        AccessibilityNodeInfoUtils.recycleNodes(sourceNode);
        return false;
    }

    LogUtils.log(this, Log.VERBOSE, "Announcing node: %s", focusedNode);

    // Populate the utterance.
    addDescription(utterance, focusedNode, event, sourceNode);
    addFeedback(utterance, focusedNode);

    // By default, touch exploration flushes all other events.
    utterance.getMetadata().putInt(Utterance.KEY_METADATA_QUEUING, DEFAULT_QUEUING_MODE);

    // Events formatted by this class should always advance continuous
    // reading, if active.
    utterance.addSpokenFlag(FeedbackItem.FLAG_ADVANCE_CONTINUOUS_READING);

    AccessibilityNodeInfoUtils.recycleNodes(sourceNode, focusedNode);

    return true;
}

From source file:com.google.android.marvin.mytalkback.ProcessorLongHover.java

/**
 * Given an {@link AccessibilityEvent}, obtains and speaks a long
 * hover utterance.//w w  w .  j ava  2s  .c  o m
 *
 * @param event The source event.
 */
private void speakLongHover(AccessibilityEvent event) {
    // Never speak hint text if the tutorial is active
    if (AccessibilityTutorialActivity.isTutorialActive()) {
        LogUtils.log(this, Log.VERBOSE, "Dropping long hover hint speech because tutorial is active.");
        return;
    }

    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    AccessibilityNodeInfoCompat source = record.getSource();

    if (source == null) {
        return;
    }

    // If this was a HOVER_ENTER event, we need to compute the focused node.
    // TODO: We're already doing this in the touch exploration formatter --
    // maybe this belongs there instead?
    if (event.getEventType() == AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER) {
        source = AccessibilityNodeInfoUtils.findFocusFromHover(mContext, source);
        if (source == null) {
            return;
        }
    }

    final CharSequence text = mRuleProcessor.getHintForNode(source);
    source.recycle();

    // Use QUEUE mode so that we don't interrupt more important messages.
    mSpeechController.speak(text, SpeechController.QUEUE_MODE_QUEUE, FeedbackItem.FLAG_NO_HISTORY, null);
}

From source file:com.google.android.marvin.mytalkback.ProcessorScrollPosition.java

private void updateRecentlyExplored(AccessibilityEvent event) {
    // TODO(alanv): We track this in several places. Need to refactor.

    // We only need to track recently explored views on ICS.
    if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
            || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)) {
        return;/*from w  ww  .ja v  a  2s  .  com*/
    }

    // Exploration is the result of HOVER_ENTER events.
    if (event.getEventType() != AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER) {
        return;
    }

    final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
    final AccessibilityNodeInfoCompat node = record.getSource();

    if (mRecentlyExplored != null) {
        if (mRecentlyExplored.equals(node)) {
            return;
        }

        mRecentlyExplored.recycle();
        mRecentlyExplored = null;
    }

    if (node != null) {
        mRecentlyExplored = AccessibilityNodeInfoCompat.obtain(node);
    }
}