packages/apps/Eleven
Revision | 15f97bbbbfebe259b439b753b3fffaf07d2b9d68 (tree) |
---|---|
Time | 2019-03-20 05:56:33 |
Author | Alexander Martinz <amartinz@shif...> |
Commiter | Michael Bestas |
ImageCache: add missing close call and cleanup
Change-Id: Id75ae226307c1045d2533e4f61443aee3d076c3e
Signed-off-by: Alexander Martinz <amartinz@shiftphones.com>
@@ -13,7 +13,6 @@ | ||
13 | 13 | |
14 | 14 | package org.lineageos.eleven.cache; |
15 | 15 | |
16 | -import android.annotation.SuppressLint; | |
17 | 16 | import android.app.Activity; |
18 | 17 | import android.app.ActivityManager; |
19 | 18 | import android.app.Fragment; |
@@ -35,6 +34,7 @@ import android.util.Log; | ||
35 | 34 | |
36 | 35 | import org.lineageos.eleven.cache.disklrucache.DiskLruCache; |
37 | 36 | import org.lineageos.eleven.utils.ElevenUtils; |
37 | +import org.lineageos.eleven.utils.IoUtils; | |
38 | 38 | |
39 | 39 | import java.io.File; |
40 | 40 | import java.io.FileDescriptor; |
@@ -50,7 +50,6 @@ import java.util.HashSet; | ||
50 | 50 | * This class holds the memory and disk bitmap caches. |
51 | 51 | */ |
52 | 52 | public final class ImageCache { |
53 | - | |
54 | 53 | private static final String TAG = ImageCache.class.getSimpleName(); |
55 | 54 | |
56 | 55 | /** |
@@ -184,7 +183,6 @@ public final class ImageCache { | ||
184 | 183 | * |
185 | 184 | * @param context The {@link Context} to use |
186 | 185 | */ |
187 | - @SuppressLint("NewApi") | |
188 | 186 | public void initLruCache(final Context context) { |
189 | 187 | final ActivityManager activityManager = (ActivityManager)context |
190 | 188 | .getSystemService(Context.ACTIVITY_SERVICE); |
@@ -195,9 +193,6 @@ public final class ImageCache { | ||
195 | 193 | // Release some memory as needed |
196 | 194 | context.registerComponentCallbacks(new ComponentCallbacks2() { |
197 | 195 | |
198 | - /** | |
199 | - * {@inheritDoc} | |
200 | - */ | |
201 | 196 | @Override |
202 | 197 | public void onTrimMemory(final int level) { |
203 | 198 | if (level >= TRIM_MEMORY_MODERATE) { |
@@ -207,17 +202,11 @@ public final class ImageCache { | ||
207 | 202 | } |
208 | 203 | } |
209 | 204 | |
210 | - /** | |
211 | - * {@inheritDoc} | |
212 | - */ | |
213 | 205 | @Override |
214 | 206 | public void onLowMemory() { |
215 | 207 | // Nothing to do |
216 | 208 | } |
217 | 209 | |
218 | - /** | |
219 | - * {@inheritDoc} | |
220 | - */ | |
221 | 210 | @Override |
222 | 211 | public void onConfigurationChanged(final Configuration newConfig) { |
223 | 212 | // Nothing to do |
@@ -234,7 +223,7 @@ public final class ImageCache { | ||
234 | 223 | * @return An existing retained ImageCache object or a new one if one did |
235 | 224 | * not exist |
236 | 225 | */ |
237 | - public static final ImageCache findOrCreateCache(final Activity activity) { | |
226 | + public static ImageCache findOrCreateCache(final Activity activity) { | |
238 | 227 | |
239 | 228 | // Search for, or create an instance of the non-UI RetainFragment |
240 | 229 | final RetainFragment retainFragment = findOrCreateRetainFragment( |
@@ -259,7 +248,7 @@ public final class ImageCache { | ||
259 | 248 | * @return The existing instance of the {@link Fragment} or the new instance |
260 | 249 | * if just created |
261 | 250 | */ |
262 | - public static final RetainFragment findOrCreateRetainFragment(final FragmentManager fm) { | |
251 | + public static RetainFragment findOrCreateRetainFragment(final FragmentManager fm) { | |
263 | 252 | // Check to see if we have retained the worker fragment |
264 | 253 | RetainFragment retainFragment = (RetainFragment)fm.findFragmentByTag(TAG); |
265 | 254 |
@@ -317,23 +306,14 @@ public final class ImageCache { | ||
317 | 306 | } |
318 | 307 | } |
319 | 308 | } catch (final IOException e) { |
320 | - Log.e(TAG, "addBitmapToCache - " + e); | |
309 | + Log.e(TAG, "addBitmapToCache", e); | |
321 | 310 | } catch (final IllegalStateException e) { |
322 | 311 | // if the user clears the cache while we have an async task going we could try |
323 | 312 | // writing to the disk cache while it isn't ready. Catching here will silently |
324 | 313 | // fail instead |
325 | - Log.e(TAG, "addBitmapToCache - " + e); | |
314 | + Log.e(TAG, "addBitmapToCache", e); | |
326 | 315 | } 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); | |
337 | 317 | } |
338 | 318 | } |
339 | 319 | } |
@@ -417,14 +397,9 @@ public final class ImageCache { | ||
417 | 397 | } |
418 | 398 | } |
419 | 399 | } catch (final IOException e) { |
420 | - Log.e(TAG, "getBitmapFromDiskCache - " + e); | |
400 | + Log.e(TAG, "getBitmapFromDiskCache", e); | |
421 | 401 | } finally { |
422 | - try { | |
423 | - if (inputStream != null) { | |
424 | - inputStream.close(); | |
425 | - } | |
426 | - } catch (final IOException e) { | |
427 | - } | |
402 | + IoUtils.closeQuietly(inputStream); | |
428 | 403 | } |
429 | 404 | } |
430 | 405 | return null; |
@@ -489,10 +464,11 @@ public final class ImageCache { | ||
489 | 464 | } |
490 | 465 | Bitmap artwork = null; |
491 | 466 | waitUntilUnpaused(); |
467 | + | |
468 | + ParcelFileDescriptor parcelFileDescriptor = null; | |
492 | 469 | try { |
493 | 470 | final Uri uri = ContentUris.withAppendedId(mArtworkUri, albumId); |
494 | - final ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver() | |
495 | - .openFileDescriptor(uri, "r"); | |
471 | + parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r"); | |
496 | 472 | if (parcelFileDescriptor != null) { |
497 | 473 | final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); |
498 | 474 | artwork = BitmapFactory.decodeFileDescriptor(fileDescriptor); |
@@ -504,6 +480,8 @@ public final class ImageCache { | ||
504 | 480 | } catch (final OutOfMemoryError evict) { |
505 | 481 | // Log.e(TAG, "OutOfMemoryError - getArtworkFromFile - ", evict); |
506 | 482 | evictAll(); |
483 | + } finally { | |
484 | + IoUtils.closeQuietly(parcelFileDescriptor); | |
507 | 485 | } |
508 | 486 | return artwork; |
509 | 487 | } |
@@ -523,12 +501,12 @@ public final class ImageCache { | ||
523 | 501 | mDiskCache.flush(); |
524 | 502 | } |
525 | 503 | } catch (final IOException e) { |
526 | - Log.e(TAG, "flush - " + e); | |
504 | + Log.e(TAG, "flush", e); | |
527 | 505 | } |
528 | 506 | } |
529 | 507 | return null; |
530 | 508 | } |
531 | - }, (Void[])null); | |
509 | + }); | |
532 | 510 | } |
533 | 511 | |
534 | 512 | /** |
@@ -546,13 +524,13 @@ public final class ImageCache { | ||
546 | 524 | mDiskCache = null; |
547 | 525 | } |
548 | 526 | } catch (final IOException e) { |
549 | - Log.e(TAG, "clearCaches - " + e); | |
527 | + Log.e(TAG, "clearCaches", e); | |
550 | 528 | } |
551 | 529 | // Clear the memory cache |
552 | 530 | evictAll(); |
553 | 531 | return null; |
554 | 532 | } |
555 | - }, (Void[])null); | |
533 | + }); | |
556 | 534 | } |
557 | 535 | |
558 | 536 | /** |
@@ -572,12 +550,12 @@ public final class ImageCache { | ||
572 | 550 | mDiskCache = null; |
573 | 551 | } |
574 | 552 | } catch (final IOException e) { |
575 | - Log.e(TAG, "close - " + e); | |
553 | + Log.e(TAG, "close", e); | |
576 | 554 | } |
577 | 555 | } |
578 | 556 | return null; |
579 | 557 | } |
580 | - }, (Void[]) null); | |
558 | + }); | |
581 | 559 | } |
582 | 560 | |
583 | 561 | /** |
@@ -609,7 +587,7 @@ public final class ImageCache { | ||
609 | 587 | mDiskCache.remove(hashKeyForDisk(key)); |
610 | 588 | } |
611 | 589 | } catch (final IOException e) { |
612 | - Log.e(TAG, "remove - " + e); | |
590 | + Log.e(TAG, "removeFromCache(" + key + ")", e); | |
613 | 591 | } |
614 | 592 | flush(); |
615 | 593 | } |
@@ -672,7 +650,7 @@ public final class ImageCache { | ||
672 | 650 | * directory |
673 | 651 | * @return The cache directory |
674 | 652 | */ |
675 | - public static final File getDiskCacheDir(final Context context, final String uniqueName) { | |
653 | + public static File getDiskCacheDir(final Context context, final String uniqueName) { | |
676 | 654 | // getExternalCacheDir(context) returns null if external storage is not ready |
677 | 655 | final String cachePath = getExternalCacheDir(context) != null |
678 | 656 | ? getExternalCacheDir(context).getPath() |
@@ -686,7 +664,7 @@ public final class ImageCache { | ||
686 | 664 | * @return True if external storage is removable (like an SD card), false |
687 | 665 | * otherwise |
688 | 666 | */ |
689 | - public static final boolean isExternalStorageRemovable() { | |
667 | + public static boolean isExternalStorageRemovable() { | |
690 | 668 | return Environment.isExternalStorageRemovable(); |
691 | 669 | } |
692 | 670 |
@@ -696,7 +674,7 @@ public final class ImageCache { | ||
696 | 674 | * @param context The {@link Context} to use |
697 | 675 | * @return The external cache directory |
698 | 676 | */ |
699 | - public static final File getExternalCacheDir(final Context context) { | |
677 | + public static File getExternalCacheDir(final Context context) { | |
700 | 678 | return context.getExternalCacheDir(); |
701 | 679 | } |
702 | 680 |
@@ -706,7 +684,7 @@ public final class ImageCache { | ||
706 | 684 | * @param path The path to check |
707 | 685 | * @return The space available in bytes |
708 | 686 | */ |
709 | - public static final long getUsableSpace(final File path) { | |
687 | + public static long getUsableSpace(final File path) { | |
710 | 688 | return path.getUsableSpace(); |
711 | 689 | } |
712 | 690 |
@@ -716,7 +694,7 @@ public final class ImageCache { | ||
716 | 694 | * |
717 | 695 | * @param key The key used to store the file |
718 | 696 | */ |
719 | - public static final String hashKeyForDisk(final String key) { | |
697 | + public static String hashKeyForDisk(final String key) { | |
720 | 698 | String cacheKey; |
721 | 699 | try { |
722 | 700 | final MessageDigest digest = MessageDigest.getInstance("MD5"); |
@@ -735,7 +713,7 @@ public final class ImageCache { | ||
735 | 713 | * @return A {@link String} converted from the bytes of a hashable key used |
736 | 714 | * to store a filename on the disk, to hex digits. |
737 | 715 | */ |
738 | - private static final String bytesToHexString(final byte[] bytes) { | |
716 | + private static String bytesToHexString(final byte[] bytes) { | |
739 | 717 | final StringBuilder builder = new StringBuilder(); |
740 | 718 | for (final byte b : bytes) { |
741 | 719 | final String hex = Integer.toHexString(0xFF & b); |
@@ -765,9 +743,6 @@ public final class ImageCache { | ||
765 | 743 | public RetainFragment() { |
766 | 744 | } |
767 | 745 | |
768 | - /** | |
769 | - * {@inheritDoc} | |
770 | - */ | |
771 | 746 | @Override |
772 | 747 | public void onCreate(final Bundle savedInstanceState) { |
773 | 748 | super.onCreate(savedInstanceState); |
@@ -811,18 +786,13 @@ public final class ImageCache { | ||
811 | 786 | /** |
812 | 787 | * Get the size in bytes of a bitmap. |
813 | 788 | */ |
814 | - public static final int getBitmapSize(final Bitmap bitmap) { | |
789 | + public static int getBitmapSize(final Bitmap bitmap) { | |
815 | 790 | return bitmap.getByteCount(); |
816 | 791 | } |
817 | 792 | |
818 | - /** | |
819 | - * {@inheritDoc} | |
820 | - */ | |
821 | 793 | @Override |
822 | 794 | protected int sizeOf(final String paramString, final Bitmap paramBitmap) { |
823 | 795 | return getBitmapSize(paramBitmap); |
824 | 796 | } |
825 | - | |
826 | 797 | } |
827 | - | |
828 | 798 | } |
@@ -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 | +} |