Search in sources :

Example 26 with Ref

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

the class ShadowArscAssetManager method addAssetPathNative.

@HiddenApi
@Implementation(minSdk = VERSION_CODES.N)
protected int addAssetPathNative(String path, boolean appAsLib) {
    if (Strings.isNullOrEmpty(path)) {
        return 0;
    }
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return 0;
    }
    final Ref<Integer> cookie = new Ref<>(null);
    boolean res = am.addAssetPath(new String8(path), cookie, appAsLib);
    return (res) ? cookie.get() : 0;
}
Also used : Ref(org.robolectric.res.android.Ref) CppAssetManager(org.robolectric.res.android.CppAssetManager) String8(org.robolectric.res.android.String8) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 27 with Ref

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

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

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

the class ShadowArscAssetManager method getConfiguration.

@VisibleForTesting
ResTable_config getConfiguration() {
    final Ref<ResTable_config> config = new Ref<>(new ResTable_config());
    assetManagerForJavaObject().getConfiguration(config);
    return config.get();
}
Also used : Ref(org.robolectric.res.android.Ref) ResTable_config(org.robolectric.res.android.ResTable_config) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 30 with Ref

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

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