diff options
author | d34d <clark@cyngn.com> | 2015-04-01 12:53:56 -0700 |
---|---|---|
committer | Gerrit Code Review <gerrit@cyngn.com> | 2015-04-03 15:18:46 +0000 |
commit | 41d8ed49f5fafea64b0525f1ea5001d80d12139a (patch) | |
tree | b7656e5927169bdbaf86391d6e1ce7b30b2c5675 /src | |
parent | a1610180219d71ae21ee92dd85dd8df0267a72e7 (diff) | |
download | packages_apps_ThemeChooser-41d8ed49f5fafea64b0525f1ea5001d80d12139a.tar.gz packages_apps_ThemeChooser-41d8ed49f5fafea64b0525f1ea5001d80d12139a.tar.bz2 packages_apps_ThemeChooser-41d8ed49f5fafea64b0525f1ea5001d80d12139a.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/com/cyngn/theme/util/Utils.java | 107 |
1 files changed, 56 insertions, 51 deletions
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; } |