List genericVariants = variablePolicy.getGenericImages();
if (genericVariants == null || genericVariants.isEmpty()) {
return null;
}
InternalDevice device = context.getDevice();
List images = new ArrayList(genericVariants);
int maxWidth = -1;
int deviceWidth = device.getPixelsX();
EncodingCollection supportedEncodings =
device.getSupportedImageEncodings();
// Check each image is supported by the requesting device. If not then
// we can discard the image from our selection. Also remove any images that
// are wider than the width of the device.
// for (int i = 0; i < images.length; i++) {
for (Iterator i = images.iterator(); i.hasNext();) {
Variant variant = (Variant) i.next();
ImageMetaData image = (ImageMetaData) variant.getMetaData();
ImageEncoding encoding = image.getImageEncoding();
if (!supportedEncodings.contains(encoding)) {
if (logger.isDebugEnabled()) {
logger.debug("Discarding variant " + variant +
" as device does not support encoding.");
}
i.remove();
continue;
}
int currentWidth = image.getWidth();
if (currentWidth > deviceWidth) {
if (logger.isDebugEnabled()) {
logger.debug("Discarding variant " + variant +
" - width greater than device width.");
}
i.remove();
continue;
}
}
// If the list is empty then the device does not support any of the
// images in the collection we are testing so we can return null.
if (images.isEmpty()) {
return null;
}
// Remove any images that are wider than the specified widthHint percentage
// of the device width. In this loop we also find the maximum width of all
// the remaining variants.
for (Iterator i = images.iterator(); i.hasNext();) {
Variant variant = (Variant) i.next();
ImageMetaData image = (ImageMetaData) variant.getMetaData();
GenericImageSelection generic = (GenericImageSelection)
variant.getSelection();
int widthHint = generic.getWidthHint();
int currentWidth = image.getWidth();
if (widthHint != 0) {
int allowedWidth = (widthHint * deviceWidth) / 100;
if (currentWidth > allowedWidth) {
if (logger.isDebugEnabled()) {
logger.debug("Discarding variant " + variant +
" - width (" + currentWidth +
") greater than " + widthHint +
"% of device width (" + deviceWidth + ")");
}
i.remove();
continue;
}
}
if (currentWidth > maxWidth) {
maxWidth = currentWidth;
}
}
// If the list is empty then the device does not support any of the
// images in the collection we are testing so we can return null.
if (images.isEmpty()) {
return null;
}
// Remove any images that are less than the maximum width as we always want
// to return the largest possible image. Here we also check if any of the
// images' rendering type matches the device.
ImageRendering deviceRenderingMode = device.getRenderMode();
boolean renderingMatch = false;
for (Iterator i = images.iterator(); i.hasNext();) {
Variant variant = (Variant) i.next();
ImageMetaData image = (ImageMetaData) variant.getMetaData();
if (image.getWidth() < maxWidth) {
if (logger.isDebugEnabled()) {
logger.debug("Discarding variant " + variant +
" - width less than best width.");
}
i.remove();
continue;
}
if (image.getRendering() == deviceRenderingMode) {
renderingMatch = true;
}
}
// If the list is empty then the device does not support any of the
// images in the collection we are testing so we can return null.
if (images.isEmpty()) {
return null;
}
int devicePixelDepth = device.getPixelDepth();
// there is an image with a pixelDepth => devicePixelDepth
boolean pixelDepthMatch = false;
// there is an image with a pixelDepth of 1 when the device supports better
boolean poorPixelDepthMatch = false;