Search in sources :

Example 1 with ResXMLTree

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

the class ShadowArscAssetManager9 method nativeOpenXmlAsset.

// static jlong NativeOpenXmlAsset(JNIEnv* env, jobject /*clazz*/, jlong ptr, jint jcookie,
// jstring asset_path) {
@Implementation(minSdk = P)
protected static long nativeOpenXmlAsset(long ptr, int jcookie, @NonNull String asset_path) 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::OpenXmlAsset(%s)", asset_path_utf8));
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Asset asset;
    if (cookie.intValue() != kInvalidCookie) {
        asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.ACCESS_RANDOM);
    } else {
        Ref<ApkAssetsCookie> cookieRef = new Ref<>(cookie);
        asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM, cookieRef);
        cookie = cookieRef.get();
    }
    if (!isTruthy(asset)) {
        throw new FileNotFoundException(asset_path_utf8);
    }
    // May be nullptr.
    DynamicRefTable dynamic_ref_table = assetmanager.GetDynamicRefTableForCookie(cookie);
    ResXMLTree xml_tree = new ResXMLTree(dynamic_ref_table);
    int err = xml_tree.setTo(asset.getBuffer(true), (int) asset.getLength(), true);
    if (err != NO_ERROR) {
        throw new FileNotFoundException("Corrupt XML binary file");
    }
    return NATIVE_RES_XML_TREES.register(xml_tree);
}
Also used : Ref(org.robolectric.res.android.Ref) ApkAssetsCookie(org.robolectric.res.android.ApkAssetsCookie) CppAssetManager2(org.robolectric.res.android.CppAssetManager2) FileNotFoundException(java.io.FileNotFoundException) ResXMLTree(org.robolectric.res.android.ResXMLTree) Asset(org.robolectric.res.android.Asset) DynamicRefTable(org.robolectric.res.android.DynamicRefTable) Implementation(org.robolectric.annotation.Implementation)

Example 2 with ResXMLTree

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

the class ShadowXmlBlock method nativeCreateParseState.

@Implementation(minSdk = VERSION_CODES.Q)
protected static long nativeCreateParseState(long obj, int resid) {
    ResXMLTree osb = Registries.NATIVE_RES_XML_TREES.getNativeObject(obj);
    // if (osb == NULL) {
    // jniThrowNullPointerException(env, NULL);
    // return 0;
    // }
    ResXMLParser st = new ResXMLParser(osb);
    // if (st == NULL) {
    // jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
    // return 0;
    // }
    st.setSourceResourceId(resid);
    st.restart();
    return Registries.NATIVE_RES_XML_PARSERS.register(st);
}
Also used : ResXMLTree(org.robolectric.res.android.ResXMLTree) ResXMLParser(org.robolectric.res.android.ResXMLParser) Implementation(org.robolectric.annotation.Implementation)

Example 3 with ResXMLTree

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

the class ShadowArscAssetManager method openXmlAssetNative.

// /*package*/@HiddenApi @Implementation public static final @NativeConfig
// int getThemeChangingConfigurations(long theme);
@HiddenApi
@Implementation
protected final Number openXmlAssetNative(int cookie, String fileName) throws FileNotFoundException {
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return RuntimeEnvironment.castNativePtr(0);
    }
    ALOGV("openXmlAsset in %s (Java object %s)\n", am, ShadowArscAssetManager.class);
    String fileName8 = fileName;
    if (fileName8 == null) {
        return RuntimeEnvironment.castNativePtr(0);
    }
    int assetCookie = cookie;
    Asset a;
    if (isTruthy(assetCookie)) {
        a = am.openNonAsset(assetCookie, fileName8, AccessMode.ACCESS_BUFFER);
    } else {
        final Ref<Integer> assetCookieRef = new Ref<>(assetCookie);
        a = am.openNonAsset(fileName8, AccessMode.ACCESS_BUFFER, assetCookieRef);
        assetCookie = assetCookieRef.get();
    }
    if (a == null) {
        throw new FileNotFoundException(fileName8);
    }
    final DynamicRefTable dynamicRefTable = am.getResources().getDynamicRefTableForCookie(assetCookie);
    ResXMLTree block = new ResXMLTree(dynamicRefTable);
    int err = block.setTo(a.getBuffer(true), (int) a.getLength(), true);
    a.close();
    if (err != NO_ERROR) {
        throw new FileNotFoundException("Corrupt XML binary file");
    }
    return RuntimeEnvironment.castNativePtr(Registries.NATIVE_RES_XML_TREES.register(block));
}
Also used : Ref(org.robolectric.res.android.Ref) CppAssetManager(org.robolectric.res.android.CppAssetManager) FileNotFoundException(java.io.FileNotFoundException) ResXMLTree(org.robolectric.res.android.ResXMLTree) Asset(org.robolectric.res.android.Asset) DynamicRefTable(org.robolectric.res.android.DynamicRefTable) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 4 with ResXMLTree

use of org.robolectric.res.android.ResXMLTree 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 5 with ResXMLTree

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

the class ShadowArscAssetManager10 method nativeOpenXmlAsset.

// static jlong NativeOpenXmlAsset(JNIEnv* env, jobject /*clazz*/, jlong ptr, jint jcookie,
// jstring asset_path) {
@Implementation(minSdk = P)
protected static long nativeOpenXmlAsset(long ptr, int jcookie, @NonNull String asset_path) 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::OpenXmlAsset(%s)", asset_path_utf8));
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Asset asset;
    if (cookie.intValue() != kInvalidCookie) {
        asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.ACCESS_RANDOM);
    } else {
        Ref<ApkAssetsCookie> cookieRef = new Ref<>(cookie);
        asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM, cookieRef);
        cookie = cookieRef.get();
    }
    if (!isTruthy(asset)) {
        throw new FileNotFoundException(asset_path_utf8);
    }
    // May be nullptr.
    DynamicRefTable dynamic_ref_table = assetmanager.GetDynamicRefTableForCookie(cookie);
    ResXMLTree xml_tree = new ResXMLTree(dynamic_ref_table);
    int err = xml_tree.setTo(asset.getBuffer(true), (int) asset.getLength(), true);
    if (err != NO_ERROR) {
        throw new FileNotFoundException("Corrupt XML binary file");
    }
    return NATIVE_RES_XML_TREES.register(xml_tree);
}
Also used : Ref(org.robolectric.res.android.Ref) ApkAssetsCookie(org.robolectric.res.android.ApkAssetsCookie) CppAssetManager2(org.robolectric.res.android.CppAssetManager2) FileNotFoundException(java.io.FileNotFoundException) ResXMLTree(org.robolectric.res.android.ResXMLTree) Asset(org.robolectric.res.android.Asset) DynamicRefTable(org.robolectric.res.android.DynamicRefTable) Implementation(org.robolectric.annotation.Implementation)

Aggregations

Implementation (org.robolectric.annotation.Implementation)7 ResXMLTree (org.robolectric.res.android.ResXMLTree)7 FileNotFoundException (java.io.FileNotFoundException)4 Asset (org.robolectric.res.android.Asset)4 DynamicRefTable (org.robolectric.res.android.DynamicRefTable)3 Ref (org.robolectric.res.android.Ref)3 ApkAssetsCookie (org.robolectric.res.android.ApkAssetsCookie)2 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)2 ResXMLParser (org.robolectric.res.android.ResXMLParser)2 HiddenApi (org.robolectric.annotation.HiddenApi)1 CppApkAssets (org.robolectric.res.android.CppApkAssets)1 CppAssetManager (org.robolectric.res.android.CppAssetManager)1