-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(android): optimize ImageView image download peformance (#13078)
* refactor(android): optimize ImageView image download peformance Fixes TIMOB-28538 * fix(android): wrong image can be loaded due to hash code collision Fixes TIMOB-18786 * chore(android): improved TiDrawableReference hashing and added toString() * chore(android): simplified ImageView image download handling * refactor(android): image decoding; "defaultRetries" default changed from 5 to 2 * chore(android): change thread pool size to use CPU core count * chore(android): make singleton instance creation thread safe * chore(android): simplify ImageView loaded image handling * chore(android): set min thread pool size to 2 for image/network downloads * test: fix lint error in ti.ui.imageview Co-authored-by: Gary Mathews <contact@garymathews.com>
- Loading branch information
1 parent
7041f6f
commit bdf7e68
Showing
15 changed files
with
444 additions
and
495 deletions.
There are no files selected for viewing
246 changes: 66 additions & 180 deletions
246
android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIImageView.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
android/titanium/src/java/org/appcelerator/titanium/util/TiImageCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2021 by Axway, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
package org.appcelerator.titanium.util; | ||
|
||
import android.graphics.Bitmap; | ||
import org.appcelerator.kroll.KrollRuntime; | ||
import org.appcelerator.titanium.view.TiDrawableReference; | ||
import java.lang.ref.SoftReference; | ||
import java.util.HashMap; | ||
|
||
public final class TiImageCache | ||
{ | ||
private static final HashMap<TiDrawableReference.Key, SoftReference<Bitmap>> bitmapCollection = new HashMap<>(64); | ||
private static final HashMap<TiDrawableReference.Key, TiExifOrientation> orientationCollection = new HashMap<>(64); | ||
|
||
static | ||
{ | ||
KrollRuntime.addOnDisposingListener((KrollRuntime runtime) -> { | ||
clear(); | ||
}); | ||
} | ||
|
||
private TiImageCache() | ||
{ | ||
} | ||
|
||
public static synchronized void add(TiImageInfo imageInfo) | ||
{ | ||
if ((imageInfo != null) && (imageInfo.getKey() != null) && (imageInfo.getBitmap() != null)) { | ||
Bitmap bitmap = imageInfo.getBitmap(); | ||
if ((bitmap != null) && !bitmap.isRecycled()) { | ||
bitmapCollection.put(imageInfo.getKey(), new SoftReference<>(imageInfo.getBitmap())); | ||
orientationCollection.put(imageInfo.getKey(), imageInfo.getOrientation()); | ||
} | ||
} | ||
} | ||
|
||
public static synchronized Bitmap getBitmap(TiDrawableReference.Key key) | ||
{ | ||
var bitmapRef = bitmapCollection.get(key); | ||
if (bitmapRef != null) { | ||
var bitmap = bitmapRef.get(); | ||
if ((bitmap != null) && !bitmap.isRecycled()) { | ||
return bitmap; | ||
} else { | ||
remove(key); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static synchronized TiExifOrientation getOrientation(TiDrawableReference.Key key) | ||
{ | ||
return orientationCollection.get(key); | ||
} | ||
|
||
public static synchronized void clear() | ||
{ | ||
bitmapCollection.clear(); | ||
orientationCollection.clear(); | ||
} | ||
|
||
private static synchronized void remove(TiDrawableReference.Key key) | ||
{ | ||
bitmapCollection.remove(key); | ||
orientationCollection.remove(key); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
android/titanium/src/java/org/appcelerator/titanium/util/TiImageInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2021 by Axway, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
package org.appcelerator.titanium.util; | ||
|
||
import android.graphics.Bitmap; | ||
import org.appcelerator.titanium.view.TiDrawableReference; | ||
|
||
public class TiImageInfo | ||
{ | ||
private final TiDrawableReference.Key key; | ||
private final Bitmap bitmap; | ||
private final TiExifOrientation orientation; | ||
|
||
public TiImageInfo(TiDrawableReference.Key key, Bitmap bitmap, TiExifOrientation orientation) | ||
{ | ||
this.key = key; | ||
this.bitmap = bitmap; | ||
this.orientation = orientation; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object value) | ||
{ | ||
if (value instanceof TiImageInfo) { | ||
return ((TiImageInfo) value).key.equals(this.key); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return (this.key != null) ? this.key.hashCode() : 0; | ||
} | ||
|
||
public TiDrawableReference.Key getKey() | ||
{ | ||
return this.key; | ||
} | ||
|
||
public Bitmap getBitmap() | ||
{ | ||
return this.bitmap; | ||
} | ||
|
||
public TiExifOrientation getOrientation() | ||
{ | ||
return this.orientation; | ||
} | ||
} |
Oops, something went wrong.