Search in sources :

Example 1 with ResStringPool

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

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

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

the class ShadowArscAssetManager method getArrayStringResource.

@HiddenApi
@Implementation
protected final String[] getArrayStringResource(int arrayResId) {
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return null;
    }
    final ResTable res = am.getResources();
    final Ref<bag_entry[]> startOfBag = new Ref<>(null);
    final int N = res.lockBag(arrayResId, startOfBag);
    if (N < 0) {
        return null;
    }
    String[] array = new String[N];
    final Ref<Res_value> valueRef = new Ref<>(null);
    final bag_entry[] bag = startOfBag.get();
    int strLen = 0;
    for (int i = 0; ((int) i) < N; i++) {
        valueRef.set(bag[i].map.value);
        String str = null;
        // Take care of resolving the found resource to its final value.
        int block = res.resolveReference(valueRef, bag[i].stringBlock, null);
        if (kThrowOnBadId) {
            if (block == BAD_INDEX) {
                throw new IllegalStateException("Bad resource!");
            }
        }
        if (valueRef.get().dataType == DataType.STRING.code()) {
            final ResStringPool pool = res.getTableStringBlock(block);
            str = pool.stringAt(valueRef.get().data);
            // }
            if (str == null) {
                res.unlockBag(startOfBag);
                return null;
            }
            array[i] = str;
        // str is not NULL at that point, otherwise ExceptionCheck would have been true.
        // If we have a large amount of strings in our array, we might
        // overflow the local reference table of the VM.
        // env.DeleteLocalRef(str);
        }
    }
    res.unlockBag(startOfBag);
    return array;
}
Also used : Res_value(org.robolectric.res.android.ResourceTypes.Res_value) CppAssetManager(org.robolectric.res.android.CppAssetManager) ResTable(org.robolectric.res.android.ResTable) Ref(org.robolectric.res.android.Ref) ResStringPool(org.robolectric.res.android.ResStringPool) ResTable.bag_entry(org.robolectric.res.android.ResTable.bag_entry) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 4 with ResStringPool

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

the class ShadowStringBlock method nativeGetStyle.

@Implementation(minSdk = LOLLIPOP)
protected static int[] nativeGetStyle(long obj, int idx) {
    ResStringPool osb = ResStringPool.getNativeObject(obj);
    ResStringPool_span spans = osb.styleAt(idx);
    if (spans == null) {
        return null;
    }
    ResStringPool_span pos = spans;
    int num = 0;
    while (pos.name.index != ResStringPool_span.END) {
        num++;
        // pos++;
        pos = new ResStringPool_span(pos.myBuf(), pos.myOffset() + ResStringPool_span.SIZEOF);
    }
    if (num == 0) {
        return null;
    }
    // jintArray array = env->NewIntArray((num*sizeof(ResStringPool_span))/sizeof(jint));
    int[] array = new int[num * ResStringPool_span.SIZEOF / SIZEOF_INT];
    if (array == null) {
        // NewIntArray already threw OutOfMemoryError.
        return null;
    }
    num = 0;
    final int numInts = ResStringPool_span.SIZEOF / SIZEOF_INT;
    while (spans.name.index != ResStringPool_span.END) {
        // env->SetIntArrayRegion(array,
        // num*numInts, numInts,
        // (jint*)spans);
        setIntArrayRegion(array, num, numInts, spans);
        // spans++;
        spans = new ResStringPool_span(spans.myBuf(), spans.myOffset() + ResStringPool_span.SIZEOF);
        num++;
    }
    return array;
}
Also used : ResStringPool_span(org.robolectric.res.android.ResourceTypes.ResStringPool_span) ResStringPool(org.robolectric.res.android.ResStringPool) Implementation(org.robolectric.annotation.Implementation)

Aggregations

Implementation (org.robolectric.annotation.Implementation)4 ResStringPool (org.robolectric.res.android.ResStringPool)4 Ref (org.robolectric.res.android.Ref)3 Res_value (org.robolectric.res.android.ResourceTypes.Res_value)3 Nullable (android.annotation.Nullable)2 ApkAssetsCookie (org.robolectric.res.android.ApkAssetsCookie)2 CppApkAssets (org.robolectric.res.android.CppApkAssets)2 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)2 ResolvedBag (org.robolectric.res.android.CppAssetManager2.ResolvedBag)2 ResTable_config (org.robolectric.res.android.ResTable_config)2 HiddenApi (org.robolectric.annotation.HiddenApi)1 CppAssetManager (org.robolectric.res.android.CppAssetManager)1 ResTable (org.robolectric.res.android.ResTable)1 ResTable.bag_entry (org.robolectric.res.android.ResTable.bag_entry)1 ResStringPool_span (org.robolectric.res.android.ResourceTypes.ResStringPool_span)1