Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 702e8f9b authored by Fabrice Di Meglio's avatar Fabrice Di Meglio
Browse files

Fix bug #6213159 FocusFinder should be able to take care of Views direction

- use RTL layout direction as input to decide what to do

Change-Id: Ied825963992e5406f546a937857c5ca4101977bb
parent 2c4eabce
Loading
Loading
Loading
Loading
+61 −21
Original line number Diff line number Diff line
@@ -79,23 +79,43 @@ public class FocusFinder {
            switch (direction) {
                case View.FOCUS_RIGHT:
                case View.FOCUS_DOWN:
                    setFocusBottomRight(root);
                    break;
                case View.FOCUS_FORWARD:
                    final int rootTop = root.getScrollY();
                    final int rootLeft = root.getScrollX();
                    mFocusedRect.set(rootLeft, rootTop, rootLeft, rootTop);
                    if (focused != null && focused.isLayoutRtl()) {
                        setFocusTopLeft(root);
                    } else {
                        setFocusBottomRight(root);
                    }
                    break;

                case View.FOCUS_LEFT:
                case View.FOCUS_UP:
                    setFocusTopLeft(root);
                    break;
                case View.FOCUS_BACKWARD:
                    if (focused != null && focused.isLayoutRtl()) {
                        setFocusBottomRight(root);
                    } else {
                        setFocusTopLeft(root);
                    break;
                }
            }
        }
        return findNextFocus(root, focused, mFocusedRect, direction);
    }

    private void setFocusTopLeft(ViewGroup root) {
        final int rootBottom = root.getScrollY() + root.getHeight();
        final int rootRight = root.getScrollX() + root.getWidth();
        mFocusedRect.set(rootRight, rootBottom,
                rootRight, rootBottom);
                    break;
            }
    }
        return findNextFocus(root, focused, mFocusedRect, direction);

    private void setFocusBottomRight(ViewGroup root) {
        final int rootTop = root.getScrollY();
        final int rootLeft = root.getScrollX();
        mFocusedRect.set(rootLeft, rootTop, rootLeft, rootTop);
    }

    /**
@@ -135,22 +155,10 @@ public class FocusFinder {
            final int count = focusables.size();
            switch (direction) {
                case View.FOCUS_FORWARD:
                    if (focused != null) {
                        int position = focusables.lastIndexOf(focused);
                        if (position >= 0 && position + 1 < count) {
                            return focusables.get(position + 1);
                        }
                    }
                    return focusables.get(0);
                    return getForwardFocusable(focused, focusables, count);

                case View.FOCUS_BACKWARD:
                    if (focused != null) {
                        int position = focusables.indexOf(focused);
                        if (position > 0) {
                            return focusables.get(position - 1);
                        }
                    }
                    return focusables.get(count - 1);
                    return getBackwardFocusable(focused, focusables, count);
            }
            return null;
        }
@@ -193,6 +201,38 @@ public class FocusFinder {
        return closest;
    }

    private View getForwardFocusable(View focused, ArrayList<View> focusables, int count) {
        return (focused != null && focused.isLayoutRtl()) ?
                getPreviousFocusable(focused, focusables, count) :
                getNextFocusable(focused, focusables, count);
    }

    private View getNextFocusable(View focused, ArrayList<View> focusables, int count) {
        if (focused != null) {
            int position = focusables.lastIndexOf(focused);
            if (position >= 0 && position + 1 < count) {
                return focusables.get(position + 1);
            }
        }
        return focusables.get(0);
    }

    private View getBackwardFocusable(View focused, ArrayList<View> focusables, int count) {
        return (focused != null && focused.isLayoutRtl()) ?
                getNextFocusable(focused, focusables, count) :
                getPreviousFocusable(focused, focusables, count);
    }

    private View getPreviousFocusable(View focused, ArrayList<View> focusables, int count) {
        if (focused != null) {
            int position = focusables.indexOf(focused);
            if (position > 0) {
                return focusables.get(position - 1);
            }
        }
        return focusables.get(count - 1);
    }

    /**
     * Is rect1 a better candidate than rect2 for a focus search in a particular
     * direction from a source rect?  This is the core routine that determines