Example usage for android.view View findFocus

List of usage examples for android.view View findFocus

Introduction

In this page you can find the example usage for android.view View findFocus.

Prototype

public View findFocus() 

Source Link

Document

Find the view in the hierarchy rooted at this view that currently has focus.

Usage

From source file:com.artifex.mupdf.view.ThumbnailViews.java

/**
 * To avoid horizontal/vertical focus searches changing the selected item,
 * we manually focus search within the selected item (as applicable), and
 * prevent focus from jumping to something within another item.
 * //from  www . ja va2  s  .c  om
 * @param direction
 *            either {@link View#FOCUS_UP} or {@link View#FOCUS_DOWN} or
 *            {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT} depending
 *            on the current view orientation.
 * 
 * @return Whether this consumes the key event.
 */
private boolean handleFocusWithinItem(int direction) {
    forceValidInnerFocusDirection(direction);

    final int numChildren = getChildCount();

    if (mItemsCanFocus && numChildren > 0 && mSelectedPosition != INVALID_POSITION) {
        final View selectedView = getSelectedView();

        if (selectedView != null && selectedView.hasFocus() && selectedView instanceof ViewGroup) {

            final View currentFocus = selectedView.findFocus();
            final View nextFocus = FocusFinder.getInstance().findNextFocus((ViewGroup) selectedView,
                    currentFocus, direction);

            if (nextFocus != null) {
                // Do the math to get interesting rect in next focus'
                // coordinates
                currentFocus.getFocusedRect(mTempRect);
                offsetDescendantRectToMyCoords(currentFocus, mTempRect);
                offsetRectIntoDescendantCoords(nextFocus, mTempRect);

                if (nextFocus.requestFocus(direction, mTempRect)) {
                    return true;
                }
            }

            // We are blocking the key from being handled (by returning
            // true)
            // if the global result is going to be some other view within
            // this
            // list. This is to achieve the overall goal of having
            // horizontal/vertical
            // d-pad navigation remain in the current item depending on the
            // current
            // orientation in this view.
            final View globalNextFocus = FocusFinder.getInstance().findNextFocus((ViewGroup) getRootView(),
                    currentFocus, direction);

            if (globalNextFocus != null) {
                return isViewAncestorOf(globalNextFocus, this);
            }
        }
    }

    return false;
}

From source file:com.artifex.mupdf.view.ThumbnailViews.java

/**
 * Do an arrow scroll based on focus searching. If a new view is given
 * focus, return the selection delta and amount to scroll via an
 * {@link ArrowScrollFocusResult}, otherwise, return null.
 * /*from  w w  w.j a v a2 s.co  m*/
 * @param direction
 *            either {@link View#FOCUS_UP} or {@link View#FOCUS_DOWN} or
 *            {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT} depending
 *            on the current view orientation.
 * 
 * @return The result if focus has changed, or <code>null</code>.
 */
private ArrowScrollFocusResult arrowScrollFocused(final int direction) {
    forceValidFocusDirection(direction);

    final View selectedView = getSelectedView();
    final View newFocus;
    final int searchPoint;

    if (selectedView != null && selectedView.hasFocus()) {
        View oldFocus = selectedView.findFocus();
        newFocus = FocusFinder.getInstance().findNextFocus(this, oldFocus, direction);
    } else {
        if (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT) {
            final int start = (mIsVertical ? getPaddingTop() : getPaddingLeft());

            final int selectedStart;
            if (selectedView != null) {
                selectedStart = (mIsVertical ? selectedView.getTop() : selectedView.getLeft());
            } else {
                selectedStart = start;
            }

            searchPoint = Math.max(selectedStart, start);
        } else {
            final int end = (mIsVertical ? getHeight() - getPaddingBottom() : getWidth() - getPaddingRight());

            final int selectedEnd;
            if (selectedView != null) {
                selectedEnd = (mIsVertical ? selectedView.getBottom() : selectedView.getRight());
            } else {
                selectedEnd = end;
            }

            searchPoint = Math.min(selectedEnd, end);
        }

        final int x = (mIsVertical ? 0 : searchPoint);
        final int y = (mIsVertical ? searchPoint : 0);
        mTempRect.set(x, y, x, y);

        newFocus = FocusFinder.getInstance().findNextFocusFromRect(this, mTempRect, direction);
    }

    if (newFocus != null) {
        final int positionOfNewFocus = positionOfNewFocus(newFocus);

        // If the focus change is in a different new position, make sure
        // we aren't jumping over another selectable position.
        if (mSelectedPosition != INVALID_POSITION && positionOfNewFocus != mSelectedPosition) {
            final int selectablePosition = lookForSelectablePositionOnScreen(direction);

            final boolean movingForward = (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT);
            final boolean movingBackward = (direction == View.FOCUS_UP || direction == View.FOCUS_LEFT);

            if (selectablePosition != INVALID_POSITION
                    && ((movingForward && selectablePosition < positionOfNewFocus)
                            || (movingBackward && selectablePosition > positionOfNewFocus))) {
                return null;
            }
        }

        int focusScroll = amountToScrollToNewFocus(direction, newFocus, positionOfNewFocus);

        final int maxScrollAmount = getMaxScrollAmount();
        if (focusScroll < maxScrollAmount) {
            // Not moving too far, safe to give next view focus
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, focusScroll);
            return mArrowScrollFocusResult;
        } else if (distanceToView(newFocus) < maxScrollAmount) {
            // Case to consider:
            // Too far to get entire next focusable on screen, but by going
            // max scroll amount, we are getting it at least partially in
            // view,
            // so give it focus and scroll the max amount.
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, maxScrollAmount);
            return mArrowScrollFocusResult;
        }
    }

    return null;
}

From source file:com.artifex.mupdflib.TwoWayView.java

/**
 * Do an arrow scroll based on focus searching.  If a new view is
 * given focus, return the selection delta and amount to scroll via
 * an {@link ArrowScrollFocusResult}, otherwise, return null.
 *
 * @param direction either {@link View#FOCUS_UP} or {@link View#FOCUS_DOWN} or
 *        {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT} depending on the
 *        current view orientation./*w  ww.  j  a v  a 2s.c o m*/
 *
 * @return The result if focus has changed, or <code>null</code>.
 */
private ArrowScrollFocusResult arrowScrollFocused(final int direction) {
    forceValidFocusDirection(direction);

    final View selectedView = getSelectedView();
    final View newFocus;
    final int searchPoint;

    if (selectedView != null && selectedView.hasFocus()) {
        View oldFocus = selectedView.findFocus();
        newFocus = FocusFinder.getInstance().findNextFocus(this, oldFocus, direction);
    } else {
        if (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT) {
            boolean fadingEdgeShowing = (mFirstPosition > 0);
            final int start = getStartEdge() + (fadingEdgeShowing ? getArrowScrollPreviewLength() : 0);

            final int selectedStart;
            if (selectedView != null) {
                selectedStart = getChildStartEdge(selectedView);
            } else {
                selectedStart = start;
            }

            searchPoint = Math.max(selectedStart, start);
        } else {
            final boolean fadingEdgeShowing = (mFirstPosition + getChildCount() - 1) < mItemCount;
            final int end = getEndEdge() - (fadingEdgeShowing ? getArrowScrollPreviewLength() : 0);

            final int selectedEnd;
            if (selectedView != null) {
                selectedEnd = getChildEndEdge(selectedView);
            } else {
                selectedEnd = end;
            }

            searchPoint = Math.min(selectedEnd, end);
        }

        final int x = (mIsVertical ? 0 : searchPoint);
        final int y = (mIsVertical ? searchPoint : 0);
        mTempRect.set(x, y, x, y);

        newFocus = FocusFinder.getInstance().findNextFocusFromRect(this, mTempRect, direction);
    }

    if (newFocus != null) {
        final int positionOfNewFocus = positionOfNewFocus(newFocus);

        // If the focus change is in a different new position, make sure
        // we aren't jumping over another selectable position.
        if (mSelectedPosition != INVALID_POSITION && positionOfNewFocus != mSelectedPosition) {
            final int selectablePosition = lookForSelectablePositionOnScreen(direction);

            final boolean movingForward = (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT);
            final boolean movingBackward = (direction == View.FOCUS_UP || direction == View.FOCUS_LEFT);

            if (selectablePosition != INVALID_POSITION
                    && ((movingForward && selectablePosition < positionOfNewFocus)
                            || (movingBackward && selectablePosition > positionOfNewFocus))) {
                return null;
            }
        }

        int focusScroll = amountToScrollToNewFocus(direction, newFocus, positionOfNewFocus);

        final int maxScrollAmount = getMaxScrollAmount();
        if (focusScroll < maxScrollAmount) {
            // Not moving too far, safe to give next view focus
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, focusScroll);
            return mArrowScrollFocusResult;
        } else if (distanceToView(newFocus) < maxScrollAmount) {
            // Case to consider:
            // Too far to get entire next focusable on screen, but by going
            // max scroll amount, we are getting it at least partially in view,
            // so give it focus and scroll the max amount.
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, maxScrollAmount);
            return mArrowScrollFocusResult;
        }
    }

    return null;
}

From source file:com.aliasapps.seq.scroller.TwoWayView.java

/**
 * Handle an arrow scroll going up or down.  Take into account whether items are selectable,
 * whether there are focusable items, etc.
 *
 * @param direction either {@link View#FOCUS_UP} or {@link View#FOCUS_DOWN} or
 *        {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT} depending on the
 *        current view orientation.// ww w  .ja v a 2  s .co  m
 *
 * @return Whether any scrolling, selection or focus change occurred.
 */
private boolean arrowScrollImpl(int direction) {
    forceValidFocusDirection(direction);

    if (getChildCount() <= 0) {
        return false;
    }

    View selectedView = getSelectedView();
    int selectedPos = mSelectedPosition;

    int nextSelectedPosition = lookForSelectablePositionOnScreen(direction);
    int amountToScroll = amountToScroll(direction, nextSelectedPosition);

    // If we are moving focus, we may OVERRIDE the default behaviour
    final ArrowScrollFocusResult focusResult = (mItemsCanFocus ? arrowScrollFocused(direction) : null);
    if (focusResult != null) {
        nextSelectedPosition = focusResult.getSelectedPosition();
        amountToScroll = focusResult.getAmountToScroll();
    }

    boolean needToRedraw = (focusResult != null);
    if (nextSelectedPosition != INVALID_POSITION) {
        handleNewSelectionChange(selectedView, direction, nextSelectedPosition, focusResult != null);

        setSelectedPositionInt(nextSelectedPosition);
        setNextSelectedPositionInt(nextSelectedPosition);

        selectedView = getSelectedView();
        selectedPos = nextSelectedPosition;

        if (mItemsCanFocus && focusResult == null) {
            // There was no new view found to take focus, make sure we
            // don't leave focus with the old selection.
            final View focused = getFocusedChild();
            if (focused != null) {
                focused.clearFocus();
            }
        }

        needToRedraw = true;
        checkSelectionChanged();
    }

    if (amountToScroll > 0) {
        scrollListItemsBy(
                direction == View.FOCUS_UP || direction == View.FOCUS_LEFT ? amountToScroll : -amountToScroll);
        needToRedraw = true;
    }

    // If we didn't find a new focusable, make sure any existing focused
    // item that was panned off screen gives up focus.
    if (mItemsCanFocus && focusResult == null && selectedView != null && selectedView.hasFocus()) {
        final View focused = selectedView.findFocus();
        if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) {
            focused.clearFocus();
        }
    }

    // If the current selection is panned off, we need to remove the selection
    if (nextSelectedPosition == INVALID_POSITION && selectedView != null
            && !isViewAncestorOf(selectedView, this)) {
        selectedView = null;
        hideSelector();

        // But we don't want to set the ressurect position (that would make subsequent
        // unhandled key events bring back the item we just scrolled off)
        mResurrectToPosition = INVALID_POSITION;
    }

    if (needToRedraw) {
        if (selectedView != null) {
            positionSelector(selectedPos, selectedView);
            mSelectedStart = selectedView.getTop();
        }

        if (!awakenScrollbarsInternal()) {
            invalidate();
        }

        invokeOnItemScrollListener();
        return true;
    }

    return false;
}

From source file:com.artifex.mupdf.view.ThumbnailViews.java

/**
 * Handle an arrow scroll going up or down. Take into account whether items
 * are selectable, whether there are focusable items, etc.
 * /*from  www  . j a  va2 s.  c o  m*/
 * @param direction
 *            either {@link View#FOCUS_UP} or {@link View#FOCUS_DOWN} or
 *            {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT} depending
 *            on the current view orientation.
 * 
 * @return Whether any scrolling, selection or focus change occurred.
 */
private boolean arrowScrollImpl(int direction) {
    forceValidFocusDirection(direction);

    if (getChildCount() <= 0) {
        return false;
    }

    View selectedView = getSelectedView();
    int selectedPos = mSelectedPosition;

    int nextSelectedPosition = lookForSelectablePositionOnScreen(direction);
    int amountToScroll = amountToScroll(direction, nextSelectedPosition);

    // If we are moving focus, we may OVERRIDE the default behaviour
    final ArrowScrollFocusResult focusResult = (mItemsCanFocus ? arrowScrollFocused(direction) : null);
    if (focusResult != null) {
        nextSelectedPosition = focusResult.getSelectedPosition();
        amountToScroll = focusResult.getAmountToScroll();
    }

    boolean needToRedraw = (focusResult != null);
    if (nextSelectedPosition != INVALID_POSITION) {
        handleNewSelectionChange(selectedView, direction, nextSelectedPosition, focusResult != null);

        setSelectedPositionInt(nextSelectedPosition);
        setNextSelectedPositionInt(nextSelectedPosition);

        selectedView = getSelectedView();
        selectedPos = nextSelectedPosition;

        if (mItemsCanFocus && focusResult == null) {
            // There was no new view found to take focus, make sure we
            // don't leave focus with the old selection.
            final View focused = getFocusedChild();
            if (focused != null) {
                focused.clearFocus();
            }
        }

        needToRedraw = true;
        checkSelectionChanged();
    }

    if (amountToScroll > 0) {
        trackMotionScroll(
                direction == View.FOCUS_UP || direction == View.FOCUS_LEFT ? amountToScroll : -amountToScroll);
        needToRedraw = true;
    }

    // If we didn't find a new focusable, make sure any existing focused
    // item that was panned off screen gives up focus.
    if (mItemsCanFocus && focusResult == null && selectedView != null && selectedView.hasFocus()) {
        final View focused = selectedView.findFocus();
        if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) {
            focused.clearFocus();
        }
    }

    // If the current selection is panned off, we need to remove the
    // selection
    if (nextSelectedPosition == INVALID_POSITION && selectedView != null
            && !isViewAncestorOf(selectedView, this)) {
        selectedView = null;
        hideSelector();

        // But we don't want to set the ressurect position (that would make
        // subsequent
        // unhandled key events bring back the item we just scrolled off)
        mResurrectToPosition = INVALID_POSITION;
    }

    if (needToRedraw) {
        if (selectedView != null) {
            positionSelector(selectedPos, selectedView);
            mSelectedStart = selectedView.getTop();
        }

        if (!awakenScrollbarsInternal()) {
            invalidate();
        }

        invokeOnItemScrollListener();
        return true;
    }

    return false;
}

From source file:com.kinth.mmspeed.ui.TwoWayView.java

/**
 * Handle an arrow scroll going up or down. Take into account whether items
 * are selectable, whether there are focusable items, etc.
 * /*  w ww  .  j a  v  a  2  s .c  o m*/
 * @param direction
 *            either {@link View#FOCUS_UP} or {@link View#FOCUS_DOWN} or
 *            {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT} depending
 *            on the current view orientation.
 * 
 * @return Whether any scrolling, selection or focus change occurred.
 */
private boolean arrowScrollImpl(int direction) {
    forceValidFocusDirection(direction);

    if (getChildCount() <= 0) {
        return false;
    }

    View selectedView = getSelectedView();
    int selectedPos = mSelectedPosition;

    int nextSelectedPosition = lookForSelectablePositionOnScreen(direction);
    int amountToScroll = amountToScroll(direction, nextSelectedPosition);

    // If we are moving focus, we may OVERRIDE the default behaviour
    final ArrowScrollFocusResult focusResult = (mItemsCanFocus ? arrowScrollFocused(direction) : null);
    if (focusResult != null) {
        nextSelectedPosition = focusResult.getSelectedPosition();
        amountToScroll = focusResult.getAmountToScroll();
    }

    boolean needToRedraw = (focusResult != null);
    if (nextSelectedPosition != INVALID_POSITION) {
        handleNewSelectionChange(selectedView, direction, nextSelectedPosition, focusResult != null);

        setSelectedPositionInt(nextSelectedPosition);
        setNextSelectedPositionInt(nextSelectedPosition);

        selectedView = getSelectedView();
        selectedPos = nextSelectedPosition;

        if (mItemsCanFocus && focusResult == null) {
            // There was no new view found to take focus, make sure we
            // don't leave focus with the old selection.
            final View focused = getFocusedChild();
            if (focused != null) {
                focused.clearFocus();
            }
        }

        needToRedraw = true;
        checkSelectionChanged();
    }

    if (amountToScroll > 0) {
        scrollListItemsBy(
                direction == View.FOCUS_UP || direction == View.FOCUS_LEFT ? amountToScroll : -amountToScroll);
        needToRedraw = true;
    }

    // If we didn't find a new focusable, make sure any existing focused
    // item that was panned off screen gives up focus.
    if (mItemsCanFocus && focusResult == null && selectedView != null && selectedView.hasFocus()) {
        final View focused = selectedView.findFocus();
        if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) {
            focused.clearFocus();
        }
    }

    // If the current selection is panned off, we need to remove the
    // selection
    if (nextSelectedPosition == INVALID_POSITION && selectedView != null
            && !isViewAncestorOf(selectedView, this)) {
        selectedView = null;
        hideSelector();

        // But we don't want to set the ressurect position (that would make
        // subsequent
        // unhandled key events bring back the item we just scrolled off)
        mResurrectToPosition = INVALID_POSITION;
    }

    if (needToRedraw) {
        if (selectedView != null) {
            positionSelector(selectedPos, selectedView);
            mSelectedStart = selectedView.getTop();
        }

        if (!awakenScrollbarsInternal()) {
            invalidate();
        }

        invokeOnItemScrollListener();
        return true;
    }

    return false;
}

From source file:com.artifex.mupdflib.TwoWayView.java

/**
 * Handle an arrow scroll going up or down.  Take into account whether items are selectable,
 * whether there are focusable items, etc.
 *
 * @param direction either {@link View#FOCUS_UP} or {@link View#FOCUS_DOWN} or
 *        {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT} depending on the
 *        current view orientation./*  w w  w .  ja  v a2s  .  c  om*/
 *
 * @return Whether any scrolling, selection or focus change occurred.
 */
private boolean arrowScrollImpl(int direction) {
    forceValidFocusDirection(direction);

    if (getChildCount() <= 0) {
        return false;
    }

    View selectedView = getSelectedView();
    int selectedPos = mSelectedPosition;

    int nextSelectedPosition = lookForSelectablePositionOnScreen(direction);
    int amountToScroll = amountToScroll(direction, nextSelectedPosition);

    // If we are moving focus, we may OVERRIDE the default behaviour
    final ArrowScrollFocusResult focusResult = (mItemsCanFocus ? arrowScrollFocused(direction) : null);
    if (focusResult != null) {
        nextSelectedPosition = focusResult.getSelectedPosition();
        amountToScroll = focusResult.getAmountToScroll();
    }

    boolean needToRedraw = (focusResult != null);
    if (nextSelectedPosition != INVALID_POSITION) {
        handleNewSelectionChange(selectedView, direction, nextSelectedPosition, focusResult != null);

        setSelectedPositionInt(nextSelectedPosition);
        setNextSelectedPositionInt(nextSelectedPosition);

        selectedView = getSelectedView();
        selectedPos = nextSelectedPosition;

        if (mItemsCanFocus && focusResult == null) {
            // There was no new view found to take focus, make sure we
            // don't leave focus with the old selection.
            final View focused = getFocusedChild();
            if (focused != null) {
                focused.clearFocus();
            }
        }

        needToRedraw = true;
        checkSelectionChanged();
    }

    if (amountToScroll > 0) {
        scrollListItemsBy(
                direction == View.FOCUS_UP || direction == View.FOCUS_LEFT ? amountToScroll : -amountToScroll);
        needToRedraw = true;
    }

    // If we didn't find a new focusable, make sure any existing focused
    // item that was panned off screen gives up focus.
    if (mItemsCanFocus && focusResult == null && selectedView != null && selectedView.hasFocus()) {
        final View focused = selectedView.findFocus();
        if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) {
            focused.clearFocus();
        }
    }

    // If the current selection is panned off, we need to remove the selection
    if (nextSelectedPosition == INVALID_POSITION && selectedView != null
            && !isViewAncestorOf(selectedView, this)) {
        selectedView = null;
        hideSelector();

        // But we don't want to set the ressurect position (that would make subsequent
        // unhandled key events bring back the item we just scrolled off)
        mResurrectToPosition = INVALID_POSITION;
    }

    if (needToRedraw) {
        if (selectedView != null) {
            positionSelector(selectedPos, selectedView);
            mSelectedStart = getChildStartEdge(selectedView);
        }

        if (!awakenScrollbarsInternal()) {
            invalidate();
        }

        invokeOnItemScrollListener();
        return true;
    }

    return false;
}