package com.reandroid.lib.arsc.value; import com.reandroid.lib.arsc.base.Block; import com.reandroid.lib.arsc.container.FixedBlockContainer; import com.reandroid.lib.arsc.io.BlockLoad; import com.reandroid.lib.arsc.io.BlockReader; import com.reandroid.lib.arsc.item.ByteArray; import com.reandroid.lib.arsc.item.IntegerItem; import com.reandroid.lib.json.JsonItem; import org.json.JSONObject; import java.io.IOException; import java.util.Arrays; public class ResConfig extends FixedBlockContainer implements BlockLoad, JsonItem { private final IntegerItem configSize; private final ByteArray mValuesContainer; private String mQualifiers; public ResConfig(){ super(2); this.configSize = new IntegerItem(SIZE_64); this.mValuesContainer = new ByteArray(SIZE_64 - 4); addChild(0, configSize); addChild(1, mValuesContainer); this.configSize.setBlockLoad(this); this.mValuesContainer.setBlockLoad(this); } @Override public void onBlockLoaded(BlockReader reader, Block sender) throws IOException { if(sender==configSize){ setConfigSize(configSize.get()); }else if(sender==mValuesContainer){ valuesChanged(); } } @Override protected void onPreRefreshRefresh(){ int count=countBytes(); configSize.set(count); } @Override protected void onRefreshed() { valuesChanged(); } public void parseQualifiers(String name){ ResConfigHelper.parseQualifiers(this, name); mQualifiers=null; } public void setConfigSize(int size){ if( size != SIZE_16 && size != SIZE_28 && size != SIZE_32 && size != SIZE_36 && size != SIZE_48 && size != SIZE_56 && size != SIZE_64 ){ throw new IllegalArgumentException("Invalid config size = " + size); } this.configSize.set(size); size=size-4; mValuesContainer.setSize(size); valuesChanged(); } public int getConfigSize(){ return this.configSize.get(); } public boolean trimToSize(int size){ int current=getConfigSize(); if(current==size){ return true; } if( size != SIZE_16 && size != SIZE_28 && size != SIZE_32 && size != SIZE_36 && size != SIZE_48 && size != SIZE_56 && size != SIZE_64 ){ return false; } if(current> 7) & 1) == 1)) { int first = b1 & 0x1F; int x=((b1 & 0xE0) >> 5); int y=((b0 & 0x03) << 3); int second = x + y; int third = (b0 & 0x7C) >> 2; return new char[] { (char) (first + base), (char) (second + base), (char) (third + base) }; } return new char[] { (char) b0, (char) b1 }; } private static byte[] packLanguageOrRegion(char[] chs){ if(chs==null || chs.length<2){ return new byte[2]; } if(chs.length==2 || chs.length>3){ byte[] result=new byte[2]; result[0]=(byte) chs[0]; result[1]=(byte) chs[1]; return result; } int base=getBase(chs[0]); int first=chs[0] - base; int second=chs[1] - base; int third=chs[2] - base; int b1Right=first & 0x1F; int b0Left=third; b0Left=b0Left << 2; int b1Left=second & 7; b1Left=b1Left<<5; int b1=(b1Left | b1Right); int b0Right=second >> 3; b0Right=b0Right & 0x3; int b0=(b0Left | b0Right); return new byte[] { (byte) b0, (byte) b1 }; } private static int getBase(char ch){ int result=ch; if(ch>='0' && ch<='9'){ result='0'; }else if(ch>='a' && ch<='z'){ result='a'; }else if(ch>='A' && ch<='Z'){ result='A'; } result=result+32; return result; } private static byte[] toByteArray(char[] chs, int len){ byte[] bts=new byte[len]; if(chs==null){ return bts; } int sz=chs.length; for(int i=0; i