Gallery2: Make sure no NPE happens

Author: Michael W <baddaemon87@gmail.com>
Date:   Sat Jun 4 16:10:05 2016 +0200

    Gallery2: Make sure no NPE happens

    Fix for the case when getExternalCacheDir returns null

    Change-Id: I15f4a4a37ac44d70d14ac08cd743d3327f91dde1

Author: Michael W <baddaemon87@gmail.com>
Date:   Tue Jun 7 21:05:00 2016 +0200

    Gallery2: Remove more possible NPEs

    getCache can return null ->
    check this before proceeding

    Change-Id: I834780a4dafbe22f2d345fe5571cb20f3f3e5e2e

Change-Id: I820d8314f855329885f6b7ca710efa981b9d327c
diff --git a/src/com/android/gallery3d/app/MoviePlayer.java b/src/com/android/gallery3d/app/MoviePlayer.java
index 4fea7f1..8bf0d0d 100755
--- a/src/com/android/gallery3d/app/MoviePlayer.java
+++ b/src/com/android/gallery3d/app/MoviePlayer.java
@@ -1703,6 +1703,7 @@
             BlobCache cache = CacheManager.getCache(mContext,
                     BOOKMARK_CACHE_FILE, BOOKMARK_CACHE_MAX_ENTRIES,
                     BOOKMARK_CACHE_MAX_BYTES, BOOKMARK_CACHE_VERSION);
+            if (cache == null) return null;
 
             byte[] data = cache.lookup(uri.hashCode());
             if (data == null) return null;
diff --git a/src/com/android/gallery3d/data/ImageCacheService.java b/src/com/android/gallery3d/data/ImageCacheService.java
index 129c113..861d4b7 100644
--- a/src/com/android/gallery3d/data/ImageCacheService.java
+++ b/src/com/android/gallery3d/data/ImageCacheService.java
@@ -56,6 +56,8 @@
      * @return true if the image data is found; false if not found.
      */
     public boolean getImageData(Path path, long timeModified, int type, BytesBuffer buffer) {
+        if (mCache == null) return false;
+
         byte[] key = makeKey(path, timeModified, type);
         long cacheKey = Utils.crc64Long(key);
         try {
@@ -81,6 +83,8 @@
     }
 
     public void putImageData(Path path, long timeModified, int type, byte[] value) {
+        if (mCache == null) return;
+
         byte[] key = makeKey(path, timeModified, type);
         long cacheKey = Utils.crc64Long(key);
         ByteBuffer buffer = ByteBuffer.allocate(key.length + value.length);
@@ -99,6 +103,8 @@
     }
 
     public void clearImageData(Path path, long timeModified, int type) {
+        if (mCache == null) return;
+
         byte[] key = makeKey(path, timeModified, type);
         long cacheKey = Utils.crc64Long(key);
         if (mCache == null) {
diff --git a/src/com/android/gallery3d/util/CacheManager.java b/src/com/android/gallery3d/util/CacheManager.java
index 0d76ce1..bba9f4e 100644
--- a/src/com/android/gallery3d/util/CacheManager.java
+++ b/src/com/android/gallery3d/util/CacheManager.java
@@ -46,13 +46,15 @@
             BlobCache cache = sCacheMap.get(filename);
             if (cache == null) {
                 File cacheDir = context.getCacheDir();
-                String path = cacheDir.getAbsolutePath() + "/" + filename;
-                try {
-                    cache = new BlobCache(path, maxEntries, maxBytes, false,
-                            version);
-                    sCacheMap.put(filename, cache);
-                } catch (IOException e) {
-                    Log.e(TAG, "Cannot instantiate cache!", e);
+                if (cacheDir != null) {
+                    String path = cacheDir.getAbsolutePath() + "/" + filename;
+                    try {
+                        cache = new BlobCache(path, maxEntries, maxBytes, false,
+                                version);
+                        sCacheMap.put(filename, cache);
+                    } catch (IOException e) {
+                        Log.e(TAG, "Cannot instantiate cache!", e);
+                    }
                 }
             }
             return cache;
@@ -73,10 +75,12 @@
         pref.edit().putInt(KEY_CACHE_UP_TO_DATE, 1).commit();
 
         File cacheDir = context.getCacheDir();
-        String prefix = cacheDir.getAbsolutePath() + "/";
+        if (cacheDir != null) {
+            String prefix = cacheDir.getAbsolutePath() + "/";
 
-        BlobCache.deleteFiles(prefix + "imgcache");
-        BlobCache.deleteFiles(prefix + "rev_geocoding");
-        BlobCache.deleteFiles(prefix + "bookmark");
+            BlobCache.deleteFiles(prefix + "imgcache");
+            BlobCache.deleteFiles(prefix + "rev_geocoding");
+            BlobCache.deleteFiles(prefix + "bookmark");
+        }
     }
 }