Search in sources :

Example 31 with Res_value

use of org.robolectric.res.android.ResourceTypes.Res_value in project robolectric by robolectric.

the class ShadowArscAssetManager method getArrayStringInfo.

@HiddenApi
@Implementation
protected final int[] getArrayStringInfo(int arrayResId) {
    CppAssetManager am = assetManagerForJavaObject();
    ResTable res = am.getResources();
    final Ref<bag_entry[]> startOfBag = new Ref<>(null);
    final int N = res.lockBag(arrayResId, startOfBag);
    if (N < 0) {
        return null;
    }
    int[] array = new int[N * 2];
    final Ref<Res_value> value = new Ref<>(null);
    bag_entry[] bag = startOfBag.get();
    for (int i = 0, j = 0; i < N; i++) {
        int stringIndex = -1;
        int stringBlock = 0;
        value.set(bag[i].map.value);
        // Take care of resolving the found resource to its final value.
        stringBlock = res.resolveReference(value, bag[i].stringBlock, null);
        if (value.get().dataType == DataType.STRING.code()) {
            stringIndex = value.get().data;
        }
        if (kThrowOnBadId) {
            if (stringBlock == BAD_INDEX) {
                throw new IllegalStateException("Bad resource!");
            }
        }
        // todo: It might be faster to allocate a C array to contain
        // the blocknums and indices, put them in there and then
        // do just one SetIntArrayRegion()
        // env->SetIntArrayRegion(array, j, 1, &stringBlock);
        array[j] = stringBlock;
        // env->SetIntArrayRegion(array, j + 1, 1, &stringIndex);
        array[j + 1] = stringIndex;
        j += 2;
    }
    res.unlockBag(startOfBag);
    return array;
}
Also used : Ref(org.robolectric.res.android.Ref) Res_value(org.robolectric.res.android.ResourceTypes.Res_value) CppAssetManager(org.robolectric.res.android.CppAssetManager) ResTable(org.robolectric.res.android.ResTable) ResTable.bag_entry(org.robolectric.res.android.ResTable.bag_entry) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 32 with Res_value

use of org.robolectric.res.android.ResourceTypes.Res_value in project robolectric by robolectric.

the class ShadowArscAssetManager method getArrayIntResource.

@HiddenApi
@Implementation
public int[] getArrayIntResource(int arrayResId) {
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return null;
    }
    final ResTable res = am.getResources();
    // final ResTable::bag_entry* startOfBag;
    final Ref<bag_entry[]> startOfBag = new Ref<>(null);
    final int N = res.lockBag(arrayResId, startOfBag);
    if (N < 0) {
        return null;
    }
    int[] array = new int[N];
    if (array == null) {
        res.unlockBag(startOfBag);
        return null;
    }
    final Ref<Res_value> valueRef = new Ref<>(null);
    bag_entry[] bag = startOfBag.get();
    for (int i = 0; i < N; i++) {
        valueRef.set(bag[i].map.value);
        // Take care of resolving the found resource to its final value.
        int block = res.resolveReference(valueRef, bag[i].stringBlock, null, null, null);
        if (kThrowOnBadId) {
            if (block == BAD_INDEX) {
                // seems like this is missing from android_util_AssetManager.cpp?
                res.unlockBag(startOfBag);
                throw new IllegalStateException("Bad resource!");
            // return array;
            }
        }
        Res_value value = valueRef.get();
        if (value.dataType >= DataType.TYPE_FIRST_INT && value.dataType <= DataType.TYPE_LAST_INT) {
            int intVal = value.data;
            // env->SetIntArrayRegion(array, i, 1, &intVal);
            array[i] = intVal;
        }
    }
    res.unlockBag(startOfBag);
    return array;
}
Also used : Ref(org.robolectric.res.android.Ref) Res_value(org.robolectric.res.android.ResourceTypes.Res_value) CppAssetManager(org.robolectric.res.android.CppAssetManager) ResTable(org.robolectric.res.android.ResTable) ResTable.bag_entry(org.robolectric.res.android.ResTable.bag_entry) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 33 with Res_value

use of org.robolectric.res.android.ResourceTypes.Res_value in project robolectric by robolectric.

the class ShadowArscAssetManager method loadThemeAttributeValue.

@HiddenApi
@Implementation(minSdk = LOLLIPOP)
protected static int loadThemeAttributeValue(long themeHandle, int ident, TypedValue outValue, boolean resolve) {
    ResTableTheme theme = Preconditions.checkNotNull(Registries.NATIVE_THEME_REGISTRY.getNativeObject(themeHandle));
    ResTable res = theme.getResTable();
    final Ref<Res_value> value = new Ref<>(null);
    // XXX value could be different in different configs!
    final Ref<Integer> typeSpecFlags = new Ref<>(0);
    int block = theme.GetAttribute(ident, value, typeSpecFlags);
    final Ref<Integer> ref = new Ref<>(0);
    if (resolve) {
        block = res.resolveReference(value, block, ref, typeSpecFlags);
        if (kThrowOnBadId) {
            if (block == BAD_INDEX) {
                throw new IllegalStateException("Bad resource!");
            }
        }
    }
    return block >= 0 ? copyValue(outValue, res, value.get(), ref.get(), block, typeSpecFlags.get(), null) : block;
}
Also used : Ref(org.robolectric.res.android.Ref) ResTableTheme(org.robolectric.res.android.ResTableTheme) Res_value(org.robolectric.res.android.ResourceTypes.Res_value) ResTable(org.robolectric.res.android.ResTable) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 34 with Res_value

use of org.robolectric.res.android.ResourceTypes.Res_value in project robolectric by robolectric.

the class ShadowArscAssetManager10 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 35 with Res_value

use of org.robolectric.res.android.ResourceTypes.Res_value in project robolectric by robolectric.

the class ShadowArscAssetManager10 method nativeGetResourceValue.

// static jint NativeGetResourceValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid,
// jshort density, jobject typed_value,
// jboolean resolve_references) {
@Implementation(minSdk = P)
protected static int nativeGetResourceValue(long ptr, @AnyRes int resid, short density, @NonNull TypedValue typed_value, boolean resolve_references) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    final Ref<Res_value> value = new Ref<>(null);
    final Ref<ResTable_config> selected_config = new Ref<>(null);
    final Ref<Integer> flags = new Ref<>(0);
    ApkAssetsCookie cookie = assetmanager.GetResource(resid, false, /*may_be_bag*/
    (short) (density), value, selected_config, flags);
    if (cookie.intValue() == kInvalidCookie) {
        return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE);
    }
    final Ref<Integer> ref = new Ref<>(resid);
    if (resolve_references) {
        cookie = assetmanager.ResolveReference(cookie, value, selected_config, flags, ref);
        if (cookie.intValue() == kInvalidCookie) {
            return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE);
        }
    }
    return CopyValue(cookie, value.get(), ref.get(), flags.get(), selected_config.get(), typed_value);
}
Also used : Ref(org.robolectric.res.android.Ref) CppAssetManager2(org.robolectric.res.android.CppAssetManager2) ApkAssetsCookie(org.robolectric.res.android.ApkAssetsCookie) Res_value(org.robolectric.res.android.ResourceTypes.Res_value) ResTable_config(org.robolectric.res.android.ResTable_config) Implementation(org.robolectric.annotation.Implementation)

Aggregations

Res_value (org.robolectric.res.android.ResourceTypes.Res_value)36 Ref (org.robolectric.res.android.Ref)22 Implementation (org.robolectric.annotation.Implementation)21 ResTable_config (org.robolectric.res.android.ResTable_config)16 ApkAssetsCookie (org.robolectric.res.android.ApkAssetsCookie)14 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)14 ResolvedBag (org.robolectric.res.android.CppAssetManager2.ResolvedBag)14 Nullable (android.annotation.Nullable)6 HiddenApi (org.robolectric.annotation.HiddenApi)6 ResTable (org.robolectric.res.android.ResTable)6 ResTable.bag_entry (org.robolectric.res.android.ResTable.bag_entry)6 CppAssetManager (org.robolectric.res.android.CppAssetManager)5 Entry (org.robolectric.res.android.CppAssetManager2.ResolvedBag.Entry)5 ResStringPool (org.robolectric.res.android.ResStringPool)3 ResTable_map_entry (org.robolectric.res.android.ResourceTypes.ResTable_map_entry)3 CppApkAssets (org.robolectric.res.android.CppApkAssets)2 Theme (org.robolectric.res.android.CppAssetManager2.Theme)2 ResTable_map (org.robolectric.res.android.ResourceTypes.ResTable_map)2 ResTable_sparseTypeEntry (org.robolectric.res.android.ResourceTypes.ResTable_sparseTypeEntry)2 AttributeSet (android.util.AttributeSet)1