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