]> rtime.felk.cvut.cz Git - orte.git/blob - orte/contrib/shape_android/src/org/ocera/orte/shape_android/PublisherActivity.java
Fix scaling.
[orte.git] / orte / contrib / shape_android / src / org / ocera / orte / shape_android / PublisherActivity.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.Manager;
23 import org.ocera.orte.types.NtpTime;
24
25 import android.annotation.SuppressLint;
26 import android.app.Activity;
27 import android.app.AlertDialog;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 import android.content.Intent;
31 import android.content.SharedPreferences;
32 import android.content.res.Configuration;
33 import android.net.wifi.WifiManager;
34 import android.net.wifi.WifiManager.WifiLock;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.preference.PreferenceManager;
38 import android.view.Menu;
39 import android.view.MenuItem;
40 import android.widget.LinearLayout;
41 import android.widget.SeekBar;
42 import android.widget.ViewSwitcher;
43
44 /**
45  * Class for object sent and received throw the ORTE middleware.
46  * 
47  * @author jiri hubacek <jiri.hubacek@gmail.com>
48  * @version %I, %G
49  * @since 1.2
50  */
51 public class PublisherActivity extends Activity {
52         //public final static Pattern IP_ADDRESS = Pattern.compile("((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))");
53
54         private static final int REDRAW_INTERVAL = 50;
55         private static final int STRENGTH_MAX = 5;
56         private static final int MINSEPARATION_MAX = 5;
57         private static final int RESULT_SETTINGS = 1;
58         
59         public static int SHAPE_WIDTH = 0;
60         public static int SHAPE_HEIGHT = 0;
61         
62         private WifiManager wifiManager;
63         private WifiLock wifiLock;
64         
65         private Manager orteManager;
66         private DomainApp appDomain;
67         
68         private PublisherView publisherView;
69         private SubscriberView subscriberView;
70         
71         private ViewSwitcher switcher;
72         private boolean actualIsPublisher;
73         private SharedPreferences sp;
74         
75         private Handler handler = new Handler();
76         private Runnable redraw = new Runnable()
77         {
78                 @Override
79                 public void run()
80                 {
81                         publisherView.countShapes();
82                         publisherView.invalidate();
83                         handler.postDelayed(this, REDRAW_INTERVAL);
84                 }
85         };
86         
87         /**
88          * Setting up main workspace.
89          * 
90          * @since 1.2
91          */
92         @Override
93         protected void onCreate(Bundle savedInstanceState) {
94                 super.onCreate(savedInstanceState);
95                 setContentView(R.layout.activity_publisher);
96                 
97                 // count default shape size
98                 SHAPE_WIDTH = (int) (this.getWindowManager().getDefaultDisplay().getWidth() * 25 / BoxType.DESTINATION_WIDTH);
99                 SHAPE_HEIGHT = (int) (this.getWindowManager().getDefaultDisplay().getHeight() * 45 / BoxType.DESTINATION_HEIGHT);
100                 
101                 // From Robot_Demo project.
102                 wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
103                 wifiLock = (WifiLock) wifiManager.createWifiLock((
104                                         (android.os.Build.VERSION.SDK_INT >= 12)
105                                         ? WifiManager.WIFI_MODE_FULL_HIGH_PERF
106                                         : WifiManager.WIFI_MODE_FULL
107                                 ), getClass().getName());
108
109                 sp = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
110                 
111                 // Start ORTE.
112                 orteManager = new Manager(sp.getString("prefManagers", ""));
113                 appDomain = new DomainApp();
114                 
115                 this.switcher = (ViewSwitcher) findViewById(R.id.switcher);
116                 this.actualIsPublisher = true;
117                 
118                 this.publisherView = (PublisherView) findViewById(R.id.publisher_view);
119                 this.subscriberView = (SubscriberView) findViewById(R.id.subscriber_view);
120                 
121                 this.redraw.run();
122                 this.subscriberView.addElements(this.appDomain);
123                 for (SubscriberElement e : this.subscriberView.elements) {
124                         e.setScale(this.getWindowManager().getDefaultDisplay().getWidth(), this.getWindowManager().getDefaultDisplay().getHeight());
125                         e.setAllowScaling(sp.getBoolean("prefScaling", true));
126                 }
127         }
128
129         /**
130          * When pause activity, release WiFi lock.
131          * 
132          * @since 1.0
133          */
134         @Override
135         public void onPause()
136         {
137                 super.onPause();
138                 
139                 wifiLock.release();
140         }
141         
142         /**
143          * When resume activity, acquire WiFi lock.
144          * 
145          * @since 1.0
146          */
147         @Override
148         public void onResume()
149         {
150                 super.onResume();
151                 
152                 wifiLock.acquire();
153         }
154         
155         /**
156          * When switching application off.
157          * 
158          * @since 1.0
159          */
160         @Override
161         public void onDestroy()
162         {
163                 super.onDestroy();
164                 
165                 if (appDomain != null) {
166                         appDomain.destroy();
167                         appDomain = null;
168                 }
169                 if (orteManager != null) {
170                         orteManager.destroy();
171                         orteManager = null;
172                 }
173         }
174         
175         /**
176          * When screen orientation is changed,
177          * scale recalculation is needed.
178          * 
179          * @since 1.0
180          */
181         @Override
182         public void onConfigurationChanged(Configuration newConfig)
183         {
184                 super.onConfigurationChanged(newConfig);
185                 
186                 // count default shape size
187                 SHAPE_WIDTH = (int) (this.getWindowManager().getDefaultDisplay().getWidth() * 25 / BoxType.DESTINATION_WIDTH);
188                 SHAPE_HEIGHT = (int) (this.getWindowManager().getDefaultDisplay().getHeight() * 45 / BoxType.DESTINATION_HEIGHT);
189                                 
190                 for (PublisherShape s : this.publisherView.shapes) {
191                         s.setScale(this.getWindowManager().getDefaultDisplay().getWidth(), this.getWindowManager().getDefaultDisplay().getHeight());
192                 }
193                 for (SubscriberElement e : this.subscriberView.elements) {
194                         e.setScale(this.getWindowManager().getDefaultDisplay().getWidth(), this.getWindowManager().getDefaultDisplay().getHeight());
195                 }
196                 
197                 //TODO  When change rotation in Subscriber view,
198                 //              there is no change in Publisher view, which
199                 //              leads to misbehavior. For example, there
200                 //              is Publisher of Black color in Publisher view,
201                 //              and we are looking at it in Subscriber view,
202                 //              when rotation is changed, Publisher view remains
203                 //              in old rotation while Subscriber view is in new
204                 //              rotation. Therefore Black Publisher thinks that
205                 //              it's still in old rotation and the problem with
206                 //              leaving screen is here.
207         }
208
209         /**
210          * When returning from SettingsActivity, apply settings.
211          * 
212          * @since 1.2
213          */
214     @Override
215     protected void onActivityResult(int requestCode, int resultCode, Intent data)
216     {
217             super.onActivityResult(requestCode, resultCode, data);
218         
219             switch(requestCode) {
220             case RESULT_SETTINGS:
221                 boolean allowScaling = sp.getBoolean("prefScaling", true);
222
223                         for (PublisherShape s : this.publisherView.shapes) {
224                                 s.box.allowScaling = allowScaling;
225                         }
226                 for (SubscriberElement e : this.subscriberView.elements) {
227                                 e.setAllowScaling(allowScaling);
228                         }
229                 
230                 if (orteManager != null) orteManager.destroy();
231                         orteManager = new Manager(sp.getString("prefManagers", ""));
232                         
233                 break;
234             }
235
236     }
237     
238     /**
239      * Creating menu.
240      * 
241      * @since 1.2
242      */
243         @Override
244         public boolean onCreateOptionsMenu(Menu menu) {
245                 // Inflate the menu; this adds items to the action bar if it is present.
246                 getMenuInflater().inflate(R.menu.publisher, menu);
247                 return true;
248         }
249         
250         /**
251          * All created publishers are in menu.
252          * @return 
253          * 
254          * @since 1.0
255          */
256         @Override
257         public boolean onPrepareOptionsMenu(Menu menu)
258         {
259                 menu.clear();
260                 
261                 if (this.actualIsPublisher) {
262                         getMenuInflater().inflate(R.menu.publisher, menu);
263                         for (PublisherShape shape : this.publisherView.shapes) {
264                                 menu.add(0, this.publisherView.shapes.indexOf(shape), 20 + this.publisherView.shapes.indexOf(shape), "#" + this.publisherView.shapes.indexOf(shape) + " " + shape.getColorName() + " " + shape.getShapeName());
265                         }
266                 } else {
267                         getMenuInflater().inflate(R.menu.subscriber, menu);
268                 }
269                 
270                 return super.onPrepareOptionsMenu(menu);
271         }
272         
273         /**
274          * Logic for menu selecting.
275          * 
276          * Contains switching between Publishers and Subscribers, adding
277          * new Publisher, setting Publishers, setting Subscribers, moving
278          * to SettingsActivity and HelpActivity.
279          * 
280          * @since 1.2
281          */
282         @SuppressLint("NewApi")
283         @Override
284         public boolean onOptionsItemSelected(MenuItem item) {
285                 // Handle action bar item clicks here. The action bar will
286                 // automatically handle clicks on the Home/Up button, so long
287                 // as you specify a parent activity in AndroidManifest.xml.
288                 final int id = item.getItemId();
289                 final Activity me = this;
290                 Intent intent;
291                 
292                 switch(id) {
293                 case R.id.action_switch:
294                         if (this.actualIsPublisher) {
295                                 this.switcher.showNext();
296                                 this.setTitle(R.string.title_activity_subscriber);
297                         } else {
298                                 this.switcher.showPrevious();
299                                 this.setTitle(R.string.title_activity_publisher);
300                         }
301                         this.actualIsPublisher = !this.actualIsPublisher;
302                         
303                         if (android.os.Build.VERSION.SDK_INT >= 11) this.invalidateOptionsMenu();
304                         
305                         return true;
306                 case R.id.action_new:
307                         String[] colors = new String[5];
308                         
309                         colors[0] = "Blue";
310                         colors[1] = "Green";
311                         colors[2] = "Red";
312                         colors[3] = "Black";
313                         colors[4] = "Yellow";
314                         
315                         AlertDialog.Builder builderNew = new AlertDialog.Builder(me);
316                         builderNew.setTitle(R.string.dialog_newPublisher)
317                         .setItems(colors, new DialogInterface.OnClickListener()
318                         {
319                                 @Override
320                                 public void onClick(DialogInterface dialog, int which)
321                                 {
322                                         // new publisher
323                                         
324                                         publisherView.addShape(which, appDomain);
325
326                                         for (PublisherShape s : publisherView.shapes) {
327                                                 s.setScale(me.getWindowManager().getDefaultDisplay().getWidth(), me.getWindowManager().getDefaultDisplay().getHeight());
328                                                 s.box.allowScaling = sp.getBoolean("prefScaling", true);
329                                         }
330                                         
331                                         if (android.os.Build.VERSION.SDK_INT >= 11) me.invalidateOptionsMenu();
332                                 }
333                         })
334                         .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener()
335                         {
336                                 @Override
337                                 public void onClick(DialogInterface dialog, int which)
338                                 {       
339                                 }
340                         });
341                         
342                         AlertDialog dialogNew = builderNew.create();
343                         dialogNew.show();
344                         
345                         return true;
346                 case R.id.action_settings:
347                         intent = new Intent(this, SettingsActivity.class);
348                         startActivityForResult(intent, RESULT_SETTINGS);
349                         return true;
350                 case R.id.action_help:
351                         intent = new Intent(this, HelpActivity.class);
352                         startActivity(intent);
353                         return true;
354                 default:
355                         // For settings of Publisher/Subscriber elements.
356                         LinearLayout l = new LinearLayout(me);
357                         l.setOrientation(LinearLayout.VERTICAL);
358                         AlertDialog.Builder builderSettings = new AlertDialog.Builder(me);
359                         
360                         if (this.actualIsPublisher) {
361                                 /*
362                                  * Parameters of Publisher shapes.
363                                  */
364                                 final int shapeStrength = publisherView.shapes.get(id).getStrength();
365                                 SeekBar seekStrength;
366                                 
367                                 /* seekBar */
368                                 seekStrength = new SeekBar(me);
369                                 seekStrength.setMax(STRENGTH_MAX);
370                                 seekStrength.setProgress(shapeStrength);
371                                 
372                                 seekStrength.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
373                                 {
374                                         @Override
375                                         public void onStopTrackingTouch(SeekBar seekBar) {
376                                         }
377                                         
378                                         @Override
379                                         public void onStartTrackingTouch(SeekBar seekBar) {
380                                         }
381                                         
382                                         @Override
383                                         public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
384                                         {
385                                                 publisherView.shapes.get(id).setStrength(progress);
386                                         }
387                                 });
388                                 
389                                 l.addView(seekStrength);
390                                 /* seekBar - end */
391                                 
392                                 builderSettings.setTitle("#" + id + " " + publisherView.shapes.get(id).getColorName() + " " + publisherView.shapes.get(id).getShapeName() + " strength:")
393                                 .setView(l)
394                                 .setPositiveButton(R.string.dialog_OK, new DialogInterface.OnClickListener()
395                                 {
396                                         @Override
397                                         public void onClick(DialogInterface dialog, int which)
398                                         {}
399                                 })
400                                 .setNeutralButton(R.string.dialog_delete, new DialogInterface.OnClickListener()
401                                 {       
402                                         @Override
403                                         public void onClick(DialogInterface dialog, int which) {
404                                                 publisherView.shapes.get(id).killMe();
405                                                 publisherView.shapes.remove(id);
406                                                 
407                                                 if (android.os.Build.VERSION.SDK_INT >= 11) me.invalidateOptionsMenu();
408                                         }
409                                 })
410                                 .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener()
411                                 {
412                                         @Override
413                                         public void onClick(DialogInterface dialog, int which)
414                                         {
415                                                 publisherView.shapes.get(id).setStrength(shapeStrength);
416                                         }
417                                 });
418                         
419                                 AlertDialog dialogSettings = builderSettings.create();
420                                 dialogSettings.show();
421                         } else {
422                                 /*
423                                  * Parameters of Subscriber shapes.
424                                  */
425                                 final int eId;
426                                 SeekBar seekMinSeparation;
427                                 
428                                 switch (id) {
429                                 case R.id.action_blue:
430                                         eId = 0;
431                                         break;
432                                 case R.id.action_green:
433                                         eId = 1;
434                                         break;
435                                 case R.id.action_red:
436                                         eId = 2;
437                                         break;
438                                 case R.id.action_black:
439                                         eId = 3;
440                                         break;
441                                 case R.id.action_yellow:
442                                         eId = 4;
443                                         break;
444                                 default:
445                                         eId = 0;
446                                 }
447                                 
448                                 /* seekBar */
449                                 seekMinSeparation = new SeekBar(me);
450                                 seekMinSeparation.setMax(MINSEPARATION_MAX);
451                                 seekMinSeparation.setProgress(0);
452                                 subscriberView.elements.get(eId).setMinSeparation(new NtpTime(0));
453                                 
454                                 seekMinSeparation.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
455                                 {
456                                         @Override
457                                         public void onStopTrackingTouch(SeekBar seekBar) {
458                                         }
459                                         
460                                         @Override
461                                         public void onStartTrackingTouch(SeekBar seekBar) {
462                                         }
463                                         
464                                         @Override
465                                         public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
466                                         {
467                                                 subscriberView.elements.get(eId).setMinSeparation(new NtpTime(progress));
468                                                 subscriberView.elements.get(eId).setEnabled(true);
469                                         }
470                                 });
471                                 
472                                 l.addView(seekMinSeparation);
473                                 /* seekBar - end */
474                                 
475                                 builderSettings.setTitle(PublisherShape.getColorName(eId) + " settings")
476                                 .setView(l)
477                                 .setPositiveButton(R.string.dialog_OK, new DialogInterface.OnClickListener()
478                                 {
479                                         @Override
480                                         public void onClick(DialogInterface dialog, int which)
481                                         {}
482                                 })
483                                 .setNeutralButton(subscriberView.elements.get(eId).getEnabled()?R.string.dialog_delete:R.string.dialog_add, new DialogInterface.OnClickListener()
484                                 {       
485                                         @Override
486                                         public void onClick(DialogInterface dialog, int which) {
487                                                 if (subscriberView.elements.get(eId).getEnabled()) {
488                                                         subscriberView.elements.get(eId).setEnabled(false);
489                                                 } else {
490                                                         subscriberView.elements.get(eId).setMinSeparation(new NtpTime(0));
491                                                         subscriberView.elements.get(eId).setEnabled(true);
492                                                 }
493                                         }
494                                 });
495                         
496                                 AlertDialog dialogSettings = builderSettings.create();
497                                 dialogSettings.show();
498                         }
499                 }
500                 
501                 return super.onOptionsItemSelected(item);
502         }
503 }