Search in sources :

Example 11 with ApkAssetsCookie

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

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

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

the class ShadowArscAssetManager10 method nativeGetResourceIntArray.

// static jintArray NativeGetResourceIntArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint
// resid) {
@Implementation(minSdk = P)
@Nullable
protected static int[] nativeGetResourceIntArray(long ptr, @ArrayRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
        return null;
    }
    int[] array = new int[bag.entry_count];
    // if (array == null) {
    // return null;
    // }
    // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(array, null));
    int[] buffer = array;
    for (int i = 0; i < bag.entry_count; i++) {
        ResolvedBag.Entry entry = bag.entries[i];
        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) {
            // env.ReleasePrimitiveArrayCritical(array, buffer, JNI_ABORT);
            return null;
        }
        if (value.get().dataType >= Res_value.TYPE_FIRST_INT && value.get().dataType <= Res_value.TYPE_LAST_INT) {
            buffer[i] = (int) (value.get().data);
        }
    }
    // env.ReleasePrimitiveArrayCritical(array, buffer, 0);
    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) Implementation(org.robolectric.annotation.Implementation) Nullable(android.annotation.Nullable)

Example 14 with ApkAssetsCookie

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

the class ShadowArscAssetManager9 method nativeGetResourceArray.

// static jint NativeGetResourceArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid,
// jintArray out_data) {
@Implementation(minSdk = P)
protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, @NonNull int[] out_data) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
        return -1;
    }
    int out_data_length = out_data.length;
    if ((int) (bag.entry_count) > out_data_length * STYLE_NUM_ENTRIES) {
        throw new IllegalArgumentException("Input array is not large enough");
    }
    int[] buffer = // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(out_data, null));
    out_data;
    if (buffer == null) {
        return -1;
    }
    int[] cursor = buffer;
    for (int i = 0; i < bag.entry_count; i++) {
        ResolvedBag.Entry entry = bag.entries[i];
        final Ref<Res_value> value = new Ref<>(entry.value);
        final Ref<ResTable_config> selected_config = new Ref<>(new ResTable_config());
        selected_config.get().density = 0;
        final Ref<Integer> flags = new Ref<>(bag.type_spec_flags);
        final Ref<Integer> ref = new Ref<>(0);
        ApkAssetsCookie cookie = assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref);
        if (cookie.intValue() == kInvalidCookie) {
            // env.ReleasePrimitiveArrayCritical(out_data, buffer, JNI_ABORT);
            return -1;
        }
        // Deal with the special @null value -- it turns back to TYPE_NULL.
        if (value.get().dataType == Res_value.TYPE_REFERENCE && value.get().data == 0) {
            value.set(Res_value.NULL_VALUE);
        }
        int offset = i * STYLE_NUM_ENTRIES;
        cursor[offset + STYLE_TYPE] = (int) (value.get().dataType);
        cursor[offset + STYLE_DATA] = (int) (value.get().data);
        cursor[offset + STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(cookie);
        cursor[offset + STYLE_RESOURCE_ID] = (int) (ref.get());
        cursor[offset + STYLE_CHANGING_CONFIGURATIONS] = (int) (flags.get());
        cursor[offset + STYLE_DENSITY] = (int) (selected_config.get().density);
    // cursor += STYLE_NUM_ENTRIES;
    }
    // env.ReleasePrimitiveArrayCritical(out_data, buffer, 0);
    return (int) (bag.entry_count);
}
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) Implementation(org.robolectric.annotation.Implementation)

Example 15 with ApkAssetsCookie

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

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

Aggregations

Implementation (org.robolectric.annotation.Implementation)20 ApkAssetsCookie (org.robolectric.res.android.ApkAssetsCookie)20 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)20 Ref (org.robolectric.res.android.Ref)16 ResTable_config (org.robolectric.res.android.ResTable_config)14 Res_value (org.robolectric.res.android.ResourceTypes.Res_value)14 ResolvedBag (org.robolectric.res.android.CppAssetManager2.ResolvedBag)10 Nullable (android.annotation.Nullable)8 FileNotFoundException (java.io.FileNotFoundException)6 Asset (org.robolectric.res.android.Asset)6 CppApkAssets (org.robolectric.res.android.CppApkAssets)2 Theme (org.robolectric.res.android.CppAssetManager2.Theme)2 DynamicRefTable (org.robolectric.res.android.DynamicRefTable)2 ResStringPool (org.robolectric.res.android.ResStringPool)2 ResXMLTree (org.robolectric.res.android.ResXMLTree)2