Search in sources :

Example 21 with CppAssetManager

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

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

the class ShadowArscAssetManager method openNonAssetNative.

@HiddenApi
@Implementation
protected final Number openNonAssetNative(int cookie, String fileName, int accessMode) throws FileNotFoundException {
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return RuntimeEnvironment.castNativePtr(0);
    }
    ALOGV("openNonAssetNative in %s (Java object %s)\n", am, AssetManager.class);
    String fileName8 = fileName;
    if (fileName8 == null) {
        return RuntimeEnvironment.castNativePtr(-1);
    }
    AccessMode mode = AccessMode.fromInt(accessMode);
    if (mode != Asset.AccessMode.ACCESS_UNKNOWN && mode != Asset.AccessMode.ACCESS_RANDOM && mode != Asset.AccessMode.ACCESS_STREAMING && mode != Asset.AccessMode.ACCESS_BUFFER) {
        throw new IllegalArgumentException("Bad access mode");
    }
    Asset a = isTruthy(cookie) ? am.openNonAsset(cookie, fileName8, mode) : am.openNonAsset(fileName8, mode, null);
    if (a == null) {
        throw new FileNotFoundException(fileName8);
    }
    long assetId = Registries.NATIVE_ASSET_REGISTRY.register(a);
    // todo: something better than this [xw]
    a.onClose = () -> destroyAsset(assetId);
    // printf("Created Asset Stream: %p\n", a);
    return RuntimeEnvironment.castNativePtr(assetId);
}
Also used : CppAssetManager(org.robolectric.res.android.CppAssetManager) FileNotFoundException(java.io.FileNotFoundException) Asset(org.robolectric.res.android.Asset) AccessMode(org.robolectric.res.android.Asset.AccessMode) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 23 with CppAssetManager

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

the class ShadowArscAssetManager method getResourceName.

@Implementation
protected String getResourceName(int resid) {
    CppAssetManager am = assetManagerForJavaObject();
    ResourceName name = new ResourceName();
    if (!am.getResources().getResourceName(resid, true, name)) {
        return null;
    }
    StringBuilder str = new StringBuilder();
    if (name.packageName != null) {
        str.append(name.packageName.trim());
    }
    if (name.type != null) {
        if (str.length() > 0) {
            char div = ':';
            str.append(div);
        }
        str.append(name.type);
    }
    if (name.name != null) {
        if (str.length() > 0) {
            char div = '/';
            str.append(div);
        }
        str.append(name.name);
    }
    return str.toString();
}
Also used : CppAssetManager(org.robolectric.res.android.CppAssetManager) ResourceName(org.robolectric.res.android.ResTable.ResourceName) Implementation(org.robolectric.annotation.Implementation)

Example 24 with CppAssetManager

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

the class ShadowArscAssetManager method newTheme.

@HiddenApi
@Implementation
protected final Number newTheme() {
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return RuntimeEnvironment.castNativePtr(0);
    }
    ResTableTheme theme = new ResTableTheme(am.getResources());
    return RuntimeEnvironment.castNativePtr(Registries.NATIVE_THEME_REGISTRY.register(theme));
}
Also used : ResTableTheme(org.robolectric.res.android.ResTableTheme) CppAssetManager(org.robolectric.res.android.CppAssetManager) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Aggregations

Implementation (org.robolectric.annotation.Implementation)24 CppAssetManager (org.robolectric.res.android.CppAssetManager)24 HiddenApi (org.robolectric.annotation.HiddenApi)18 ResTable (org.robolectric.res.android.ResTable)9 Ref (org.robolectric.res.android.Ref)8 FileNotFoundException (java.io.FileNotFoundException)6 Asset (org.robolectric.res.android.Asset)5 Res_value (org.robolectric.res.android.ResourceTypes.Res_value)5 ResourceName (org.robolectric.res.android.ResTable.ResourceName)4 ResTable.bag_entry (org.robolectric.res.android.ResTable.bag_entry)4 ResTable_config (org.robolectric.res.android.ResTable_config)3 String8 (org.robolectric.res.android.String8)2 SparseArray (android.util.SparseArray)1 Path (java.nio.file.Path)1 AccessMode (org.robolectric.res.android.Asset.AccessMode)1 AssetDir (org.robolectric.res.android.AssetDir)1 AssetPath (org.robolectric.res.android.AssetPath)1 DynamicRefTable (org.robolectric.res.android.DynamicRefTable)1 ResStringPool (org.robolectric.res.android.ResStringPool)1 ResTableTheme (org.robolectric.res.android.ResTableTheme)1