Skip to content

Commit e32a4ac

Browse files
Fabrice Di MeglioAndroid (Google) Code Review
authored andcommitted
Merge "Improve FocusFinder for RTL support"
2 parents 1505ce2 + 7e0a372 commit e32a4ac

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

core/java/android/view/FocusFinder.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private FocusFinder() {}
5454
/**
5555
* Find the next view to take focus in root's descendants, starting from the view
5656
* that currently is focused.
57-
* @param root Contains focused
57+
* @param root Contains focused. Cannot be null.
5858
* @param focused Has focus now.
5959
* @param direction Direction to look.
6060
* @return The next focusable view, or null if none exists.
@@ -82,7 +82,7 @@ public final View findNextFocus(ViewGroup root, View focused, int direction) {
8282
setFocusBottomRight(root);
8383
break;
8484
case View.FOCUS_FORWARD:
85-
if (focused != null && focused.isLayoutRtl()) {
85+
if (root.isLayoutRtl()) {
8686
setFocusTopLeft(root);
8787
} else {
8888
setFocusBottomRight(root);
@@ -94,7 +94,7 @@ public final View findNextFocus(ViewGroup root, View focused, int direction) {
9494
setFocusTopLeft(root);
9595
break;
9696
case View.FOCUS_BACKWARD:
97-
if (focused != null && focused.isLayoutRtl()) {
97+
if (root.isLayoutRtl()) {
9898
setFocusBottomRight(root);
9999
} else {
100100
setFocusTopLeft(root);
@@ -121,7 +121,7 @@ private void setFocusBottomRight(ViewGroup root) {
121121
/**
122122
* Find the next view to take focus in root's descendants, searching from
123123
* a particular rectangle in root's coordinates.
124-
* @param root Contains focusedRect.
124+
* @param root Contains focusedRect. Cannot be null.
125125
* @param focusedRect The starting point of the search.
126126
* @param direction Direction to look.
127127
* @return The next focusable view, or null if none exists.
@@ -155,10 +155,10 @@ private View findNextFocus(ViewGroup root, View focused, Rect focusedRect, int d
155155
final int count = focusables.size();
156156
switch (direction) {
157157
case View.FOCUS_FORWARD:
158-
return getForwardFocusable(focused, focusables, count);
158+
return getForwardFocusable(root, focused, focusables, count);
159159

160160
case View.FOCUS_BACKWARD:
161-
return getBackwardFocusable(focused, focusables, count);
161+
return getBackwardFocusable(root, focused, focusables, count);
162162
}
163163
return null;
164164
}
@@ -201,13 +201,14 @@ private View findNextFocus(ViewGroup root, View focused, Rect focusedRect, int d
201201
return closest;
202202
}
203203

204-
private View getForwardFocusable(View focused, ArrayList<View> focusables, int count) {
205-
return (focused != null && focused.isLayoutRtl()) ?
204+
private static View getForwardFocusable(ViewGroup root, View focused,
205+
ArrayList<View> focusables, int count) {
206+
return (root.isLayoutRtl()) ?
206207
getPreviousFocusable(focused, focusables, count) :
207208
getNextFocusable(focused, focusables, count);
208209
}
209210

210-
private View getNextFocusable(View focused, ArrayList<View> focusables, int count) {
211+
private static View getNextFocusable(View focused, ArrayList<View> focusables, int count) {
211212
if (focused != null) {
212213
int position = focusables.lastIndexOf(focused);
213214
if (position >= 0 && position + 1 < count) {
@@ -217,13 +218,14 @@ private View getNextFocusable(View focused, ArrayList<View> focusables, int coun
217218
return focusables.get(0);
218219
}
219220

220-
private View getBackwardFocusable(View focused, ArrayList<View> focusables, int count) {
221-
return (focused != null && focused.isLayoutRtl()) ?
221+
private static View getBackwardFocusable(ViewGroup root, View focused,
222+
ArrayList<View> focusables, int count) {
223+
return (root.isLayoutRtl()) ?
222224
getNextFocusable(focused, focusables, count) :
223225
getPreviousFocusable(focused, focusables, count);
224226
}
225227

226-
private View getPreviousFocusable(View focused, ArrayList<View> focusables, int count) {
228+
private static View getPreviousFocusable(View focused, ArrayList<View> focusables, int count) {
227229
if (focused != null) {
228230
int position = focusables.indexOf(focused);
229231
if (position > 0) {
@@ -353,7 +355,7 @@ boolean isCandidate(Rect srcRect, Rect destRect, int direction) {
353355

354356

355357
/**
356-
* Do the "beams" w.r.t the given direcition's axis of rect1 and rect2 overlap?
358+
* Do the "beams" w.r.t the given direction's axis of rect1 and rect2 overlap?
357359
* @param direction the direction (up, down, left, right)
358360
* @param rect1 The first rectangle
359361
* @param rect2 The second rectangle
@@ -441,7 +443,7 @@ static int majorAxisDistanceToFarEdgeRaw(int direction, Rect source, Rect dest)
441443

442444
/**
443445
* Find the distance on the minor axis w.r.t the direction to the nearest
444-
* edge of the destination rectange.
446+
* edge of the destination rectangle.
445447
* @param direction the direction (up, down, left, right)
446448
* @param source The source rect.
447449
* @param dest The destination rect.

0 commit comments

Comments
 (0)