|
@@ -10,6 +10,8 @@ import android.os.StrictMode;
|
10
|
10
|
import android.support.annotation.NonNull;
|
11
|
11
|
import android.support.annotation.Nullable;
|
12
|
12
|
|
|
13
|
+import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
|
|
14
|
+import com.reactnativenavigation.BuildConfig;
|
13
|
15
|
import com.reactnativenavigation.NavigationApplication;
|
14
|
16
|
|
15
|
17
|
import java.io.FileNotFoundException;
|
|
@@ -19,27 +21,56 @@ import java.net.URL;
|
19
|
21
|
|
20
|
22
|
public class ImageLoader {
|
21
|
23
|
|
22
|
|
- public interface ImageLoadingListener {
|
23
|
|
- void onComplete(@NonNull Drawable drawable);
|
|
24
|
+ public interface ImageLoadingListener {
|
|
25
|
+ void onComplete(@NonNull Drawable drawable);
|
24
|
26
|
|
25
|
|
- void onError(Throwable error);
|
26
|
|
- }
|
|
27
|
+ void onError(Throwable error);
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ private static final String FILE_SCHEME = "file";
|
27
|
31
|
|
28
|
|
- public void loadIcon(final Context context, final String uri, final ImageLoadingListener listener) {
|
|
32
|
+ public void loadIcon(final Context context, final String uri, final ImageLoadingListener listener) {
|
29
|
33
|
try {
|
30
|
|
- StrictMode.ThreadPolicy threadPolicy = adjustThreadPolicyDebug();
|
31
|
|
-
|
32
|
|
- InputStream is = openStream(context, uri);
|
33
|
|
- Bitmap bitmap = BitmapFactory.decodeStream(is);
|
34
|
|
- Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
|
|
34
|
+ Drawable drawable = getDrawable(context, uri);
|
35
|
35
|
listener.onComplete(drawable);
|
36
|
|
-
|
37
|
|
- restoreThreadPolicyDebug(threadPolicy);
|
38
|
36
|
} catch (IOException e) {
|
39
|
37
|
listener.onError(e);
|
40
|
38
|
}
|
41
|
39
|
}
|
42
|
40
|
|
|
41
|
+ @NonNull
|
|
42
|
+ private Drawable getDrawable(Context context, String source) throws IOException {
|
|
43
|
+ if (BuildConfig.DEBUG) {
|
|
44
|
+ return readJsDevImage(context, source);
|
|
45
|
+ } else if (isLocalFile(Uri.parse(source))) {
|
|
46
|
+ return loadFile(source);
|
|
47
|
+ } else {
|
|
48
|
+ return loadResource(source);
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ @NonNull
|
|
53
|
+ private Drawable readJsDevImage(Context context, String source) throws IOException {
|
|
54
|
+ StrictMode.ThreadPolicy threadPolicy = adjustThreadPolicyDebug();
|
|
55
|
+ InputStream is = openStream(context, source);
|
|
56
|
+ Bitmap bitmap = BitmapFactory.decodeStream(is);
|
|
57
|
+ restoreThreadPolicyDebug(threadPolicy);
|
|
58
|
+ return new BitmapDrawable(context.getResources(), bitmap);
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ private boolean isLocalFile(Uri uri) {
|
|
62
|
+ return FILE_SCHEME.equals(uri.getScheme());
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ private Drawable loadFile(String uri) {
|
|
66
|
+ Bitmap bitmap = BitmapFactory.decodeFile(uri);
|
|
67
|
+ return new BitmapDrawable(NavigationApplication.instance.getResources(), bitmap);
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ private static Drawable loadResource(String iconSource) {
|
|
71
|
+ return ResourceDrawableIdHelper.getInstance().getResourceDrawable(NavigationApplication.instance, iconSource);
|
|
72
|
+ }
|
|
73
|
+
|
43
|
74
|
private StrictMode.ThreadPolicy adjustThreadPolicyDebug() {
|
44
|
75
|
StrictMode.ThreadPolicy threadPolicy = null;
|
45
|
76
|
if (NavigationApplication.instance.isDebug()) {
|