Themes: Restore original value if getValue fails

Add a catch all around the guts of IconCustomizer.getValue and
restore the original outValue before returning if an exception
does occur, with some log output for our own sanity.

Change-Id: I021760cd5dc07d9cfbbced09c514b86fb997f7d0
TICKET: CYNGNOS-2432
diff --git a/core/java/android/app/IconPackHelper.java b/core/java/android/app/IconPackHelper.java
index 80fb401..9c71ddd 100644
--- a/core/java/android/app/IconPackHelper.java
+++ b/core/java/android/app/IconPackHelper.java
@@ -633,33 +633,42 @@
             }
             TypedValue tempValue = new TypedValue();
             tempValue.setTo(outValue);
-            outValue.assetCookie = COMPOSED_ICON_COOKIE;
-            outValue.data = resId & (COMPOSED_ICON_COOKIE << 24 | 0x00ffffff);
-            outValue.string = getCachedIconPath(pkgName, resId, outValue.density);
-            int hashCode = outValue.string.hashCode() & 0x7fffffff;
-            int defaultSwatchColor = 0;
+            // Catch all exceptions and restore outValue to tempValue if one occurs
+            try {
+                outValue.assetCookie = COMPOSED_ICON_COOKIE;
+                outValue.data = resId & (COMPOSED_ICON_COOKIE << 24 | 0x00ffffff);
+                outValue.string = getCachedIconPath(pkgName, resId, outValue.density);
+                int hashCode = outValue.string.hashCode() & 0x7fffffff;
+                int defaultSwatchColor = 0;
 
-            if (!(new File(outValue.string.toString()).exists())) {
-                // compose the icon and cache it
-                int back = 0;
-                if (iconInfo.swatchType != ComposedIconInfo.SwatchType.None) {
-                    back = iconInfo.iconPaletteBack;
-                    if (iconInfo.defaultSwatchColors.length > 0) {
-                        defaultSwatchColor =iconInfo.defaultSwatchColors[
-                                hashCode % iconInfo.defaultSwatchColors.length];
+                if (!(new File(outValue.string.toString()).exists())) {
+                    // compose the icon and cache it
+                    int back = 0;
+                    if (iconInfo.swatchType != ComposedIconInfo.SwatchType.None) {
+                        back = iconInfo.iconPaletteBack;
+                        if (iconInfo.defaultSwatchColors.length > 0) {
+                            defaultSwatchColor = iconInfo.defaultSwatchColors[
+                                    hashCode % iconInfo.defaultSwatchColors.length];
+                        }
+                    } else if (iconInfo.iconBacks != null && iconInfo.iconBacks.length > 0) {
+                        back = iconInfo.iconBacks[hashCode % iconInfo.iconBacks.length];
                     }
-                } else if (iconInfo.iconBacks != null && iconInfo.iconBacks.length > 0) {
-                    back = iconInfo.iconBacks[hashCode % iconInfo.iconBacks.length];
+                    if (DEBUG) {
+                        Log.d(TAG, "Composing icon for " + pkgName);
+                    }
+                    Bitmap bmp = createIconBitmap(baseIcon, res, back, defaultSwatchColor,
+                            iconInfo);
+                    if (!cacheComposedIcon(bmp,
+                            getCachedIconName(pkgName, resId, outValue.density))) {
+                        Log.w(TAG, "Unable to cache icon " + outValue.string);
+                        // restore the original TypedValue
+                        outValue.setTo(tempValue);
+                    }
                 }
-                if (DEBUG) {
-                    Log.d(TAG, "Composing icon for " + pkgName);
-                }
-                Bitmap bmp = createIconBitmap(baseIcon, res, back, defaultSwatchColor, iconInfo);
-                if (!cacheComposedIcon(bmp, getCachedIconName(pkgName, resId, outValue.density))) {
-                    Log.w(TAG, "Unable to cache icon " + outValue.string);
-                    // restore the original TypedValue
-                    outValue.setTo(tempValue);
-                }
+            } catch (Exception e) {
+                // catch all, restore the original value and log it
+                outValue.setTo(tempValue);
+                Log.w(TAG, "getValue failed for " + outValue.string, e);
             }
         }