diff --git a/.idea/misc.xml b/.idea/misc.xml index d96f21c..eeb4ab3 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/seriallibrary/build.gradle b/seriallibrary/build.gradle index 026bf78..ccc90d3 100644 --- a/seriallibrary/build.gradle +++ b/seriallibrary/build.gradle @@ -31,5 +31,5 @@ dependencies { androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' - implementation 'com.github.Sheedon:SerialDispatcher:1.0.0' + implementation 'com.github.Sheedon:SerialDispatcher:1.0.2' } diff --git a/seriallibrary/src/main/java/org/sheedon/serial/retrofit/Call.java b/seriallibrary/src/main/java/org/sheedon/serial/retrofit/Call.java index 11bfed5..f9dab66 100644 --- a/seriallibrary/src/main/java/org/sheedon/serial/retrofit/Call.java +++ b/seriallibrary/src/main/java/org/sheedon/serial/retrofit/Call.java @@ -31,6 +31,16 @@ public interface Call extends Cloneable { */ void enqueue(Callback responseCallback); + /** + * 添加绑定额外 + */ + void addBindCallback(Callback callback); + + /** + * 移除绑定 + */ + void unBindCallback(); + /** * Cancels the request, if possible. Requests that are already complete cannot be canceled. */ diff --git a/seriallibrary/src/main/java/org/sheedon/serial/retrofit/ExecutorCallAdapterFactory.java b/seriallibrary/src/main/java/org/sheedon/serial/retrofit/ExecutorCallAdapterFactory.java index 8caf2c5..1947ba2 100644 --- a/seriallibrary/src/main/java/org/sheedon/serial/retrofit/ExecutorCallAdapterFactory.java +++ b/seriallibrary/src/main/java/org/sheedon/serial/retrofit/ExecutorCallAdapterFactory.java @@ -81,6 +81,45 @@ public void publishNotCallback() { enqueue(null); } + @Override + public void addBindCallback(final Callback callback) { + if(callback == null){ + return; + } + + delegate.addBindCallback(new Callback() { + @Override + public void onResponse(Call call, final Response response) { + callbackExecutor.execute(new Runnable() { + @Override + public void run() { + if (delegate.isCanceled()) { + // Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation. + callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled")); + } else { + callback.onResponse(ExecutorCallbackCall.this, response); + } + } + }); + } + + @Override + public void onFailure(Call call, final Throwable t) { + callbackExecutor.execute(new Runnable() { + @Override + public void run() { + callback.onFailure(ExecutorCallbackCall.this, t); + } + }); + } + }); + } + + @Override + public void unBindCallback() { + delegate.unBindCallback(); + } + @Override public void enqueue(final Callback callback) { // checkNotNull(callback, "callback == null"); diff --git a/seriallibrary/src/main/java/org/sheedon/serial/retrofit/SerialCall.java b/seriallibrary/src/main/java/org/sheedon/serial/retrofit/SerialCall.java index 1c76025..9abeee5 100644 --- a/seriallibrary/src/main/java/org/sheedon/serial/retrofit/SerialCall.java +++ b/seriallibrary/src/main/java/org/sheedon/serial/retrofit/SerialCall.java @@ -12,6 +12,7 @@ /** * 串口Call,用于触发串口调度 + * * @Author: sheedon * @Email: sheedonsun@163.com * @Date: 2020/2/23 22:27 @@ -74,6 +75,90 @@ public void publishNotCallback() { enqueue(null); } + @Override + public void addBindCallback(final Callback callback) { + org.sheedon.serial.Call call; + Throwable failure; + + synchronized (this) { + + call = rawCall; + failure = creationFailure; + if (call == null && failure == null) { + try { + call = rawCall = createRawCall(); + } catch (Throwable t) { + failure = creationFailure = t; + } + } + } + + if (failure != null) { + dealWithCallback(callback, SerialCall.this, null, failure, false); + return; + } + + if (canceled) { + call.cancel(); + } + + if (callback == null) + return; + + call.addBindCallback(new org.sheedon.serial.Callback() { + @Override + public void onFailure(Throwable e) { + try { + dealWithCallback(callback, SerialCall.this, null, e, false); + } catch (Throwable t) { + t.printStackTrace(); + } + } + + @Override + public void onResponse(org.sheedon.serial.Response rawResponse) { + Response response; + try { + response = parseResponse(rawResponse); + } catch (Throwable e) { + callFailure(e); + return; + } + callSuccess(response); + } + + private void callFailure(Throwable e) { + try { + dealWithCallback(callback, SerialCall.this, null, e, false); + } catch (Throwable t) { + t.printStackTrace(); + } + } + + private void callSuccess(Response response) { + try { + dealWithCallback(callback, SerialCall.this, response, null, true); + } catch (Throwable t) { + t.printStackTrace(); + } + } + }); + } + + @Override + public void unBindCallback() { + org.sheedon.serial.Call call; + + synchronized (this) { + call = rawCall; + } + + if (call == null) + return; + + call.removeBindCallback(); + } + @Override public void enqueue(final Callback callback) { @@ -178,7 +263,7 @@ private org.sheedon.serial.Call createRawCall() throws IOException { return call; } - Response parseResponse(org.sheedon.serial.Response rawResponse) throws IOException { + Response parseResponse(org.sheedon.serial.Response rawResponse) { ResponseBody rawBody = rawResponse.body(); try {