From 41d8ed49f5fafea64b0525f1ea5001d80d12139a Mon Sep 17 00:00:00 2001 From: d34d Date: Wed, 1 Apr 2015 12:53:56 -0700 Subject: Return original wallpaper when cropping/resizing fails Some images have dimensions that don't work with the scaling/cropping methods. An IllegalArgumentException is thrown when this happens resulting in the background being the grayscale grid. This patch simply catches the exception and returns the original wallpaper so that the chooser can display it as the background. Change-Id: Ic0f8a021ee2e2dd32aeede05583cfc2fa25261c4 REF: CHOOSER-66 --- src/com/cyngn/theme/util/Utils.java | 107 +++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 51 deletions(-) (limited to 'src/com') diff --git a/src/com/cyngn/theme/util/Utils.java b/src/com/cyngn/theme/util/Utils.java index 6054bf8..720a542 100644 --- a/src/com/cyngn/theme/util/Utils.java +++ b/src/com/cyngn/theme/util/Utils.java @@ -337,66 +337,71 @@ public class Utils { Log.d(TAG, "yOffset: " + yOffset); } - if (wallpaper.getHeight() < dh) { - // need to scale it up vertically - - if (wallpaper.getHeight() > wallpaper.getWidth()) { - // handle portrait wallpaper - float diff = scaledWidth - dw; - int diffhalf = Math.round(diff / 2); + try { + if (wallpaper.getHeight() < dh) { + // need to scale it up vertically + + if (wallpaper.getHeight() > wallpaper.getWidth()) { + // handle portrait wallpaper + float diff = scaledWidth - dw; + int diffhalf = Math.round(diff / 2); + + bitmap = Bitmap.createScaledBitmap(wallpaper, scaledWidth, scaledHeight, true); + bitmap = Bitmap.createBitmap(bitmap, diffhalf, 0, dw, dh); + bitmap = Bitmap.createBitmap(bitmap, xOffset, 0, dw, dh); + } else { + int goldenWidth = Math.round(wallpaper.getHeight() * 1.125f); + int spaceA = (wallpaper.getWidth() - goldenWidth) / 2; + int spaceB = (goldenWidth - Math.round(dh / scale)) / 2; + + bitmap = Bitmap.createBitmap(wallpaper, spaceA, 0, goldenWidth, + wallpaper.getHeight()); + int left = spaceB + Math.round(xOffset / scale); + bitmap = Bitmap.createBitmap(bitmap, left, 0, Math.round(dw / scale), + Math.round(dh / scale)); + } - bitmap = Bitmap.createScaledBitmap(wallpaper, scaledWidth, scaledHeight, true); - bitmap = Bitmap.createBitmap(bitmap, diffhalf, 0, dw, dh); - bitmap = Bitmap.createBitmap(bitmap, xOffset, 0, dw, dh); - } else { - int goldenWidth = Math.round(wallpaper.getHeight() * 1.125f); - int spaceA = (wallpaper.getWidth() - goldenWidth) / 2; - int spaceB = (goldenWidth - Math.round(dh/scale)) / 2; - - bitmap = Bitmap.createBitmap(wallpaper, spaceA, 0, goldenWidth, - wallpaper.getHeight()); - int left = spaceB + Math.round(xOffset/scale); - bitmap = Bitmap.createBitmap(bitmap, left, 0, Math.round(dw / scale), - Math.round(dh / scale)); - } + } else if (wallpaper.getWidth() < dw) { + // need to scale it up horizontally - } else if (wallpaper.getWidth() < dw) { - // need to scale it up horizontally + if (wallpaper.getHeight() > wallpaper.getWidth()) { + // handle portrait wallpaper + return wallpaper; - if (wallpaper.getHeight() > wallpaper.getWidth()) { - // handle portrait wallpaper - return wallpaper; + } else { + // handle landscape wallpaper + float diff = wallpaper.getHeight() - wallpaper.getWidth(); + int diffhalf = Math.round(diff / 2); - } else { - // handle landscape wallpaper - float diff = wallpaper.getHeight() - wallpaper.getWidth(); - int diffhalf = Math.round(diff / 2); + if (diffhalf < 0) { + return wallpaper; + } - if (diffhalf < 0) { - return wallpaper; - } + bitmap = Bitmap.createBitmap( + wallpaper, diffhalf, 0, + wallpaper.getWidth(), wallpaper.getWidth()); - bitmap = Bitmap.createBitmap( - wallpaper, diffhalf, 0, - wallpaper.getWidth(), wallpaper.getWidth()); + // blow it up + bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledWidth, true); - // blow it up - bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledWidth, true); - - bitmap = Bitmap.createBitmap(bitmap, 0, 0, dw, dh); - } + bitmap = Bitmap.createBitmap(bitmap, 0, 0, dw, dh); + } - } else { - // sometimes the wallpaper manager gives incorrect offsets, - // and adds like 200 pixels randomly. If it's bigger than we can handle, calculate - // our own :) - if (yOffset + dh > wallpaper.getHeight()) { - yOffset = (wallpaper.getHeight() - dh) / 2; - } - if (xOffset + dw > wallpaper.getWidth()) { - yOffset = (wallpaper.getWidth() - dw) / 2; + } else { + // sometimes the wallpaper manager gives incorrect offsets, + // and adds like 200 pixels randomly. If it's bigger than we can handle, calculate + // our own :) + if (yOffset + dh > wallpaper.getHeight()) { + yOffset = (wallpaper.getHeight() - dh) / 2; + } + if (xOffset + dw > wallpaper.getWidth()) { + yOffset = (wallpaper.getWidth() - dw) / 2; + } + bitmap = Bitmap.createBitmap(wallpaper, xOffset, yOffset, dw, dh); } - bitmap = Bitmap.createBitmap(wallpaper, xOffset, yOffset, dw, dh); + } catch (IllegalArgumentException e) { + // Cropping/resizing failed so return the original + bitmap = wallpaper; } return bitmap; } -- cgit v1.2.3