Skip to content

Commit

Permalink
Merge pull request #425 from orientalsensation/AsyncHttpResponseHandler
Browse files Browse the repository at this point in the history
Strengthen the reference for the Handler responder.
  • Loading branch information
smarek committed Feb 18, 2014
2 parents f7817fe + 61f41ad commit 5ee354d
Showing 1 changed file with 14 additions and 28 deletions.
Expand Up @@ -32,7 +32,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URI;

/**
Expand Down Expand Up @@ -92,7 +91,7 @@ public abstract class AsyncHttpResponseHandler implements ResponseHandlerInterfa

protected static final int BUFFER_SIZE = 4096;

private Handler handler;
private final Handler handler;
public static final String DEFAULT_CHARSET = "UTF-8";
private String responseCharset = DEFAULT_CHARSET;
private Boolean useSynchronousMode = false;
Expand Down Expand Up @@ -121,21 +120,18 @@ public void setRequestHeaders(Header[] requestHeaders) {
}

/**
* Avoid leaks by using a non-anonymous handler class with a weak reference
* Avoid leaks by using a non-anonymous handler class.
*/
static class ResponderHandler extends Handler {
private final WeakReference<AsyncHttpResponseHandler> mResponder;
private static class ResponderHandler extends Handler {
private final AsyncHttpResponseHandler mResponder;

ResponderHandler(AsyncHttpResponseHandler service) {
mResponder = new WeakReference<AsyncHttpResponseHandler>(service);
ResponderHandler(AsyncHttpResponseHandler mResponder) {
this.mResponder = mResponder;
}

@Override
public void handleMessage(Message msg) {
AsyncHttpResponseHandler service = mResponder.get();
if (null != service) {
service.handleMessage(msg);
}
mResponder.handleMessage(msg);
}
}

Expand Down Expand Up @@ -167,7 +163,10 @@ public String getCharset() {
* Creates a new AsyncHttpResponseHandler
*/
public AsyncHttpResponseHandler() {
// Init Looper by calling postRunnable without argument
// There is always a handler ready for delivering messages.
handler = new ResponderHandler(this);

// Init Looper by calling postRunnable without an argument.
postRunnable(null);
}

Expand All @@ -178,7 +177,7 @@ public AsyncHttpResponseHandler() {
* @param totalSize total size of file
*/
public void onProgress(int bytesWritten, int totalSize) {
Log.d(LOG_TAG, String.format("Progress %d from %d (%d%%)", bytesWritten, totalSize, (totalSize > 0) ? (bytesWritten / totalSize) * 100 : -1));
Log.v(LOG_TAG, String.format("Progress %d from %d (%d%%)", bytesWritten, totalSize, (totalSize > 0) ? (bytesWritten / totalSize) * 100 : -1));
}

/**
Expand Down Expand Up @@ -307,7 +306,7 @@ protected void handleMessage(Message message) {
}

protected void sendMessage(Message msg) {
if (getUseSynchronousMode() || handler == null) {
if (getUseSynchronousMode()) {
handleMessage(msg);
} else if (!Thread.currentThread().isInterrupted()) { // do not send messages if request has been cancelled
handler.sendMessage(msg);
Expand All @@ -324,9 +323,6 @@ protected void postRunnable(Runnable runnable) {
if (missingLooper) {
Looper.prepare();
}
if (null == handler) {
handler = new ResponderHandler(this);
}
if (null != runnable) {
handler.post(runnable);
}
Expand All @@ -343,17 +339,7 @@ protected void postRunnable(Runnable runnable) {
* @return Message instance, should not be null
*/
protected Message obtainMessage(int responseMessageId, Object responseMessageData) {
Message msg;
if (handler != null) {
msg = handler.obtainMessage(responseMessageId, responseMessageData);
} else {
msg = Message.obtain();
if (msg != null) {
msg.what = responseMessageId;
msg.obj = responseMessageData;
}
}
return msg;
return handler.obtainMessage(responseMessageId, responseMessageData);
}

@Override
Expand Down

1 comment on commit 5ee354d

@donghaol5
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lll

Please sign in to comment.