Search in sources :

Example 1 with ApkAssets

use of android.content.res.ApkAssets in project robolectric by robolectric.

the class ShadowArscAssetManager9 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)

Example 2 with ApkAssets

use of android.content.res.ApkAssets in project robolectric by robolectric.

the class ShadowArscAssetManager10 method getAllAssetDirs.

// @Override
// protected int addAssetPathNative(String path) {
// throw new UnsupportedOperationException(); // todo
// }
@Override
Collection<Path> getAllAssetDirs() {
    ApkAssets[] apkAssetsArray = reflector(_AssetManager28_.class, realAssetManager).getApkAssets();
    ArrayList<Path> assetDirs = new ArrayList<>();
    for (ApkAssets apkAssets : apkAssetsArray) {
        long apk_assets_native_ptr = ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr();
        CppApkAssets cppApkAssets = Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr);
        if (new File(cppApkAssets.GetPath()).isFile()) {
            assetDirs.add(Fs.forJar(Paths.get(cppApkAssets.GetPath())).getPath("assets"));
        } else {
            assetDirs.add(Paths.get(cppApkAssets.GetPath()));
        }
    }
    return assetDirs;
}
Also used : Path(java.nio.file.Path) AssetPath(org.robolectric.res.android.AssetPath) ArrayList(java.util.ArrayList) CppApkAssets(org.robolectric.res.android.CppApkAssets) ApkAssets(android.content.res.ApkAssets) CppApkAssets(org.robolectric.res.android.CppApkAssets) File(java.io.File)

Example 3 with ApkAssets

use of android.content.res.ApkAssets in project robolectric by robolectric.

the class ShadowLegacyAssetManager method setApkAssets.

@HiddenApi
@Implementation(minSdk = P)
public void setApkAssets(Object apkAssetsObject, Object invalidateCachesObject) {
    ApkAssets[] apkAssets = (ApkAssets[]) apkAssetsObject;
    boolean invalidateCaches = (boolean) invalidateCachesObject;
    for (ApkAssets apkAsset : apkAssets) {
        assetDirs.add(Fs.fromUrl(apkAsset.getAssetPath()));
    }
    reflector(AssetManagerReflector.class, realObject).setApkAssets(apkAssets, invalidateCaches);
}
Also used : ApkAssets(android.content.res.ApkAssets) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 4 with ApkAssets

use of android.content.res.ApkAssets in project robolectric by robolectric.

the class ShadowArscAssetManager9 method getAllAssetDirs.

// @Override
// protected int addAssetPathNative(String path) {
// throw new UnsupportedOperationException(); // todo
// }
@Override
Collection<Path> getAllAssetDirs() {
    ApkAssets[] apkAssetsArray = reflector(_AssetManager28_.class, realAssetManager).getApkAssets();
    ArrayList<Path> assetDirs = new ArrayList<>();
    for (ApkAssets apkAssets : apkAssetsArray) {
        long apk_assets_native_ptr = ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr();
        CppApkAssets cppApkAssets = Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr);
        if (new File(cppApkAssets.GetPath()).isFile()) {
            assetDirs.add(Fs.forJar(Paths.get(cppApkAssets.GetPath())).getPath("assets"));
        } else {
            assetDirs.add(Paths.get(cppApkAssets.GetPath()));
        }
    }
    return assetDirs;
}
Also used : Path(java.nio.file.Path) AssetPath(org.robolectric.res.android.AssetPath) ArrayList(java.util.ArrayList) CppApkAssets(org.robolectric.res.android.CppApkAssets) ApkAssets(android.content.res.ApkAssets) CppApkAssets(org.robolectric.res.android.CppApkAssets) File(java.io.File)

Example 5 with ApkAssets

use of android.content.res.ApkAssets in project robolectric by robolectric.

the class ShadowArscApkAssets9 method getFromCacheOrLoad.

protected static ApkAssets getFromCacheOrLoad(Key key, ApkAssetMaker callable) {
    synchronized (cachedApkAssets) {
        WeakReference<ApkAssets> cachedRef = cachedApkAssets.get(key);
        ApkAssets apkAssets;
        if (cachedRef != null) {
            apkAssets = cachedRef.get();
            if (apkAssets != null) {
                return apkAssets;
            } else {
                cachedApkAssets.remove(key);
                long nativePtr = cachedNativePtrs.remove(key);
                Registries.NATIVE_APK_ASSETS_REGISTRY.unregister(nativePtr);
            }
        }
        apkAssets = callable.call();
        cachedApkAssets.put(key, new WeakReference<>(apkAssets));
        long nativePtr = ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr();
        cachedNativePtrs.put(key, nativePtr);
        return apkAssets;
    }
}
Also used : CppApkAssets(org.robolectric.res.android.CppApkAssets) ApkAssets(android.content.res.ApkAssets)

Aggregations

ApkAssets (android.content.res.ApkAssets)6 CppApkAssets (org.robolectric.res.android.CppApkAssets)5 ArrayList (java.util.ArrayList)4 Implementation (org.robolectric.annotation.Implementation)3 File (java.io.File)2 Path (java.nio.file.Path)2 AssetPath (org.robolectric.res.android.AssetPath)2 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)2 HiddenApi (org.robolectric.annotation.HiddenApi)1