]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape_android/SubscriberElement.java
Android Shape: Add locks for synchronization of the receive callback and GUI draw...
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape_android / SubscriberElement.java
1 /**
2  * 
3  *      This file is part of shape_android.
4  *
5  *  shape_android is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  shape_android is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with shape_android.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 package org.ocera.orte.shape_android;
20
21 import android.graphics.Canvas;
22 import android.graphics.drawable.ShapeDrawable;
23 import android.graphics.drawable.shapes.OvalShape;
24 import android.graphics.drawable.shapes.PathShape;
25 import android.graphics.drawable.shapes.RectShape;
26 import android.view.View;
27
28 import org.ocera.orte.DomainApp;
29 import org.ocera.orte.Subscription;
30 import org.ocera.orte.SubscriptionCallback;
31 import org.ocera.orte.types.MessageData;
32 import org.ocera.orte.types.NtpTime;
33 import org.ocera.orte.types.RecvInfo;
34 import org.ocera.orte.types.SubsProp;
35 import org.ocera.orte.types.ORTEConstant;
36
37 import java.util.concurrent.locks.ReentrantLock;
38
39 /**
40  * Subscribing objects are made by this class.
41  * 
42  * @author jiri hubacek <jiri.hubacek@gmail.com>
43  * @version %I%, %G%
44  * @since 1.0
45  */
46 public class SubscriberElement extends SubscriptionCallback
47 {
48         private Subscription subscription;
49         
50         private NtpTime deadline;
51         private NtpTime minSeparation;
52         
53         private BoxType box;
54         private ShapeDrawable shape;
55         
56         private View parentView;
57         
58         private boolean receiving;
59         private boolean enabled;
60
61         public final ReentrantLock lock = new ReentrantLock();
62
63         /**
64          * Set default variables of subscriber's object.
65          * 
66          * @param appDomain
67          * @param color Topic is dependent on color.
68          * @since 1.0
69          */
70         public SubscriberElement(DomainApp appDomain, int color, View v) {
71                 this.deadline = new NtpTime(6);
72                 this.minSeparation = new NtpTime(0);
73                 
74                 this.box = new BoxType(appDomain, PublisherShape.getColorName(color));
75                 SubsProp subscriberProperties = new SubsProp(this.box.getTopic(),
76                                 "BoxType",
77                                 this.minSeparation,
78                                 this.deadline,
79                                 ORTEConstant.IMMEDIATE,
80                                 ORTEConstant.BEST_EFFORTS,
81                                 0);
82                 
83                 this.subscription = appDomain.createSubscription(subscriberProperties, this.box, this);
84                 
85                 this.shape = new ShapeDrawable();
86                 this.shape.getPaint().setColor(PublisherShape.getColorConstant(color));
87                 
88                 this.setShape();
89                 
90                 this.parentView = v;
91                 
92                 this.enabled = true;
93         }
94         
95         /**
96          * What should be done when new data are received.
97          * 
98          * @param info
99          * @param msg
100          * @since 1.0
101          */
102         public void callback(RecvInfo info, MessageData msg)
103         {
104                 switch (info.getRecvStatus()) {
105                 case ORTEConstant.NEW_DATA:
106
107                         lock.lock();
108                         try {
109                                 this.receiving = true;
110                                 //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);
111
112                                 this.shape.setBounds(this.box.rectangle.top_left_x,
113                                                 this.box.rectangle.top_left_y,
114                                                 this.box.rectangle.bottom_right_x,
115                                                 this.box.rectangle.bottom_right_y);
116
117                                 this.setShape();
118                         } finally {
119                                 lock.unlock();
120                         }
121
122                         this.parentView.postInvalidate();
123                         break;
124                 case ORTEConstant.DEADLINE:
125                         lock.lock();
126                         try {
127                                 this.receiving = false;
128                         } finally {
129                                 lock.unlock();
130                         }
131                         this.parentView.postInvalidate();
132                         break;
133                 }
134         }
135         
136         /**
137          * Draw shape, which is private object.
138          * 
139          * @param canvas
140          * @since 1.0
141          */
142         public void drawMe(Canvas canvas)
143         {
144                 this.shape.draw(canvas);
145         }
146         
147         /**
148          * Set desired shape of object.
149          * 
150          * @since 1.0
151          */
152         public void setShape()
153         {
154                 switch (this.box.shape) {
155                 case 0:
156                         this.shape.setShape(new RectShape());
157                         break;
158                 case 1:
159                         this.shape.setShape(new OvalShape());
160                         break;
161                 case 2:
162                         this.shape.setShape(new PathShape(PublisherShape.returnTrianglePath(), 2*PublisherActivity.SHAPE_WIDTH, 2*PublisherActivity.SHAPE_HEIGHT));
163                         break;
164                 default:
165                         this.shape.setShape(new RectShape());
166                 }
167         }
168
169         /**
170          * Getter for enabled value. Should be object
171          * drawn or not?
172          * 
173          * @return Enabled?
174          * @since 1.0
175          */
176         public boolean getEnabled()     { return this.enabled; }
177         
178         /**
179          * Set value enabled. Should be object
180          * drawn or not?
181          * 
182          * @param b Enable?
183          * @since 1.0
184          */
185         public void setEnabled(boolean b) { this.enabled = b; }
186
187         /**
188          * Getter for enabled value. Should be object
189          * drawn or not?
190          * 
191          * @return Enabled?
192          * @since 1.0
193          */
194         public boolean getAllowScaling()        { return this.box.allowScaling; }
195         
196         /**
197          * Set value enabled. Should be object
198          * drawn or not?
199          * 
200          * @param b Enable?
201          * @since 1.0
202          */
203         public void setAllowScaling(boolean b) { this.box.allowScaling = b; }
204
205         /**
206          * Getter for receiving value. Should be object
207          * drawn or not?
208          * 
209          * @return Receiving?
210          * @since 1.0
211          */
212         public boolean getReceiving()   { return this.receiving; }
213         
214         /**
215          * Set value receiving. Should be object
216          * drawn or not?
217          * 
218          * @param b Receiving?
219          * @since 1.0
220          */
221         public void setReceiving(boolean b) { this.receiving = b; }
222         
223         /**
224          * Min. Separation value is used for desired update delay.
225          * 
226          * @return NtpTime value of Min. Separation.
227          * @since 1.0
228          */
229         public NtpTime getMinSeparation()
230         {
231                 return this.subscription.getProperties().getMinSeparation();
232         }
233         
234         /**
235          * Min. Separation value is used for desired update delay,
236          * this is setter for this value.
237          * 
238          * @param newMinSeparation
239          * @since 1.0
240          */
241         public void setMinSeparation(NtpTime newMinSeparation)
242         {
243                 SubsProp properties = this.subscription.getProperties();
244                 properties.setMinSeparation(newMinSeparation);
245                 this.subscription.setProperties(properties);
246         }
247         
248         /**
249          * Pass parameters to box.
250          * 
251          * @param currentWidth
252          * @param currentHeight
253          * @since 1.0
254          */
255         public void setScale(int currentWidth, int currentHeight)
256         {
257                 this.box.setScale(currentWidth, currentHeight);
258                 this.setShape();
259         }
260
261         /**
262          * When subscriber is removed, destroy subscription.
263          * 
264          * @since 1.0
265          */
266         public void killMe() {
267                 if (this.subscription != null) {
268                         this.subscription.destroy();
269                         this.subscription = null;
270                 }
271         }
272 }