|
@@ -8,7 +8,11 @@ import android.graphics.drawable.Drawable;
|
8
|
8
|
import android.net.Uri;
|
9
|
9
|
import android.os.StrictMode;
|
10
|
10
|
import android.support.annotation.NonNull;
|
|
11
|
+import android.support.annotation.Nullable;
|
11
|
12
|
|
|
13
|
+import com.reactnativenavigation.NavigationApplication;
|
|
14
|
+
|
|
15
|
+import java.io.FileNotFoundException;
|
12
|
16
|
import java.io.IOException;
|
13
|
17
|
import java.io.InputStream;
|
14
|
18
|
import java.net.URL;
|
|
@@ -21,46 +25,45 @@ public class ImageUtils {
|
21
|
25
|
void onError(Throwable error);
|
22
|
26
|
}
|
23
|
27
|
|
24
|
|
- public static void tryLoadIcon(final Context context, final String uri, final ImageLoadingListener listener) {
|
25
|
|
- if (uri == null) {
|
26
|
|
- if (listener != null) {
|
27
|
|
- listener.onError(new IllegalArgumentException("Uri is null"));
|
28
|
|
- }
|
29
|
|
- return;
|
30
|
|
- }
|
31
|
|
- runWorkerThread(new Runnable() {
|
32
|
|
- @Override
|
33
|
|
- public void run() {
|
34
|
|
- loadIcon(context, uri, listener);
|
35
|
|
- }
|
36
|
|
- });
|
37
|
|
- }
|
|
28
|
+ public static void loadIcon(final Context context, final String uri, final ImageLoadingListener listener) {
|
|
29
|
+ 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);
|
|
35
|
+ listener.onComplete(drawable);
|
38
|
36
|
|
39
|
|
- private static void loadIcon(Context context, String uri, final ImageLoadingListener listener) {
|
40
|
|
- try {
|
41
|
|
- InputStream is = null;
|
42
|
|
- if (uri.contains("http")) {
|
43
|
|
- URL url = new URL(uri);
|
44
|
|
- is = url.openStream();
|
45
|
|
- } else {
|
46
|
|
- is = context.getContentResolver().openInputStream(Uri.parse(uri));
|
47
|
|
- }
|
|
37
|
+ restoreThreadPolicyDebug(threadPolicy);
|
|
38
|
+ } catch (IOException e) {
|
|
39
|
+ listener.onError(e);
|
|
40
|
+ }
|
|
41
|
+ }
|
48
|
42
|
|
49
|
|
- Bitmap bitmap = BitmapFactory.decodeStream(is);
|
50
|
|
- Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
|
51
|
|
- if (listener != null) {
|
52
|
|
- listener.onComplete(drawable);
|
53
|
|
- }
|
54
|
|
- } catch (IOException e) {
|
55
|
|
- if (listener != null) {
|
56
|
|
- listener.onError(e);
|
57
|
|
- } else {
|
58
|
|
- e.printStackTrace();
|
59
|
|
- }
|
60
|
|
- }
|
61
|
|
- }
|
|
43
|
+ private static StrictMode.ThreadPolicy adjustThreadPolicyDebug() {
|
|
44
|
+ StrictMode.ThreadPolicy threadPolicy = null;
|
|
45
|
+ if (NavigationApplication.instance.isDebug()) {
|
|
46
|
+ threadPolicy = StrictMode.getThreadPolicy();
|
|
47
|
+ StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
|
|
48
|
+ }
|
|
49
|
+ return threadPolicy;
|
|
50
|
+ }
|
62
|
51
|
|
63
|
|
- private static void runWorkerThread(Runnable runnable) {
|
64
|
|
- new Thread(runnable).start();
|
65
|
|
- }
|
|
52
|
+ private static void restoreThreadPolicyDebug(@Nullable StrictMode.ThreadPolicy threadPolicy) {
|
|
53
|
+ if (NavigationApplication.instance.isDebug() && threadPolicy != null) {
|
|
54
|
+ StrictMode.setThreadPolicy(threadPolicy);
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ private static InputStream openStream(Context context, String uri) throws IOException {
|
|
59
|
+ return uri.contains("http") ? remoteUrl(uri) : localFile(context, uri);
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ private static InputStream remoteUrl(String uri) throws IOException {
|
|
63
|
+ return new URL(uri).openStream();
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ private static InputStream localFile(Context context, String uri) throws FileNotFoundException {
|
|
67
|
+ return context.getContentResolver().openInputStream(Uri.parse(uri));
|
|
68
|
+ }
|
66
|
69
|
}
|