Skip to content

Commit

Permalink
fix(android): throw exception from background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
garymathews committed Apr 15, 2020
1 parent e754615 commit 3c8e3de
Showing 1 changed file with 25 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
package org.appcelerator.titanium.util;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.lang.reflect.Constructor;
Expand All @@ -18,18 +21,13 @@
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import java.util.concurrent.atomic.AtomicReference;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.util.KrollStreamHelper;
import org.appcelerator.titanium.io.TiInputStreamWrapper;
import org.appcelerator.titanium.TiApplication;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import org.appcelerator.titanium.io.TiInputStreamWrapper;

/**
* Manages the asynchronous opening of InputStreams from URIs so that
Expand Down Expand Up @@ -86,7 +84,7 @@ public void download(URI uri, TiDownloadListener listener)
* @return
* Returns a stream to the file/content being downloaded.
* <p>
* Returns null if failed to download content or if given an invalid argument.
* Throws exception if failed to download content.
*/
public InputStream blockingDownload(final URI uri) throws Exception
{
Expand All @@ -113,33 +111,29 @@ public InputStream blockingDownload(final URI uri) throws Exception
// Note: Using "HttpUrlConnection" on UI thread will cause a "NetworkOnMainThreadException" to be thrown.
if (TiApplication.isUIThread()) {
// Perform the blocking download on another thread.
// Downloaded content will be made available via Titanium's "TiResponseCache".
try {
Thread thread = new Thread(new Runnable() {
@Override
public void run()
{
try (InputStream stream = blockingDownload(uri)) {
if (stream != null) {
KrollStreamHelper.pump(stream, null);
}
} catch (Exception ex) {
Log.e(TAG, "Exception downloading from: " + uri.toString(), ex);
}
// Downloaded content will be made available via Titanium's "TiResponseCache"
AtomicReference<Exception> exception = new AtomicReference<>(null);
Thread thread = new Thread(() -> {
try (InputStream stream = blockingDownload(uri)) {
if (stream != null) {
KrollStreamHelper.pump(stream, null);
}
});
thread.start();
thread.join();
} catch (Exception ex) {
} catch (Exception ex) {
exception.set(ex);
}
});
thread.start();
thread.join();

// Handle download thread exception.
if (exception.get() != null) {
throw exception.get();
}

// Return a stream to the downloaded file/content via our response cache.
URI cachedUri = TiResponseCache.fetchEndpointFollowingRedirects(uri);
if (cachedUri != null) {
try {
inputStream = TiResponseCache.openCachedStream(cachedUri);
} catch (Exception ex) {
}
inputStream = TiResponseCache.openCachedStream(cachedUri);
}
return inputStream;
}
Expand Down

0 comments on commit 3c8e3de

Please sign in to comment.