resolve string refs in plurals

This commit is contained in:
Ax333l 2023-04-16 20:05:41 +02:00
parent 2fac1b2235
commit c3fdcdd53b
No known key found for this signature in database
GPG Key ID: D2B4D85271127D23
2 changed files with 45 additions and 7 deletions

View File

@ -15,6 +15,7 @@
*/ */
package com.reandroid.arsc.value.plurals; package com.reandroid.arsc.value.plurals;
import com.reandroid.arsc.value.ResConfig;
import com.reandroid.arsc.value.ResValueMap; import com.reandroid.arsc.value.ResValueMap;
import com.reandroid.arsc.value.ValueType; import com.reandroid.arsc.value.ValueType;
import com.reandroid.arsc.value.bag.MapBag; import com.reandroid.arsc.value.bag.MapBag;
@ -50,12 +51,15 @@
return PluralsQuantity.valueOf(valueMap); return PluralsQuantity.valueOf(valueMap);
} }
public String getQuantityString(PluralsQuantity quantity) { public String getQuantityString(PluralsQuantity quantity, ResConfig resConfig) {
PluralsBagItem item = get(quantity); PluralsBagItem item = get(quantity);
if (item == null) { if (item == null) {
return null; return null;
} }
return item.getQualityString(); return item.getQualityString(resConfig);
}
public String getQuantityString(PluralsQuantity quantity) {
return getQuantityString(quantity, null);
} }
public void setQuantityString(PluralsQuantity quantity, String str) { public void setQuantityString(PluralsQuantity quantity, String str) {

View File

@ -15,12 +15,14 @@
*/ */
package com.reandroid.arsc.value.plurals; package com.reandroid.arsc.value.plurals;
import com.reandroid.arsc.chunk.TableBlock;
import com.reandroid.arsc.item.StringItem; import com.reandroid.arsc.item.StringItem;
import com.reandroid.arsc.item.TableString; import com.reandroid.arsc.item.TableString;
import com.reandroid.arsc.value.ResValueMap; import com.reandroid.arsc.value.*;
import com.reandroid.arsc.value.ValueType;
import com.reandroid.arsc.value.bag.BagItem; import com.reandroid.arsc.value.bag.BagItem;
import java.util.List;
public class PluralsBagItem extends BagItem { public class PluralsBagItem extends BagItem {
private PluralsBagItem(ResValueMap bagItem) { private PluralsBagItem(ResValueMap bagItem) {
super(bagItem); super(bagItem);
@ -41,17 +43,49 @@
return PluralsQuantity.valueOf(mBagItem); return PluralsQuantity.valueOf(mBagItem);
} }
public String getQualityString() { public String getQualityString(ResConfig resConfig) {
switch (getValueType()) { switch (getValueType()) {
case STRING: case STRING:
return getStringValue(); return getStringValue();
case REFERENCE: case REFERENCE:
// TODO: resolve string reference based on language Entry entry = null;
if (mBagItem != null) {
entry = mBagItem.getEntry();
}
if (entry == null) {
return null;
}
if (resConfig == null) {
resConfig = entry.getResConfig();
}
Entry stringRes = null;
if (resConfig != null) {
TableBlock tableBlock = entry.getPackageBlock().getTableBlock();
List<Entry> resolvedList = tableBlock.resolveReferenceWithConfig(getValue(), resConfig);
if (resolvedList.size() > 0) {
stringRes = resolvedList.get(0);
}
}
if (stringRes == null) {
return null;
}
ResValue resValue = stringRes.getResValue();
if (resValue == null || resValue.getValueType() != ValueType.STRING) {
throw new IllegalArgumentException("Not a STR reference: " + formattedRefValue());
}
return resValue.getValueAsString();
default: default:
throw new IllegalArgumentException("Not STR/REFERENCE ValueType=" + getValueType()); throw new IllegalArgumentException("Not STR/REFERENCE ValueType=" + getValueType());
} }
} }
private String formattedRefValue() {
return String.format("@0x%08x", getValue());
}
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
@ -61,7 +95,7 @@
if (hasStringValue()) { if (hasStringValue()) {
builder.append(getStringValue()); builder.append(getStringValue());
} else { } else {
builder.append(String.format("@0x%08x", getValue())); builder.append(formattedRefValue());
} }
builder.append("</item>"); builder.append("</item>");
return builder.toString(); return builder.toString();