Search in sources :

Example 11 with ResTable_config

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

the class ShadowArscAssetManager9 method nativeGetSizeConfigurations.

// static jobjectArray NativeGetSizeConfigurations(JNIEnv* env, jclass /*clazz*/, jlong ptr) {
@Implementation(minSdk = P)
@Nullable
protected static Configuration[] nativeGetSizeConfigurations(long ptr) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Set<ResTable_config> configurations = assetmanager.GetResourceConfigurations(true, /*exclude_system*/
    false);
    Configuration[] array = new Configuration[configurations.size()];
    // env.NewObjectArray(configurations.size(), gConfigurationOffsets.classObject, null);
    // if (array == null) {
    // return null;
    // }
    int idx = 0;
    for (ResTable_config configuration : configurations) {
        Configuration java_configuration = ConstructConfigurationObject(configuration);
        // if (java_configuration == null) {
        // return null;
        // }
        // env.SetObjectArrayElement(array, idx++, java_configuration);
        array[idx++] = java_configuration;
    // env.DeleteLocalRef(java_configuration);
    }
    return array;
}
Also used : CppAssetManager2(org.robolectric.res.android.CppAssetManager2) Configuration(android.content.res.Configuration) ResTable_config(org.robolectric.res.android.ResTable_config) Implementation(org.robolectric.annotation.Implementation) Nullable(android.annotation.Nullable)

Example 12 with ResTable_config

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

the class ShadowArscAssetManager method loadResourceValue.

@HiddenApi
@Implementation
protected int loadResourceValue(int ident, short density, TypedValue outValue, boolean resolve) {
    if (outValue == null) {
        throw new NullPointerException("outValue");
    // return 0;
    }
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return 0;
    }
    final ResTable res = am.getResources();
    final Ref<Res_value> value = new Ref<>(null);
    final Ref<ResTable_config> config = new Ref<>(null);
    final Ref<Integer> typeSpecFlags = new Ref<>(null);
    int block = res.getResource(ident, value, false, density, typeSpecFlags, config);
    if (kThrowOnBadId) {
        if (block == BAD_INDEX) {
            throw new IllegalStateException("Bad resource!");
        // return 0;
        }
    }
    final Ref<Integer> ref = new Ref<>(ident);
    if (resolve) {
        block = res.resolveReference(value, block, ref, typeSpecFlags, config);
        if (kThrowOnBadId) {
            if (block == BAD_INDEX) {
                throw new IllegalStateException("Bad resource!");
            // return 0;
            }
        }
    }
    if (block >= 0) {
        // return copyValue(env, outValue, &res, value, ref, block, typeSpecFlags, &config);
        return copyValue(outValue, res, value.get(), ref.get(), block, typeSpecFlags.get(), config.get());
    }
    return block;
}
Also used : Ref(org.robolectric.res.android.Ref) 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) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 13 with ResTable_config

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

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

the class ShadowArscAssetManager9 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 15 with ResTable_config

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

the class DeviceConfig method applyToConfiguration.

static void applyToConfiguration(Qualifiers qualifiers, int apiLevel, Configuration configuration, DisplayMetrics displayMetrics) {
    ResTable_config resTab = qualifiers.getConfig();
    if (resTab.mcc != 0) {
        configuration.mcc = resTab.mcc;
    }
    if (resTab.mnc != 0) {
        configuration.mnc = resTab.mnc;
    }
    // screenLayout includes size, long, layoutdir, and round.
    // layoutdir may be overridden by setLocale(), so do this first:
    int screenLayoutSize = getScreenLayoutSize(configuration);
    int resTabSize = resTab.screenLayoutSize();
    if (resTabSize != ResTable_config.SCREENSIZE_ANY) {
        screenLayoutSize = resTabSize;
        if (resTab.screenWidthDp == 0) {
            configuration.screenWidthDp = 0;
        }
        if (resTab.screenHeightDp == 0) {
            configuration.screenHeightDp = 0;
        }
    }
    int screenLayoutLong = getScreenLayoutLong(configuration);
    int resTabLong = resTab.screenLayoutLong();
    if (resTabLong != ResTable_config.SCREENLONG_ANY) {
        screenLayoutLong = resTabLong;
    }
    int screenLayoutLayoutDir = getScreenLayoutLayoutDir(configuration);
    int resTabLayoutDir = resTab.screenLayoutDirection();
    if (resTabLayoutDir != ResTable_config.LAYOUTDIR_ANY) {
        screenLayoutLayoutDir = resTabLayoutDir;
    }
    int screenLayoutRound = getScreenLayoutRound(configuration);
    int resTabRound = resTab.screenLayoutRound();
    if (resTabRound != ResTable_config.SCREENROUND_ANY) {
        screenLayoutRound = resTabRound << 8;
    }
    configuration.screenLayout = screenLayoutSize | screenLayoutLong | screenLayoutLayoutDir | screenLayoutRound;
    // locale...
    String lang = resTab.languageString();
    String region = resTab.regionString();
    String script = resTab.scriptString();
    Locale locale;
    if (isNullOrEmpty(lang) && isNullOrEmpty(region) && isNullOrEmpty(script)) {
        locale = null;
    } else {
        locale = new Locale.Builder().setLanguage(lang).setRegion(region).setScript(script == null ? "" : script).build();
    }
    if (locale != null) {
        setLocale(apiLevel, configuration, locale);
    }
    if (resTab.smallestScreenWidthDp != 0) {
        configuration.smallestScreenWidthDp = resTab.smallestScreenWidthDp;
    }
    if (resTab.screenWidthDp != 0) {
        configuration.screenWidthDp = resTab.screenWidthDp;
    }
    if (resTab.screenHeightDp != 0) {
        configuration.screenHeightDp = resTab.screenHeightDp;
    }
    if (resTab.orientation != ResTable_config.ORIENTATION_ANY) {
        configuration.orientation = resTab.orientation;
    }
    // uiMode includes type and night...
    int uiModeType = getUiModeType(configuration);
    int resTabType = resTab.uiModeType();
    if (resTabType != ResTable_config.UI_MODE_TYPE_ANY) {
        uiModeType = resTabType;
    }
    int uiModeNight = getUiModeNight(configuration);
    int resTabNight = resTab.uiModeNight();
    if (resTabNight != ResTable_config.UI_MODE_NIGHT_ANY) {
        uiModeNight = resTabNight;
    }
    configuration.uiMode = uiModeType | uiModeNight;
    if (resTab.density != ResTable_config.DENSITY_DEFAULT) {
        setDensity(resTab.density, apiLevel, configuration, displayMetrics);
    }
    setDimensions(apiLevel, configuration, displayMetrics);
    if (resTab.touchscreen != ResTable_config.TOUCHSCREEN_ANY) {
        configuration.touchscreen = resTab.touchscreen;
    }
    if (resTab.keyboard != ResTable_config.KEYBOARD_ANY) {
        configuration.keyboard = resTab.keyboard;
    }
    if (resTab.keyboardHidden() != ResTable_config.KEYSHIDDEN_ANY) {
        configuration.keyboardHidden = resTab.keyboardHidden();
    }
    if (resTab.navigation != ResTable_config.NAVIGATION_ANY) {
        configuration.navigation = resTab.navigation;
    }
    if (resTab.navigationHidden() != ResTable_config.NAVHIDDEN_ANY) {
        configuration.navigationHidden = resTab.navigationHidden();
    }
    if (apiLevel >= VERSION_CODES.O) {
        if (resTab.colorModeWideColorGamut() != ResTable_config.WIDE_COLOR_GAMUT_ANY) {
            setColorModeGamut(configuration, resTab.colorMode & ResTable_config.MASK_WIDE_COLOR_GAMUT);
        }
        if (resTab.colorModeHdr() != ResTable_config.HDR_ANY) {
            setColorModeHdr(configuration, resTab.colorMode & ResTable_config.MASK_HDR);
        }
    }
}
Also used : Locale(java.util.Locale) ResTable_config(org.robolectric.res.android.ResTable_config)

Aggregations

ResTable_config (org.robolectric.res.android.ResTable_config)31 Implementation (org.robolectric.annotation.Implementation)22 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)18 Ref (org.robolectric.res.android.Ref)17 Res_value (org.robolectric.res.android.ResourceTypes.Res_value)16 ApkAssetsCookie (org.robolectric.res.android.ApkAssetsCookie)14 ResolvedBag (org.robolectric.res.android.CppAssetManager2.ResolvedBag)10 Nullable (android.annotation.Nullable)8 Test (org.junit.Test)5 HiddenApi (org.robolectric.annotation.HiddenApi)4 CppAssetManager (org.robolectric.res.android.CppAssetManager)3 Configuration (android.content.res.Configuration)2 Path (java.nio.file.Path)2 CppApkAssets (org.robolectric.res.android.CppApkAssets)2 Theme (org.robolectric.res.android.CppAssetManager2.Theme)2 ResStringPool (org.robolectric.res.android.ResStringPool)2 ResTable (org.robolectric.res.android.ResTable)2 SuppressLint (android.annotation.SuppressLint)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Locale (java.util.Locale)1