Search in sources :

Example 1 with Asset

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

the class ShadowArscAssetManager9 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);
}
Also used : ApkAssetsCookie(org.robolectric.res.android.ApkAssetsCookie) CppAssetManager2(org.robolectric.res.android.CppAssetManager2) FileNotFoundException(java.io.FileNotFoundException) Asset(org.robolectric.res.android.Asset) Implementation(org.robolectric.annotation.Implementation)

Example 2 with Asset

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

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

the class ShadowArscAssetManager10 method nativeAssetReadChar.

// static jint NativeAssetReadChar(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) {
@Implementation(minSdk = P)
protected static int nativeAssetReadChar(long asset_ptr) {
    Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr);
    byte[] b = new byte[1];
    int res = asset.read(b, 1);
    return res == 1 ? (int) (b[0]) & 0xff : -1;
}
Also used : Asset(org.robolectric.res.android.Asset) Implementation(org.robolectric.annotation.Implementation)

Example 4 with Asset

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

the class ShadowArscAssetManager10 method nativeOpenNonAssetFd.

// static jobject NativeOpenNonAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie,
// jstring asset_path, jlongArray out_offsets) {
@Implementation(minSdk = P)
@Nullable
protected static ParcelFileDescriptor nativeOpenNonAssetFd(long ptr, int jcookie, @NonNull String asset_path, @NonNull long[] out_offsets) throws IOException {
    ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie);
    String asset_path_utf8 = asset_path;
    if (asset_path_utf8 == null) {
        // This will throw NPE.
        return null;
    }
    ATRACE_NAME(String.format("AssetManager::OpenNonAssetFd(%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 {
        asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM);
    }
    if (!isTruthy(asset)) {
        throw new FileNotFoundException(asset_path_utf8);
    }
    return ReturnParcelFileDescriptor(asset, out_offsets);
}
Also used : ApkAssetsCookie(org.robolectric.res.android.ApkAssetsCookie) CppAssetManager2(org.robolectric.res.android.CppAssetManager2) FileNotFoundException(java.io.FileNotFoundException) Asset(org.robolectric.res.android.Asset) Implementation(org.robolectric.annotation.Implementation) Nullable(android.annotation.Nullable)

Example 5 with Asset

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

the class ShadowAssetInputStream method createAssetInputStream.

static AssetInputStream createAssetInputStream(InputStream delegateInputStream, long assetPtr, AssetManager assetManager) {
    Asset asset = NATIVE_ASSET_REGISTRY.getNativeObject(assetPtr);
    AssetInputStream ais = ReflectionHelpers.callConstructor(AssetInputStream.class, from(AssetManager.class, assetManager), from(long.class, assetPtr));
    ShadowAssetInputStream sais = Shadow.extract(ais);
    if (sais instanceof ShadowLegacyAssetInputStream) {
        ShadowLegacyAssetInputStream slais = (ShadowLegacyAssetInputStream) sais;
        slais.setDelegate(delegateInputStream);
        slais.setNinePatch(asset.isNinePatch());
    }
    return ais;
}
Also used : AssetInputStream(android.content.res.AssetManager.AssetInputStream) AssetManager(android.content.res.AssetManager) Asset(org.robolectric.res.android.Asset)

Aggregations

Asset (org.robolectric.res.android.Asset)25 Implementation (org.robolectric.annotation.Implementation)24 FileNotFoundException (java.io.FileNotFoundException)16 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)11 HiddenApi (org.robolectric.annotation.HiddenApi)8 ApkAssetsCookie (org.robolectric.res.android.ApkAssetsCookie)6 CppAssetManager (org.robolectric.res.android.CppAssetManager)5 IOException (java.io.IOException)4 ResXMLTree (org.robolectric.res.android.ResXMLTree)4 DynamicRefTable (org.robolectric.res.android.DynamicRefTable)3 Ref (org.robolectric.res.android.Ref)3 Nullable (android.annotation.Nullable)2 AssetManager (android.content.res.AssetManager)1 AssetInputStream (android.content.res.AssetManager.AssetInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 FileTypedResource (org.robolectric.res.FileTypedResource)1 ResName (org.robolectric.res.ResName)1 AccessMode (org.robolectric.res.android.Asset.AccessMode)1 CppApkAssets (org.robolectric.res.android.CppApkAssets)1