From 2a9b4ef7de68196945920de480880a2b7829ba2a Mon Sep 17 00:00:00 2001 From: Xiaowei Zhao Date: Sun, 8 Nov 2015 23:42:48 +0800 Subject: [PATCH] Make AsyncHttpResponseHandler.ctor more clear --- .../http/AsyncHttpResponseHandler.java | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java b/library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java index 609cc21a7..20475601f 100755 --- a/library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java +++ b/library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java @@ -117,13 +117,8 @@ public AsyncHttpResponseHandler() { * @param looper The looper to work with */ public AsyncHttpResponseHandler(Looper looper) { - this.looper = looper == null ? Looper.myLooper() : looper; - - // Use asynchronous mode by default. - setUseSynchronousMode(false); - // Do not use the pool's thread to fire callbacks by default. - setUsePoolThread(false); + this(looper == null ? Looper.myLooper() : looper, false); } /** @@ -133,17 +128,24 @@ public AsyncHttpResponseHandler(Looper looper) { * @param usePoolThread Whether to use the pool's thread to fire callbacks */ public AsyncHttpResponseHandler(boolean usePoolThread) { - // Whether to use the pool's thread to fire callbacks. - setUsePoolThread(usePoolThread); - - // When using the pool's thread, there's no sense in having a looper. - if (!getUsePoolThread()) { - // Use the current thread's looper. - this.looper = Looper.myLooper(); + this(usePoolThread ? null : Looper.myLooper(), usePoolThread); + } - // Use asynchronous mode by default. - setUseSynchronousMode(false); + private AsyncHttpResponseHandler(Looper looper, boolean usePoolThread) { + if (!usePoolThread) { + Utils.asserts(looper != null, "use looper thread, must call Looper.prepare() first!"); + this.looper = looper; + // Create a handler on current thread to submit tasks + this.handler = new ResponderHandler(this, looper); + } else { + Utils.asserts(looper == null, "use pool thread, looper should be null!"); + // If pool thread is to be used, there's no point in keeping a reference + // to the looper and handler. + this.looper = null; + this.handler = null; } + + this.usePoolThread = usePoolThread; } @Override