use of org.robolectric.res.android.CppAssetManager2 in project robolectric by robolectric.
the class ShadowArscAssetManager9 method nativeList.
// static jobjectArray NativeList(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring path) {
@Implementation(minSdk = P)
@Nullable
protected static String[] nativeList(long ptr, @NonNull String path) throws IOException {
String path_utf8 = path;
if (path_utf8 == null) {
// This will throw NPE.
return null;
}
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
AssetDir asset_dir = assetmanager.OpenDir(path_utf8);
if (asset_dir == null) {
throw new FileNotFoundException(path_utf8);
}
int file_count = asset_dir.getFileCount();
// env.NewObjectArray(file_count, g_stringClass, null);
String[] array = new String[file_count];
for (int i = 0; i < file_count; i++) {
String java_string = asset_dir.getFileName(i).string();
// 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.CppAssetManager2 in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeOpenNonAsset.
// static jlong NativeOpenNonAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie,
// jstring asset_path, jint access_mode) {
@Implementation(minSdk = P)
protected static long nativeOpenNonAsset(long ptr, int jcookie, @NonNull String asset_path, int access_mode) throws FileNotFoundException {
ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie);
String asset_path_utf8 = asset_path;
if (asset_path_utf8 == null) {
// This will throw NPE.
return 0;
}
ATRACE_NAME(String.format("AssetManager::OpenNonAsset(%s)", asset_path_utf8));
if (access_mode != Asset.AccessMode.ACCESS_UNKNOWN.mode() && access_mode != Asset.AccessMode.ACCESS_RANDOM.mode() && access_mode != Asset.AccessMode.ACCESS_STREAMING.mode() && access_mode != Asset.AccessMode.ACCESS_BUFFER.mode()) {
throw new IllegalArgumentException("Bad access mode");
}
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
Asset asset;
if (cookie.intValue() != kInvalidCookie) {
asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.fromInt(access_mode));
} else {
asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.fromInt(access_mode));
}
if (!isTruthy(asset)) {
throw new FileNotFoundException(asset_path_utf8);
}
return Registries.NATIVE_ASSET_REGISTRY.register(asset);
}
use of org.robolectric.res.android.CppAssetManager2 in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeGetResourceArray.
// static jint NativeGetResourceArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid,
// jintArray out_data) {
@Implementation(minSdk = P)
protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, @NonNull int[] out_data) {
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
ResolvedBag bag = assetmanager.GetBag(resid);
if (bag == null) {
return -1;
}
int out_data_length = out_data.length;
if ((int) (bag.entry_count) > out_data_length * STYLE_NUM_ENTRIES) {
throw new IllegalArgumentException("Input array is not large enough");
}
int[] buffer = // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(out_data, null));
out_data;
if (buffer == null) {
return -1;
}
int[] cursor = buffer;
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<>(new ResTable_config());
selected_config.get().density = 0;
final Ref<Integer> flags = new Ref<>(bag.type_spec_flags);
final Ref<Integer> ref = new Ref<>(0);
ApkAssetsCookie cookie = assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref);
if (cookie.intValue() == kInvalidCookie) {
// env.ReleasePrimitiveArrayCritical(out_data, buffer, JNI_ABORT);
return -1;
}
// Deal with the special @null value -- it turns back to TYPE_NULL.
if (value.get().dataType == Res_value.TYPE_REFERENCE && value.get().data == 0) {
value.set(Res_value.NULL_VALUE);
}
int offset = i * STYLE_NUM_ENTRIES;
cursor[offset + STYLE_TYPE] = (int) (value.get().dataType);
cursor[offset + STYLE_DATA] = (int) (value.get().data);
cursor[offset + STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(cookie);
cursor[offset + STYLE_RESOURCE_ID] = (int) (ref.get());
cursor[offset + STYLE_CHANGING_CONFIGURATIONS] = (int) (flags.get());
cursor[offset + STYLE_DENSITY] = (int) (selected_config.get().density);
// cursor += STYLE_NUM_ENTRIES;
}
// env.ReleasePrimitiveArrayCritical(out_data, buffer, 0);
return (int) (bag.entry_count);
}
use of org.robolectric.res.android.CppAssetManager2 in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeGetSizeConfigurations.
// static jobjectArray NativeGetSizeConfigurations(JNIEnv* env, jclass /*clazz*/, jlong ptr) {
@Implementation(minSdk = P)
@Nullable
protected static Configuration[] nativeGetSizeConfigurations(long ptr) {
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
Set<ResTable_config> configurations = assetmanager.GetResourceConfigurations(true, /*exclude_system*/
false);
Configuration[] array = new Configuration[configurations.size()];
// env.NewObjectArray(configurations.size(), gConfigurationOffsets.classObject, null);
// if (array == null) {
// return null;
// }
int idx = 0;
for (ResTable_config configuration : configurations) {
Configuration java_configuration = ConstructConfigurationObject(configuration);
// if (java_configuration == null) {
// return null;
// }
// env.SetObjectArrayElement(array, idx++, java_configuration);
array[idx++] = java_configuration;
// env.DeleteLocalRef(java_configuration);
}
return array;
}
use of org.robolectric.res.android.CppAssetManager2 in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeGetStyleAttributes.
// static jintArray NativeGetStyleAttributes(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid)
// {
@Implementation(minSdk = P)
@Nullable
@AttrRes
protected static int[] nativeGetStyleAttributes(long ptr, @StyleRes int resid) {
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
ResolvedBag bag = assetmanager.GetBag(resid);
if (bag == null) {
return null;
}
int[] array = new int[bag.entry_count];
for (int i = 0; i < bag.entry_count; i++) {
int attr_resid = bag.entries[i].key;
// env.SetIntArrayRegion(array, i, 1, &attr_resid);
array[i] = attr_resid;
}
return array;
}
Aggregations