From d632de9297ae49c61dda8968d15ba8d5bd78554a Mon Sep 17 00:00:00 2001 From: Martin Vajnar Date: Fri, 9 Nov 2018 20:13:42 +0100 Subject: [PATCH] Android Shape: Add locks for synchronization of the receive callback and GUI draw threads This commit adds the missing locks and changes setting of bound through getBounds() object to setBounds() function as recommended by the documentation (see: https://developer.android.com/reference/android/graphics/drawable/Drawable.html#getBounds()) --- .../orte/shape_android/SubscriberElement.java | 35 +++++++++++++------ .../orte/shape_android/SubscriberView.java | 7 +++- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/orte/contrib/shape_android/src/org/ocera/orte/shape_android/SubscriberElement.java b/orte/contrib/shape_android/src/org/ocera/orte/shape_android/SubscriberElement.java index 6b74f63..4d65efc 100644 --- a/orte/contrib/shape_android/src/org/ocera/orte/shape_android/SubscriberElement.java +++ b/orte/contrib/shape_android/src/org/ocera/orte/shape_android/SubscriberElement.java @@ -34,6 +34,8 @@ import org.ocera.orte.types.RecvInfo; import org.ocera.orte.types.SubsProp; import org.ocera.orte.types.ORTEConstant; +import java.util.concurrent.locks.ReentrantLock; + /** * Subscribing objects are made by this class. * @@ -56,6 +58,8 @@ public class SubscriberElement extends SubscriptionCallback private boolean receiving; private boolean enabled; + public final ReentrantLock lock = new ReentrantLock(); + /** * Set default variables of subscriber's object. * @@ -99,20 +103,31 @@ public class SubscriberElement extends SubscriptionCallback { switch (info.getRecvStatus()) { case ORTEConstant.NEW_DATA: - this.receiving = true; - //Log.d("SubscriberElement", "new data: " + (int) ((Box) msg).strength + "(was "+ this.box.strength +"); " + ((Box) msg).rectangle.top_left_x + ", " + ((Box) msg).rectangle.top_left_y + ", " +((Box) msg).rectangle.bottom_right_x + ", " + ((Box) msg).rectangle.bottom_right_y); - - this.shape.getBounds().left = this.box.rectangle.top_left_x; - this.shape.getBounds().top = this.box.rectangle.top_left_y; - this.shape.getBounds().right = this.box.rectangle.bottom_right_x; - this.shape.getBounds().bottom = this.box.rectangle.bottom_right_y; - - this.setShape(); + + lock.lock(); + try { + this.receiving = true; + //Log.d("SubscriberElement", "new data: " + (int) ((Box) msg).strength + "(was "+ this.box.strength +"); " + ((Box) msg).rectangle.top_left_x + ", " + ((Box) msg).rectangle.top_left_y + ", " +((Box) msg).rectangle.bottom_right_x + ", " + ((Box) msg).rectangle.bottom_right_y); + + this.shape.setBounds(this.box.rectangle.top_left_x, + this.box.rectangle.top_left_y, + this.box.rectangle.bottom_right_x, + this.box.rectangle.bottom_right_y); + + this.setShape(); + } finally { + lock.unlock(); + } this.parentView.postInvalidate(); break; case ORTEConstant.DEADLINE: - this.receiving = false; + lock.lock(); + try { + this.receiving = false; + } finally { + lock.unlock(); + } this.parentView.postInvalidate(); break; } diff --git a/orte/contrib/shape_android/src/org/ocera/orte/shape_android/SubscriberView.java b/orte/contrib/shape_android/src/org/ocera/orte/shape_android/SubscriberView.java index 31f84d6..343a8c9 100644 --- a/orte/contrib/shape_android/src/org/ocera/orte/shape_android/SubscriberView.java +++ b/orte/contrib/shape_android/src/org/ocera/orte/shape_android/SubscriberView.java @@ -56,7 +56,12 @@ public class SubscriberView extends View { protected void onDraw(Canvas canvas) { for (SubscriberElement element : elements) { - if (element.getReceiving() && element.getEnabled()) element.drawMe(canvas); + element.lock.lock(); + try { + if (element.getReceiving() && element.getEnabled()) element.drawMe(canvas); + } finally { + element.lock.unlock(); + } } } -- 2.39.2