Search in sources :

Example 1 with CppApkAssets

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

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

the class ShadowArscAssetManager9 method nativeGetResourceStringArray.

// static jobjectArray NativeGetResourceStringArray(JNIEnv* env, jclass /*clazz*/, jlong ptr,
// jint resid) {
@Implementation(minSdk = P)
@Nullable
protected static String[] nativeGetResourceStringArray(long ptr, @ArrayRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
        return null;
    }
    String[] array = new String[bag.entry_count];
    if (array == null) {
        return null;
    }
    for (int i = 0; i < bag.entry_count; i++) {
        ResolvedBag.Entry entry = bag.entries[i];
        // Resolve any references to their final value.
        final Ref<Res_value> value = new Ref<>(entry.value);
        final Ref<ResTable_config> selected_config = new Ref<>(null);
        final Ref<Integer> flags = new Ref<>(0);
        final Ref<Integer> ref = new Ref<>(0);
        ApkAssetsCookie cookie = assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref);
        if (cookie.intValue() == kInvalidCookie) {
            return null;
        }
        if (value.get().dataType == Res_value.TYPE_STRING) {
            CppApkAssets apk_assets = assetmanager.GetApkAssets().get(cookie.intValue());
            ResStringPool pool = apk_assets.GetLoadedArsc().GetStringPool();
            String java_string = null;
            int str_len;
            String str_utf8 = pool.stringAt(value.get().data);
            if (str_utf8 != null) {
                java_string = str_utf8;
            } else {
                String str_utf16 = pool.stringAt(value.get().data);
                java_string = str_utf16;
            }
            // // Check for errors creating the strings (if malformed or no memory).
            // if (env.ExceptionCheck()) {
            // return null;
            // }
            // env.SetObjectArrayElement(array, i, java_string);
            array[i] = java_string;
        // If we have a large amount of string in our array, we might overflow the
        // local reference table of the VM.
        // env.DeleteLocalRef(java_string);
        }
    }
    return array;
}
Also used : CppAssetManager2(org.robolectric.res.android.CppAssetManager2) ResolvedBag(org.robolectric.res.android.CppAssetManager2.ResolvedBag) Res_value(org.robolectric.res.android.ResourceTypes.Res_value) ResTable_config(org.robolectric.res.android.ResTable_config) Ref(org.robolectric.res.android.Ref) ApkAssetsCookie(org.robolectric.res.android.ApkAssetsCookie) ResStringPool(org.robolectric.res.android.ResStringPool) CppApkAssets(org.robolectric.res.android.CppApkAssets) Implementation(org.robolectric.annotation.Implementation) Nullable(android.annotation.Nullable)

Example 3 with CppApkAssets

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

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

the class ShadowArscAssetManager10 method nativeGetResourceStringArray.

// static jobjectArray NativeGetResourceStringArray(JNIEnv* env, jclass /*clazz*/, jlong ptr,
// jint resid) {
@Implementation(minSdk = P)
@Nullable
protected static String[] nativeGetResourceStringArray(long ptr, @ArrayRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
        return null;
    }
    String[] array = new String[bag.entry_count];
    if (array == null) {
        return null;
    }
    for (int i = 0; i < bag.entry_count; i++) {
        ResolvedBag.Entry entry = bag.entries[i];
        // Resolve any references to their final value.
        final Ref<Res_value> value = new Ref<>(entry.value);
        final Ref<ResTable_config> selected_config = new Ref<>(null);
        final Ref<Integer> flags = new Ref<>(0);
        final Ref<Integer> ref = new Ref<>(0);
        ApkAssetsCookie cookie = assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref);
        if (cookie.intValue() == kInvalidCookie) {
            return null;
        }
        if (value.get().dataType == Res_value.TYPE_STRING) {
            CppApkAssets apk_assets = assetmanager.GetApkAssets().get(cookie.intValue());
            ResStringPool pool = apk_assets.GetLoadedArsc().GetStringPool();
            String java_string = null;
            String str_utf8 = pool.stringAt(value.get().data);
            if (str_utf8 != null) {
                java_string = str_utf8;
            } else {
                String str_utf16 = pool.stringAt(value.get().data);
                java_string = str_utf16;
            }
            // // Check for errors creating the strings (if malformed or no memory).
            // if (env.ExceptionCheck()) {
            // return null;
            // }
            // env.SetObjectArrayElement(array, i, java_string);
            array[i] = java_string;
        // If we have a large amount of string in our array, we might overflow the
        // local reference table of the VM.
        // env.DeleteLocalRef(java_string);
        }
    }
    return array;
}
Also used : CppAssetManager2(org.robolectric.res.android.CppAssetManager2) ResolvedBag(org.robolectric.res.android.CppAssetManager2.ResolvedBag) Res_value(org.robolectric.res.android.ResourceTypes.Res_value) ResTable_config(org.robolectric.res.android.ResTable_config) Ref(org.robolectric.res.android.Ref) ApkAssetsCookie(org.robolectric.res.android.ApkAssetsCookie) ResStringPool(org.robolectric.res.android.ResStringPool) CppApkAssets(org.robolectric.res.android.CppApkAssets) Implementation(org.robolectric.annotation.Implementation) Nullable(android.annotation.Nullable)

Example 5 with CppApkAssets

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

Aggregations

CppApkAssets (org.robolectric.res.android.CppApkAssets)8 Implementation (org.robolectric.annotation.Implementation)6 ApkAssets (android.content.res.ApkAssets)4 ArrayList (java.util.ArrayList)4 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)4 Nullable (android.annotation.Nullable)2 File (java.io.File)2 Path (java.nio.file.Path)2 ApkAssetsCookie (org.robolectric.res.android.ApkAssetsCookie)2 AssetPath (org.robolectric.res.android.AssetPath)2 ResolvedBag (org.robolectric.res.android.CppAssetManager2.ResolvedBag)2 Ref (org.robolectric.res.android.Ref)2 ResStringPool (org.robolectric.res.android.ResStringPool)2 ResTable_config (org.robolectric.res.android.ResTable_config)2 Res_value (org.robolectric.res.android.ResourceTypes.Res_value)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Asset (org.robolectric.res.android.Asset)1 ResXMLTree (org.robolectric.res.android.ResXMLTree)1