import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.SortedSet;
FolderConfiguration testConfig = new FolderConfiguration();
+ // get a locale that match the host locale roughly (may not be exact match on the region.)
+ ResourceQualifier[] localeHostMatch = getLocaleMatch();
+
mainloop: for (LayoutDevice device : mDeviceList) {
for (DeviceConfig config : device.getConfigs()) {
testConfig.set(config.getConfig());
- // look on the locales.
- for (int i = 0 ; i < mLocaleList.size() ; i++) {
- ResourceQualifier[] locale = mLocaleList.get(i);
+ // 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)
+ if (mEditedConfig.getLanguageQualifier() != null || localeHostMatch == null) {
+ // look on the locales.
+ for (int i = 0 ; i < mLocaleList.size() ; i++) {
+ ResourceQualifier[] locale = mLocaleList.get(i);
+
+ // update the test config with the locale qualifiers
+ testConfig.setLanguageQualifier((LanguageQualifier)locale[LOCALE_LANG]);
+ testConfig.setRegionQualifier((RegionQualifier)locale[LOCALE_REGION]);
+
+ if (mEditedConfig.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.
+ if (anyDeviceMatch == null) {
+ anyDeviceMatch = device;
+ anyConfigMatchName = config.getName();
+ anyLocaleIndex = i;
+ }
+ if (isCurrentFileBestMatchFor(testConfig)) {
+ // this is what we want.
+ bestDeviceMatch = device;
+ bestConfigMatchName = config.getName();
+ bestLocaleIndex = i;
+ break mainloop;
+ }
+ }
+ }
+ } else {
// update the test config with the locale qualifiers
- testConfig.setLanguageQualifier((LanguageQualifier)locale[LOCALE_LANG]);
- testConfig.setRegionQualifier((RegionQualifier)locale[LOCALE_REGION]);
+ testConfig.setLanguageQualifier(
+ (LanguageQualifier)localeHostMatch[LOCALE_LANG]);
+ testConfig.setRegionQualifier(
+ (RegionQualifier)localeHostMatch[LOCALE_REGION]);
if (mEditedConfig.isMatchFor(testConfig)) {
// this is a basic match. record it in case we don't find a match
if (anyDeviceMatch == null) {
anyDeviceMatch = device;
anyConfigMatchName = config.getName();
- anyLocaleIndex = i;
+ anyLocaleIndex = mLocaleList.indexOf(localeHostMatch);
}
if (isCurrentFileBestMatchFor(testConfig)) {
// this is what we want.
bestDeviceMatch = device;
bestConfigMatchName = config.getName();
- bestLocaleIndex = i;
+ bestLocaleIndex = mLocaleList.indexOf(localeHostMatch);
break mainloop;
}
}
}
}
+ private ResourceQualifier[] getLocaleMatch() {
+ Locale locale = Locale.getDefault();
+ if (locale != null) {
+ String currentLanguage = locale.getLanguage();
+ String currentRegion = locale.getCountry();
+
+ final int count = mLocaleList.size();
+ for (int l = 0 ; l < count ; l++) {
+ ResourceQualifier[] localeArray = mLocaleList.get(l);
+ LanguageQualifier langQ = (LanguageQualifier)localeArray[LOCALE_LANG];
+ RegionQualifier regionQ = (RegionQualifier)localeArray[LOCALE_REGION];
+
+ // there's always a ##/Other or ##/Any (which is the same, the region
+ // contains FAKE_REGION_VALUE). If we don't find a perfect region match
+ // we take the fake region. Since it's last in the list, this makes the
+ // test easy.
+ if (langQ.getValue().equals(currentLanguage) &&
+ (regionQ.getValue().equals(currentRegion) ||
+ regionQ.getValue().equals(RegionQualifier.FAKE_REGION_VALUE))) {
+ return localeArray;
+ }
+ }
+
+ // if no locale match the current local locale, it's likely that it is
+ // the default one which is the last one.
+ return mLocaleList.get(count - 1);
+ }
+
+ return null;
+ }
+
/**
* Updates the theme combo.
* This must be called from the UI thread.