Search in sources :

Example 11 with Ref

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

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

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

the class ShadowArscAssetManager9 method ReturnParcelFileDescriptor.

static ParcelFileDescriptor ReturnParcelFileDescriptor(/* JNIEnv* env,*/
Asset asset, long[] out_offsets) throws FileNotFoundException {
    final Ref<Long> start_offset = new Ref<>(0L);
    final Ref<Long> length = new Ref<>(0L);
    FileDescriptor fd = asset.openFileDescriptor(start_offset, length);
    if (fd == null) {
        throw new FileNotFoundException("This file can not be opened as a file descriptor; it is probably compressed");
    }
    long[] offsets = // reinterpret_cast<long*>(env.GetPrimitiveArrayCritical(out_offsets, 0));
    out_offsets;
    if (offsets == null) {
        // close(fd);
        return null;
    }
    offsets[0] = start_offset.get();
    offsets[1] = length.get();
    // env.ReleasePrimitiveArrayCritical(out_offsets, offsets, 0);
    // jniCreateFileDescriptor(env, fd);
    FileDescriptor file_desc = fd;
    // return new ParcelFileDescriptor(file_desc);
    return ParcelFileDescriptor.open(asset.getFile(), ParcelFileDescriptor.MODE_READ_ONLY);
}
Also used : Ref(org.robolectric.res.android.Ref) FileNotFoundException(java.io.FileNotFoundException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 14 with Ref

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

the class ShadowXmlBlock method nativeGetStyleAttribute.

@Implementation(minSdk = VERSION_CODES.LOLLIPOP)
protected static int nativeGetStyleAttribute(long state) {
    ResXMLParser resXMLParser = getResXMLParser(state);
    int idx = resXMLParser.indexOfStyle();
    if (idx < 0) {
        return 0;
    }
    final Ref<Res_value> valueRef = new Ref<>(new Res_value());
    if (resXMLParser.getAttributeValue(idx, valueRef) < 0) {
        return 0;
    }
    Res_value value = valueRef.get();
    return value.dataType == org.robolectric.res.android.ResourceTypes.Res_value.TYPE_REFERENCE || value.dataType == org.robolectric.res.android.ResourceTypes.Res_value.TYPE_ATTRIBUTE ? value.data : 0;
}
Also used : Ref(org.robolectric.res.android.Ref) Res_value(org.robolectric.res.android.ResourceTypes.Res_value) ResXMLParser(org.robolectric.res.android.ResXMLParser) Implementation(org.robolectric.annotation.Implementation)

Example 15 with Ref

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

the class ShadowArscAssetManager method loadResourceValue.

@HiddenApi
@Implementation
protected int loadResourceValue(int ident, short density, TypedValue outValue, boolean resolve) {
    if (outValue == null) {
        throw new NullPointerException("outValue");
    // return 0;
    }
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return 0;
    }
    final ResTable res = am.getResources();
    final Ref<Res_value> value = new Ref<>(null);
    final Ref<ResTable_config> config = new Ref<>(null);
    final Ref<Integer> typeSpecFlags = new Ref<>(null);
    int block = res.getResource(ident, value, false, density, typeSpecFlags, config);
    if (kThrowOnBadId) {
        if (block == BAD_INDEX) {
            throw new IllegalStateException("Bad resource!");
        // return 0;
        }
    }
    final Ref<Integer> ref = new Ref<>(ident);
    if (resolve) {
        block = res.resolveReference(value, block, ref, typeSpecFlags, config);
        if (kThrowOnBadId) {
            if (block == BAD_INDEX) {
                throw new IllegalStateException("Bad resource!");
            // return 0;
            }
        }
    }
    if (block >= 0) {
        // return copyValue(env, outValue, &res, value, ref, block, typeSpecFlags, &config);
        return copyValue(outValue, res, value.get(), ref.get(), block, typeSpecFlags.get(), config.get());
    }
    return block;
}
Also used : Ref(org.robolectric.res.android.Ref) Res_value(org.robolectric.res.android.ResourceTypes.Res_value) CppAssetManager(org.robolectric.res.android.CppAssetManager) ResTable_config(org.robolectric.res.android.ResTable_config) ResTable(org.robolectric.res.android.ResTable) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Aggregations

Ref (org.robolectric.res.android.Ref)34 Implementation (org.robolectric.annotation.Implementation)26 Res_value (org.robolectric.res.android.ResourceTypes.Res_value)22 ResTable_config (org.robolectric.res.android.ResTable_config)17 ApkAssetsCookie (org.robolectric.res.android.ApkAssetsCookie)16 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)16 ResolvedBag (org.robolectric.res.android.CppAssetManager2.ResolvedBag)10 HiddenApi (org.robolectric.annotation.HiddenApi)9 CppAssetManager (org.robolectric.res.android.CppAssetManager)8 ResTable (org.robolectric.res.android.ResTable)7 Nullable (android.annotation.Nullable)6 FileNotFoundException (java.io.FileNotFoundException)6 ResTable.bag_entry (org.robolectric.res.android.ResTable.bag_entry)6 ParcelFileDescriptor (android.os.ParcelFileDescriptor)3 FileDescriptor (java.io.FileDescriptor)3 Asset (org.robolectric.res.android.Asset)3 DynamicRefTable (org.robolectric.res.android.DynamicRefTable)3 ResStringPool (org.robolectric.res.android.ResStringPool)3 ResXMLTree (org.robolectric.res.android.ResXMLTree)3 Handler (android.os.Handler)2