Search in sources :

Example 11 with Asset

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

the class ShadowFontBuilder method nGetNativeAsset.

// transliterated from frameworks/base/core/jni/android/graphics/fonts/Font.cpp
@Implementation(maxSdk = Q)
protected static long nGetNativeAsset(AssetManager assetMgr, String path, boolean isAsset, int cookie) {
    // NPE_CHECK_RETURN_ZERO(env, assetMgr);
    Preconditions.checkNotNull(assetMgr);
    // NPE_CHECK_RETURN_ZERO(env, path);
    Preconditions.checkNotNull(path);
    // Guarded<AssetManager2>* mgr = AssetManagerForJavaObject(env, assetMgr);
    CppAssetManager2 mgr = ShadowArscAssetManager10.AssetManagerForJavaObject(assetMgr);
    // if (mgr == nullptr) {
    if (mgr == null) {
        return 0;
    }
    // ScopedUtfChars str(env, path);
    // if (str.c_str() == nullptr) {
    // return 0;
    // }
    // std::unique_ptr<Asset> asset;
    Asset asset;
    {
        // ScopedLock<AssetManager2> locked_mgr(*mgr);
        if (isAsset) {
            // asset = locked_mgr->Open(str.c_str(), Asset::ACCESS_BUFFER);
            asset = mgr.Open(path, AccessMode.ACCESS_BUFFER);
        } else if (cookie > 0) {
            // Valid java cookies are 1-based, but AssetManager cookies are 0-based.
            // asset = locked_mgr->OpenNonAsset(str.c_str(), static_cast<ApkAssetsCookie>(cookie - 1), Asset::ACCESS_BUFFER);
            asset = mgr.OpenNonAsset(path, ApkAssetsCookie.forInt(cookie - 1), AccessMode.ACCESS_BUFFER);
        } else {
            // asset = locked_mgr->OpenNonAsset(str.c_str(), Asset::ACCESS_BUFFER);
            asset = mgr.OpenNonAsset(path, AccessMode.ACCESS_BUFFER);
        }
    }
    // return reinterpret_cast<jlong>(asset.release());
    return Registries.NATIVE_ASSET_REGISTRY.register(asset);
}
Also used : CppAssetManager2(org.robolectric.res.android.CppAssetManager2) Asset(org.robolectric.res.android.Asset) Implementation(org.robolectric.annotation.Implementation)

Example 12 with Asset

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

the class ShadowArscAssetManager method openAssetFd.

@HiddenApi
@Implementation
protected ParcelFileDescriptor openAssetFd(String fileName, long[] outOffsets) throws IOException {
    CppAssetManager am = assetManagerForJavaObject();
    ALOGV("openAssetFd in %s", am);
    String fileName8 = fileName;
    if (fileName8 == null) {
        return null;
    }
    Asset a = am.open(fileName8, Asset.AccessMode.ACCESS_RANDOM);
    if (a == null) {
        throw new FileNotFoundException(fileName8);
    }
    return returnParcelFileDescriptor(a, outOffsets);
}
Also used : CppAssetManager(org.robolectric.res.android.CppAssetManager) FileNotFoundException(java.io.FileNotFoundException) Asset(org.robolectric.res.android.Asset) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 13 with Asset

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

the class ShadowArscAssetManager method openAsset.

@HiddenApi
@Implementation
protected final Number openAsset(String fileName, int mode) throws FileNotFoundException {
    CppAssetManager am = assetManagerForJavaObject();
    ALOGV("openAsset in %s", am);
    String fileName8 = fileName;
    if (fileName8 == null) {
        throw new IllegalArgumentException("Empty file name");
    }
    if (mode != AccessMode.ACCESS_UNKNOWN.mode() && mode != AccessMode.ACCESS_RANDOM.mode() && mode != AccessMode.ACCESS_STREAMING.mode() && mode != AccessMode.ACCESS_BUFFER.mode()) {
        throw new IllegalArgumentException("Bad access mode");
    }
    Asset a = am.open(fileName8, AccessMode.fromInt(mode));
    if (a == null) {
        throw new FileNotFoundException(fileName8);
    }
    return RuntimeEnvironment.castNativePtr(Registries.NATIVE_ASSET_REGISTRY.register(a));
}
Also used : CppAssetManager(org.robolectric.res.android.CppAssetManager) FileNotFoundException(java.io.FileNotFoundException) Asset(org.robolectric.res.android.Asset) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 14 with Asset

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

the class ShadowArscAssetManager method readAsset.

@HiddenApi
@Implementation(minSdk = LOLLIPOP)
protected final int readAsset(long asset, byte[] bArray, int off, int len) throws IOException {
    Asset a = getAsset(asset);
    if (a == null || bArray == null) {
        throw new NullPointerException("asset");
    }
    if (len == 0) {
        return 0;
    }
    int bLen = bArray.length;
    if (off < 0 || off >= bLen || len < 0 || len > bLen || (off + len) > bLen) {
        throw new IndexOutOfBoundsException();
    }
    byte[] b = bArray;
    int res = a.read(b, off, len);
    if (res > 0)
        return res;
    if (res < 0) {
        throw new IOException();
    }
    return -1;
}
Also used : Asset(org.robolectric.res.android.Asset) IOException(java.io.IOException) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 15 with Asset

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

the class ShadowArscAssetManager method readAssetChar.

@HiddenApi
@Implementation(minSdk = LOLLIPOP)
protected final int readAssetChar(long asset) {
    Asset a = getAsset(asset);
    byte[] b = new byte[1];
    int res = a.read(b, 1);
    return res == 1 ? b[0] & 0xff : -1;
}
Also used : Asset(org.robolectric.res.android.Asset) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

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