Search in sources :

Example 1 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class Configuration method syncFolderConfig.

/**
   * Updates the folder configuration such that it reflects changes in
   * configuration state such as the device orientation, the UI mode, the
   * rendering target, etc.
   */
protected void syncFolderConfig() {
    Device device = getDevice();
    if (device == null) {
        return;
    }
    // get the device config from the device/state combos.
    State deviceState = getDeviceState();
    if (deviceState == null) {
        deviceState = device.getDefaultState();
    }
    FolderConfiguration config = getFolderConfig(getModule(), deviceState, getLocale(), getTarget());
    // replace the config with the one from the device
    myFullConfig.set(config);
    // sync the selected locale
    Locale locale = getLocale();
    myFullConfig.setLocaleQualifier(locale.qualifier);
    if (myEditedConfig.getLayoutDirectionQualifier() != null) {
        myFullConfig.setLayoutDirectionQualifier(myEditedConfig.getLayoutDirectionQualifier());
    } else if (!locale.hasLanguage()) {
        // Avoid getting the layout library if the locale doesn't have any language.
        myFullConfig.setLayoutDirectionQualifier(new LayoutDirectionQualifier(LayoutDirection.LTR));
    } else {
        LayoutLibrary layoutLib = RenderService.getLayoutLibrary(getModule(), getTarget());
        if (layoutLib != null) {
            if (layoutLib.isRtl(locale.toLocaleId())) {
                myFullConfig.setLayoutDirectionQualifier(new LayoutDirectionQualifier(LayoutDirection.RTL));
            } else {
                myFullConfig.setLayoutDirectionQualifier(new LayoutDirectionQualifier(LayoutDirection.LTR));
            }
        }
    }
    // Replace the UiMode with the selected one, if one is selected
    UiMode uiMode = getUiMode();
    myFullConfig.setUiModeQualifier(new UiModeQualifier(uiMode));
    // Replace the NightMode with the selected one, if one is selected
    NightMode nightMode = getNightMode();
    myFullConfig.setNightModeQualifier(new NightModeQualifier(nightMode));
    // replace the API level by the selection of the combo
    IAndroidTarget target = getTarget();
    if (target != null) {
        int apiLevel = target.getVersion().getFeatureLevel();
        myFullConfig.setVersionQualifier(new VersionQualifier(apiLevel));
    }
    myFolderConfigDirty = 0;
    myProjectStateVersion = myManager.getStateVersion();
}
Also used : Locale(com.android.tools.idea.rendering.Locale) LayoutLibrary(com.android.ide.common.rendering.LayoutLibrary) Device(com.android.sdklib.devices.Device) IAndroidTarget(com.android.sdklib.IAndroidTarget) State(com.android.sdklib.devices.State)

Example 2 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class ConfigurationManager method getLocales.

@NotNull
public List<Locale> getLocales() {
    // Get locales from modules, but not libraries!
    LocalResourceRepository projectResources = ProjectResourceRepository.getProjectResources(myModule, true);
    assert projectResources != null;
    if (projectResources.getModificationCount() != myLocaleCacheStamp) {
        myLocales = null;
    }
    if (myLocales == null) {
        List<Locale> locales = new ArrayList<Locale>();
        for (LocaleQualifier locale : projectResources.getLocales()) {
            locales.add(Locale.create(locale));
        }
        myLocales = locales;
        myLocaleCacheStamp = projectResources.getModificationCount();
    }
    return myLocales;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class ConfigurationMatcher method findAndSetCompatibleConfig.

/**
   * Finds a device/config that can display a configuration.
   * <p/>
   * Once found the device and config combos are set to the config.
   * <p/>
   * If there is no compatible configuration, a custom one is created.
   *
   * @param favorCurrentConfig if true, and no best match is found, don't
   *                           change the current config. This must only be true if the
   *                           current config is compatible.
   */
void findAndSetCompatibleConfig(boolean favorCurrentConfig) {
    List<Locale> localeList = getPrioritizedLocales();
    List<Device> deviceList = myManager.getDevices();
    FolderConfiguration editedConfig = myConfiguration.getEditedConfig();
    FolderConfiguration currentConfig = myConfiguration.getFullConfig();
    // list of compatible device/state/locale
    List<ConfigMatch> anyMatches = new ArrayList<ConfigMatch>();
    // list of actual best match (ie the file is a best match for the
    // device/state)
    List<ConfigMatch> bestMatches = new ArrayList<ConfigMatch>();
    // get a locale that matches the host locale roughly (may not be exact match on the region.)
    int localeHostMatch = getLocaleMatch();
    // build a list of combinations of non standard qualifiers to add to each device's
    // qualifier set when testing for a match.
    // These qualifiers are: locale, night-mode, car dock.
    List<ConfigBundle> configBundles = new ArrayList<ConfigBundle>(200);
    // If the edited file has locales, then we have to select a matching locale from
    // the list.
    // However, if it doesn't, we don't randomly take the first locale, we take one
    // matching the current host locale (making sure it actually exist in the project)
    int start, max;
    if (editedConfig.getLocaleQualifier() != null || localeHostMatch == -1) {
        // add all the locales
        start = 0;
        max = localeList.size();
    } else {
        // only add the locale host match
        start = localeHostMatch;
        // test is <
        max = localeHostMatch + 1;
    }
    for (int i = start; i < max; i++) {
        Locale l = localeList.get(i);
        ConfigBundle bundle = new ConfigBundle();
        bundle.config.setLocaleQualifier(l.qualifier);
        bundle.localeIndex = i;
        configBundles.add(bundle);
    }
    // add the dock mode to the bundle combinations.
    addDockModeToBundles(configBundles);
    // add the night mode to the bundle combinations.
    addNightModeToBundles(configBundles);
    addRenderTargetToBundles(configBundles);
    Locale currentLocale = myConfiguration.getLocale();
    IAndroidTarget currentTarget = myConfiguration.getTarget();
    Module module = myConfiguration.getModule();
    for (Device device : deviceList) {
        for (State state : device.getAllStates()) {
            // loop on the list of config bundles to create full
            // configurations.
            FolderConfiguration stateConfig = Configuration.getFolderConfig(module, state, currentLocale, currentTarget);
            for (ConfigBundle bundle : configBundles) {
                // create a new config with device config
                FolderConfiguration testConfig = new FolderConfiguration();
                testConfig.set(stateConfig);
                // add on top of it, the extra qualifiers from the bundle
                testConfig.add(bundle.config);
                if (editedConfig.isMatchFor(testConfig)) {
                    // this is a basic match. record it in case we don't
                    // find a match
                    // where the edited file is a best config.
                    anyMatches.add(new ConfigMatch(testConfig, device, state, bundle));
                    if (isCurrentFileBestMatchFor(testConfig)) {
                        // this is what we want.
                        bestMatches.add(new ConfigMatch(testConfig, device, state, bundle));
                    }
                }
            }
        }
    }
    if (bestMatches.size() == 0) {
        if (favorCurrentConfig) {
            // quick check
            if (!editedConfig.isMatchFor(currentConfig)) {
                LOG.warn("favorCurrentConfig can only be true if the current config is compatible");
            }
            // just display the warning
            LOG.warn(String.format("'%1$s' is not a best match for any device/locale combination for %2$s.\n" + "Displaying it with '%3$s'.", editedConfig.toDisplayString(), myConfiguration.getFile(), currentConfig.toDisplayString()));
        } else if (anyMatches.size() > 0) {
            // select the best device anyway.
            ConfigMatch match = selectConfigMatch(anyMatches);
            myConfiguration.startBulkEditing();
            myConfiguration.setEffectiveDevice(match.device, match.state);
            myConfiguration.setUiMode(UiMode.getByIndex(match.bundle.dockModeIndex));
            myConfiguration.setNightMode(NightMode.getByIndex(match.bundle.nightModeIndex));
            myConfiguration.finishBulkEditing();
            // TODO: display a better warning!
            LOG.warn(String.format("'%1$s' is not a best match for any device/locale combination for %2$s.\n" + "Displaying it with\n" + "  %3$s\n" + "which is compatible, but will actually be displayed with " + "another more specific version of the layout.", editedConfig.toDisplayString(), myConfiguration.getFile(), currentConfig.toDisplayString()));
        } else {
        // TODO: there is no device/config able to display the layout, create one.
        // For the base config values, we'll take the first device and state,
        // and replace whatever qualifier required by the layout file.
        }
    } else {
        ConfigMatch match = selectConfigMatch(bestMatches);
        myConfiguration.startBulkEditing();
        myConfiguration.setEffectiveDevice(match.device, match.state);
        myConfiguration.setUiMode(UiMode.getByIndex(match.bundle.dockModeIndex));
        myConfiguration.setNightMode(NightMode.getByIndex(match.bundle.nightModeIndex));
        myConfiguration.finishBulkEditing();
    }
}
Also used : Locale(com.android.tools.idea.rendering.Locale) Device(com.android.sdklib.devices.Device) IAndroidTarget(com.android.sdklib.IAndroidTarget) State(com.android.sdklib.devices.State) Module(com.intellij.openapi.module.Module)

Example 4 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class LocaleMenuAction method updatePresentation.

private void updatePresentation(Presentation presentation) {
    Configuration configuration = myRenderContext.getConfiguration();
    boolean visible = configuration != null;
    if (visible) {
        // TEMPORARY WORKAROUND:
        // We don't properly sync the project locale to layouts yet, so in the mean time
        // show the actual locale being used rather than the intended locale, so as not
        // to be totally confusing:
        //Locale locale = configuration.isLocaleSpecificLayout()
        //                ? configuration.getLocale() : configuration.getConfigurationManager().getLocale();
        Locale locale = configuration.getLocale();
        if (!myClassicStyle) {
            presentation.setIcon(AndroidIcons.NeleIcons.Language);
        } else if (locale == Locale.ANY) {
            presentation.setIcon(AndroidIcons.Globe);
        } else {
            presentation.setIcon(locale.getFlagImage());
        }
        String brief = getLocaleLabel(locale, true, myClassicStyle);
        presentation.setText(brief);
    } else {
        presentation.setIcon(AndroidIcons.Globe);
    }
    if (visible != presentation.isVisible()) {
        presentation.setVisible(visible);
    }
}
Also used : Locale(com.android.tools.idea.rendering.Locale) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Example 5 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class StringResourceDataTest method testResourceToStringPsi.

public void testResourceToStringPsi() {
    Locale locale = Locale.create("fr");
    assertEquals("L'Étranger", data.getStringResource("key8").getTranslationAsString(locale));
    assertEquals("<![CDATA[L'Étranger]]>", data.getStringResource("key9").getTranslationAsString(locale));
    assertEquals("<xliff:g>L'Étranger</xliff:g>", data.getStringResource("key10").getTranslationAsString(locale));
}
Also used : Locale(com.android.tools.idea.rendering.Locale)

Aggregations

Locale (com.android.tools.idea.rendering.Locale)19 NotNull (org.jetbrains.annotations.NotNull)7 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)5 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 XmlTag (com.intellij.psi.xml.XmlTag)4 IAndroidTarget (com.android.sdklib.IAndroidTarget)3 Device (com.android.sdklib.devices.Device)3 State (com.android.sdklib.devices.State)3 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)3 Module (com.intellij.openapi.module.Module)3 ResourceItem (com.android.ide.common.res2.ResourceItem)2 ArrayList (java.util.ArrayList)2 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)1 DefaultActionGroup (com.intellij.openapi.actionSystem.DefaultActionGroup)1 Project (com.intellij.openapi.project.Project)1 java.util (java.util)1 HashMap (java.util.HashMap)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 Nullable (org.jetbrains.annotations.Nullable)1