1 package edu.ucla.iBeaconNav;
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;
32 import android.os.Bundle;
34 import com.google.android.gms.maps.MapView;
36 public class Main extends Activity
39 private Handler handler;
40 private Messenger messenger;
42 private Sensors sensors;
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;
55 /* Private helper methods */
56 private void notice(String text)
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);
64 /* Private handler methods */
65 private void onRegister(Task task)
67 Util.debug("Main: onRegister_task");
71 private void onRegister(Sensors sensors)
73 Util.debug("Main: onRegister_sensors");
74 this.sensors = sensors;
77 private void onPosition()
79 Util.debug("Main: onPosition");
82 private void onNotify(String text)
84 Util.debug("Main: onNotify - " + text);
86 this.toast.setText(text);
90 private void onShowData(float[] data){
91 TextView textView1 = null;
92 TextView textView2 = null;
93 TextView textView3 = null;
95 int dataId = (int)(data[0]+0.5);
96 CMD.Data dataType = CMD.Data.values()[dataId];
99 textView1 = (TextView)findViewById(R.id.accText1);
100 textView2 = (TextView)findViewById(R.id.accText2);
101 textView3 = (TextView)findViewById(R.id.accText3);
105 /*textView1 = (TextView)findViewById(R.id.magText1);
106 textView2 = (TextView)findViewById(R.id.magText2);
107 textView3 = (TextView)findViewById(R.id.magText3);
110 textView1 = (TextView)findViewById(R.id.gyrText1);
111 textView2 = (TextView)findViewById(R.id.gyrText2);
112 textView3 = (TextView)findViewById(R.id.gyrText3);
115 textView1 = (TextView)findViewById(R.id.gyr1hzx);
116 textView2 = (TextView)findViewById(R.id.gyr1hzy);
117 textView3 = (TextView)findViewById(R.id.gyr1hzz);
120 textView1 = (TextView)findViewById(R.id.orientText1);
121 textView2 = (TextView)findViewById(R.id.orientText2);
122 textView3 = (TextView)findViewById(R.id.orientText3);
125 textView1 = (TextView)findViewById(R.id.wrdAccText1);
126 textView2 = (TextView)findViewById(R.id.wrdAccText2);
127 textView3 = (TextView)findViewById(R.id.wrdAccText3);
130 textView1 = (TextView)findViewById(R.id.stepCntText);
131 textView2 = (TextView)findViewById(R.id.calGrvText);
135 textView1 = (TextView)findViewById(R.id.curPosXText);
136 textView2 = (TextView)findViewById(R.id.curPosYText);
140 textView1 = (TextView)findViewById(R.id.roll);
141 textView2 = (TextView)findViewById(R.id.pitch);
142 textView3 = (TextView)findViewById(R.id.yaw);
145 textView1 = (TextView)findViewById(R.id.headingText);
149 textView1 = (TextView)findViewById(R.id.wrdGyrText1);
153 textView1 = (TextView)findViewById(R.id.stableText);
157 Util.debug("Main: Nothing Matches");
160 textView1.setText(Float.toString(data[1]));
162 textView2.setText(Float.toString(data[2]));
165 textView3.setText(Float.toString(data[3]));
169 /* Private service methods */
170 private void register()
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));
181 private void connect()
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));
190 private void disconnect()
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));
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);
209 /* Activity Methods */
211 public void onCreate(Bundle savedInstanceState)
214 super.onCreate(savedInstanceState);
215 Util.debug("Main: onCreate");
218 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
220 // Setup communication
221 this.handler = new MainHandler();
222 this.messenger = new Messenger(this.handler);
225 this.setContentView(R.layout.main);
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);
237 // TODO - remove these
238 rstHdBttn.setOnClickListener(new View.OnClickListener() {
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));
247 rstDstBttn.setOnClickListener(new View.OnClickListener() {
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));
256 // Get a handle to the Map Fragment
257 //GoogleMap map = ((MapFragment)getFragmentManager()
258 // .findFragmentById(R.id.map_fragment)).getMap();
260 //LatLng sydney = new LatLng(-33.867, 151.206);
262 //map.setMyLocationEnabled(true);
263 //map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
265 //map.addMarker(new MarkerOptions()
267 // .snippet("The most populous city in Australia.")
268 // .position(sydney));
273 this.window.addTab(this.window
275 .setIndicator("State")
276 .setContent(R.id.state));
277 this.window.addTab(this.window
280 .setContent(R.id.map));
281 this.window.addTab(this.window
283 .setIndicator("Debug")
284 .setContent(R.id.debug));
286 // Attach to background service
289 } catch (Exception e) {
290 Util.debug("Error setting content view", e);
296 public void onStart()
300 Util.debug("Main: onStart");
304 public void onResume()
307 Util.debug("Main: onResume");
311 public void onPause()
314 Util.debug("Main: onPause");
321 Util.debug("Main: onStop");
325 public void onRestart()
328 Util.debug("Main: onRestart");
332 public void onDestroy()
335 Util.debug("Main: onDestroy");
339 public boolean onCreateOptionsMenu(Menu menu)
341 MenuInflater inflater = getMenuInflater();
342 inflater.inflate(R.menu.main, menu);
347 public boolean onPrepareOptionsMenu(Menu menu)
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);
356 public boolean onOptionsItemSelected(MenuItem item)
358 switch (item.getItemId()) {
362 case R.id.disconnect:
374 class MainHandler extends Handler
377 public void handleMessage(android.os.Message msg)
379 CMD.Response resp = CMD.Response.values()[msg.what];
382 Main.this.onRegister((Task)msg.obj);
384 case REGISTER_SENSORS:
385 Main.this.onRegister((Sensors)msg.obj);
388 Main.this.onNotify((String)msg.obj);
391 Main.this.onShowData((float[])msg.obj);
394 Util.debug("Main: unknown message - " + resp);