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