Skip to content

Commit

Permalink
rework styled strings
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Feb 3, 2024
1 parent 6087395 commit 216cacb
Show file tree
Hide file tree
Showing 30 changed files with 1,206 additions and 1,010 deletions.
38 changes: 28 additions & 10 deletions src/main/java/com/reandroid/arsc/array/StringArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.reandroid.arsc.item.IntegerItem;
import com.reandroid.arsc.item.StringItem;
import com.reandroid.arsc.item.StyleItem;
import com.reandroid.arsc.pool.StringPool;
import com.reandroid.json.JSONConvert;
import com.reandroid.json.JSONArray;
Expand Down Expand Up @@ -137,6 +138,13 @@ public boolean isUtf8() {
protected void refreshChildes(){
// Not required
}
private StyleArray getStyleArray(){
StringPool<?> stringPool = getParentInstance(StringPool.class);
if(stringPool != null){
return stringPool.getStyleArray();
}
return null;
}
// Only styled strings
@Override
public JSONArray toJson() {
Expand Down Expand Up @@ -169,16 +177,26 @@ public JSONArray toJson(boolean styledOnly) {
// Only styled strings
@Override
public void fromJson(JSONArray json) {
throw new IllegalArgumentException(getClass().getSimpleName()+".fromJson() NOT implemented");
}

public static final Comparator<StringItem> COMPARATOR = (stringItem1, stringItem2) -> {
if(stringItem1 == stringItem2){
return 0;
int length;
if(json != null){
length = json.length();
}else {
length = 0;
}
if(stringItem1 == null){
return 1;
setChildesCount(length);
StyleArray styleArray = getStyleArray();
if(styleArray == null){
throw new NullPointerException("Null StyleArray");
}
return stringItem1.compareTo(stringItem2);
};
styleArray.setChildesCount(length);
if(length == 0){
return;
}
for(int i = 0; i < length; i++){
JSONObject jsonObject = json.getJSONObject(i);
StringItem stringItem = get(i);
StyleItem styleItem = styleArray.get(i);
stringItem.fromJson(jsonObject, styleItem);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/arsc/array/StyleArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected void onStringShifted(int index){
styleItem = newInstance();
setItem(index, styleItem);
styleItem.setNull(true);
styleItem.onDataLoaded();
styleItem.linkStringsInternal();
}
@Override
public void clearChildes(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.reandroid.arsc.chunk.PackageBlock;
import com.reandroid.arsc.model.ResourceLibrary;
import com.reandroid.xml.XMLUtil;
import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;
Expand Down Expand Up @@ -274,17 +275,7 @@ public void cdsect(String text) throws IOException, IllegalArgumentException, Il

@Override
public void entityRef(String text) throws IOException, IllegalArgumentException, IllegalStateException {
String decoded;
if("lt".equals(text)){
decoded = "<";
}else if("gt".equals(text)){
decoded = ">";
}else if("amp".equals(text)){
decoded = "&";
}else {
decoded = text;
}
appendText(decoded);
appendText(XMLUtil.decodeEntityRef(text));
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/reandroid/arsc/io/BlockReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public short readShort() throws IOException {
seek(pos);
return toShort(bts, 0);
}
public int readInteger() throws IOException {
int pos = getPosition();
byte[] bytes = new byte[4];
readFully(bytes);
seek(pos);
return toInt(bytes, 0);
}
public SpecHeader readSpecHeader() throws IOException{
SpecHeader specHeader = new SpecHeader();
if(available() < specHeader.countBytes()){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.reandroid.dex.base;
package com.reandroid.arsc.item;

import com.reandroid.arsc.base.Block;
import com.reandroid.arsc.item.BlockItem;
import com.reandroid.arsc.item.IndirectItem;
import com.reandroid.arsc.item.IntegerReference;

public class IndirectInteger extends IndirectItem<BlockItem> implements IntegerReference {
public IndirectInteger(BlockItem blockItem, int offset){
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/reandroid/arsc/item/StringItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.reandroid.arsc.coder.XmlSanitizer;
import com.reandroid.arsc.io.BlockReader;
import com.reandroid.arsc.pool.StringPool;
import com.reandroid.utils.ObjectsUtil;
import com.reandroid.utils.StringsUtil;
import com.reandroid.utils.collection.ComputeIterator;
import com.reandroid.utils.collection.EmptyIterator;
Expand Down Expand Up @@ -290,7 +291,7 @@ public boolean hasStyle(){
if(styleItem==null){
return false;
}
return styleItem.getSpanInfoList().size()>0;
return styleItem.size()>0;
}
public StyleItem getStyle(){
StringPool<?> stringPool = getParentInstance(StringPool.class);
Expand Down Expand Up @@ -355,10 +356,12 @@ public JSONObject toJson() {
}
@Override
public void fromJson(JSONObject json) {
String str = json.getString(NAME_string);
set(str);
throw new IllegalArgumentException("Not implemented");
}
public void fromJson(JSONObject json, StyleItem styleItem) {
set(json.getString(NAME_string));
styleItem.fromJson(json.getJSONObject(NAME_style));
}
@Override
public String toString(){
String xml = getXml();
Expand Down Expand Up @@ -510,6 +513,6 @@ private static byte[] addBytes(byte[] bts1, byte[] bts2, byte[] bts3){
private static final CharsetDecoder UTF16LE_DECODER = StandardCharsets.UTF_16LE.newDecoder();
private static final CharsetDecoder DECODER_3B = ThreeByteCharsetDecoder.INSTANCE;

public static final String NAME_string="string";
public static final String NAME_style="style";
public static final String NAME_string = ObjectsUtil.of("string");
public static final String NAME_style = ObjectsUtil.of("style");
}
Loading

0 comments on commit 216cacb

Please sign in to comment.