Search in sources :

Example 6 with Asset

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

the class ShadowArscAssetManager9 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 7 with Asset

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

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

the class ShadowArscAssetManager method openNonAssetFdNative.

@HiddenApi
@Implementation
protected ParcelFileDescriptor openNonAssetFdNative(int cookie, String fileName, long[] outOffsets) throws IOException {
    CppAssetManager am = assetManagerForJavaObject();
    ALOGV("openNonAssetFd in %s (Java object %s)", am, this);
    if (fileName == null) {
        return null;
    }
    Asset a = isTruthy(cookie) ? am.openNonAsset(cookie, fileName, Asset.AccessMode.ACCESS_RANDOM) : am.openNonAsset(fileName, Asset.AccessMode.ACCESS_RANDOM, null);
    if (a == null) {
        throw new FileNotFoundException(fileName);
    }
    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 9 with Asset

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

the class ShadowArscAssetManager9 method nativeOpenAsset.

// static jlong NativeOpenAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path,
// jint access_mode) {
@Implementation(minSdk = P)
protected static long nativeOpenAsset(long ptr, @NonNull String asset_path, int access_mode) throws FileNotFoundException {
    String asset_path_utf8 = asset_path;
    if (asset_path_utf8 == null) {
        // This will throw NPE.
        return 0;
    }
    ATRACE_NAME(String.format("AssetManager::OpenAsset(%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 = assetmanager.Open(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 : CppAssetManager2(org.robolectric.res.android.CppAssetManager2) FileNotFoundException(java.io.FileNotFoundException) Asset(org.robolectric.res.android.Asset) Implementation(org.robolectric.annotation.Implementation)

Example 10 with Asset

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

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