From 6010a87659b4dbc6f54354fa5f2b7dc0b82a7f3e Mon Sep 17 00:00:00 2001 From: jrfeng Date: Sun, 28 Jun 2020 18:27:09 +0800 Subject: [PATCH] fix bugs - fix bug: HandlerPipe, remove weak reference of 'Dispatcher' - fix bug: MessengerPipe, remove weak reference of 'Dispatcher' --- .../java/channel/helper/pipe/HandlerPipe.java | 16 +++++----- .../channel/helper/pipe/MessengerPipe.java | 30 +++++-------------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/pipe/src/main/java/channel/helper/pipe/HandlerPipe.java b/pipe/src/main/java/channel/helper/pipe/HandlerPipe.java index 4b34a90..5ada4a8 100644 --- a/pipe/src/main/java/channel/helper/pipe/HandlerPipe.java +++ b/pipe/src/main/java/channel/helper/pipe/HandlerPipe.java @@ -16,7 +16,7 @@ public class HandlerPipe extends Handler implements Emitter { private static final String TAG = "HandlerPipe"; - private final WeakReference mDispatcherWeakReference; + private final Dispatcher mDispatcher; /** * Use Main Looper @@ -27,7 +27,12 @@ public HandlerPipe(Dispatcher dispatcher) { public HandlerPipe(Looper looper, Dispatcher dispatcher) { super(looper); - mDispatcherWeakReference = new WeakReference<>(dispatcher); + + if (dispatcher == null) { + throw new IllegalArgumentException("param 'dispatcher' is not null."); + } + + mDispatcher = dispatcher; } @Override @@ -39,12 +44,7 @@ public void emit(Map data) { @Override public void handleMessage(@NonNull Message msg) { - Dispatcher dispatcher = mDispatcherWeakReference.get(); - if (dispatcher == null) { - return; - } - - dispatcher.dispatch(getData(msg)); + mDispatcher.dispatch(getData(msg)); } @SuppressWarnings("unchecked cast") diff --git a/pipe/src/main/java/channel/helper/pipe/MessengerPipe.java b/pipe/src/main/java/channel/helper/pipe/MessengerPipe.java index 48a7e00..092702c 100644 --- a/pipe/src/main/java/channel/helper/pipe/MessengerPipe.java +++ b/pipe/src/main/java/channel/helper/pipe/MessengerPipe.java @@ -57,12 +57,9 @@ public class MessengerPipe extends Handler implements Emitter { private static final String TAG = "MessengerPipe"; private Messenger mMessenger; - - private WeakReference mDispatcherWeakReference; - private boolean mDispatcher; + private Dispatcher mDispatcher; public MessengerPipe(IBinder binder) { - mDispatcher = false; mMessenger = new Messenger(binder); } @@ -75,28 +72,22 @@ public MessengerPipe(Dispatcher dispatcher) { public MessengerPipe(Looper looper, Dispatcher dispatcher) { super(looper); - mDispatcher = true; + + if (dispatcher == null) { + throw new IllegalArgumentException("param 'dispatcher' is not null."); + } + mMessenger = new Messenger(this); - mDispatcherWeakReference = new WeakReference<>(dispatcher); + mDispatcher = dispatcher; } @Override public void handleMessage(@NonNull Message msg) { - Dispatcher dispatcher = mDispatcherWeakReference.get(); - if (dispatcher == null) { - return; - } - - dispatcher.dispatch(getData(msg)); + mDispatcher.dispatch(getData(msg)); } @Override public void emit(Map data) { - if (mDispatcher) { - Log.e(TAG, "The current MessengerPipe can only be a dispatcher."); - return; - } - Message message = Message.obtain(); message.obj = new MapWrapper(data); @@ -108,11 +99,6 @@ public void emit(Map data) { } private Map getData(Message dataWrapper) { - if (!mDispatcher) { - Log.e(TAG, "The current MessengerPipe can only be a emitter."); - return new HashMap<>(); - } - if (dataWrapper.obj == null) { Log.d(TAG, "dataWrapper is empty."); return new HashMap<>();