Why infinity loop?
Closed this issue · 4 comments
while (true) {
try {
layout = StaticLayoutHelper.make(
mParams.text,
0,
mParams.text.length(),
mParams.paint,
width,
mParams.alignment,
mParams.spacingMult,
mParams.spacingAdd,
mParams.includePadding,
mParams.ellipsize,
width,
numLines,
mParams.textDirection);
} catch (IndexOutOfBoundsException e) {
// Workaround for https://code.google.com/p/android/issues/detail?id=35412
if (!(mParams.text instanceof String)) {
// remove all Spannables and re-try
Log.e("TextLayoutBuilder", "Hit bug #35412, retrying with Spannables removed", e);
mParams.text = mParams.text.toString();
continue;
} else {
// If it still happens with all Spannables removed we'll bubble the exception up
throw e;
}
}
break;
}
the infinite loop is a workaround for this bug, so that if the IndexOutOfBoundsException
is thrown, we can remove the spans and try again. it's possible to re-write this with a method instead (and just re-call the method without the Spannables in the IndexOutOfBounds
scenario, but that's not much nicer either).
is there any specific issue your'e running into with this? this should never actually infinite loop (due to the break when no exception is thrown, a single loop if an IndexOutOfBoundsException
is thrown and we are in the spannable case, or an exception being bubbled up).
Ok, then better remove loop, because it's confused
try {
layout = makeStaticLayout(numLines, width);
} catch (IndexOutOfBoundsException e) {
// Workaround for https://code.google.com/p/android/issues/detail?id=35412
if (!(mParams.text instanceof String)) {
// remove all Spannables and re-try
Log.e("TextLayoutBuilder", "Hit bug #35412, retrying with Spannables removed", e);
mParams.text = mParams.text.toString();
layout = makeStaticLayout(numLines, width);
} else {
// If it still happens with all Spannables removed we'll bubble the exception up
throw e;
}
}
private StaticLayout makeStaticLayout(int numLines, int width) {
return StaticLayoutHelper.make(
mParams.text,
0,
mParams.text.length(),
mParams.paint,
width,
mParams.alignment,
mParams.spacingMult,
mParams.spacingAdd,
mParams.includePadding,
mParams.ellipsize,
width,
numLines,
mParams.textDirection);
}
If you try the workaround for a bug on some Android API version, then better to check current API version, because current solution is not optimal.
Can you please send in a pull request? Closing this for now.