]> Pileus Git - ~andy/iBeaconNav/blob - src/edu/ucla/iBeaconNav/Main.java
d6cc092df02781d8dd77d2b6aeccde9f80fed1a9
[~andy/iBeaconNav] / src / edu / ucla / iBeaconNav / Main.java
1 package edu.ucla.iBeaconNav;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.graphics.Color;
6 import android.graphics.Typeface;
7 import android.os.Bundle;
8 import android.os.Handler;
9 import android.os.Messenger;
10 import android.preference.PreferenceManager;
11 import android.text.Spannable;
12 import android.text.SpannableString;
13 import android.text.format.DateFormat;
14 import android.text.style.BackgroundColorSpan;
15 import android.text.style.ForegroundColorSpan;
16 import android.text.style.StrikethroughSpan;
17 import android.text.style.StyleSpan;
18 import android.text.style.UnderlineSpan;
19 import android.view.Menu;
20 import android.view.MenuInflater;
21 import android.view.MenuItem;
22 import android.view.View;
23 import android.widget.Button;
24 import android.widget.EditText;
25 import android.widget.LinearLayout;
26 import android.widget.ScrollView;
27 import android.widget.TabHost;
28 import android.widget.TabWidget;
29 import android.widget.TextView;
30 import android.widget.Toast;
31
32 import android.os.Bundle;
33  
34 import com.google.android.gms.maps.*;
35 import com.google.android.gms.maps.model.*;
36
37 public class Main extends Activity
38 {
39         /* Private data */
40         private Handler      handler;
41         private Messenger    messenger;
42         private Task         task;
43         private Sensors      sensors;
44         private Toast        toast;
45
46         /* Widgets */
47         private TabHost      window;
48         private TabWidget    tabs;
49         private LinearLayout map;
50         private LinearLayout state;
51         private TextView     debug;
52         private ScrollView   scroll;
53         private Button       rstHdBttn;
54         private Button       rstDstBttn;
55
56         /* Private helper methods */
57         private void notice(String text)
58         {
59                 String    msg  = "*** " + text + "\n";
60                 Spannable span = new SpannableString(msg);
61                 span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0);
62                 this.debug.append(span);
63         }
64
65         /* Private handler methods */
66         private void onRegister(Task task)
67         {
68                 Util.debug("Main: onRegister_task");
69                 this.task    = task;
70         }
71         
72         private void onRegister(Sensors sensors)
73         {
74                 Util.debug("Main: onRegister_sensors");
75                 this.sensors = sensors;
76         }
77
78         private void onPosition()
79         {
80                 Util.debug("Main: onPosition");
81         }
82
83         private void onNotify(String text)
84         {
85                 Util.debug("Main: onNotify - " + text);
86                 this.notice(text);
87                 this.toast.setText(text);
88                 this.toast.show();
89         }
90
91         private void onShowData(float[] data){
92                 TextView textView1 = null;
93                 TextView textView2 = null;
94                 TextView textView3 = null;
95                 int displayNum = 3;
96                 int dataId = (int)(data[0]+0.5);
97                 CMD.Data dataType = CMD.Data.values()[dataId];
98                 switch (dataType){
99                 case ACC:
100                         textView1 = (TextView)findViewById(R.id.accText1);
101                         textView2 = (TextView)findViewById(R.id.accText2);
102                         textView3 = (TextView)findViewById(R.id.accText3);
103                         break;
104                 case MAG:
105                         return;
106                         /*textView1 = (TextView)findViewById(R.id.magText1);
107                         textView2 = (TextView)findViewById(R.id.magText2);
108                         textView3 = (TextView)findViewById(R.id.magText3);
109                         break;*/
110                 case GYR:
111                         textView1 = (TextView)findViewById(R.id.gyrText1);
112                         textView2 = (TextView)findViewById(R.id.gyrText2);
113                         textView3 = (TextView)findViewById(R.id.gyrText3);
114                         break;
115                 case GYR1HZ:
116                         textView1 = (TextView)findViewById(R.id.gyr1hzx);
117                         textView2 = (TextView)findViewById(R.id.gyr1hzy);
118                         textView3 = (TextView)findViewById(R.id.gyr1hzz);
119                         break;
120                 case ORIENT:
121                         textView1 = (TextView)findViewById(R.id.orientText1);
122                         textView2 = (TextView)findViewById(R.id.orientText2);
123                         textView3 = (TextView)findViewById(R.id.orientText3);
124                         break;
125                 case WRDACC:
126                         textView1 = (TextView)findViewById(R.id.wrdAccText1);
127                         textView2 = (TextView)findViewById(R.id.wrdAccText2);
128                         textView3 = (TextView)findViewById(R.id.wrdAccText3);
129                         break;
130                 case STPCNT:
131                         textView1 = (TextView)findViewById(R.id.stepCntText);
132                         textView2 = (TextView)findViewById(R.id.calGrvText);
133                         displayNum = 2;
134                         break;
135                 case POSITION:
136                         textView1 = (TextView)findViewById(R.id.curPosXText);
137                         textView2 = (TextView)findViewById(R.id.curPosYText);
138                         displayNum = 2;
139                         break;
140                 case ROTATION:
141                         textView1 = (TextView)findViewById(R.id.roll);
142                         textView2 = (TextView)findViewById(R.id.pitch);
143                         textView3 = (TextView)findViewById(R.id.yaw);
144                         break;
145                 case HEADING:
146                         textView1 = (TextView)findViewById(R.id.headingText);
147                         displayNum = 1;
148                         break;
149                 case WRDGYR:
150                         textView1 = (TextView)findViewById(R.id.wrdGyrText1);
151                         displayNum = 1;
152                         break;
153                 case STABLE:
154                         textView1 = (TextView)findViewById(R.id.stableText);
155                         displayNum = 1;
156                         break;
157                 default:
158                         Util.debug("Main: Nothing Matches");
159                 }
160                 
161                 textView1.setText(Float.toString(data[1]));
162                 if (displayNum >1){
163                         textView2.setText(Float.toString(data[2]));
164                 }
165                 if (displayNum >2){
166                         textView3.setText(Float.toString(data[3]));
167                 }
168         }
169         
170         /* Private service methods */
171         private void register()
172         {
173                 Util.debug("Main: register");
174                 startService(new Intent(this, Task.class)
175                                 .putExtra("Command",   CMD.Command.REGISTER)
176                                 .putExtra("Messenger", this.messenger));
177                 startService(new Intent(this, Sensors.class)
178                                 .putExtra("Command",   CMD.Command.REGISTER)
179                                 .putExtra("Messenger", this.messenger));
180         }
181
182         private void connect()
183         {
184                 Util.debug("Main: connect");
185                 startService(new Intent(this, Task.class)
186                                 .putExtra("Command", CMD.Command.CONNECT));
187                 startService(new Intent(this, Sensors.class)
188                                 .putExtra("Command", CMD.Command.CONNECT));
189         }
190
191         private void disconnect()
192         {
193                 Util.debug("Main: disconnect");
194                 startService(new Intent(this, Task.class)
195                                 .putExtra("Command", CMD.Command.DISCONNECT));
196                 startService(new Intent(this, Sensors.class)
197                                 .putExtra("Command", CMD.Command.DISCONNECT));
198         }
199
200         private void quit()
201         {
202                 this.debug.setText("");
203                 stopService(new Intent(this, Task.class));
204                 Intent intent = new Intent(Intent.ACTION_MAIN);
205                 intent.addCategory(Intent.CATEGORY_HOME);
206                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
207                 startActivity(intent);
208         }
209
210         /* Activity Methods */
211         @Override
212         public void onCreate(Bundle savedInstanceState)
213         {
214                 try {
215                         super.onCreate(savedInstanceState);
216                         Util.debug("Main: onCreate");
217
218                         // Setup toast
219                         this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
220
221                         // Setup communication
222                         this.handler   = new MainHandler();
223                         this.messenger = new Messenger(this.handler);
224
225                         // Setup main layout
226                         this.setContentView(R.layout.main);
227
228                         // Find widgets
229                         this.window    = (TabHost)      findViewById(android.R.id.tabhost);
230                         this.tabs      = (TabWidget)    findViewById(android.R.id.tabs);
231                         this.map       = (LinearLayout) findViewById(R.id.map);
232                         this.state     = (LinearLayout) findViewById(R.id.state);
233                         this.debug     = (TextView)     findViewById(R.id.debug);
234                         this.scroll    = (ScrollView)   findViewById(R.id.debug_scroll);
235                         this.rstHdBttn = (Button)       findViewById(R.id.rstHdBttn);
236                         this.rstDstBttn= (Button)       findViewById(R.id.rstDstBttn);
237                         
238                         // TODO - remove these
239                         rstHdBttn.setOnClickListener(new View.OnClickListener() {
240                                 @Override
241                                 public void onClick(View v) {
242                                         // TODO Auto-generated method stub
243                                         startService(new Intent(Main.this, Sensors.class)
244                                         .putExtra("Command",   CMD.Command.RSTHEAD));
245                                 }
246                         });
247                         
248                         rstDstBttn.setOnClickListener(new View.OnClickListener() {
249                                 @Override
250                                 public void onClick(View v) {
251                                         // TODO Auto-generated method stub
252                                         startService(new Intent(Main.this, Sensors.class)
253                                         .putExtra("Command",   CMD.Command.RSTDST));
254                                 }
255                         });
256
257                         // Get a handle to the Map Fragment
258                         GoogleMap map = ((MapFragment)getFragmentManager()
259                                 .findFragmentById(R.id.map_fragment)).getMap();
260                         Marker mark = map.addMarker(new MarkerOptions()
261                                         .position(new LatLng(34.0722, -118.4441))
262                                         .title("UCLA"));
263
264                         //LatLng sydney = new LatLng(-33.867, 151.206);
265
266                         //map.setMyLocationEnabled(true);
267                         //map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
268
269                         //map.addMarker(new MarkerOptions()
270                         //      .title("Sydney")
271                         //      .snippet("The most populous city in Australia.")
272                         //      .position(sydney));
273
274                         // Add window tabs
275                         this.window.setup();
276
277                         this.window.addTab(this.window
278                                         .newTabSpec("state")
279                                         .setIndicator("State")
280                                         .setContent(R.id.state));
281                         this.window.addTab(this.window
282                                         .newTabSpec("map")
283                                         .setIndicator("Map")
284                                         .setContent(R.id.map));
285                         this.window.addTab(this.window
286                                         .newTabSpec("debug")
287                                         .setIndicator("Debug")
288                                         .setContent(R.id.debug));
289
290                         // Attach to background service
291                         this.register();
292
293                 } catch (Exception e) {
294                         Util.debug("Error setting content view", e);
295                         return;
296                 }
297         }
298
299         @Override
300         public void onStart()
301         {
302                 super.onStart();
303                 this.register();
304                 Util.debug("Main: onStart");
305         }
306
307         @Override
308         public void onResume()
309         {
310                 super.onResume();
311                 Util.debug("Main: onResume");
312         }
313
314         @Override
315         public void onPause()
316         {
317                 super.onPause();
318                 Util.debug("Main: onPause");
319         }
320
321         @Override
322         public void onStop()
323         {
324                 super.onStop();
325                 Util.debug("Main: onStop");
326         }
327
328         @Override
329         public void onRestart()
330         {
331                 super.onRestart();
332                 Util.debug("Main: onRestart");
333         }
334
335         @Override
336         public void onDestroy()
337         {
338                 super.onDestroy();
339                 Util.debug("Main: onDestroy");
340         }
341
342         @Override
343         public boolean onCreateOptionsMenu(Menu menu)
344         {
345                 MenuInflater inflater = getMenuInflater();
346                 inflater.inflate(R.menu.main, menu);
347                 return true;
348         }
349
350         @Override
351         public boolean onPrepareOptionsMenu(Menu menu)
352         {
353                 boolean running = this.task != null && this.task.isRunning();
354                 menu.findItem(R.id.connect).setVisible(!running);
355                 menu.findItem(R.id.disconnect).setVisible(running);
356                 return true;
357         }
358
359         @Override
360         public boolean onOptionsItemSelected(MenuItem item)
361         {
362                 switch (item.getItemId()) {
363                         case R.id.connect:
364                                 this.connect();
365                                 return true;
366                         case R.id.disconnect:
367                                 this.disconnect();
368                                 return true;
369                         case R.id.quit:
370                                 this.quit();
371                                 return true;
372                         default:
373                                 return false;
374                 }
375         }
376
377         /* Handler class */
378         class MainHandler extends Handler
379         {
380                 @Override
381                 public void handleMessage(android.os.Message msg)
382                 {
383                         CMD.Response resp = CMD.Response.values()[msg.what];
384                         switch (resp) {
385                                 case REGISTER_TASK:
386                                         Main.this.onRegister((Task)msg.obj);
387                                         break;
388                                 case REGISTER_SENSORS:
389                                         Main.this.onRegister((Sensors)msg.obj);
390                                         break;
391                                 case NOTIFY:
392                                         Main.this.onNotify((String)msg.obj);
393                                         break;
394                                 case SHOWDATA:
395                                         Main.this.onShowData((float[])msg.obj);
396                                         break;
397                                 default:
398                                         Util.debug("Main: unknown message - " + resp);
399                                         break;
400                         }
401                 }
402         }
403 }