]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape_android/PublisherShape.java
shape_android: fix freezes on app restart
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape_android / PublisherShape.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 org.ocera.orte.DomainApp;
22 import org.ocera.orte.Publication;
23 import org.ocera.orte.types.NtpTime;
24 import org.ocera.orte.types.PublProp;
25
26 import android.graphics.Color;
27 import android.graphics.Path;
28 import android.graphics.Path.FillType;
29 import android.graphics.drawable.ShapeDrawable;
30 import android.graphics.drawable.shapes.OvalShape;
31 import android.graphics.drawable.shapes.PathShape;
32 import android.graphics.drawable.shapes.RectShape;
33 import android.graphics.drawable.shapes.Shape;
34
35 /**
36  * Some additional parameters for {@link ShapeDrawable}
37  * to make it Publisher-friendly.
38  * 
39  * @author jiri hubacek <jiri.hubacek@gmail.com>
40  * @version %I%, %G%
41  * @since 1.0
42  */
43 public class PublisherShape extends ShapeDrawable
44 {
45         // Prefer at least 48x48 shape size (Full asset), for more
46         //  info, please refer to http://developer.android.com/
47         
48         private boolean manual;
49         private int incX, incY;
50         
51         private PublProp publisherProperties;
52         private NtpTime persistence;
53         
54         public Publication publication;
55         public BoxType box;
56         
57         /**
58          * Set new {@link Shape}, strength and color.
59          * 
60          * @param s strength, shape
61          * @param c color
62          * @param appDomain
63          * @since 1.0
64          */
65         public PublisherShape(int s, int c, DomainApp appDomain)
66         {
67                 super((s == 0)
68                                 ?new RectShape()
69                                 :((s == 1)
70                                         ?new OvalShape()
71                                         :((s == 2)
72                                                 ?new PathShape(new  Path(), PublisherActivity.SHAPE_WIDTH, PublisherActivity.SHAPE_HEIGHT)
73                                                 :new RectShape()
74                                         )
75                                 )
76                         );
77                 
78                 if (s == 2) { // fix triangle
79                         this.setShape(new PathShape(PublisherShape.returnTrianglePath(), 2*PublisherActivity.SHAPE_WIDTH, 2*PublisherActivity.SHAPE_HEIGHT));
80                 }
81                 
82                 this.manual = false;
83                 this.persistence = new NtpTime(5);
84
85                 this.box = new BoxType(appDomain, PublisherShape.getColorName(c));
86                 this.publisherProperties = new PublProp(this.box.getTopic(),
87                                 "BoxType",
88                                 this.persistence,
89                                 s+1);
90                 this.publication = appDomain.createPublication(this.publisherProperties, this.box);
91                 
92                 this.box.shape = s;
93                 this.box.color = c;
94
95                 this.getPaint().setColor(PublisherShape.getColorConstant(c));
96                 this.setPadding(0, 0, 0, 0);
97                 this.setBounds(0, 0, PublisherActivity.SHAPE_WIDTH, PublisherActivity.SHAPE_HEIGHT);
98                 
99                 //Log.d("PublisherShape", "s: "+s+", c: "+c+", left: "+this.getBounds().left+", top: "+this.getBounds().top+", right: "+this.getBounds().right+", bottom: "+this.getBounds().bottom);
100         }
101         
102         /**
103          * Update parameters of object to send.
104          * 
105          * @return Object to send.
106          * @since 1.0
107          */
108         public BoxType toSend()
109         {
110                 this.box.rectangle.top_left_x = (short) this.getBounds().left;
111                 this.box.rectangle.top_left_y = (short) this.getBounds().top;
112                 this.box.rectangle.bottom_right_x = (short) this.getBounds().right;
113                 this.box.rectangle.bottom_right_y = (short) this.getBounds().bottom;
114                 
115                 return this.box;
116         }
117         
118         /**
119          * Revert proprietary color to android color constant.
120          * 
121          * @param c Proprietary value of color.
122          * @return Android color constant.
123          * @since 1.0
124          */
125         public static int getColorConstant(int c)
126         {
127                 switch (c) {
128                 case 0:
129                         return Color.BLUE;
130                 case 1:
131                         return Color.GREEN;
132                 case 2:
133                         return Color.RED;
134                 case 3:
135                         return Color.BLACK;
136                 case 4:
137                         return Color.YELLOW;
138                 default:
139                         return 0;
140                 }
141         }
142         
143         /**
144          * Revert proprietary color to android color constant.
145          * 
146          * @return Android color constant.
147          * @since 1.0
148          */
149         public int getColorConstant()
150         {
151                 return PublisherShape.getColorConstant(this.box.color);
152         }
153         
154         /**
155          * Revert proprietary color to word.
156          * 
157          * @param c Proprietary value of color.
158          * @return Name of color.
159          * @since 1.0
160          */
161         public static String getColorName(int c)
162         {
163                 // should be done better way
164                 // texts shouldn't be static
165                 // but dynamically mapped to
166                 // strings.xml
167                 
168                 switch (c) {
169                 case 0:
170                         return "Blue";
171                 case 1:
172                         return "Green";
173                 case 2:
174                         return "Red";
175                 case 3:
176                         return "Black";
177                 case 4:
178                         return "Yellow";
179                 default:
180                         return "NONE";
181                 }
182         }
183         
184         /**
185          * Revert proprietary color to word.
186          * 
187          * @return Name of color.
188          * @since 1.0
189          */
190         public String getColorName()
191         {
192                 return PublisherShape.getColorName(this.box.color);
193         }
194         
195         /**
196          * Revert strength to shape.
197          * 
198          * @return Name of shape.
199          * @since 1.0
200          */
201         public String getShapeName()
202         {
203                 switch (this.box.shape) {
204                 case 0:
205                         return "Square";
206                 case 1:
207                         return "Circle";
208                 case 2:
209                         return "Triangle";
210                 default:
211                         return "NONE";
212                 }
213         }
214         
215         /* getters, setters */
216         
217         /**
218          * Set value of incX - how much move object
219          * in x-axis direction.
220          * 
221          * @param x
222          * @since 1.0
223          */
224         public void setIncX(int x) { this.incX = x; }
225         /**
226          * Get value of incX - how much move object
227          * in x-axis direction.
228          * 
229          * @return Distance to move in x-axis. 
230          * @since 1.0
231          */
232         public int getIncX() { return this.incX; }
233         
234         /**
235          * Set value of incY - how much move object
236          * in y-axis direction.
237          * 
238          * @param y
239          * @since 1.0
240          */
241         public void setIncY(int y) { this.incY = y; }
242         /**
243          * Get value of incY - how much move object
244          * in y-axis direction.
245          * 
246          * @return Distance to move in y-axis. 
247          * @since 1.0
248          */
249         public int getIncY() { return this.incY; }
250         
251         /**
252          * Set strength of publishing.
253          * 
254          * @param s
255          * @since 1.0
256          */
257         public void setStrength(int s) {
258                 PublProp properties = this.publication.getProperties();
259                 properties.setStrength(s);
260                 this.publication.setProperties(properties);
261         }
262         /**
263          * Get strength of publishing.
264          * 
265          * @return Strength of publishing.
266          * @since 1.0
267          */
268         public int getStrength() { return this.publication.getProperties().getStrength(); }
269         
270         /**
271          * Set if object is moved automatically or by
272          * user.
273          *      TRUE - by user
274          *  FALSE - automatically
275          *  
276          * @param b
277          * @since 1.0
278          */
279         public void setManual(boolean b) { this.manual = b; }
280         /**
281          * Get if object is moved automatically or by
282          * user.
283          * 
284          * @return How is object moved.
285          * @since 1.0
286          */
287         public boolean getManual() { return this.manual; }
288
289         /**
290          * Make triangle path for new instance.
291          * 
292          * @return Triangle path.
293          * @since 1.0
294          */
295         public static Path returnTrianglePath()
296         {
297                 Path path = new Path();
298                 path.setFillType(FillType.EVEN_ODD);
299                 path.moveTo(PublisherActivity.SHAPE_WIDTH, 0); // triangle top center
300                 path.lineTo(2*PublisherActivity.SHAPE_WIDTH, 2*PublisherActivity.SHAPE_HEIGHT); // triangle right bottom corner
301                 path.lineTo(0, 2*PublisherActivity.SHAPE_HEIGHT); // triangle left bottom corner
302                 path.lineTo(PublisherActivity.SHAPE_WIDTH, 0); // back to triangle top center
303                 path.close();
304                 
305                 return path;
306         }
307         
308         /**
309          * When publisher is removed, destroy publication.
310          * 
311          * @since 1.0
312          */
313         public void killMe()
314         {
315                 if (this.publication != null) {
316                         this.publication.destroy();
317                         this.publication = null;
318                 }
319         }
320         
321         /**
322          * Pass parameters to box.
323          * 
324          * @param currentWidth
325          * @param currentHeight
326          * @since 1.0
327          */
328         public void setScale(int currentWidth, int currentHeight)
329         {
330                 this.box.setScale(currentWidth, currentHeight);
331                 this.setBounds(this.box.rectangle.top_left_x, this.box.rectangle.top_left_y, this.box.rectangle.top_left_x+PublisherActivity.SHAPE_WIDTH, this.box.rectangle.top_left_y+PublisherActivity.SHAPE_HEIGHT);
332         }
333 }