use of org.robolectric.res.android.ResStringPool in project robolectric by robolectric.
the class ShadowArscAssetManager9 method nativeGetResourceStringArray.
// static jobjectArray NativeGetResourceStringArray(JNIEnv* env, jclass /*clazz*/, jlong ptr,
// jint resid) {
@Implementation(minSdk = P)
@Nullable
protected static String[] nativeGetResourceStringArray(long ptr, @ArrayRes int resid) {
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
ResolvedBag bag = assetmanager.GetBag(resid);
if (bag == null) {
return null;
}
String[] array = new String[bag.entry_count];
if (array == null) {
return null;
}
for (int i = 0; i < bag.entry_count; i++) {
ResolvedBag.Entry entry = bag.entries[i];
// Resolve any references to their final value.
final Ref<Res_value> value = new Ref<>(entry.value);
final Ref<ResTable_config> selected_config = new Ref<>(null);
final Ref<Integer> flags = new Ref<>(0);
final Ref<Integer> ref = new Ref<>(0);
ApkAssetsCookie cookie = assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref);
if (cookie.intValue() == kInvalidCookie) {
return null;
}
if (value.get().dataType == Res_value.TYPE_STRING) {
CppApkAssets apk_assets = assetmanager.GetApkAssets().get(cookie.intValue());
ResStringPool pool = apk_assets.GetLoadedArsc().GetStringPool();
String java_string = null;
int str_len;
String str_utf8 = pool.stringAt(value.get().data);
if (str_utf8 != null) {
java_string = str_utf8;
} else {
String str_utf16 = pool.stringAt(value.get().data);
java_string = str_utf16;
}
// // Check for errors creating the strings (if malformed or no memory).
// if (env.ExceptionCheck()) {
// return null;
// }
// env.SetObjectArrayElement(array, i, java_string);
array[i] = java_string;
// If we have a large amount of string in our array, we might overflow the
// local reference table of the VM.
// env.DeleteLocalRef(java_string);
}
}
return array;
}
use of org.robolectric.res.android.ResStringPool in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeGetResourceStringArray.
// static jobjectArray NativeGetResourceStringArray(JNIEnv* env, jclass /*clazz*/, jlong ptr,
// jint resid) {
@Implementation(minSdk = P)
@Nullable
protected static String[] nativeGetResourceStringArray(long ptr, @ArrayRes int resid) {
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
ResolvedBag bag = assetmanager.GetBag(resid);
if (bag == null) {
return null;
}
String[] array = new String[bag.entry_count];
if (array == null) {
return null;
}
for (int i = 0; i < bag.entry_count; i++) {
ResolvedBag.Entry entry = bag.entries[i];
// Resolve any references to their final value.
final Ref<Res_value> value = new Ref<>(entry.value);
final Ref<ResTable_config> selected_config = new Ref<>(null);
final Ref<Integer> flags = new Ref<>(0);
final Ref<Integer> ref = new Ref<>(0);
ApkAssetsCookie cookie = assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref);
if (cookie.intValue() == kInvalidCookie) {
return null;
}
if (value.get().dataType == Res_value.TYPE_STRING) {
CppApkAssets apk_assets = assetmanager.GetApkAssets().get(cookie.intValue());
ResStringPool pool = apk_assets.GetLoadedArsc().GetStringPool();
String java_string = null;
String str_utf8 = pool.stringAt(value.get().data);
if (str_utf8 != null) {
java_string = str_utf8;
} else {
String str_utf16 = pool.stringAt(value.get().data);
java_string = str_utf16;
}
// // Check for errors creating the strings (if malformed or no memory).
// if (env.ExceptionCheck()) {
// return null;
// }
// env.SetObjectArrayElement(array, i, java_string);
array[i] = java_string;
// If we have a large amount of string in our array, we might overflow the
// local reference table of the VM.
// env.DeleteLocalRef(java_string);
}
}
return array;
}
use of org.robolectric.res.android.ResStringPool in project robolectric by robolectric.
the class ShadowArscAssetManager method getArrayStringResource.
@HiddenApi
@Implementation
protected final String[] getArrayStringResource(int arrayResId) {
CppAssetManager am = assetManagerForJavaObject();
if (am == null) {
return null;
}
final ResTable res = am.getResources();
final Ref<bag_entry[]> startOfBag = new Ref<>(null);
final int N = res.lockBag(arrayResId, startOfBag);
if (N < 0) {
return null;
}
String[] array = new String[N];
final Ref<Res_value> valueRef = new Ref<>(null);
final bag_entry[] bag = startOfBag.get();
int strLen = 0;
for (int i = 0; ((int) i) < N; i++) {
valueRef.set(bag[i].map.value);
String str = null;
// Take care of resolving the found resource to its final value.
int block = res.resolveReference(valueRef, bag[i].stringBlock, null);
if (kThrowOnBadId) {
if (block == BAD_INDEX) {
throw new IllegalStateException("Bad resource!");
}
}
if (valueRef.get().dataType == DataType.STRING.code()) {
final ResStringPool pool = res.getTableStringBlock(block);
str = pool.stringAt(valueRef.get().data);
// }
if (str == null) {
res.unlockBag(startOfBag);
return null;
}
array[i] = str;
// str is not NULL at that point, otherwise ExceptionCheck would have been true.
// If we have a large amount of strings in our array, we might
// overflow the local reference table of the VM.
// env.DeleteLocalRef(str);
}
}
res.unlockBag(startOfBag);
return array;
}
use of org.robolectric.res.android.ResStringPool in project robolectric by robolectric.
the class ShadowStringBlock method nativeGetStyle.
@Implementation(minSdk = LOLLIPOP)
protected static int[] nativeGetStyle(long obj, int idx) {
ResStringPool osb = ResStringPool.getNativeObject(obj);
ResStringPool_span spans = osb.styleAt(idx);
if (spans == null) {
return null;
}
ResStringPool_span pos = spans;
int num = 0;
while (pos.name.index != ResStringPool_span.END) {
num++;
// pos++;
pos = new ResStringPool_span(pos.myBuf(), pos.myOffset() + ResStringPool_span.SIZEOF);
}
if (num == 0) {
return null;
}
// jintArray array = env->NewIntArray((num*sizeof(ResStringPool_span))/sizeof(jint));
int[] array = new int[num * ResStringPool_span.SIZEOF / SIZEOF_INT];
if (array == null) {
// NewIntArray already threw OutOfMemoryError.
return null;
}
num = 0;
final int numInts = ResStringPool_span.SIZEOF / SIZEOF_INT;
while (spans.name.index != ResStringPool_span.END) {
// env->SetIntArrayRegion(array,
// num*numInts, numInts,
// (jint*)spans);
setIntArrayRegion(array, num, numInts, spans);
// spans++;
spans = new ResStringPool_span(spans.myBuf(), spans.myOffset() + ResStringPool_span.SIZEOF);
num++;
}
return array;
}
Aggregations