]> rtime.felk.cvut.cz Git - orte/eurobot.git/blob - orte/Robot_Demo/src/org/ocera/orte/demo/MainActivity.java
ROBOT_DEMO: add "About" dialog
[orte/eurobot.git] / orte / Robot_Demo / src / org / ocera / orte / demo / MainActivity.java
1 package org.ocera.orte.demo;
2
3 import org.ocera.orte.DomainApp;
4 import org.ocera.orte.Manager;
5
6 import android.app.Activity;
7 import android.app.AlertDialog;
8 import android.app.Dialog;
9 import android.content.Context;
10 import android.content.pm.ActivityInfo;
11 import android.hardware.Sensor;
12 import android.hardware.SensorEvent;
13 import android.hardware.SensorEventListener;
14 import android.hardware.SensorManager;
15 import android.net.wifi.WifiManager;
16 import android.net.wifi.WifiManager.WifiLock;
17 import android.os.Bundle;
18 import android.os.Handler;
19 import android.os.Message;
20 import android.os.PowerManager;
21 import android.os.PowerManager.WakeLock;
22 import android.view.LayoutInflater;
23 import android.view.Menu;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.view.View.OnClickListener;
27 import android.widget.Button;
28 import android.widget.EditText;
29  
30 public class MainActivity extends Activity {
31         private Dialog voltageDialog = null;
32         private AlertDialog aboutDialog = null;
33         static EditText voltage33 = null;
34         static EditText voltage50 = null;
35         static EditText voltage80 = null;
36         static EditText voltageBAT = null;
37         static Handler dialogHandler = new Handler() {
38                 @Override
39                 public void handleMessage(Message msg) {
40                         double[] values = msg.getData().getDoubleArray("voltages");
41                         if (values != null) {
42                                 if (voltage33 != null) {
43                                         voltage33.setText(Double.toString(values[0]));
44                                 }
45                                 if (voltage50 != null) {
46                                         voltage50.setText(Double.toString(values[1]));
47                                 }
48                                 if (voltage80 != null) {
49                                         voltage80.setText(Double.toString(values[2]));
50                                 }
51                                 if (voltageBAT != null) {
52                                         voltageBAT.setText(Double.toString(values[3]));
53                                 }
54                         }
55                 }
56     };
57         
58         private Manager manager = null;
59     private String[] mgrs = {"192.168.1.5","192.168.1.8","192.168.1.29",
60                 "10.1.1.1","10.1.1.2","10.1.1.3","10.1.1.4","10.1.1.5","10.1.1.6","10.1.1.7","10.1.1.8","10.1.1.9","10.1.1.10",
61                 "10.1.1.11","10.1.1.12","10.1.1.13","10.1.1.14","10.1.1.15","10.1.1.16","10.1.1.17","10.1.1.18","10.1.1.19","10.1.1.20"};
62         private MotionSpeedPublish motion_speed_publ = null;
63         private MotionSpeedSubscribe motion_speed_subs = null;
64         private HokuyoScanSubscribe hokuyo_scan = null;
65         private PwrVoltageSubscribe pwr_voltage = null;
66         private MagnetCmdPublish magnet_cmd = null;
67         private CraneCmdPublish crane_cmd = null;
68     private SensorManager mSensorManager = null;
69     private Sensor mGravity = null;
70     private SensorEventListener accel = null;
71     private PowerManager mPowerManager = null;
72     private WifiManager mWifiManager = null;
73     private WakeLock mWakeLock = null;
74     private WifiLock mWifiLock = null;
75     private DomainApp appDomain = null;
76     private HokuyoView hokuyo_view = null;
77     private MenuItem speed_publ_item = null;
78     private MenuItem speed_subs_item = null;
79     private MenuItem hokuyo_item = null;
80
81     static {
82         System.loadLibrary("jorte");     
83     }
84
85     @Override
86     protected void onResume() {
87         super.onResume();
88
89         mWakeLock.acquire();
90         mWifiLock.acquire();
91     }
92
93     @Override
94     protected void onPause() {
95         super.onPause();
96         
97         if (motion_speed_publ != null && !motion_speed_publ.isCancelled()) {
98                 motion_speed_publ.cancel();
99             mSensorManager.unregisterListener(accel);
100             this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
101                 speed_publ_item.setTitle("Start motion control");
102         }
103         
104         if (motion_speed_subs != null && !motion_speed_subs.isCancelled()) {
105                 hokuyo_view.runMotion(false);
106                 motion_speed_subs.cancel();
107                 hokuyo_view.invalidate();
108                 speed_subs_item.setTitle("Start motion monitor");
109         }
110
111         if (hokuyo_scan != null && !hokuyo_scan.isCancelled()) {
112                         hokuyo_view.run(false);
113                         hokuyo_scan.cancel();
114                         hokuyo_view.invalidate();
115                         hokuyo_item.setTitle("Start LRF");
116         }
117
118         if (pwr_voltage != null && !pwr_voltage.isCancelled()) {
119                 if (voltageDialog.isShowing())
120                         voltageDialog.dismiss();
121                         pwr_voltage.cancel();
122         }
123         
124         mWakeLock.release();
125         mWifiLock.release();
126     }
127     
128     @Override
129     protected void onDestroy() {
130         super.onDestroy();
131         
132         if (crane_cmd != null && !crane_cmd.isCancelled()) {
133                 crane_cmd.cancel();
134         }
135         
136         if (magnet_cmd != null && !crane_cmd.isCancelled()) {
137                 magnet_cmd.cancel();
138         }
139         
140         if (appDomain != null) {
141                 appDomain.destroy();
142                 appDomain = null;
143         }
144         
145         if (manager != null) {
146                 manager.destroy();
147                 manager = null;
148         }
149     }
150         
151         @SuppressWarnings("deprecation")
152         @Override
153     public void onCreate(Bundle savedInstanceState) {
154         super.onCreate(savedInstanceState);
155         setContentView(R.layout.hokuyo_view);
156         
157         voltageDialog = new Dialog(this,R.style.voltage_dialog);
158         voltageDialog.setCancelable(false);
159                 voltageDialog.setContentView(R.layout.status_dialog);
160                 Button okButton = (Button)voltageDialog.findViewById(R.id.button1);
161                 okButton.setOnClickListener(new OnClickListener() {
162                         @Override
163                         public void onClick(View arg0) {
164                                 voltageDialog.dismiss();
165                                 pwr_voltage.cancel();
166                         }
167                 });
168                 voltage33 = (EditText)voltageDialog.findViewById(R.id.editText1);
169                 voltage50 = (EditText)voltageDialog.findViewById(R.id.editText2);
170                 voltage80 = (EditText)voltageDialog.findViewById(R.id.editText3);
171                 voltageBAT = (EditText)voltageDialog.findViewById(R.id.editText4);
172                 
173                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
174                 LayoutInflater inflater = getLayoutInflater();
175                 View aboutView = inflater.inflate(R.layout.about_dialog, null);
176                 builder.setView(aboutView);
177                 builder.setPositiveButton("OK", null);
178                 aboutDialog = builder.create();
179                 
180         mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
181         mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
182         
183         mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
184         mWakeLock = mPowerManager.newWakeLock(
185                         PowerManager.SCREEN_BRIGHT_WAKE_LOCK
186                         | PowerManager.ACQUIRE_CAUSES_WAKEUP
187                         | PowerManager.ON_AFTER_RELEASE,
188                         getClass().getName());
189         
190         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
191         mWifiLock = mWifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, getClass().getName());
192         
193         hokuyo_view = (HokuyoView) findViewById(R.id.hokuyo_view);
194         
195         manager = new Manager(mgrs);
196
197         appDomain = new DomainApp();
198         magnet_cmd = new MagnetCmdPublish(appDomain);
199         magnet_cmd.start();
200         crane_cmd = new CraneCmdPublish(appDomain);
201         crane_cmd.start();
202     }
203     
204         @Override
205         public boolean onCreateOptionsMenu(Menu menu) {
206                 getMenuInflater().inflate(R.menu.activity_main, menu);
207
208                 return true;
209         }
210         
211         @Override
212         public boolean onOptionsItemSelected (MenuItem item) {
213                 System.out.println(item.getTitle());
214                 
215                 if(item.getTitle().equals("Start motion control")) {
216                         accel = new HandleAccelerometer();
217                         mSensorManager.registerListener(accel, mGravity, SensorManager.SENSOR_DELAY_GAME);
218                         if (motion_speed_publ == null)
219                                 motion_speed_publ = new MotionSpeedPublish(mGravity.getMaximumRange(),appDomain);
220                         motion_speed_publ.start();
221                         speed_publ_item = item;
222                         this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
223                         item.setTitle("Stop motion control");
224                 }
225                 else if (item.getTitle().equals("Stop motion control")) {
226                         mSensorManager.unregisterListener(accel);
227                         motion_speed_publ.cancel();
228                         this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
229                         item.setTitle("Start motion control");
230                 }
231                 else if (item.getTitle().equals("Start motion monitor")) {
232                         if (motion_speed_subs == null)
233                                 motion_speed_subs = new MotionSpeedSubscribe(appDomain, hokuyo_view);
234                         motion_speed_subs.start();
235                         hokuyo_view.runMotion(true);
236                         hokuyo_view.invalidate();
237                         speed_subs_item = item;
238                         item.setTitle("Stop motion monitor");
239                 }
240                 else if (item.getTitle().equals("Stop motion monitor")) {
241                         hokuyo_view.runMotion(false);
242                         motion_speed_subs.cancel();
243                         hokuyo_view.invalidate();
244                         item.setTitle("Start motion monitor");
245                 }
246                 else if (item.getTitle().equals("Start LRF")) {
247                         if (hokuyo_scan == null)
248                                 hokuyo_scan = new HokuyoScanSubscribe(appDomain, hokuyo_view);
249                         hokuyo_scan.start();
250                         hokuyo_view.run(true);
251                         hokuyo_view.invalidate();
252                         hokuyo_item = item;
253                         item.setTitle("Stop LRF");
254                 }
255                 else if (item.getTitle().equals("Stop LRF")) {
256                         hokuyo_view.run(false);
257                         hokuyo_scan.cancel();
258                         hokuyo_view.invalidate();
259                         item.setTitle("Start LRF");
260                 }
261                 else if (item.getTitle().equals("Crane up")) {
262                         crane_cmd.send(0x100);
263                         item.setTitle("Crane down");
264                 }
265                 else if (item.getTitle().equals("Crane down")) {
266                         crane_cmd.send(0x190);
267                         item.setTitle("Crane up");
268                 }
269                 else if (item.getTitle().equals("Magnet on")) {
270                         magnet_cmd.send(1);
271                         item.setTitle("Magnet off");                    
272                 }
273                 else if (item.getTitle().equals("Magnet off")) {
274                         magnet_cmd.send(0);
275                         item.setTitle("Magnet on");                     
276                 }
277                 else if (item.getTitle().equals("Voltage monitor")) {
278                         if (pwr_voltage == null)
279                                 pwr_voltage = new PwrVoltageSubscribe(appDomain, dialogHandler);
280                         pwr_voltage.start();
281                         voltageDialog.show();
282                 }
283                 else if (item.getTitle().equals("About")) {
284                         aboutDialog.show();
285                 }
286                 else if (item.getTitle().equals("Exit")) {
287                         finish();
288                 }
289                 
290                 return true;
291         }
292         
293     private class HandleAccelerometer implements SensorEventListener {
294
295         @Override
296         public void onAccuracyChanged(Sensor sensor, int accuracy) {}
297
298         @Override
299         public void onSensorChanged(SensorEvent event) {
300                  if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
301                          if (motion_speed_publ != null)
302                                  motion_speed_publ.setSpeed(event.values[0], event.values[1]);
303                  }
304         }
305     }
306 }