From 047b6b68881c3a52d7239aeecca4964ae02d57e8 Mon Sep 17 00:00:00 2001 From: atanasovg Date: Wed, 6 Jul 2016 16:59:21 +0300 Subject: [PATCH] Add async image tasks --- .../java/org/nativescript/widgets/Async.java | 199 ++++++++++++------ 1 file changed, 138 insertions(+), 61 deletions(-) diff --git a/android/widgets/src/main/java/org/nativescript/widgets/Async.java b/android/widgets/src/main/java/org/nativescript/widgets/Async.java index bc08d654d..de8e64b94 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/Async.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/Async.java @@ -17,24 +17,153 @@ import java.util.Locale; import java.util.Map; import java.util.Stack; -import android.app.Application; +import android.content.Context; +import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.drawable.BitmapDrawable; import android.os.AsyncTask; -import android.util.DisplayMetrics; +import android.util.Base64; import java.net.CookieManager; public class Async { - public interface CompleteCallback - { - void onComplete(Object result, Object context); + public interface CompleteCallback { + void onComplete(Object result, Object tag); } - public static void DownloadImage(String url, CompleteCallback callback, Object context) - { - new ImageDownloadTask(callback, context).execute(url); + public static class Image { + /* + * The request id parameter is needed for the sake of the JavaScript implementation. + * Because we want to use only one extend of the CompleteCallback interface (for the sake of better performance) + * we use this id to detect the initial request, which result is currently received in the complete callback. + * When the async task completes it will pass back this id to JavaScript. + */ + public static void fromResource(String name, Context context, int requestId, CompleteCallback callback) { + new LoadImageFromResourceTask(context, requestId, callback).execute(name); + } + + public static void fromFile(String fileName, int requestId, CompleteCallback callback) { + new LoadImageFromFileTask(requestId, callback).execute(fileName); + } + + public static void fromBase64(String source, int requestId, CompleteCallback callback) { + new LoadImageFromBase64StringTask(requestId, callback).execute(source); + } + + public static void download(String url, CompleteCallback callback, Object context) { + new DownloadImageTask(callback, context).execute(url); + } + + static class DownloadImageTask extends AsyncTask { + private CompleteCallback callback; + private Object context; + + public DownloadImageTask(CompleteCallback callback, Object context) { + this.callback = callback; + this.context = context; + } + + protected Bitmap doInBackground(String... params) { + InputStream stream = null; + try { + stream = new java.net.URL(params[0]).openStream(); + Bitmap bmp = BitmapFactory.decodeStream(stream); + return bmp; + } + catch (MalformedURLException e) { + e.printStackTrace(); + return null; + } + catch (IOException e) { + e.printStackTrace(); + return null; + } + finally { + if (stream != null) { + try { + stream.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + protected void onPostExecute(final Bitmap result) { + this.callback.onComplete(result, this.context); + } + } + + static class LoadImageFromResourceTask extends AsyncTask { + private CompleteCallback callback; + private Context context; + private int requestId; + + public LoadImageFromResourceTask(Context context, int requestId, CompleteCallback callback) { + this.callback = callback; + this.context = context; + this.requestId = requestId; + } + + protected Bitmap doInBackground(String... params) { + String name = params[0]; + Resources res = this.context.getResources(); + int id = res.getIdentifier(name, "drawable", context.getPackageName()); + + if(id > 0) { + BitmapDrawable result = (BitmapDrawable)res.getDrawable(id); + return result.getBitmap(); + } + + return null; + } + + protected void onPostExecute(final Bitmap result) { + this.callback.onComplete(result, this.requestId); + } + } + + static class LoadImageFromFileTask extends AsyncTask { + private CompleteCallback callback; + private int requestId; + + public LoadImageFromFileTask(int requestId, CompleteCallback callback) { + this.callback = callback; + this.requestId = requestId; + } + + protected Bitmap doInBackground(String... params) { + String fileName = params[0]; + return BitmapFactory.decodeFile(fileName); + } + + protected void onPostExecute(final Bitmap result) { + this.callback.onComplete(result, this.requestId); + } + } + + static class LoadImageFromBase64StringTask extends AsyncTask { + private CompleteCallback callback; + private int requestId; + + public LoadImageFromBase64StringTask(int requestId, CompleteCallback callback) { + this.callback = callback; + this.requestId = requestId; + } + + protected Bitmap doInBackground(String... params) { + String source = params[0]; + byte[] bytes = Base64.decode(source, Base64.DEFAULT); + return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); + } + + protected void onPostExecute(final Bitmap result) { + this.callback.onComplete(result, this.requestId); + } + } } public static class Http @@ -346,56 +475,4 @@ public class Async } } } - - static class ImageDownloadTask extends AsyncTask - { - private CompleteCallback callback; - private Object context; - - public ImageDownloadTask(CompleteCallback callback, Object context) - { - this.callback = callback; - this.context = context; - } - - protected Bitmap doInBackground(String... params) - { - InputStream stream = null; - try - { - stream = new URL(params[0]).openStream(); - Bitmap bmp = BitmapFactory.decodeStream(stream); - return bmp; - } - catch (MalformedURLException e) - { - e.printStackTrace(); - return null; - } - catch (IOException e) - { - e.printStackTrace(); - return null; - } - finally - { - if (stream != null) - { - try - { - stream.close(); - } - catch (IOException e) - { - e.printStackTrace(); - } - } - } - } - - protected void onPostExecute(final Bitmap result) - { - this.callback.onComplete(result, this.context); - } - } -} +} \ No newline at end of file