Skip to content

Commit 4c1e73b

Browse files
author
Xavier Ducrohet
committed
Merge e57aa434 from honeycomb-mr1. do not merge.
LayoutLib: Fix issue where <include> with no layout params wouldn't display. The issue is that the layout params from the root element of the included layout should be used but this failed because loading the layout params from the <include> tag didn't throw a RuntimeException in our modified code (BridgeTypedArray). Because we don't want to throw exception in general we only throw it when reading the layout params of an include node which is pretty crappy, but works for now. Change-Id: I83ccf956e8b476f34dfc9a70aebae2288d53746e
1 parent f791863 commit 4c1e73b

3 files changed

Lines changed: 72 additions & 32 deletions

File tree

tools/layoutlib/bridge/src/android/view/LayoutInflater_Delegate.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
*/
4444
public class LayoutInflater_Delegate {
4545

46+
public static boolean sIsInInclude = false;
47+
4648
@LayoutlibDelegate
4749
/*package*/ static void parseInclude(LayoutInflater thisInflater,
4850
XmlPullParser parser, View parent, AttributeSet attrs)
@@ -109,10 +111,22 @@ public class LayoutInflater_Delegate {
109111
// false means we need to rely on the included layout params.
110112
ViewGroup.LayoutParams params = null;
111113
try {
114+
// ---- START CHANGES
115+
sIsInInclude = true;
116+
// ---- END CHANGES
117+
112118
params = group.generateLayoutParams(attrs);
113119
} catch (RuntimeException e) {
120+
// ---- START CHANGES
121+
sIsInInclude = false;
122+
// ---- END CHANGES
123+
114124
params = group.generateLayoutParams(childAttrs);
115125
} finally {
126+
// ---- START CHANGES
127+
sIsInInclude = false;
128+
// ---- END CHANGES
129+
116130
if (params != null) {
117131
view.setLayoutParams(params);
118132
}

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeTypedArray.java

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import android.graphics.drawable.Drawable;
3737
import android.util.DisplayMetrics;
3838
import android.util.TypedValue;
39+
import android.view.LayoutInflater_Delegate;
3940
import android.view.ViewGroup.LayoutParams;
4041

4142
import java.io.File;
@@ -471,40 +472,23 @@ public int getDimensionPixelOffset(int index, int defValue) {
471472
*/
472473
@Override
473474
public int getDimensionPixelSize(int index, int defValue) {
474-
if (mResourceData[index] == null) {
475-
return defValue;
476-
}
475+
try {
476+
return getDimension(index);
477+
} catch (RuntimeException e) {
478+
if (mResourceData[index] != null) {
479+
String s = mResourceData[index].getValue();
477480

478-
String s = mResourceData[index].getValue();
481+
if (s != null) {
482+
// looks like we were unable to resolve the dimension value
483+
Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT,
484+
String.format(
485+
"\"%1$s\" in attribute \"%2$s\" is not a valid format.",
486+
s, mNames[index]), null /*data*/);
487+
}
488+
}
479489

480-
if (s == null) {
481-
return defValue;
482-
} else if (s.equals(BridgeConstants.MATCH_PARENT) ||
483-
s.equals(BridgeConstants.FILL_PARENT)) {
484-
return LayoutParams.MATCH_PARENT;
485-
} else if (s.equals(BridgeConstants.WRAP_CONTENT)) {
486-
return LayoutParams.WRAP_CONTENT;
487-
} else if (RenderResources.REFERENCE_NULL.equals(s)) {
488490
return defValue;
489491
}
490-
491-
if (ResourceHelper.stringToFloat(s, mValue)) {
492-
float f = mValue.getDimension(mBridgeResources.mMetrics);
493-
494-
final int res = (int)(f+0.5f);
495-
if (res != 0) return res;
496-
if (f == 0) return 0;
497-
if (f > 0) return 1;
498-
return defValue; // this is basically unreachable.
499-
}
500-
501-
// looks like we were unable to resolve the dimension value
502-
Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT,
503-
String.format(
504-
"\"%1$s\" in attribute \"%2$s\" is not a valid format.",
505-
s, mNames[index]), null /*data*/);
506-
507-
return defValue;
508492
}
509493

510494
/**
@@ -521,14 +505,57 @@ public int getDimensionPixelSize(int index, int defValue) {
521505
*/
522506
@Override
523507
public int getLayoutDimension(int index, String name) {
524-
return getDimensionPixelSize(index, 0);
508+
try {
509+
// this will throw an exception
510+
return getDimension(index);
511+
} catch (RuntimeException e) {
512+
513+
if (LayoutInflater_Delegate.sIsInInclude) {
514+
throw new RuntimeException();
515+
}
516+
517+
Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT,
518+
"You must supply a " + name + " attribute.", null);
519+
520+
return 0;
521+
}
525522
}
526523

527524
@Override
528525
public int getLayoutDimension(int index, int defValue) {
529526
return getDimensionPixelSize(index, defValue);
530527
}
531528

529+
private int getDimension(int index) {
530+
if (mResourceData[index] == null) {
531+
throw new RuntimeException();
532+
}
533+
534+
String s = mResourceData[index].getValue();
535+
536+
if (s == null) {
537+
throw new RuntimeException();
538+
} else if (s.equals(BridgeConstants.MATCH_PARENT) ||
539+
s.equals(BridgeConstants.FILL_PARENT)) {
540+
return LayoutParams.MATCH_PARENT;
541+
} else if (s.equals(BridgeConstants.WRAP_CONTENT)) {
542+
return LayoutParams.WRAP_CONTENT;
543+
} else if (RenderResources.REFERENCE_NULL.equals(s)) {
544+
throw new RuntimeException();
545+
}
546+
547+
if (ResourceHelper.stringToFloat(s, mValue)) {
548+
float f = mValue.getDimension(mBridgeResources.mMetrics);
549+
550+
final int res = (int)(f+0.5f);
551+
if (res != 0) return res;
552+
if (f == 0) return 0;
553+
if (f > 0) return 1;
554+
}
555+
556+
throw new RuntimeException();
557+
}
558+
532559
/**
533560
* Retrieve a fractional unit attribute at <var>index</var>.
534561
*

tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public String[] getDeleteReturns() {
102102
"android.view.LayoutInflater#parseInclude",
103103
"android.view.View#isInEditMode",
104104
"com.android.internal.util.XmlUtils#convertValueToInt",
105-
// TODO: comment out once DelegateClass is working
106105
};
107106

108107
/**

0 commit comments

Comments
 (0)