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