You've already forked android_translation_layer
mirror of
https://gitlab.com/android_translation_layer/android_translation_layer.git
synced 2025-10-27 11:48:10 -07:00
use libandroidfw for resource XML parsing
androidfw is implemented in native code and has much better performance than ARSClib
This commit is contained in:
@@ -466,20 +466,10 @@ public final class AssetManager {
|
||||
*/
|
||||
public final XmlResourceParser openXmlResourceParser(int cookie,
|
||||
String fileName) throws IOException {
|
||||
/* XmlBlock block = openXmlBlockAsset(cookie, fileName);
|
||||
XmlResourceParser rp = block.newParser();
|
||||
block.close();
|
||||
return rp;*/
|
||||
|
||||
InputStream inStream = ClassLoader.getSystemClassLoader().getResourceAsStream(fileName);
|
||||
if (inStream == null) {
|
||||
return null;
|
||||
}
|
||||
ResXmlDocument resXmlDocument = new ResXmlDocument();
|
||||
resXmlDocument.readBytes(inStream);
|
||||
ResXmlPullParser xpp = new ResXmlPullParser();
|
||||
xpp.setResXmlDocument(resXmlDocument);
|
||||
return xpp;
|
||||
XmlBlock block = openXmlBlockAsset(cookie, fileName);
|
||||
XmlResourceParser rp = block.newParser();
|
||||
block.close();
|
||||
return rp;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -503,7 +493,7 @@ public final class AssetManager {
|
||||
* @param fileName Name of the asset to retrieve.
|
||||
*/
|
||||
/*package*/ final XmlBlock openXmlBlockAsset(int cookie, String fileName) throws IOException {
|
||||
int xmlBlock;
|
||||
long xmlBlock;
|
||||
synchronized (this) {
|
||||
if (!mOpen) {
|
||||
throw new RuntimeException("Assetmanager has been closed");
|
||||
@@ -765,17 +755,21 @@ public final class AssetManager {
|
||||
int defStyleAttr, int defStyleRes, AttributeSet set,
|
||||
int[] inAttrs, int[] outValues, int[] outIndices) {
|
||||
TypedValue value = new TypedValue();
|
||||
ResXmlPullParser parser = (ResXmlPullParser)set;
|
||||
XmlResourceParser parser = (XmlResourceParser)set;
|
||||
if (defStyleRes == 0 && theme != 0 && loadThemeAttributeValue(theme, defStyleAttr, value, true) >= 0)
|
||||
defStyleRes = value.data;
|
||||
if (defStyleRes == 0 && set != null) {
|
||||
ValueItem valueItem = parser.getAttribute(null, "style");
|
||||
if (valueItem != null) {
|
||||
value.type = valueItem.getType();
|
||||
value.data = valueItem.getData();
|
||||
if (theme != 0 && (value.type == TypedValue.TYPE_ATTRIBUTE))
|
||||
loadThemeAttributeValue(theme, value.data, value, true);
|
||||
defStyleRes = value.data;
|
||||
if (parser instanceof ResXmlPullParser) {
|
||||
ValueItem valueItem = ((ResXmlPullParser)parser).getAttribute(null, "style");
|
||||
if (valueItem != null) {
|
||||
value.type = valueItem.getType();
|
||||
value.data = valueItem.getData();
|
||||
if (theme != 0 && (value.type == TypedValue.TYPE_ATTRIBUTE))
|
||||
loadThemeAttributeValue(theme, value.data, value, true);
|
||||
defStyleRes = value.data;
|
||||
}
|
||||
} else {
|
||||
defStyleRes = parser.getStyleAttribute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -804,9 +798,14 @@ public final class AssetManager {
|
||||
value.assetCookie = -1;
|
||||
found = true;
|
||||
} else if (xmlCache.containsKey(resId)) {
|
||||
ValueItem valueItem = parser.getResXmlAttributeAt(xmlCache.get(resId));
|
||||
value.type = valueItem.getType();
|
||||
value.data = valueItem.getData();
|
||||
if (parser instanceof ResXmlPullParser) {
|
||||
ValueItem valueItem = ((ResXmlPullParser)parser).getResXmlAttributeAt(xmlCache.get(resId));
|
||||
value.type = valueItem.getType();
|
||||
value.data = valueItem.getData();
|
||||
} else {
|
||||
value.type = XmlBlock.nativeGetAttributeDataType(((XmlBlock.Parser)parser).mParseState, xmlCache.get(resId));
|
||||
value.data = XmlBlock.nativeGetAttributeData(((XmlBlock.Parser)parser).mParseState, xmlCache.get(resId));
|
||||
}
|
||||
value.resourceId = 0;
|
||||
value.assetCookie = -1;
|
||||
if (value.type != TypedValue.TYPE_ATTRIBUTE)
|
||||
@@ -888,9 +887,7 @@ public final class AssetManager {
|
||||
boolean resolve);
|
||||
/*package*/ native static final void dumpTheme(long theme, int priority, String tag, String prefix);
|
||||
|
||||
private /*native*/ final int openXmlAssetNative(int cookie, String fileName) {
|
||||
return openAsset("../" + fileName, 0);
|
||||
}
|
||||
private native final long openXmlAssetNative(int cookie, String fileName);
|
||||
|
||||
private native final String[] getArrayStringResource(int arrayRes);
|
||||
private native final int[] getArrayStringInfo(int arrayRes);
|
||||
|
||||
@@ -1376,7 +1376,7 @@ public class Resources {
|
||||
// To support generic XML files we will need to manually parse
|
||||
// out the attributes from the XML file (applying type information
|
||||
// contained in the resources and such).
|
||||
ResXmlPullParser parser = (ResXmlPullParser)set;
|
||||
XmlResourceParser parser = (XmlResourceParser)set;
|
||||
mAssets.applyStyle(theme, defStyleAttr, defStyleRes,
|
||||
set, attrs, array.mData, array.mIndices);
|
||||
array.mRsrcs = attrs;
|
||||
@@ -1495,7 +1495,7 @@ public class Resources {
|
||||
// To support generic XML files we will need to manually parse
|
||||
// out the attributes from the XML file (applying type information
|
||||
// contained in the resources and such).
|
||||
ResXmlPullParser parser = (ResXmlPullParser)set;
|
||||
XmlResourceParser parser = (XmlResourceParser)set;
|
||||
mAssets.applyStyle(0, 0, 0,
|
||||
set, attrs, array.mData, array.mIndices);
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ import java.util.Arrays;
|
||||
*/
|
||||
public class TypedArray {
|
||||
private final Resources mResources;
|
||||
/*package*/ ResXmlPullParser mXml;
|
||||
/*package*/ XmlResourceParser mXml;
|
||||
/*package*/ int[] mRsrcs;
|
||||
/*package*/ int[] mData;
|
||||
/*package*/ int[] mIndices;
|
||||
@@ -156,7 +156,12 @@ public class TypedArray {
|
||||
if (type == TypedValue.TYPE_STRING) {
|
||||
final int cookie = data[index + AssetManager.STYLE_ASSET_COOKIE];
|
||||
if (cookie < 0) {
|
||||
return mXml.getResXmlDocument().getStringPool().get(data[index + AssetManager.STYLE_DATA]).get();
|
||||
if (mXml instanceof ResXmlPullParser)
|
||||
return ((ResXmlPullParser )mXml).getResXmlDocument().getStringPool().get(data[index + AssetManager.STYLE_DATA]).get();
|
||||
else {
|
||||
Thread.dumpStack();
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -695,9 +700,15 @@ public class TypedArray {
|
||||
final int cookie = data[index + AssetManager.STYLE_ASSET_COOKIE];
|
||||
if (cookie < 0) {
|
||||
if (mXml != null) {
|
||||
ResXmlString xmlString = mXml.getResXmlDocument().getStringPool().get(data[index + AssetManager.STYLE_DATA]);
|
||||
if (xmlString != null)
|
||||
return xmlString.get();
|
||||
if (mXml instanceof ResXmlPullParser) {
|
||||
ResXmlString xmlString = ((ResXmlPullParser)mXml).getResXmlDocument().getStringPool().get(data[index + AssetManager.STYLE_DATA]);
|
||||
if (xmlString != null)
|
||||
return xmlString.get();
|
||||
} else {
|
||||
CharSequence string = ((XmlBlock.Parser)mXml).getPooledString(data[index + AssetManager.STYLE_DATA]);
|
||||
if (string != null)
|
||||
return string;
|
||||
}
|
||||
}
|
||||
if (data[index + AssetManager.STYLE_RESOURCE_ID] != 0) {
|
||||
return mResources.mAssets.getResourceText(data[index + AssetManager.STYLE_RESOURCE_ID]);
|
||||
|
||||
@@ -72,7 +72,7 @@ final class XmlBlock {
|
||||
}
|
||||
|
||||
/*package*/ final class Parser implements XmlResourceParser {
|
||||
Parser(int parseState, XmlBlock block) {
|
||||
Parser(long parseState, XmlBlock block) {
|
||||
mParseState = parseState;
|
||||
mBlock = block;
|
||||
block.mOpenCount++;
|
||||
@@ -168,8 +168,7 @@ final class XmlBlock {
|
||||
return id >= 0 ? mStrings.get(id).toString() : "";
|
||||
}
|
||||
public String getName() {
|
||||
int id = nativeGetName(mParseState);
|
||||
return id >= 0 ? mStrings.get(id).toString() : null;
|
||||
return nativeGetName(mParseState);
|
||||
}
|
||||
public String getAttributeNamespace(int index) {
|
||||
int id = nativeGetAttributeNamespace(mParseState, index);
|
||||
@@ -200,11 +199,9 @@ final class XmlBlock {
|
||||
return mEventType == START_TAG ? nativeGetAttributeCount(mParseState) : -1;
|
||||
}
|
||||
public String getAttributeValue(int index) {
|
||||
int id = nativeGetAttributeStringValue(mParseState, index);
|
||||
if (DEBUG)
|
||||
System.out.println("getAttributeValue of " + index + " = " + id);
|
||||
if (id >= 0)
|
||||
return mStrings.get(id).toString();
|
||||
String value = nativeGetAttributeStringValue(mParseState, index);
|
||||
if (value != null)
|
||||
return value;
|
||||
|
||||
// May be some other type... check and try to convert if so.
|
||||
int t = nativeGetAttributeDataType(mParseState, index);
|
||||
@@ -423,8 +420,7 @@ final class XmlBlock {
|
||||
return id >= 0 ? mStrings.get(id).toString() : null;
|
||||
}
|
||||
public String getClassAttribute() {
|
||||
int id = nativeGetClassAttribute(mParseState);
|
||||
return id >= 0 ? mStrings.get(id).toString() : null;
|
||||
return nativeGetClassAttribute(mParseState);
|
||||
}
|
||||
|
||||
public int getIdAttributeResourceValue(int defaultValue) {
|
||||
@@ -451,10 +447,12 @@ final class XmlBlock {
|
||||
}
|
||||
|
||||
/*package*/ final CharSequence getPooledString(int id) {
|
||||
return mStrings.get(id);
|
||||
if (id < 0)
|
||||
return null;
|
||||
return nativeGetPooledString(mParseState, id);
|
||||
}
|
||||
|
||||
/*package*/ int mParseState;
|
||||
/*package*/ long mParseState;
|
||||
private final XmlBlock mBlock;
|
||||
private boolean mStarted = false;
|
||||
private boolean mDecNextDepth = false;
|
||||
@@ -472,14 +470,15 @@ final class XmlBlock {
|
||||
* are doing! The given native object must exist for the entire lifetime
|
||||
* of this newly creating XmlBlock.
|
||||
*/
|
||||
XmlBlock(AssetManager assets, int xmlBlock) {
|
||||
XmlBlock(AssetManager assets, long xmlBlock) {
|
||||
mAssets = assets;
|
||||
mNative = xmlBlock;
|
||||
mStrings = new StringBlock(nativeGetStringBlock(xmlBlock), false);
|
||||
// mStrings = new StringBlock(nativeGetStringBlock(xmlBlock), false);
|
||||
mStrings = null;
|
||||
}
|
||||
|
||||
private final AssetManager mAssets;
|
||||
private final int mNative;
|
||||
private final long mNative;
|
||||
/*package*/ final StringBlock mStrings;
|
||||
private boolean mOpen = true;
|
||||
private int mOpenCount = 1;
|
||||
@@ -487,26 +486,27 @@ final class XmlBlock {
|
||||
private static final native int nativeCreate(byte[] data,
|
||||
int offset,
|
||||
int size);
|
||||
private static final native int nativeGetStringBlock(int obj);
|
||||
private static final native int nativeGetStringBlock(long obj);
|
||||
|
||||
private static final native int nativeCreateParseState(int obj);
|
||||
/*package*/ static final native int nativeNext(int state);
|
||||
private static final native int nativeGetNamespace(int state);
|
||||
/*package*/ static final native int nativeGetName(int state);
|
||||
private static final native int nativeGetText(int state);
|
||||
private static final native int nativeGetLineNumber(int state);
|
||||
private static final native int nativeGetAttributeCount(int state);
|
||||
private static final native int nativeGetAttributeNamespace(int state, int idx);
|
||||
private static final native int nativeGetAttributeName(int state, int idx);
|
||||
private static final native int nativeGetAttributeResource(int state, int idx);
|
||||
private static final native int nativeGetAttributeDataType(int state, int idx);
|
||||
private static final native int nativeGetAttributeData(int state, int idx);
|
||||
private static final native int nativeGetAttributeStringValue(int state, int idx);
|
||||
private static final native int nativeGetIdAttribute(int state);
|
||||
private static final native int nativeGetClassAttribute(int state);
|
||||
private static final native int nativeGetStyleAttribute(int state);
|
||||
private static final native int nativeGetAttributeIndex(int state, String namespace, String name);
|
||||
private static final native void nativeDestroyParseState(int state);
|
||||
private static final native long nativeCreateParseState(long obj);
|
||||
/*package*/ static final native int nativeNext(long state);
|
||||
private static final native int nativeGetNamespace(long state);
|
||||
/*package*/ static final native String nativeGetName(long state);
|
||||
private static final native int nativeGetText(long state);
|
||||
private static final native int nativeGetLineNumber(long state);
|
||||
private static final native int nativeGetAttributeCount(long state);
|
||||
private static final native int nativeGetAttributeNamespace(long state, int idx);
|
||||
private static final native int nativeGetAttributeName(long state, int idx);
|
||||
private static final native int nativeGetAttributeResource(long state, int idx);
|
||||
static final native int nativeGetAttributeDataType(long state, int idx);
|
||||
static final native int nativeGetAttributeData(long state, int idx);
|
||||
private static final native String nativeGetAttributeStringValue(long state, int idx);
|
||||
private static final native int nativeGetIdAttribute(long state);
|
||||
private static final native String nativeGetClassAttribute(long state);
|
||||
private static final native int nativeGetStyleAttribute(long state);
|
||||
private static final native int nativeGetAttributeIndex(long state, String namespace, String name);
|
||||
private static final native void nativeDestroyParseState(long state);
|
||||
private static final native String nativeGetPooledString(long state, int id);
|
||||
|
||||
private static final native void nativeDestroy(int obj);
|
||||
private static final native void nativeDestroy(long obj);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user