Update xml blocks

* Fix FIRST_INT on attributes
* Fix version editing on manifest
* Make easy attribute creation
This commit is contained in:
REAndroid 2022-12-27 07:02:25 -05:00
parent b09f85e58a
commit 28c6d98571
4 changed files with 104 additions and 29 deletions

View File

@ -38,6 +38,14 @@ public class ResXmlAttributeArray extends BlockArray<ResXmlAttribute>
this.mAttributeStart=attributeStart;
this.mAttributeCount=attributeCount;
}
public ResXmlAttribute getFirstIntAttribute(){
for(ResXmlAttribute attribute:listItems()){
if(attribute.hasIntegerValue()){
return attribute;
}
}
return null;
}
public void sortAttributes(){
sort(this);
}

View File

@ -115,7 +115,7 @@ public class AndroidManifestBlock extends ResXmlBlock{
return exist;
}
ResXmlElement result = manifestElement.createChildElement(TAG_uses_permission);
ResXmlAttribute attr = result.createAndroidAttribute(NAME_name, ID_name);
ResXmlAttribute attr = result.getOrCreateAndroidAttribute(NAME_name, ID_name);
attr.setValueAsString(permissionName);
return result;
}
@ -145,8 +145,8 @@ public class AndroidManifestBlock extends ResXmlBlock{
public Integer getCompileSdkVersion(){
return getManifestAttributeInt(ID_compileSdkVersion);
}
public boolean setCompileSdkVersion(int val){
return setManifestAttributeInt(ID_compileSdkVersion, val);
public void setCompileSdkVersion(int val){
setManifestAttributeInt(NAME_compileSdkVersion, ID_compileSdkVersion, val);
}
public String getCompileSdkVersionCodename(){
return getManifestAttributeString(ID_compileSdkVersionCodename);
@ -166,14 +166,14 @@ public class AndroidManifestBlock extends ResXmlBlock{
public Integer getVersionCode(){
return getManifestAttributeInt(ID_versionCode);
}
public boolean setVersionCode(int val){
return setManifestAttributeInt(ID_versionCode, val);
public void setVersionCode(int val){
setManifestAttributeInt(NAME_versionCode, ID_versionCode, val);
}
public String getVersionName(){
return getManifestAttributeString(ID_versionName);
}
public boolean setVersionName(String packageName){
return setManifestAttributeString(ID_versionName, packageName);
return setManifestAttributeString(NAME_versionName, ID_versionName, packageName);
}
private String getManifestAttributeString(int resourceId){
ResXmlElement manifest=getManifestElement();
@ -186,29 +186,18 @@ public class AndroidManifestBlock extends ResXmlBlock{
}
return attribute.getValueAsString();
}
private boolean setManifestAttributeString(int resourceId, String value){
ResXmlElement manifestElement=getManifestElement();
if(manifestElement==null){
return false;
}
ResXmlAttribute attribute = manifestElement.searchAttributeByResourceId(resourceId);
if(attribute==null){
return false;
}
private boolean setManifestAttributeString(String attributeName, int resourceId, String value){
ResXmlElement manifestElement=getOrCreateManifestElement();
ResXmlAttribute attribute = manifestElement
.getOrCreateAndroidAttribute(attributeName, resourceId);
attribute.setValueAsString(value);
return true;
}
private boolean setManifestAttributeInt(int resId, int value){
ResXmlElement manifestElement=getManifestElement();
if(manifestElement==null){
return false;
}
ResXmlAttribute attribute= manifestElement.searchAttributeByResourceId(resId);
if(attribute==null){
return false;
}
attribute.setValueAsIntegerDec(value);
return true;
private void setManifestAttributeInt(String attributeName, int resourceId, int value){
ResXmlElement manifestElement=getOrCreateManifestElement();
ResXmlAttribute attribute = manifestElement
.getOrCreateAndroidAttribute(attributeName, resourceId);
attribute.setValueAsInteger(value);
}
private Integer getManifestAttributeInt(int resourceId){
ResXmlElement manifestElement=getManifestElement();
@ -216,10 +205,10 @@ public class AndroidManifestBlock extends ResXmlBlock{
return null;
}
ResXmlAttribute attribute= manifestElement.searchAttributeByResourceId(resourceId);
if(attribute==null || attribute.getValueType()!=ValueType.INT_DEC){
if(attribute==null || !attribute.hasIntegerValue()){
return null;
}
return attribute.getRawValue();
return attribute.getValueAsInteger();
}
public ResXmlElement getApplicationElement(){
ResXmlElement manifestElement=getManifestElement();
@ -238,6 +227,16 @@ public class AndroidManifestBlock extends ResXmlBlock{
}
return manifestElement;
}
private ResXmlElement getOrCreateManifestElement(){
ResXmlElement manifestElement=getResXmlElement();
if(manifestElement==null){
manifestElement=createRootElement(TAG_manifest);
}
if(!TAG_manifest.equals(manifestElement.getTag())){
manifestElement.setTag(TAG_manifest);
}
return manifestElement;
}
@Override
public String toString(){
StringBuilder builder=new StringBuilder();

View File

@ -15,6 +15,7 @@
*/
package com.reandroid.lib.arsc.chunk.xml;
import com.reandroid.lib.arsc.array.ResXmlAttributeArray;
import com.reandroid.lib.arsc.array.ResXmlIDArray;
import com.reandroid.lib.arsc.base.Block;
import com.reandroid.lib.arsc.container.FixedBlockContainer;
@ -142,9 +143,11 @@ import java.util.Set;
return ValueType.valueOf(getValueTypeByte());
}
public void setValueType(ValueType valueType){
byte b=0;
if(valueType!=null){
setValueTypeByte(valueType.getByte());
b=valueType.getByte();
}
setValueTypeByte(b);
}
public String getFullName(){
String name=getName();
@ -301,6 +304,36 @@ import java.util.Set;
setRawValue(ref);
setValueStringReference(-1);
}
public boolean hasIntegerValue(){
ValueType valueType=getValueType();
return valueType==ValueType.INT_DEC
||valueType==ValueType.FIRST_INT;
}
public Integer getValueAsInteger(){
if(hasIntegerValue()){
return getRawValue();
}
return null;
}
public void setValueAsInteger(int val){
setValueType(ValueType.INT_DEC);
ResXmlAttributeArray array=getParentResXmlAttributeArray();
if(array!=null && array.getFirstIntAttribute()==this){
setValueType(ValueType.FIRST_INT);
}
setRawValue(val);
setValueStringReference(-1);
}
private ResXmlAttributeArray getParentResXmlAttributeArray(){
Block parent=this;
while(parent!=null){
if(parent instanceof ResXmlAttributeArray){
return (ResXmlAttributeArray)parent;
}
parent=parent.getParent();
}
return null;
}
public void setValueAsIntegerDec(int val){
setValueType(ValueType.INT_DEC);
setRawValue(val);

View File

@ -107,6 +107,28 @@ import java.util.*;
}
return resXmlElement;
}
public ResXmlAttribute getOrCreateAndroidAttribute(String name, int resourceId){
return getOrCreateAttribute(NS_ANDROID_URI, NS_ANDROID_PREFIX, name, resourceId);
}
public ResXmlAttribute getOrCreateAttribute(String uri, String prefix, String name, int resourceId){
ResXmlAttribute attribute=searchAttribute(name, resourceId);
if(attribute==null){
attribute = createAttribute(name, resourceId);
if(uri!=null){
ResXmlElement root = getRootResXmlElement();
ResXmlStartNamespace ns = root.getOrCreateNamespace(uri, prefix);
attribute.setNamespaceReference(ns.getUriReference());
}
}
return attribute;
}
public ResXmlAttribute getOrCreateAttribute(String name, int resourceId){
ResXmlAttribute attribute=searchAttribute(name, resourceId);
if(attribute==null){
attribute=createAttribute(name, resourceId);
}
return attribute;
}
public ResXmlAttribute createAndroidAttribute(String name, int resourceId){
ResXmlAttribute attribute=createAttribute(name, resourceId);
ResXmlStartNamespace ns = getOrCreateNamespace(NS_ANDROID_URI, NS_ANDROID_PREFIX);
@ -133,6 +155,12 @@ import java.util.*;
}
return null;
}
private ResXmlAttribute searchAttribute(String name, int resourceId){
if(resourceId==0){
return searchAttributeByName(name);
}
return searchAttributeByResourceId(resourceId);
}
public ResXmlAttribute searchAttributeByName(String name){
ResXmlStartElement startElement=getStartElement();
if(startElement!=null){
@ -266,6 +294,13 @@ import java.util.*;
}
return results;
}
public ResXmlElement getRootResXmlElement(){
ResXmlElement parent=getParentResXmlElement();
if(parent!=null){
return parent.getRootResXmlElement();
}
return this;
}
public ResXmlElement getParentResXmlElement(){
Block parent=getParent();
while (parent!=null){