Search in sources :

Example 16 with Ref

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

the class ShadowArscAssetManager method retrieveArray.

@Implementation
@HiddenApi
protected int retrieveArray(int id, int[] outValues) {
    if (outValues == null) {
        throw new NullPointerException("out values");
    }
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return 0;
    }
    ResTable res = am.getResources();
    final Ref<ResTable_config> config = new Ref<>(new ResTable_config());
    Res_value value;
    int block;
    int NV = outValues.length;
    // int[] baseDest = (int[])env->GetPrimitiveArrayCritical(outValues, 0);
    int[] baseDest = outValues;
    int[] dest = baseDest;
    // if (dest == null) {
    // throw new NullPointerException(env, "java/lang/OutOfMemoryError", "");
    // return JNI_FALSE;
    // }
    // Now lock down the resource object and start pulling stuff from it.
    res.lock();
    final Ref<bag_entry[]> arrayEnt = new Ref<>(null);
    final Ref<Integer> arrayTypeSetFlags = new Ref<>(0);
    int bagOff = res.getBagLocked(id, arrayEnt, arrayTypeSetFlags);
    // final ResTable::bag_entry* endArrayEnt = arrayEnt +
    // (bagOff >= 0 ? bagOff : 0);
    int destOffset = 0;
    final Ref<Integer> typeSetFlags = new Ref<>(0);
    while (destOffset < NV && destOffset < bagOff * STYLE_NUM_ENTRIES) /*&& arrayEnt < endArrayEnt*/
    {
        bag_entry curArrayEnt = arrayEnt.get()[destOffset / STYLE_NUM_ENTRIES];
        block = curArrayEnt.stringBlock;
        typeSetFlags.set(arrayTypeSetFlags.get());
        config.get().density = 0;
        value = curArrayEnt.map.value;
        final Ref<Integer> resid = new Ref<>(0);
        if (value.dataType != DataType.NULL.code()) {
            // Take care of resolving the found resource to its final value.
            // printf("Resolving attribute reference\n");
            final Ref<Res_value> resValueRef = new Ref<>(value);
            int newBlock = res.resolveReference(resValueRef, block, resid, typeSetFlags, config);
            value = resValueRef.get();
            if (kThrowOnBadId) {
                if (newBlock == BAD_INDEX) {
                    throw new IllegalStateException("Bad resource!");
                }
            }
            if (newBlock >= 0)
                block = newBlock;
        }
        // Deal with the special @null value -- it turns back to TYPE_NULL.
        if (value.dataType == DataType.REFERENCE.code() && value.data == 0) {
            value = Res_value.NULL_VALUE;
        }
        // printf("Attribute 0x%08x: final type=0x%x, data=0x%08x\n", curIdent, value.dataType, value.data);
        // Write the final value back to Java.
        dest[destOffset + STYLE_TYPE] = value.dataType;
        dest[destOffset + STYLE_DATA] = value.data;
        dest[destOffset + STYLE_ASSET_COOKIE] = res.getTableCookie(block);
        dest[destOffset + STYLE_RESOURCE_ID] = resid.get();
        dest[destOffset + STYLE_CHANGING_CONFIGURATIONS] = typeSetFlags.get();
        dest[destOffset + STYLE_DENSITY] = config.get().density;
        // dest += STYLE_NUM_ENTRIES;
        destOffset += STYLE_NUM_ENTRIES;
    // arrayEnt++;
    }
    destOffset /= STYLE_NUM_ENTRIES;
    res.unlock();
    return destOffset;
}
Also used : 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) Ref(org.robolectric.res.android.Ref) ResTable.bag_entry(org.robolectric.res.android.ResTable.bag_entry) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 17 with Ref

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

the class ShadowArscAssetManager method returnParcelFileDescriptor.

static ParcelFileDescriptor returnParcelFileDescriptor(Asset a, long[] outOffsets) throws FileNotFoundException {
    final Ref<Long> startOffset = new Ref<Long>(-1L);
    final Ref<Long> length = new Ref<Long>(-1L);
    ;
    FileDescriptor fd = a.openFileDescriptor(startOffset, length);
    if (fd == null) {
        throw new FileNotFoundException("This file can not be opened as a file descriptor; it is probably compressed");
    }
    long[] offsets = outOffsets;
    if (offsets == null) {
        // fd.close();
        return null;
    }
    offsets[0] = startOffset.get();
    offsets[1] = length.get();
    // return new ParcelFileDescriptor(fileDesc);
    return ParcelFileDescriptor.open(a.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 18 with Ref

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

the class ShadowArscAssetManager method openXmlAssetNative.

// /*package*/@HiddenApi @Implementation public static final @NativeConfig
// int getThemeChangingConfigurations(long theme);
@HiddenApi
@Implementation
protected final Number openXmlAssetNative(int cookie, String fileName) throws FileNotFoundException {
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return RuntimeEnvironment.castNativePtr(0);
    }
    ALOGV("openXmlAsset in %s (Java object %s)\n", am, ShadowArscAssetManager.class);
    String fileName8 = fileName;
    if (fileName8 == null) {
        return RuntimeEnvironment.castNativePtr(0);
    }
    int assetCookie = cookie;
    Asset a;
    if (isTruthy(assetCookie)) {
        a = am.openNonAsset(assetCookie, fileName8, AccessMode.ACCESS_BUFFER);
    } else {
        final Ref<Integer> assetCookieRef = new Ref<>(assetCookie);
        a = am.openNonAsset(fileName8, AccessMode.ACCESS_BUFFER, assetCookieRef);
        assetCookie = assetCookieRef.get();
    }
    if (a == null) {
        throw new FileNotFoundException(fileName8);
    }
    final DynamicRefTable dynamicRefTable = am.getResources().getDynamicRefTableForCookie(assetCookie);
    ResXMLTree block = new ResXMLTree(dynamicRefTable);
    int err = block.setTo(a.getBuffer(true), (int) a.getLength(), true);
    a.close();
    if (err != NO_ERROR) {
        throw new FileNotFoundException("Corrupt XML binary file");
    }
    return RuntimeEnvironment.castNativePtr(Registries.NATIVE_RES_XML_TREES.register(block));
}
Also used : Ref(org.robolectric.res.android.Ref) CppAssetManager(org.robolectric.res.android.CppAssetManager) FileNotFoundException(java.io.FileNotFoundException) ResXMLTree(org.robolectric.res.android.ResXMLTree) Asset(org.robolectric.res.android.Asset) DynamicRefTable(org.robolectric.res.android.DynamicRefTable) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 19 with Ref

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

the class ShadowArscAssetManager method getArraySize.

@HiddenApi
@Implementation
protected int getArraySize(int id) {
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return 0;
    }
    final ResTable res = am.getResources();
    res.lock();
    final Ref<bag_entry[]> defStyleEnt = new Ref<>(null);
    int bagOff = res.getBagLocked(id, defStyleEnt, null);
    res.unlock();
    return bagOff;
}
Also used : Ref(org.robolectric.res.android.Ref) CppAssetManager(org.robolectric.res.android.CppAssetManager) ResTable(org.robolectric.res.android.ResTable) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 20 with Ref

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

the class ShadowArscAssetManager method loadResourceBagValueInternal.

private static int loadResourceBagValueInternal(int ident, int bagEntryId, TypedValue outValue, boolean resolve, ResTable res) {
    // Now lock down the resource object and start pulling stuff from it.
    res.lock();
    int block = -1;
    final Ref<Res_value> valueRef = new Ref<>(null);
    final Ref<bag_entry[]> entryRef = new Ref<>(null);
    final Ref<Integer> typeSpecFlags = new Ref<>(0);
    int entryCount = res.getBagLocked(ident, entryRef, typeSpecFlags);
    bag_entry[] bag_entries = entryRef.get();
    for (int i = 0; i < entryCount; i++) {
        bag_entry entry = bag_entries[i];
        if (bagEntryId == entry.map.name.ident) {
            block = entry.stringBlock;
            valueRef.set(entry.map.value);
        }
    }
    res.unlock();
    if (block < 0) {
        return block;
    }
    final Ref<Integer> ref = new Ref<>(ident);
    if (resolve) {
        block = res.resolveReference(valueRef, block, ref, typeSpecFlags);
        if (kThrowOnBadId) {
            if (block == BAD_INDEX) {
                throw new IllegalStateException("Bad resource!");
            }
        }
    }
    if (block >= 0) {
        return copyValue(outValue, res, valueRef.get(), ref.get(), block, typeSpecFlags.get());
    }
    return block;
}
Also used : Ref(org.robolectric.res.android.Ref) Res_value(org.robolectric.res.android.ResourceTypes.Res_value) ResTable.bag_entry(org.robolectric.res.android.ResTable.bag_entry)

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