Fix plurals support in layoutlib
Bug: http://b.android.com/213000 Test: Manually tested on Android Studio. Adding tests for this specific case requires bringing a new sdk-common prebuilt to the the maintenance branch so it's not really practical. Change-Id: I0646aab656d5d5769355c6cc026e877b8784d173
This commit is contained in:
@@ -21,6 +21,7 @@ import com.android.ide.common.rendering.api.ArrayResourceValue;
|
||||
import com.android.ide.common.rendering.api.DensityBasedResourceValue;
|
||||
import com.android.ide.common.rendering.api.LayoutLog;
|
||||
import com.android.ide.common.rendering.api.LayoutlibCallback;
|
||||
import com.android.ide.common.rendering.api.PluralsResourceValue;
|
||||
import com.android.ide.common.rendering.api.RenderResources;
|
||||
import com.android.ide.common.rendering.api.ResourceValue;
|
||||
import com.android.layoutlib.bridge.Bridge;
|
||||
@@ -43,6 +44,7 @@ import android.annotation.Nullable;
|
||||
import android.content.res.Resources.NotFoundException;
|
||||
import android.content.res.Resources.Theme;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.icu.text.PluralRules;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.LruCache;
|
||||
@@ -734,6 +736,48 @@ public class Resources_Delegate {
|
||||
return null;
|
||||
}
|
||||
|
||||
@LayoutlibDelegate
|
||||
static String getQuantityString(Resources resources, int id, int quantity) throws
|
||||
NotFoundException {
|
||||
Pair<String, ResourceValue> value = getResourceValue(resources, id, mPlatformResourceFlag);
|
||||
|
||||
if (value != null) {
|
||||
if (value.getSecond() instanceof PluralsResourceValue) {
|
||||
PluralsResourceValue pluralsResourceValue = (PluralsResourceValue) value.getSecond();
|
||||
PluralRules pluralRules = PluralRules.forLocale(resources.getConfiguration().getLocales()
|
||||
.get(0));
|
||||
String strValue = pluralsResourceValue.getValue(pluralRules.select(quantity));
|
||||
if (strValue == null) {
|
||||
strValue = pluralsResourceValue.getValue(PluralRules.KEYWORD_OTHER);
|
||||
}
|
||||
|
||||
return strValue;
|
||||
}
|
||||
else {
|
||||
return value.getSecond().getValue();
|
||||
}
|
||||
}
|
||||
|
||||
// id was not found or not resolved. Throw a NotFoundException.
|
||||
throwException(resources, id);
|
||||
|
||||
// this is not used since the method above always throws
|
||||
return null;
|
||||
}
|
||||
|
||||
@LayoutlibDelegate
|
||||
static String getQuantityString(Resources resources, int id, int quantity, Object... formatArgs)
|
||||
throws NotFoundException {
|
||||
String raw = getQuantityString(resources, id, quantity);
|
||||
return String.format(resources.getConfiguration().getLocales().get(0), raw, formatArgs);
|
||||
}
|
||||
|
||||
@LayoutlibDelegate
|
||||
static CharSequence getQuantityText(Resources resources, int id, int quantity) throws
|
||||
NotFoundException {
|
||||
return getQuantityString(resources, id, quantity);
|
||||
}
|
||||
|
||||
@LayoutlibDelegate
|
||||
static void getValue(Resources resources, int id, TypedValue outValue, boolean resolveRefs)
|
||||
throws NotFoundException {
|
||||
|
||||
@@ -154,6 +154,8 @@ public final class CreateInfo implements ICreateInfo {
|
||||
"android.content.res.Resources#getIntArray",
|
||||
"android.content.res.Resources#getInteger",
|
||||
"android.content.res.Resources#getLayout",
|
||||
"android.content.res.Resources#getQuantityString",
|
||||
"android.content.res.Resources#getQuantityText",
|
||||
"android.content.res.Resources#getResourceEntryName",
|
||||
"android.content.res.Resources#getResourceName",
|
||||
"android.content.res.Resources#getResourcePackageName",
|
||||
|
||||
Reference in New Issue
Block a user