Merge pull request #28 from revanced/additional-bag-methods

Additional bag methods
This commit is contained in:
REAndroid 2023-04-19 20:27:12 +02:00 committed by GitHub
commit 6cba5d1d7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 7 deletions

View File

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

View File

@ -15,12 +15,14 @@
*/
package com.reandroid.arsc.value.plurals;
import com.reandroid.arsc.chunk.TableBlock;
import com.reandroid.arsc.item.StringItem;
import com.reandroid.arsc.item.TableString;
import com.reandroid.arsc.value.ResValueMap;
import com.reandroid.arsc.value.ValueType;
import com.reandroid.arsc.value.*;
import com.reandroid.arsc.value.bag.BagItem;
import java.util.List;
public class PluralsBagItem extends BagItem {
private PluralsBagItem(ResValueMap bagItem) {
super(bagItem);
@ -41,17 +43,49 @@
return PluralsQuantity.valueOf(mBagItem);
}
public String getQualityString() {
public String getQualityString(ResConfig resConfig) {
switch (getValueType()) {
case STRING:
return getStringValue();
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:
throw new IllegalArgumentException("Not STR/REFERENCE ValueType=" + getValueType());
}
}
private String formattedRefValue() {
return String.format("@0x%08x", getValue());
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
@ -61,7 +95,7 @@
if (hasStringValue()) {
builder.append(getStringValue());
} else {
builder.append(String.format("@0x%08x", getValue()));
builder.append(formattedRefValue());
}
builder.append("</item>");
return builder.toString();

View File

@ -40,6 +40,9 @@
public int getParentId() {
return getTableEntry().getParentId();
}
public void setParentId(int id) {
getTableEntry().setParentId(id);
}
public int getResourceId() {
com.reandroid.arsc.value.Entry entry = getEntry();

View File

@ -18,10 +18,13 @@
import com.reandroid.arsc.decoder.ValueDecoder;
import com.reandroid.arsc.item.StringItem;
import com.reandroid.arsc.item.TableString;
import com.reandroid.arsc.value.attribute.AttributeBag;
import com.reandroid.arsc.value.attribute.AttributeBagItem;
import com.reandroid.arsc.value.bag.BagItem;
import com.reandroid.arsc.value.Entry;
import com.reandroid.arsc.value.ResValueMap;
import com.reandroid.arsc.value.ValueType;
import com.reandroid.common.EntryStore;
public class StyleBagItem extends BagItem {
private StyleBagItem(ResValueMap bagItem) {
@ -47,6 +50,12 @@
char prefix = 0;
return block.buildResourceName(mBagItem.getName(), prefix, false);
}
public Entry getAttributeEntry(EntryStore entryStore) {
if (mBagItem == null) {
return null;
}
return entryStore.getEntryGroup(mBagItem.getName()).pickOne();
}
public int getNameId() {
if (mBagItem == null) {
@ -58,6 +67,10 @@
public boolean hasAttributeValue() {
return getValueType() == ValueType.ATTRIBUTE;
}
public boolean hasIntValue() {
ValueType valueType = getValueType();
return valueType == ValueType.INT_DEC || valueType == ValueType.INT_HEX;
}
public String getValueAsReference() {
ValueType valueType = getValueType();
@ -77,6 +90,18 @@
int id = getValue();
return entry.buildResourceName(id, prefix, includeType);
}
public String decodeAttributeValue(AttributeBag attr, EntryStore entryStore) {
if (!hasIntValue()) {
return null;
}
return attr.decodeAttributeValue(entryStore, getValue());
}
public AttributeBagItem[] getFlagsOrEnum(AttributeBag attr) {
if (!hasIntValue()) {
return null;
}
return attr.searchValue(getValue());
}
@Override
public String toString() {
@ -158,4 +183,7 @@
public static StyleBagItem createFloat(float n) {
return new StyleBagItem(ValueType.FLOAT, Float.floatToIntBits(n));
}
public static StyleBagItem enumOrFlag(AttributeBag attr, String valueString) {
return encoded(attr.encodeEnumOrFlagValue(valueString));
}
}