Search in sources :

Example 6 with CppApkAssets

use of org.robolectric.res.android.CppApkAssets in project robolectric by robolectric.

the class ShadowArscApkAssets9 method nativeOpenXml.

// static jlong NativeOpenXml(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring file_name) {
@Implementation
protected static long nativeOpenXml(long ptr, String file_name) throws FileNotFoundException {
    String path_utf8 = file_name;
    if (path_utf8 == null) {
        return 0;
    }
    CppApkAssets apk_assets = Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(ptr);
    Asset asset = apk_assets.Open(path_utf8, Asset.AccessMode.ACCESS_RANDOM);
    if (asset == null) {
        throw new FileNotFoundException(path_utf8);
    }
    // DynamicRefTable is only needed when looking up resource references. Opening an XML file
    // directly from an ApkAssets has no notion of proper resource references.
    // util.make_unique<ResXMLTree>(nullptr /*dynamicRefTable*/);
    ResXMLTree xml_tree = new ResXMLTree(null);
    int err = xml_tree.setTo(asset.getBuffer(true), (int) asset.getLength(), true);
    if (err != NO_ERROR) {
        throw new FileNotFoundException("Corrupt XML binary file");
    }
    // reinterpret_cast<jlong>(xml_tree.release());
    return Registries.NATIVE_RES_XML_TREES.register(xml_tree);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) ResXMLTree(org.robolectric.res.android.ResXMLTree) Asset(org.robolectric.res.android.Asset) CppApkAssets(org.robolectric.res.android.CppApkAssets) Implementation(org.robolectric.annotation.Implementation)

Example 7 with CppApkAssets

use of org.robolectric.res.android.CppApkAssets in project robolectric by robolectric.

the class ShadowArscApkAssets9 method nativeLoad.

// static jlong NativeLoad(JNIEnv* env, jclass /*clazz*/, jstring java_path, jboolean system,
// jboolean force_shared_lib, jboolean overlay) {
@Implementation
protected static long nativeLoad(String path, boolean system, boolean forceSharedLib, boolean overlay) throws IOException {
    if (path == null) {
        return 0;
    }
    ATRACE_NAME(String.format("LoadApkAssets(%s)", path));
    CppApkAssets apk_assets;
    try {
        if (overlay) {
            apk_assets = CppApkAssets.LoadOverlay(path, system);
        } else if (forceSharedLib) {
            apk_assets = CppApkAssets.LoadAsSharedLibrary(path, system);
        } else {
            apk_assets = CppApkAssets.Load(path, system);
        }
    } catch (OutOfMemoryError e) {
        OutOfMemoryError outOfMemoryError = new OutOfMemoryError("Failed to load " + path);
        outOfMemoryError.initCause(e);
        throw outOfMemoryError;
    }
    if (apk_assets == null) {
        String error_msg = String.format("Failed to load asset path %s", path);
        throw new IOException(error_msg);
    }
    return Registries.NATIVE_APK_ASSETS_REGISTRY.register(apk_assets);
}
Also used : IOException(java.io.IOException) CppApkAssets(org.robolectric.res.android.CppApkAssets) Implementation(org.robolectric.annotation.Implementation)

Example 8 with CppApkAssets

use of org.robolectric.res.android.CppApkAssets in project robolectric by robolectric.

the class ShadowArscAssetManager10 method nativeSetApkAssets.

// static void NativeSetApkAssets(JNIEnv* env, jclass /*clazz*/, jlong ptr,
// jobjectArray apk_assets_array, jboolean invalidate_caches) {
@Implementation(minSdk = P)
protected static void nativeSetApkAssets(long ptr, @NonNull android.content.res.ApkAssets[] apk_assets_array, boolean invalidate_caches) {
    ATRACE_NAME("AssetManager::SetApkAssets");
    int apk_assets_len = apk_assets_array.length;
    List<CppApkAssets> apk_assets = new ArrayList<>();
    // apk_assets.reserve(apk_assets_len);
    for (int i = 0; i < apk_assets_len; i++) {
        android.content.res.ApkAssets apkAssets = // env.GetObjectArrayElement(apk_assets_array, i);
        apk_assets_array[i];
        if (apkAssets == null) {
            throw new NullPointerException(String.format("ApkAssets at index %d is null", i));
        }
        long apk_assets_native_ptr = ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr();
        // if (env.ExceptionCheck()) {
        // return;
        // }
        apk_assets.add(Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr));
    }
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    assetmanager.SetApkAssets(apk_assets, invalidate_caches);
}
Also used : CppAssetManager2(org.robolectric.res.android.CppAssetManager2) ArrayList(java.util.ArrayList) CppApkAssets(org.robolectric.res.android.CppApkAssets) ApkAssets(android.content.res.ApkAssets) Implementation(org.robolectric.annotation.Implementation)

Aggregations

CppApkAssets (org.robolectric.res.android.CppApkAssets)8 Implementation (org.robolectric.annotation.Implementation)6 ApkAssets (android.content.res.ApkAssets)4 ArrayList (java.util.ArrayList)4 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)4 Nullable (android.annotation.Nullable)2 File (java.io.File)2 Path (java.nio.file.Path)2 ApkAssetsCookie (org.robolectric.res.android.ApkAssetsCookie)2 AssetPath (org.robolectric.res.android.AssetPath)2 ResolvedBag (org.robolectric.res.android.CppAssetManager2.ResolvedBag)2 Ref (org.robolectric.res.android.Ref)2 ResStringPool (org.robolectric.res.android.ResStringPool)2 ResTable_config (org.robolectric.res.android.ResTable_config)2 Res_value (org.robolectric.res.android.ResourceTypes.Res_value)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Asset (org.robolectric.res.android.Asset)1 ResXMLTree (org.robolectric.res.android.ResXMLTree)1