use of com.android.ide.common.resources.configuration.DensityQualifier 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);
}
use of com.android.ide.common.resources.configuration.DensityQualifier in project android by JetBrains.
the class AndroidColorAnnotator method pickBitmapFromXml.
@Nullable
private static File pickBitmapFromXml(@NotNull File file, @NotNull ResourceResolver resourceResolver, @NotNull Project project, @NonNull AndroidFacet facet, @NonNull ResourceValue resourceValue) {
try {
String xml = Files.toString(file, Charsets.UTF_8);
Document document = XmlUtils.parseDocumentSilently(xml, true);
if (document != null && document.getDocumentElement() != null) {
Element root = document.getDocumentElement();
String tag = root.getTagName();
Element target = null;
String attribute = null;
if ("vector".equals(tag)) {
// Take a look and see if we have a bitmap we can fall back to
AppResourceRepository resourceRepository = AppResourceRepository.getAppResources(facet, true);
List<com.android.ide.common.res2.ResourceItem> items = resourceRepository.getResourceItem(resourceValue.getResourceType(), resourceValue.getName());
if (items != null) {
for (com.android.ide.common.res2.ResourceItem item : items) {
FolderConfiguration configuration = item.getConfiguration();
DensityQualifier densityQualifier = configuration.getDensityQualifier();
if (densityQualifier != null) {
Density density = densityQualifier.getValue();
if (density != null && density.isValidValueForDevice()) {
File bitmap = item.getFile();
if (bitmap != null && bitmap.isFile()) {
return bitmap;
}
}
}
}
}
// Vectors are handled in the icon cache
return file;
} else if ("bitmap".equals(tag) || "nine-patch".equals(tag)) {
target = root;
attribute = ATTR_SRC;
} else if ("selector".equals(tag) || "level-list".equals(tag) || "layer-list".equals(tag) || "transition".equals(tag)) {
NodeList children = root.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--) {
Node item = children.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE && TAG_ITEM.equals(item.getNodeName())) {
target = (Element) item;
if (target.hasAttributeNS(ANDROID_URI, ATTR_DRAWABLE)) {
attribute = ATTR_DRAWABLE;
break;
}
}
}
} else if ("clip".equals(tag) || "inset".equals(tag) || "scale".equals(tag)) {
target = root;
attribute = ATTR_DRAWABLE;
} else {
// <shape> etc - no bitmap to be found
return null;
}
if (attribute != null && target.hasAttributeNS(ANDROID_URI, attribute)) {
String src = target.getAttributeNS(ANDROID_URI, attribute);
ResourceValue value = resourceResolver.findResValue(src, false);
if (value != null) {
return ResourceHelper.resolveDrawable(resourceResolver, value, project);
}
}
}
} catch (Throwable ignore) {
// Not logging for now; afraid to risk unexpected crashes in upcoming preview. TODO: Re-enable.
//Logger.getInstance(AndroidColorAnnotator.class).warn(String.format("Could not read/render icon image %1$s", file), e);
}
return null;
}
use of com.android.ide.common.resources.configuration.DensityQualifier in project android by JetBrains.
the class AndroidColorAnnotator method findSmallestDpiVersion.
@Nullable
private static File findSmallestDpiVersion(@NonNull File bitmap) {
File parentFile = bitmap.getParentFile();
if (parentFile == null) {
return null;
}
File resFolder = parentFile.getParentFile();
if (resFolder == null) {
return null;
}
String parentName = parentFile.getName();
FolderConfiguration config = FolderConfiguration.getConfigForFolder(parentName);
if (config == null) {
return null;
}
DensityQualifier qualifier = config.getDensityQualifier();
if (qualifier == null) {
return null;
}
Density density = qualifier.getValue();
if (density != null && density.isValidValueForDevice()) {
String fileName = bitmap.getName();
Density[] densities = Density.values();
// Iterate in reverse, since the Density enum is in descending order
for (int i = densities.length - 1; i >= 0; i--) {
Density d = densities[i];
if (d.isValidValueForDevice()) {
String folder = parentName.replace(density.getResourceValue(), d.getResourceValue());
bitmap = new File(resFolder, folder + File.separator + fileName);
if (bitmap.exists()) {
if (bitmap.length() > MAX_ICON_SIZE) {
// No point continuing the loop; the other densities will be too big too
return null;
}
return bitmap;
}
}
}
}
return null;
}
use of com.android.ide.common.resources.configuration.DensityQualifier in project android_frameworks_base by DirtyUnicorns.
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;
}
Aggregations