Search in sources :

Example 6 with LocaleQualifier

use of com.android.ide.common.resources.configuration.LocaleQualifier in project android by JetBrains.

the class StringResourceParser method parseUnderReadLock.

@NotNull
private static StringResourceData parseUnderReadLock(AndroidFacet facet, LocalResourceRepository repository) {
    List<String> keys = Lists.newArrayList(repository.getItemsOfType(ResourceType.STRING));
    Collections.sort(keys);
    Map<String, StringResource> keyToResourceMap = new HashMap<>();
    Project project = facet.getModule().getProject();
    for (String key : keys) {
        List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
        if (items == null) {
            continue;
        }
        StringResource stringResource = new StringResource(key);
        for (ResourceItem item : items) {
            XmlTag tag = LocalResourceRepository.getItemTag(project, item);
            if (tag != null && SdkConstants.VALUE_FALSE.equals(tag.getAttributeValue(SdkConstants.ATTR_TRANSLATABLE))) {
                stringResource.setTranslatable(false);
            }
            String itemStringRepresentation = resourceToString(project, item);
            FolderConfiguration config = item.getConfiguration();
            LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
            if (qualifier == null) {
                stringResource.setDefaultValue(item, itemStringRepresentation);
            } else {
                Locale locale = Locale.create(qualifier);
                stringResource.putTranslation(locale, item, itemStringRepresentation);
            }
        }
        keyToResourceMap.put(key, stringResource);
    }
    return new StringResourceData(facet, keyToResourceMap);
}
Also used : Locale(com.android.tools.idea.rendering.Locale) HashMap(java.util.HashMap) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Project(com.intellij.openapi.project.Project) ResourceItem(com.android.ide.common.res2.ResourceItem) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with LocaleQualifier

use of com.android.ide.common.resources.configuration.LocaleQualifier in project android by JetBrains.

the class StringsWriteUtils method getStringResourceItem.

@Nullable
private static ResourceItem getStringResourceItem(@NotNull AndroidFacet facet, @NotNull String key, @Nullable Locale locale) {
    LocalResourceRepository repository = facet.getModuleResources(true);
    // Ensure that items *just* created are processed by the resource repository
    repository.sync();
    List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
    if (items == null) {
        return null;
    }
    for (ResourceItem item : items) {
        FolderConfiguration config = item.getConfiguration();
        LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
        if (qualifier == null) {
            if (locale == null) {
                return item;
            } else {
                continue;
            }
        }
        Locale l = Locale.create(qualifier);
        if (l.equals(locale)) {
            return item;
        }
    }
    return null;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with LocaleQualifier

use of com.android.ide.common.resources.configuration.LocaleQualifier in project android_frameworks_base by crdroidandroid.

the class ConfigGenerator method getFolderConfig.

public FolderConfiguration getFolderConfig() {
    FolderConfiguration config = new FolderConfiguration();
    config.createDefault();
    config.setDensityQualifier(new DensityQualifier(mDensity));
    config.setNavigationMethodQualifier(new NavigationMethodQualifier(mNavigation));
    if (mScreenWidth > mScreenHeight) {
        config.setScreenDimensionQualifier(new ScreenDimensionQualifier(mScreenWidth, mScreenHeight));
    } else {
        config.setScreenDimensionQualifier(new ScreenDimensionQualifier(mScreenHeight, mScreenWidth));
    }
    config.setScreenRatioQualifier(new ScreenRatioQualifier(mRatio));
    config.setScreenSizeQualifier(new ScreenSizeQualifier(mSize));
    config.setTextInputMethodQualifier(new TextInputMethodQualifier(mKeyboard));
    config.setTouchTypeQualifier(new TouchScreenQualifier(mTouchScreen));
    config.setKeyboardStateQualifier(new KeyboardStateQualifier(mKeyboardState));
    config.setScreenOrientationQualifier(new ScreenOrientationQualifier(mOrientation));
    config.updateScreenWidthAndHeight();
    // some default qualifiers.
    config.setUiModeQualifier(new UiModeQualifier(UiMode.NORMAL));
    config.setNightModeQualifier(new NightModeQualifier(NightMode.NOTNIGHT));
    config.setCountryCodeQualifier(new CountryCodeQualifier());
    config.setLayoutDirectionQualifier(new LayoutDirectionQualifier());
    config.setNetworkCodeQualifier(new NetworkCodeQualifier());
    config.setLocaleQualifier(new LocaleQualifier());
    config.setVersionQualifier(new VersionQualifier());
    return config;
}
Also used : VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) NavigationMethodQualifier(com.android.ide.common.resources.configuration.NavigationMethodQualifier) ScreenSizeQualifier(com.android.ide.common.resources.configuration.ScreenSizeQualifier) KeyboardStateQualifier(com.android.ide.common.resources.configuration.KeyboardStateQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) NightModeQualifier(com.android.ide.common.resources.configuration.NightModeQualifier) ScreenOrientationQualifier(com.android.ide.common.resources.configuration.ScreenOrientationQualifier) UiModeQualifier(com.android.ide.common.resources.configuration.UiModeQualifier) TouchScreenQualifier(com.android.ide.common.resources.configuration.TouchScreenQualifier) LayoutDirectionQualifier(com.android.ide.common.resources.configuration.LayoutDirectionQualifier) ScreenDimensionQualifier(com.android.ide.common.resources.configuration.ScreenDimensionQualifier) ScreenRatioQualifier(com.android.ide.common.resources.configuration.ScreenRatioQualifier) TextInputMethodQualifier(com.android.ide.common.resources.configuration.TextInputMethodQualifier) DensityQualifier(com.android.ide.common.resources.configuration.DensityQualifier) NetworkCodeQualifier(com.android.ide.common.resources.configuration.NetworkCodeQualifier) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) CountryCodeQualifier(com.android.ide.common.resources.configuration.CountryCodeQualifier)

Example 9 with LocaleQualifier

use of com.android.ide.common.resources.configuration.LocaleQualifier in project android_frameworks_base by AOSPA.

the class ConfigGenerator method getFolderConfig.

public FolderConfiguration getFolderConfig() {
    FolderConfiguration config = new FolderConfiguration();
    config.createDefault();
    config.setDensityQualifier(new DensityQualifier(mDensity));
    config.setNavigationMethodQualifier(new NavigationMethodQualifier(mNavigation));
    if (mScreenWidth > mScreenHeight) {
        config.setScreenDimensionQualifier(new ScreenDimensionQualifier(mScreenWidth, mScreenHeight));
    } else {
        config.setScreenDimensionQualifier(new ScreenDimensionQualifier(mScreenHeight, mScreenWidth));
    }
    config.setScreenRatioQualifier(new ScreenRatioQualifier(mRatio));
    config.setScreenSizeQualifier(new ScreenSizeQualifier(mSize));
    config.setTextInputMethodQualifier(new TextInputMethodQualifier(mKeyboard));
    config.setTouchTypeQualifier(new TouchScreenQualifier(mTouchScreen));
    config.setKeyboardStateQualifier(new KeyboardStateQualifier(mKeyboardState));
    config.setScreenOrientationQualifier(new ScreenOrientationQualifier(mOrientation));
    config.updateScreenWidthAndHeight();
    // some default qualifiers.
    config.setUiModeQualifier(new UiModeQualifier(UiMode.NORMAL));
    config.setNightModeQualifier(new NightModeQualifier(NightMode.NOTNIGHT));
    config.setCountryCodeQualifier(new CountryCodeQualifier());
    config.setLayoutDirectionQualifier(new LayoutDirectionQualifier());
    config.setNetworkCodeQualifier(new NetworkCodeQualifier());
    config.setLocaleQualifier(new LocaleQualifier());
    config.setVersionQualifier(new VersionQualifier());
    return config;
}
Also used : VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) NavigationMethodQualifier(com.android.ide.common.resources.configuration.NavigationMethodQualifier) ScreenSizeQualifier(com.android.ide.common.resources.configuration.ScreenSizeQualifier) KeyboardStateQualifier(com.android.ide.common.resources.configuration.KeyboardStateQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) NightModeQualifier(com.android.ide.common.resources.configuration.NightModeQualifier) ScreenOrientationQualifier(com.android.ide.common.resources.configuration.ScreenOrientationQualifier) UiModeQualifier(com.android.ide.common.resources.configuration.UiModeQualifier) TouchScreenQualifier(com.android.ide.common.resources.configuration.TouchScreenQualifier) LayoutDirectionQualifier(com.android.ide.common.resources.configuration.LayoutDirectionQualifier) ScreenDimensionQualifier(com.android.ide.common.resources.configuration.ScreenDimensionQualifier) ScreenRatioQualifier(com.android.ide.common.resources.configuration.ScreenRatioQualifier) TextInputMethodQualifier(com.android.ide.common.resources.configuration.TextInputMethodQualifier) DensityQualifier(com.android.ide.common.resources.configuration.DensityQualifier) NetworkCodeQualifier(com.android.ide.common.resources.configuration.NetworkCodeQualifier) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) CountryCodeQualifier(com.android.ide.common.resources.configuration.CountryCodeQualifier)

Example 10 with LocaleQualifier

use of com.android.ide.common.resources.configuration.LocaleQualifier in project android by JetBrains.

the class ConfigurationTest method test.

public void test() throws Exception {
    final AndroidFacet facet = AndroidFacet.getInstance(myModule);
    assertNotNull(facet);
    ConfigurationManager manager = facet.getConfigurationManager();
    assertNotNull(manager);
    assertSame(manager, facet.getConfigurationManager());
    Configuration configuration = Configuration.create(manager, null, new FolderConfiguration());
    assertNotNull(configuration);
    configuration.startBulkEditing();
    configuration.setDisplayName("myconfig");
    configuration.setTheme("@style/Theme1");
    configuration.setNightMode(NightMode.NIGHT);
    configuration.setActivity("tes.tpkg.MyActivity1");
    configuration.setUiMode(UiMode.TELEVISION);
    IAndroidTarget target = configuration.getConfigurationManager().getTarget();
    Device device = configuration.getConfigurationManager().getDefaultDevice();
    State deviceState = device != null ? device.getState("Portrait") : null;
    if (target != null) {
        configuration.setTarget(target);
    }
    if (device != null) {
        configuration.setDevice(device, false);
        assertNotNull(deviceState);
        configuration.setDeviceState(deviceState);
    }
    configuration.setLocale(Locale.create("en-rUS"));
    configuration.finishBulkEditing();
    assertEquals("myconfig", configuration.getDisplayName());
    assertEquals("@style/Theme1", configuration.getTheme());
    assertEquals(NightMode.NIGHT, configuration.getNightMode());
    assertEquals("tes.tpkg.MyActivity1", configuration.getActivity());
    assertEquals(UiMode.TELEVISION, configuration.getUiMode());
    assertEquals(Locale.create("en-rUS"), configuration.getLocale());
    if (target != null) {
        assertSame(target, configuration.getRealTarget());
    }
    if (device != null) {
        assertSame(device, configuration.getDevice());
        assertNotNull(deviceState);
        assertSame(deviceState, configuration.getDeviceState());
    }
    FolderConfiguration fullConfig = configuration.getFullConfig();
    LocaleQualifier languageQualifier = fullConfig.getLocaleQualifier();
    String configDisplayString = fullConfig.toDisplayString();
    assertNotNull(configDisplayString, languageQualifier);
    assertEquals("en", languageQualifier.getLanguage());
    String region = fullConfig.getLocaleQualifier().getRegion();
    assertNotNull(configDisplayString, region);
    assertEquals("US", region);
    assertEquals(UiMode.TELEVISION, fullConfig.getUiModeQualifier().getValue());
    assertEquals(NightMode.NIGHT, fullConfig.getNightModeQualifier().getValue());
    assertEquals(ScreenOrientation.PORTRAIT, fullConfig.getScreenOrientationQualifier().getValue());
    if (device != null) {
        State landscape = device.getState("Landscape");
        assertNotNull(landscape);
        configuration.setDeviceState(landscape);
        assertEquals(ScreenOrientation.LANDSCAPE, configuration.getFullConfig().getScreenOrientationQualifier().getValue());
    }
    Density density = configuration.getDensity();
    assertEquals(Density.XHIGH, density);
    DensityQualifier qualifier = new DensityQualifier().getNullQualifier();
    configuration.getFullConfig().setDensityQualifier(qualifier);
    density = configuration.getDensity();
    assertEquals(Density.MEDIUM, density);
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Device(com.android.sdklib.devices.Device) State(com.android.sdklib.devices.State) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IAndroidTarget(com.android.sdklib.IAndroidTarget) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) DensityQualifier(com.android.ide.common.resources.configuration.DensityQualifier)

Aggregations

LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)16 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)10 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)6 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)6 CountryCodeQualifier (com.android.ide.common.resources.configuration.CountryCodeQualifier)5 KeyboardStateQualifier (com.android.ide.common.resources.configuration.KeyboardStateQualifier)5 LayoutDirectionQualifier (com.android.ide.common.resources.configuration.LayoutDirectionQualifier)5 NavigationMethodQualifier (com.android.ide.common.resources.configuration.NavigationMethodQualifier)5 NetworkCodeQualifier (com.android.ide.common.resources.configuration.NetworkCodeQualifier)5 NightModeQualifier (com.android.ide.common.resources.configuration.NightModeQualifier)5 ScreenDimensionQualifier (com.android.ide.common.resources.configuration.ScreenDimensionQualifier)5 ScreenOrientationQualifier (com.android.ide.common.resources.configuration.ScreenOrientationQualifier)5 ScreenRatioQualifier (com.android.ide.common.resources.configuration.ScreenRatioQualifier)5 ScreenSizeQualifier (com.android.ide.common.resources.configuration.ScreenSizeQualifier)5 TextInputMethodQualifier (com.android.ide.common.resources.configuration.TextInputMethodQualifier)5 TouchScreenQualifier (com.android.ide.common.resources.configuration.TouchScreenQualifier)5 UiModeQualifier (com.android.ide.common.resources.configuration.UiModeQualifier)5 Locale (com.android.tools.idea.rendering.Locale)5 NotNull (org.jetbrains.annotations.NotNull)4 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)3