use of org.robolectric.res.android.Ref 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.Ref in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeGetResourceIntArray.
// static jintArray NativeGetResourceIntArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint
// resid) {
@Implementation(minSdk = P)
@Nullable
protected static int[] nativeGetResourceIntArray(long ptr, @ArrayRes int resid) {
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
ResolvedBag bag = assetmanager.GetBag(resid);
if (bag == null) {
return null;
}
int[] array = new int[bag.entry_count];
// if (array == null) {
// return null;
// }
// reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(array, null));
int[] buffer = array;
for (int i = 0; i < bag.entry_count; i++) {
ResolvedBag.Entry entry = bag.entries[i];
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) {
// env.ReleasePrimitiveArrayCritical(array, buffer, JNI_ABORT);
return null;
}
if (value.get().dataType >= Res_value.TYPE_FIRST_INT && value.get().dataType <= Res_value.TYPE_LAST_INT) {
buffer[i] = (int) (value.get().data);
}
}
// env.ReleasePrimitiveArrayCritical(array, buffer, 0);
return array;
}
use of org.robolectric.res.android.Ref in project robolectric by robolectric.
the class ShadowArscAssetManager9 method ReturnParcelFileDescriptor.
static ParcelFileDescriptor ReturnParcelFileDescriptor(/* JNIEnv* env,*/
Asset asset, long[] out_offsets) throws FileNotFoundException {
final Ref<Long> start_offset = new Ref<>(0L);
final Ref<Long> length = new Ref<>(0L);
FileDescriptor fd = asset.openFileDescriptor(start_offset, length);
if (fd == null) {
throw new FileNotFoundException("This file can not be opened as a file descriptor; it is probably compressed");
}
long[] offsets = // reinterpret_cast<long*>(env.GetPrimitiveArrayCritical(out_offsets, 0));
out_offsets;
if (offsets == null) {
// close(fd);
return null;
}
offsets[0] = start_offset.get();
offsets[1] = length.get();
// env.ReleasePrimitiveArrayCritical(out_offsets, offsets, 0);
// jniCreateFileDescriptor(env, fd);
FileDescriptor file_desc = fd;
// return new ParcelFileDescriptor(file_desc);
return ParcelFileDescriptor.open(asset.getFile(), ParcelFileDescriptor.MODE_READ_ONLY);
}
use of org.robolectric.res.android.Ref in project robolectric by robolectric.
the class ShadowXmlBlock method nativeGetStyleAttribute.
@Implementation(minSdk = VERSION_CODES.LOLLIPOP)
protected static int nativeGetStyleAttribute(long state) {
ResXMLParser resXMLParser = getResXMLParser(state);
int idx = resXMLParser.indexOfStyle();
if (idx < 0) {
return 0;
}
final Ref<Res_value> valueRef = new Ref<>(new Res_value());
if (resXMLParser.getAttributeValue(idx, valueRef) < 0) {
return 0;
}
Res_value value = valueRef.get();
return value.dataType == org.robolectric.res.android.ResourceTypes.Res_value.TYPE_REFERENCE || value.dataType == org.robolectric.res.android.ResourceTypes.Res_value.TYPE_ATTRIBUTE ? value.data : 0;
}
use of org.robolectric.res.android.Ref in project robolectric by robolectric.
the class ShadowArscAssetManager method loadResourceValue.
@HiddenApi
@Implementation
protected int loadResourceValue(int ident, short density, TypedValue outValue, boolean resolve) {
if (outValue == null) {
throw new NullPointerException("outValue");
// return 0;
}
CppAssetManager am = assetManagerForJavaObject();
if (am == null) {
return 0;
}
final ResTable res = am.getResources();
final Ref<Res_value> value = new Ref<>(null);
final Ref<ResTable_config> config = new Ref<>(null);
final Ref<Integer> typeSpecFlags = new Ref<>(null);
int block = res.getResource(ident, value, false, density, typeSpecFlags, config);
if (kThrowOnBadId) {
if (block == BAD_INDEX) {
throw new IllegalStateException("Bad resource!");
// return 0;
}
}
final Ref<Integer> ref = new Ref<>(ident);
if (resolve) {
block = res.resolveReference(value, block, ref, typeSpecFlags, config);
if (kThrowOnBadId) {
if (block == BAD_INDEX) {
throw new IllegalStateException("Bad resource!");
// return 0;
}
}
}
if (block >= 0) {
// return copyValue(env, outValue, &res, value, ref, block, typeSpecFlags, &config);
return copyValue(outValue, res, value.get(), ref.get(), block, typeSpecFlags.get(), config.get());
}
return block;
}
Aggregations