use of com.android.tools.idea.rendering.Locale in project android by JetBrains.
the class ConfigurationMatcher method adaptConfigSelection.
/**
* Adapts the current device/config selection so that it's compatible with
* the configuration.
* <p/>
* If the current selection is compatible, nothing is changed.
* <p/>
* If it's not compatible, configs from the current devices are tested.
* <p/>
* If none are compatible, it reverts to
* {@link #findAndSetCompatibleConfig(boolean)}
*/
void adaptConfigSelection(boolean needBestMatch) {
// check the device config (ie sans locale)
// if still true, we need to find another config.
boolean needConfigChange = true;
boolean currentConfigIsCompatible = false;
State selectedState = myConfiguration.getDeviceState();
FolderConfiguration editedConfig = myConfiguration.getEditedConfig();
Module module = myConfiguration.getModule();
if (selectedState != null) {
FolderConfiguration currentConfig = Configuration.getFolderConfig(module, selectedState, myConfiguration.getLocale(), myConfiguration.getTarget());
if (currentConfig != null && editedConfig.isMatchFor(currentConfig)) {
// current config is compatible
currentConfigIsCompatible = true;
if (!needBestMatch || isCurrentFileBestMatchFor(currentConfig)) {
needConfigChange = false;
}
}
}
if (needConfigChange) {
List<Locale> localeList = getPrioritizedLocales();
// if the current state/locale isn't a correct match, then
// look for another state/locale in the same device.
FolderConfiguration testConfig = new FolderConfiguration();
// first look in the current device.
State matchState = null;
int localeIndex = -1;
Device device = myConfiguration.getDevice();
IAndroidTarget target = myConfiguration.getTarget();
if (device != null && target != null) {
VersionQualifier versionQualifier = new VersionQualifier(target.getVersion().getFeatureLevel());
mainloop: for (State state : device.getAllStates()) {
testConfig.set(Configuration.getFolderConfig(module, state, myConfiguration.getLocale(), target));
testConfig.setVersionQualifier(versionQualifier);
// loop on the locales.
for (int i = 0; i < localeList.size(); i++) {
Locale locale = localeList.get(i);
// update the test config with the locale qualifiers
testConfig.setLocaleQualifier(locale.qualifier);
if (editedConfig.isMatchFor(testConfig) && isCurrentFileBestMatchFor(testConfig)) {
matchState = state;
localeIndex = i;
break mainloop;
}
}
}
}
if (matchState != null) {
myConfiguration.startBulkEditing();
myConfiguration.setDeviceState(matchState);
myConfiguration.setEffectiveDevice(device, matchState);
myConfiguration.finishBulkEditing();
} else {
// no match in current device with any state/locale
// attempt to find another device that can display this
// particular state.
findAndSetCompatibleConfig(currentConfigIsCompatible);
}
}
}
use of com.android.tools.idea.rendering.Locale in project android by JetBrains.
the class ConfigurationManagerTest method testGetLocales.
public void testGetLocales() {
myFixture.copyFileToProject("xmlpull/layout.xml", "res/layout/layout1.xml");
myFixture.copyFileToProject("xmlpull/layout.xml", "res/layout-no-rNO/layout1.xml");
myFixture.copyFileToProject("xmlpull/layout.xml", "res/layout-no/layout1.xml");
myFixture.copyFileToProject("xmlpull/layout.xml", "res/layout-se/layout2.xml");
AndroidFacet facet = AndroidFacet.getInstance(myModule);
assertNotNull(facet);
ConfigurationManager manager = facet.getConfigurationManager();
assertNotNull(manager);
assertSame(manager, facet.getConfigurationManager());
List<Locale> locales = manager.getLocales();
assertEquals(Arrays.asList(Locale.create("no"), Locale.create("no-rNO"), Locale.create("se")), locales);
}
use of com.android.tools.idea.rendering.Locale in project android by JetBrains.
the class LocaleMenuAction method getRelevantLocales.
/**
* Like {@link ConfigurationManager#getLocales} but filters out locales not compatible
* with language and region qualifiers in the current configuration's folder config
*
* @return the list of relevant locales in the project
*/
@NotNull
private List<Locale> getRelevantLocales() {
List<Locale> locales = new ArrayList<Locale>();
Configuration configuration = myRenderContext.getConfiguration();
if (configuration == null) {
return Collections.emptyList();
}
Module module = configuration.getConfigurationManager().getModule();
LocaleQualifier specificLocale = configuration.getEditedConfig().getLocaleQualifier();
// locale.
if (specificLocale != null) {
List<VirtualFile> variations = ResourceHelper.getResourceVariations(configuration.getFile(), false);
for (VirtualFile variation : variations) {
FolderConfiguration config = FolderConfiguration.getConfigForFolder(variation.getParent().getName());
if (config != null && config.getLocaleQualifier() == null) {
specificLocale = null;
break;
}
}
}
LocalResourceRepository projectResources = ProjectResourceRepository.getProjectResources(module, true);
Set<LocaleQualifier> languages = projectResources != null ? projectResources.getLocales() : Collections.<LocaleQualifier>emptySet();
for (LocaleQualifier l : languages) {
if (specificLocale != null && !specificLocale.isMatchFor(l)) {
continue;
}
locales.add(Locale.create(l));
}
return locales;
}
use of com.android.tools.idea.rendering.Locale in project android by JetBrains.
the class LocaleMenuAction method getAllLocales.
@NotNull
public static List<Locale> getAllLocales() {
List<String> sorted = LocaleManager.getLanguageCodes(true);
List<Locale> locales = new ArrayList<Locale>(sorted.size());
for (String language : sorted) {
LocaleQualifier qualifier = new LocaleQualifier(language.length() == 2 ? language : BCP_47_PREFIX + language, language, null, null);
Locale locale = Locale.create(qualifier);
locales.add(locale);
}
return locales;
}
use of com.android.tools.idea.rendering.Locale in project android by JetBrains.
the class LocaleMenuAction method createPopupActionGroup.
@Override
@NotNull
protected DefaultActionGroup createPopupActionGroup() {
DefaultActionGroup group = new DefaultActionGroup(null, true);
// TODO: Offer submenus, lazily populated, which offer languages either by code or by name.
// However, this doesn't currently work for the JBPopup dialog we're using as part
// of the combo action (and using the JBPopup dialog rather than a Swing menu has some
// other advantages: fitting in with the overall IDE look and feel (e.g. dark colors),
// allowing typing to filter, etc.
List<Locale> locales = getRelevantLocales();
Configuration configuration = myRenderContext.getConfiguration();
if (configuration != null && locales.size() > 0) {
group.add(new SetLocaleAction(myRenderContext, getLocaleLabel(Locale.ANY, false, myClassicStyle), Locale.ANY));
group.addSeparator();
Collections.sort(locales, Locale.LANGUAGE_CODE_COMPARATOR);
for (Locale locale : locales) {
String title = getLocaleLabel(locale, false, myClassicStyle);
VirtualFile better = ConfigurationMatcher.getBetterMatch(configuration, null, null, locale, null);
if (better != null) {
title = ConfigurationAction.getBetterMatchLabel(getLocaleLabel(locale, true, myClassicStyle), better, configuration.getFile());
}
group.add(new SetLocaleAction(myRenderContext, title, locale));
}
group.addSeparator();
}
group.add(new EditTranslationAction());
return group;
}
Aggregations