Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

packages-apps-Eleven: Commit

packages/apps/Eleven


Commit MetaInfo

Revision15f97bbbbfebe259b439b753b3fffaf07d2b9d68 (tree)
Time2019-03-20 05:56:33
AuthorAlexander Martinz <amartinz@shif...>
CommiterMichael Bestas

Log Message

ImageCache: add missing close call and cleanup

Change-Id: Id75ae226307c1045d2533e4f61443aee3d076c3e
Signed-off-by: Alexander Martinz <amartinz@shiftphones.com>

Change Summary

Incremental Difference

--- a/src/org/lineageos/eleven/cache/ImageCache.java
+++ b/src/org/lineageos/eleven/cache/ImageCache.java
@@ -13,7 +13,6 @@
1313
1414 package org.lineageos.eleven.cache;
1515
16-import android.annotation.SuppressLint;
1716 import android.app.Activity;
1817 import android.app.ActivityManager;
1918 import android.app.Fragment;
@@ -35,6 +34,7 @@ import android.util.Log;
3534
3635 import org.lineageos.eleven.cache.disklrucache.DiskLruCache;
3736 import org.lineageos.eleven.utils.ElevenUtils;
37+import org.lineageos.eleven.utils.IoUtils;
3838
3939 import java.io.File;
4040 import java.io.FileDescriptor;
@@ -50,7 +50,6 @@ import java.util.HashSet;
5050 * This class holds the memory and disk bitmap caches.
5151 */
5252 public final class ImageCache {
53-
5453 private static final String TAG = ImageCache.class.getSimpleName();
5554
5655 /**
@@ -184,7 +183,6 @@ public final class ImageCache {
184183 *
185184 * @param context The {@link Context} to use
186185 */
187- @SuppressLint("NewApi")
188186 public void initLruCache(final Context context) {
189187 final ActivityManager activityManager = (ActivityManager)context
190188 .getSystemService(Context.ACTIVITY_SERVICE);
@@ -195,9 +193,6 @@ public final class ImageCache {
195193 // Release some memory as needed
196194 context.registerComponentCallbacks(new ComponentCallbacks2() {
197195
198- /**
199- * {@inheritDoc}
200- */
201196 @Override
202197 public void onTrimMemory(final int level) {
203198 if (level >= TRIM_MEMORY_MODERATE) {
@@ -207,17 +202,11 @@ public final class ImageCache {
207202 }
208203 }
209204
210- /**
211- * {@inheritDoc}
212- */
213205 @Override
214206 public void onLowMemory() {
215207 // Nothing to do
216208 }
217209
218- /**
219- * {@inheritDoc}
220- */
221210 @Override
222211 public void onConfigurationChanged(final Configuration newConfig) {
223212 // Nothing to do
@@ -234,7 +223,7 @@ public final class ImageCache {
234223 * @return An existing retained ImageCache object or a new one if one did
235224 * not exist
236225 */
237- public static final ImageCache findOrCreateCache(final Activity activity) {
226+ public static ImageCache findOrCreateCache(final Activity activity) {
238227
239228 // Search for, or create an instance of the non-UI RetainFragment
240229 final RetainFragment retainFragment = findOrCreateRetainFragment(
@@ -259,7 +248,7 @@ public final class ImageCache {
259248 * @return The existing instance of the {@link Fragment} or the new instance
260249 * if just created
261250 */
262- public static final RetainFragment findOrCreateRetainFragment(final FragmentManager fm) {
251+ public static RetainFragment findOrCreateRetainFragment(final FragmentManager fm) {
263252 // Check to see if we have retained the worker fragment
264253 RetainFragment retainFragment = (RetainFragment)fm.findFragmentByTag(TAG);
265254
@@ -317,23 +306,14 @@ public final class ImageCache {
317306 }
318307 }
319308 } catch (final IOException e) {
320- Log.e(TAG, "addBitmapToCache - " + e);
309+ Log.e(TAG, "addBitmapToCache", e);
321310 } catch (final IllegalStateException e) {
322311 // if the user clears the cache while we have an async task going we could try
323312 // writing to the disk cache while it isn't ready. Catching here will silently
324313 // fail instead
325- Log.e(TAG, "addBitmapToCache - " + e);
314+ Log.e(TAG, "addBitmapToCache", e);
326315 } finally {
327- try {
328- if (out != null) {
329- out.close();
330- out = null;
331- }
332- } catch (final IOException e) {
333- Log.e(TAG, "addBitmapToCache - " + e);
334- } catch (final IllegalStateException e) {
335- Log.e(TAG, "addBitmapToCache - " + e);
336- }
316+ IoUtils.closeQuietly(out);
337317 }
338318 }
339319 }
@@ -417,14 +397,9 @@ public final class ImageCache {
417397 }
418398 }
419399 } catch (final IOException e) {
420- Log.e(TAG, "getBitmapFromDiskCache - " + e);
400+ Log.e(TAG, "getBitmapFromDiskCache", e);
421401 } finally {
422- try {
423- if (inputStream != null) {
424- inputStream.close();
425- }
426- } catch (final IOException e) {
427- }
402+ IoUtils.closeQuietly(inputStream);
428403 }
429404 }
430405 return null;
@@ -489,10 +464,11 @@ public final class ImageCache {
489464 }
490465 Bitmap artwork = null;
491466 waitUntilUnpaused();
467+
468+ ParcelFileDescriptor parcelFileDescriptor = null;
492469 try {
493470 final Uri uri = ContentUris.withAppendedId(mArtworkUri, albumId);
494- final ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver()
495- .openFileDescriptor(uri, "r");
471+ parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
496472 if (parcelFileDescriptor != null) {
497473 final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
498474 artwork = BitmapFactory.decodeFileDescriptor(fileDescriptor);
@@ -504,6 +480,8 @@ public final class ImageCache {
504480 } catch (final OutOfMemoryError evict) {
505481 // Log.e(TAG, "OutOfMemoryError - getArtworkFromFile - ", evict);
506482 evictAll();
483+ } finally {
484+ IoUtils.closeQuietly(parcelFileDescriptor);
507485 }
508486 return artwork;
509487 }
@@ -523,12 +501,12 @@ public final class ImageCache {
523501 mDiskCache.flush();
524502 }
525503 } catch (final IOException e) {
526- Log.e(TAG, "flush - " + e);
504+ Log.e(TAG, "flush", e);
527505 }
528506 }
529507 return null;
530508 }
531- }, (Void[])null);
509+ });
532510 }
533511
534512 /**
@@ -546,13 +524,13 @@ public final class ImageCache {
546524 mDiskCache = null;
547525 }
548526 } catch (final IOException e) {
549- Log.e(TAG, "clearCaches - " + e);
527+ Log.e(TAG, "clearCaches", e);
550528 }
551529 // Clear the memory cache
552530 evictAll();
553531 return null;
554532 }
555- }, (Void[])null);
533+ });
556534 }
557535
558536 /**
@@ -572,12 +550,12 @@ public final class ImageCache {
572550 mDiskCache = null;
573551 }
574552 } catch (final IOException e) {
575- Log.e(TAG, "close - " + e);
553+ Log.e(TAG, "close", e);
576554 }
577555 }
578556 return null;
579557 }
580- }, (Void[]) null);
558+ });
581559 }
582560
583561 /**
@@ -609,7 +587,7 @@ public final class ImageCache {
609587 mDiskCache.remove(hashKeyForDisk(key));
610588 }
611589 } catch (final IOException e) {
612- Log.e(TAG, "remove - " + e);
590+ Log.e(TAG, "removeFromCache(" + key + ")", e);
613591 }
614592 flush();
615593 }
@@ -672,7 +650,7 @@ public final class ImageCache {
672650 * directory
673651 * @return The cache directory
674652 */
675- public static final File getDiskCacheDir(final Context context, final String uniqueName) {
653+ public static File getDiskCacheDir(final Context context, final String uniqueName) {
676654 // getExternalCacheDir(context) returns null if external storage is not ready
677655 final String cachePath = getExternalCacheDir(context) != null
678656 ? getExternalCacheDir(context).getPath()
@@ -686,7 +664,7 @@ public final class ImageCache {
686664 * @return True if external storage is removable (like an SD card), false
687665 * otherwise
688666 */
689- public static final boolean isExternalStorageRemovable() {
667+ public static boolean isExternalStorageRemovable() {
690668 return Environment.isExternalStorageRemovable();
691669 }
692670
@@ -696,7 +674,7 @@ public final class ImageCache {
696674 * @param context The {@link Context} to use
697675 * @return The external cache directory
698676 */
699- public static final File getExternalCacheDir(final Context context) {
677+ public static File getExternalCacheDir(final Context context) {
700678 return context.getExternalCacheDir();
701679 }
702680
@@ -706,7 +684,7 @@ public final class ImageCache {
706684 * @param path The path to check
707685 * @return The space available in bytes
708686 */
709- public static final long getUsableSpace(final File path) {
687+ public static long getUsableSpace(final File path) {
710688 return path.getUsableSpace();
711689 }
712690
@@ -716,7 +694,7 @@ public final class ImageCache {
716694 *
717695 * @param key The key used to store the file
718696 */
719- public static final String hashKeyForDisk(final String key) {
697+ public static String hashKeyForDisk(final String key) {
720698 String cacheKey;
721699 try {
722700 final MessageDigest digest = MessageDigest.getInstance("MD5");
@@ -735,7 +713,7 @@ public final class ImageCache {
735713 * @return A {@link String} converted from the bytes of a hashable key used
736714 * to store a filename on the disk, to hex digits.
737715 */
738- private static final String bytesToHexString(final byte[] bytes) {
716+ private static String bytesToHexString(final byte[] bytes) {
739717 final StringBuilder builder = new StringBuilder();
740718 for (final byte b : bytes) {
741719 final String hex = Integer.toHexString(0xFF & b);
@@ -765,9 +743,6 @@ public final class ImageCache {
765743 public RetainFragment() {
766744 }
767745
768- /**
769- * {@inheritDoc}
770- */
771746 @Override
772747 public void onCreate(final Bundle savedInstanceState) {
773748 super.onCreate(savedInstanceState);
@@ -811,18 +786,13 @@ public final class ImageCache {
811786 /**
812787 * Get the size in bytes of a bitmap.
813788 */
814- public static final int getBitmapSize(final Bitmap bitmap) {
789+ public static int getBitmapSize(final Bitmap bitmap) {
815790 return bitmap.getByteCount();
816791 }
817792
818- /**
819- * {@inheritDoc}
820- */
821793 @Override
822794 protected int sizeOf(final String paramString, final Bitmap paramBitmap) {
823795 return getBitmapSize(paramBitmap);
824796 }
825-
826797 }
827-
828798 }
--- /dev/null
+++ b/src/org/lineageos/eleven/utils/IoUtils.java
@@ -0,0 +1,36 @@
1+/*
2+ * Copyright (C) 2019 The LineageOS Project
3+ * Copyright (C) 2019 SHIFT GmbH
4+ *
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+package org.lineageos.eleven.utils;
18+
19+import android.support.annotation.Nullable;
20+
21+import java.io.Closeable;
22+import java.net.Socket;
23+
24+public class IoUtils {
25+ public static void closeQuietly(@Nullable final Object object) {
26+ try {
27+ if (object instanceof Socket) {
28+ ((Socket) object).close();
29+ } else if (object instanceof Closeable) {
30+ ((Closeable) object).close();
31+ }
32+ } catch (Exception ignored) {
33+ // ignored
34+ }
35+ }
36+}
Show on old repository browser