Skip to content

Commit 669aa7c

Browse files
Fabrice Di MeglioAndroid (Google) Code Review
authored andcommitted
Merge "Other improvements for bug #6427629 Clean up layout direction APIs" into jb-mr1-dev
2 parents 287f8a6 + 9a04856 commit 669aa7c

File tree

3 files changed

+115
-83
lines changed

3 files changed

+115
-83
lines changed

api/current.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25042,7 +25042,6 @@ package android.view {
2504225042
method public boolean isInEditMode();
2504325043
method public boolean isInTouchMode();
2504425044
method public boolean isLayoutRequested();
25045-
method public boolean isLayoutRtl();
2504625045
method public boolean isLongClickable();
2504725046
method public boolean isOpaque();
2504825047
method protected boolean isPaddingOffsetRequired();

core/java/android/view/View.java

Lines changed: 80 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5887,6 +5887,8 @@ public int getLayoutDirection() {
58875887
* layout attribute and/or the inherited value from the parent
58885888
*
58895889
* @return true if the layout is right-to-left.
5890+
*
5891+
* @hide
58905892
*/
58915893
@ViewDebug.ExportedProperty(category = "layout")
58925894
public boolean isLayoutRtl() {
@@ -11628,9 +11630,11 @@ public void onRtlPropertiesChanged() {
1162811630
* Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
1162911631
* that the parent directionality can and will be resolved before its children.
1163011632
*
11633+
* @return true if resolution has been done, false otherwise.
11634+
*
1163111635
* @hide
1163211636
*/
11633-
public void resolveLayoutDirection() {
11637+
public boolean resolveLayoutDirection() {
1163411638
// Clear any previous layout direction resolution
1163511639
mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
1163611640

@@ -11641,15 +11645,13 @@ public void resolveLayoutDirection() {
1164111645
case LAYOUT_DIRECTION_INHERIT:
1164211646
// We cannot resolve yet. LTR is by default and let the resolution happen again
1164311647
// later to get the correct resolved value
11644-
if (!canResolveLayoutDirection()) return;
11645-
11646-
ViewGroup viewGroup = ((ViewGroup) mParent);
11648+
if (!canResolveLayoutDirection()) return false;
1164711649

11648-
// We cannot resolve yet on the parent too. LTR is by default and let the
11649-
// resolution happen again later
11650-
if (!viewGroup.canResolveLayoutDirection()) return;
11650+
View parent = ((View) mParent);
11651+
// Parent has not yet resolved, LTR is still the default
11652+
if (!parent.isLayoutDirectionResolved()) return false;
1165111653

11652-
if (viewGroup.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11654+
if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
1165311655
mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
1165411656
}
1165511657
break;
@@ -11669,6 +11671,7 @@ public void resolveLayoutDirection() {
1166911671

1167011672
// Set to resolved
1167111673
mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11674+
return true;
1167211675
}
1167311676

1167411677
/**
@@ -11679,10 +11682,10 @@ public void resolveLayoutDirection() {
1167911682
* @hide
1168011683
*/
1168111684
public boolean canResolveLayoutDirection() {
11682-
switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11683-
PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
11685+
switch (getRawLayoutDirection()) {
1168411686
case LAYOUT_DIRECTION_INHERIT:
11685-
return (mParent != null) && (mParent instanceof ViewGroup);
11687+
return (mParent != null) && (mParent instanceof ViewGroup) &&
11688+
((ViewGroup) mParent).canResolveLayoutDirection();
1168611689
default:
1168711690
return true;
1168811691
}
@@ -16640,9 +16643,11 @@ public int getTextDirection() {
1664016643
/**
1664116644
* Resolve the text direction.
1664216645
*
16646+
* @return true if resolution has been done, false otherwise.
16647+
*
1664316648
* @hide
1664416649
*/
16645-
public void resolveTextDirection() {
16650+
public boolean resolveTextDirection() {
1664616651
// Reset any previous text direction resolution
1664716652
mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
1664816653

@@ -16651,29 +16656,35 @@ public void resolveTextDirection() {
1665116656
final int textDirection = getRawTextDirection();
1665216657
switch(textDirection) {
1665316658
case TEXT_DIRECTION_INHERIT:
16654-
if (canResolveTextDirection()) {
16655-
ViewGroup viewGroup = ((ViewGroup) mParent);
16656-
16657-
// Set current resolved direction to the same value as the parent's one
16658-
final int parentResolvedDirection = viewGroup.getTextDirection();
16659-
switch (parentResolvedDirection) {
16660-
case TEXT_DIRECTION_FIRST_STRONG:
16661-
case TEXT_DIRECTION_ANY_RTL:
16662-
case TEXT_DIRECTION_LTR:
16663-
case TEXT_DIRECTION_RTL:
16664-
case TEXT_DIRECTION_LOCALE:
16665-
mPrivateFlags2 |=
16666-
(parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16667-
break;
16668-
default:
16669-
// Default resolved direction is "first strong" heuristic
16670-
mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16671-
}
16672-
} else {
16659+
if (!canResolveTextDirection()) {
1667316660
// We cannot do the resolution if there is no parent, so use the default one
1667416661
mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
1667516662
// Resolution will need to happen again later
16676-
return;
16663+
return false;
16664+
}
16665+
16666+
View parent = ((View) mParent);
16667+
// Parent has not yet resolved, so we still return the default
16668+
if (!parent.isTextDirectionResolved()) {
16669+
mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16670+
// Resolution will need to happen again later
16671+
return false;
16672+
}
16673+
16674+
// Set current resolved direction to the same value as the parent's one
16675+
final int parentResolvedDirection = parent.getTextDirection();
16676+
switch (parentResolvedDirection) {
16677+
case TEXT_DIRECTION_FIRST_STRONG:
16678+
case TEXT_DIRECTION_ANY_RTL:
16679+
case TEXT_DIRECTION_LTR:
16680+
case TEXT_DIRECTION_RTL:
16681+
case TEXT_DIRECTION_LOCALE:
16682+
mPrivateFlags2 |=
16683+
(parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16684+
break;
16685+
default:
16686+
// Default resolved direction is "first strong" heuristic
16687+
mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
1667716688
}
1667816689
break;
1667916690
case TEXT_DIRECTION_FIRST_STRONG:
@@ -16695,6 +16706,7 @@ public void resolveTextDirection() {
1669516706

1669616707
// Set to resolved
1669716708
mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
16709+
return true;
1669816710
}
1669916711

1670016712
/**
@@ -16705,7 +16717,8 @@ public void resolveTextDirection() {
1670516717
private boolean canResolveTextDirection() {
1670616718
switch (getRawTextDirection()) {
1670716719
case TEXT_DIRECTION_INHERIT:
16708-
return (mParent != null) && (mParent instanceof ViewGroup);
16720+
return (mParent != null) && (mParent instanceof View) &&
16721+
((View) mParent).canResolveTextDirection();
1670916722
default:
1671016723
return true;
1671116724
}
@@ -16835,9 +16848,11 @@ public int getTextAlignment() {
1683516848
/**
1683616849
* Resolve the text alignment.
1683716850
*
16851+
* @return true if resolution has been done, false otherwise.
16852+
*
1683816853
* @hide
1683916854
*/
16840-
public void resolveTextAlignment() {
16855+
public boolean resolveTextAlignment() {
1684116856
// Reset any previous text alignment resolution
1684216857
mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
1684316858

@@ -16847,32 +16862,37 @@ public void resolveTextAlignment() {
1684716862
switch (textAlignment) {
1684816863
case TEXT_ALIGNMENT_INHERIT:
1684916864
// Check if we can resolve the text alignment
16850-
if (canResolveTextAlignment() && mParent instanceof View) {
16851-
View view = (View) mParent;
16852-
16853-
final int parentResolvedTextAlignment = view.getTextAlignment();
16854-
switch (parentResolvedTextAlignment) {
16855-
case TEXT_ALIGNMENT_GRAVITY:
16856-
case TEXT_ALIGNMENT_TEXT_START:
16857-
case TEXT_ALIGNMENT_TEXT_END:
16858-
case TEXT_ALIGNMENT_CENTER:
16859-
case TEXT_ALIGNMENT_VIEW_START:
16860-
case TEXT_ALIGNMENT_VIEW_END:
16861-
// Resolved text alignment is the same as the parent resolved
16862-
// text alignment
16863-
mPrivateFlags2 |=
16864-
(parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16865-
break;
16866-
default:
16867-
// Use default resolved text alignment
16868-
mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16869-
}
16870-
}
16871-
else {
16865+
if (!canResolveTextAlignment()) {
1687216866
// We cannot do the resolution if there is no parent so use the default
1687316867
mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
1687416868
// Resolution will need to happen again later
16875-
return;
16869+
return false;
16870+
}
16871+
View parent = (View) mParent;
16872+
16873+
// Parent has not yet resolved, so we still return the default
16874+
if (!parent.isTextAlignmentResolved()) {
16875+
mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16876+
// Resolution will need to happen again later
16877+
return false;
16878+
}
16879+
16880+
final int parentResolvedTextAlignment = parent.getTextAlignment();
16881+
switch (parentResolvedTextAlignment) {
16882+
case TEXT_ALIGNMENT_GRAVITY:
16883+
case TEXT_ALIGNMENT_TEXT_START:
16884+
case TEXT_ALIGNMENT_TEXT_END:
16885+
case TEXT_ALIGNMENT_CENTER:
16886+
case TEXT_ALIGNMENT_VIEW_START:
16887+
case TEXT_ALIGNMENT_VIEW_END:
16888+
// Resolved text alignment is the same as the parent resolved
16889+
// text alignment
16890+
mPrivateFlags2 |=
16891+
(parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16892+
break;
16893+
default:
16894+
// Use default resolved text alignment
16895+
mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
1687616896
}
1687716897
break;
1687816898
case TEXT_ALIGNMENT_GRAVITY:
@@ -16895,6 +16915,7 @@ public void resolveTextAlignment() {
1689516915

1689616916
// Set the resolved
1689716917
mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
16918+
return true;
1689816919
}
1689916920

1690016921
/**
@@ -16905,7 +16926,8 @@ public void resolveTextAlignment() {
1690516926
private boolean canResolveTextAlignment() {
1690616927
switch (getRawTextAlignment()) {
1690716928
case TEXT_DIRECTION_INHERIT:
16908-
return (mParent != null);
16929+
return (mParent != null) && (mParent instanceof View) &&
16930+
((View) mParent).canResolveTextAlignment();
1690916931
default:
1691016932
return true;
1691116933
}

core/java/android/view/ViewGroup.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3382,6 +3382,11 @@ private void addViewInner(View child, int index, LayoutParams params,
33823382
ai.mKeepScreenOn = lastKeepOn;
33833383
}
33843384

3385+
if (child.isLayoutDirectionInherited()) {
3386+
child.resetResolvedLayoutDirection();
3387+
child.resolveRtlPropertiesIfNeeded();
3388+
}
3389+
33853390
onViewAdded(child);
33863391

33873392
if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) {
@@ -5256,48 +5261,54 @@ public void requestTransitionStart(LayoutTransition transition) {
52565261
* @hide
52575262
*/
52585263
@Override
5259-
public void resolveLayoutDirection() {
5260-
super.resolveLayoutDirection();
5261-
5262-
int count = getChildCount();
5263-
for (int i = 0; i < count; i++) {
5264-
final View child = getChildAt(i);
5265-
if (child.isLayoutDirectionInherited()) {
5266-
child.resolveLayoutDirection();
5264+
public boolean resolveLayoutDirection() {
5265+
final boolean result = super.resolveLayoutDirection();
5266+
if (result) {
5267+
int count = getChildCount();
5268+
for (int i = 0; i < count; i++) {
5269+
final View child = getChildAt(i);
5270+
if (child.isLayoutDirectionInherited()) {
5271+
child.resolveLayoutDirection();
5272+
}
52675273
}
52685274
}
5275+
return result;
52695276
}
52705277

52715278
/**
52725279
* @hide
52735280
*/
52745281
@Override
5275-
public void resolveTextDirection() {
5276-
super.resolveTextDirection();
5277-
5278-
int count = getChildCount();
5279-
for (int i = 0; i < count; i++) {
5280-
final View child = getChildAt(i);
5281-
if (child.isTextDirectionInherited()) {
5282-
child.resolveTextDirection();
5282+
public boolean resolveTextDirection() {
5283+
final boolean result = super.resolveTextDirection();
5284+
if (result) {
5285+
int count = getChildCount();
5286+
for (int i = 0; i < count; i++) {
5287+
final View child = getChildAt(i);
5288+
if (child.isTextDirectionInherited()) {
5289+
child.resolveTextDirection();
5290+
}
52835291
}
52845292
}
5293+
return result;
52855294
}
52865295

52875296
/**
52885297
* @hide
52895298
*/
52905299
@Override
5291-
public void resolveTextAlignment() {
5292-
super.resolveTextAlignment();
5293-
5294-
int count = getChildCount();
5295-
for (int i = 0; i < count; i++) {
5296-
final View child = getChildAt(i);
5297-
if (child.isTextAlignmentInherited()) {
5298-
child.resolveTextAlignment();
5300+
public boolean resolveTextAlignment() {
5301+
final boolean result = super.resolveTextAlignment();
5302+
if (result) {
5303+
int count = getChildCount();
5304+
for (int i = 0; i < count; i++) {
5305+
final View child = getChildAt(i);
5306+
if (child.isTextAlignmentInherited()) {
5307+
child.resolveTextAlignment();
5308+
}
52995309
}
53005310
}
5311+
return result;
53015312
}
53025313

53035314
/**

0 commit comments

Comments
 (0)