]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape_android/SubscriberElement.java
Merge branch 'master' of https://github.com/Vajnar/orte
[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 /**
38  * Subscribing objects are made by this class.
39  * 
40  * @author jiri hubacek <jiri.hubacek@gmail.com>
41  * @version %I%, %G%
42  * @since 1.0
43  */
44 public class SubscriberElement extends SubscriptionCallback
45 {
46         private Subscription subscription;
47         
48         private NtpTime deadline;
49         private NtpTime minSeparation;
50         
51         private BoxType box;
52         private ShapeDrawable shape;
53         
54         private View parentView;
55         
56         private boolean receiving;
57         private boolean enabled;
58
59         /**
60          * Set default variables of subscriber's object.
61          * 
62          * @param appDomain
63          * @param color Topic is dependent on color.
64          * @since 1.0
65          */
66         public SubscriberElement(DomainApp appDomain, int color, View v) {
67                 this.deadline = new NtpTime(6);
68                 this.minSeparation = new NtpTime(0);
69                 
70                 this.box = new BoxType(appDomain, PublisherShape.getColorName(color));
71                 SubsProp subscriberProperties = new SubsProp(this.box.getTopic(),
72                                 "BoxType",
73                                 this.minSeparation,
74                                 this.deadline,
75                                 ORTEConstant.IMMEDIATE,
76                                 ORTEConstant.BEST_EFFORTS,
77                                 0);
78                 
79                 this.subscription = appDomain.createSubscription(subscriberProperties, this.box, this);
80                 
81                 this.shape = new ShapeDrawable();
82                 this.shape.getPaint().setColor(PublisherShape.getColorConstant(color));
83                 
84                 this.setShape();
85                 
86                 this.parentView = v;
87                 
88                 this.enabled = true;
89         }
90         
91         /**
92          * What should be done when new data are received.
93          * 
94          * @param info
95          * @param msg
96          * @since 1.0
97          */
98         public void callback(RecvInfo info, MessageData msg)
99         {
100                 switch (info.getRecvStatus()) {
101                 case ORTEConstant.NEW_DATA:
102                         this.receiving = true;
103                         //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);
104                         
105                         this.shape.getBounds().left = this.box.rectangle.top_left_x;
106                         this.shape.getBounds().top = this.box.rectangle.top_left_y;
107                         this.shape.getBounds().right = this.box.rectangle.bottom_right_x;
108                         this.shape.getBounds().bottom = this.box.rectangle.bottom_right_y;
109                         
110                         this.setShape();
111
112                         this.parentView.postInvalidate();
113                         break;
114                 case ORTEConstant.DEADLINE:
115                         this.receiving = false;
116                         this.parentView.postInvalidate();
117                         break;
118                 }
119         }
120         
121         /**
122          * Draw shape, which is private object.
123          * 
124          * @param canvas
125          * @since 1.0
126          */
127         public void drawMe(Canvas canvas)
128         {
129                 this.shape.draw(canvas);
130         }
131         
132         /**
133          * Set desired shape of object.
134          * 
135          * @since 1.0
136          */
137         public void setShape()
138         {
139                 switch (this.box.shape) {
140                 case 0:
141                         this.shape.setShape(new RectShape());
142                         break;
143                 case 1:
144                         this.shape.setShape(new OvalShape());
145                         break;
146                 case 2:
147                         this.shape.setShape(new PathShape(PublisherShape.returnTrianglePath(), 2*PublisherActivity.SHAPE_WIDTH, 2*PublisherActivity.SHAPE_HEIGHT));
148                         break;
149                 default:
150                         this.shape.setShape(new RectShape());
151                 }
152         }
153
154         /**
155          * Getter for enabled value. Should be object
156          * drawn or not?
157          * 
158          * @return Enabled?
159          * @since 1.0
160          */
161         public boolean getEnabled()     { return this.enabled; }
162         
163         /**
164          * Set value enabled. Should be object
165          * drawn or not?
166          * 
167          * @param b Enable?
168          * @since 1.0
169          */
170         public void setEnabled(boolean b) { this.enabled = b; }
171
172         /**
173          * Getter for enabled value. Should be object
174          * drawn or not?
175          * 
176          * @return Enabled?
177          * @since 1.0
178          */
179         public boolean getAllowScaling()        { return this.box.allowScaling; }
180         
181         /**
182          * Set value enabled. Should be object
183          * drawn or not?
184          * 
185          * @param b Enable?
186          * @since 1.0
187          */
188         public void setAllowScaling(boolean b) { this.box.allowScaling = b; }
189
190         /**
191          * Getter for receiving value. Should be object
192          * drawn or not?
193          * 
194          * @return Receiving?
195          * @since 1.0
196          */
197         public boolean getReceiving()   { return this.receiving; }
198         
199         /**
200          * Set value receiving. Should be object
201          * drawn or not?
202          * 
203          * @param b Receiving?
204          * @since 1.0
205          */
206         public void setReceiving(boolean b) { this.receiving = b; }
207         
208         /**
209          * Min. Separation value is used for desired update delay.
210          * 
211          * @return NtpTime value of Min. Separation.
212          * @since 1.0
213          */
214         public NtpTime getMinSeparation()
215         {
216                 return this.subscription.getProperties().getMinSeparation();
217         }
218         
219         /**
220          * Min. Separation value is used for desired update delay,
221          * this is setter for this value.
222          * 
223          * @param newMinSeparation
224          * @since 1.0
225          */
226         public void setMinSeparation(NtpTime newMinSeparation)
227         {
228                 SubsProp properties = this.subscription.getProperties();
229                 properties.setMinSeparation(newMinSeparation);
230                 this.subscription.setProperties(properties);
231         }
232         
233         /**
234          * Pass parameters to box.
235          * 
236          * @param currentWidth
237          * @param currentHeight
238          * @since 1.0
239          */
240         public void setScale(int currentWidth, int currentHeight)
241         {
242                 this.box.setScale(currentWidth, currentHeight);
243                 this.setShape();
244         }
245
246         /**
247          * When subscriber is removed, destroy subscription.
248          * 
249          * @since 1.0
250          */
251         public void killMe() {
252                 if (this.subscription != null) {
253                         this.subscription.destroy();
254                         this.subscription = null;
255                 }
256         }
257 }