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.*;
35 import com.google.android.gms.maps.model.*;
37 public class Main extends Activity
40 private Handler handler;
41 private Messenger messenger;
43 private Sensors sensors;
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;
56 /* Private helper methods */
57 private void notice(String text)
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);
65 /* Private handler methods */
66 private void onRegister(Task task)
68 Util.debug("Main: onRegister_task");
72 private void onRegister(Sensors sensors)
74 Util.debug("Main: onRegister_sensors");
75 this.sensors = sensors;
78 private void onPosition()
80 Util.debug("Main: onPosition");
83 private void onNotify(String text)
85 Util.debug("Main: onNotify - " + text);
87 this.toast.setText(text);
91 private void onShowData(float[] data){
92 TextView textView1 = null;
93 TextView textView2 = null;
94 TextView textView3 = null;
96 int dataId = (int)(data[0]+0.5);
97 CMD.Data dataType = CMD.Data.values()[dataId];
100 textView1 = (TextView)findViewById(R.id.accText1);
101 textView2 = (TextView)findViewById(R.id.accText2);
102 textView3 = (TextView)findViewById(R.id.accText3);
106 /*textView1 = (TextView)findViewById(R.id.magText1);
107 textView2 = (TextView)findViewById(R.id.magText2);
108 textView3 = (TextView)findViewById(R.id.magText3);
111 textView1 = (TextView)findViewById(R.id.gyrText1);
112 textView2 = (TextView)findViewById(R.id.gyrText2);
113 textView3 = (TextView)findViewById(R.id.gyrText3);
116 textView1 = (TextView)findViewById(R.id.gyr1hzx);
117 textView2 = (TextView)findViewById(R.id.gyr1hzy);
118 textView3 = (TextView)findViewById(R.id.gyr1hzz);
121 textView1 = (TextView)findViewById(R.id.orientText1);
122 textView2 = (TextView)findViewById(R.id.orientText2);
123 textView3 = (TextView)findViewById(R.id.orientText3);
126 textView1 = (TextView)findViewById(R.id.wrdAccText1);
127 textView2 = (TextView)findViewById(R.id.wrdAccText2);
128 textView3 = (TextView)findViewById(R.id.wrdAccText3);
131 textView1 = (TextView)findViewById(R.id.stepCntText);
132 textView2 = (TextView)findViewById(R.id.calGrvText);
136 textView1 = (TextView)findViewById(R.id.curPosXText);
137 textView2 = (TextView)findViewById(R.id.curPosYText);
141 textView1 = (TextView)findViewById(R.id.roll);
142 textView2 = (TextView)findViewById(R.id.pitch);
143 textView3 = (TextView)findViewById(R.id.yaw);
146 textView1 = (TextView)findViewById(R.id.headingText);
150 textView1 = (TextView)findViewById(R.id.wrdGyrText1);
154 textView1 = (TextView)findViewById(R.id.stableText);
158 Util.debug("Main: Nothing Matches");
161 textView1.setText(Float.toString(data[1]));
163 textView2.setText(Float.toString(data[2]));
166 textView3.setText(Float.toString(data[3]));
170 /* Private service methods */
171 private void register()
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));
182 private void connect()
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));
191 private void disconnect()
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));
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);
210 /* Activity Methods */
212 public void onCreate(Bundle savedInstanceState)
215 super.onCreate(savedInstanceState);
216 Util.debug("Main: onCreate");
219 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
221 // Setup communication
222 this.handler = new MainHandler();
223 this.messenger = new Messenger(this.handler);
226 this.setContentView(R.layout.main);
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);
238 // TODO - remove these
239 rstHdBttn.setOnClickListener(new View.OnClickListener() {
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));
248 rstDstBttn.setOnClickListener(new View.OnClickListener() {
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));
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))
264 //LatLng sydney = new LatLng(-33.867, 151.206);
266 //map.setMyLocationEnabled(true);
267 //map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
269 //map.addMarker(new MarkerOptions()
271 // .snippet("The most populous city in Australia.")
272 // .position(sydney));
277 this.window.addTab(this.window
279 .setIndicator("State")
280 .setContent(R.id.state));
281 this.window.addTab(this.window
284 .setContent(R.id.map));
285 this.window.addTab(this.window
287 .setIndicator("Debug")
288 .setContent(R.id.debug));
290 // Attach to background service
293 } catch (Exception e) {
294 Util.debug("Error setting content view", e);
300 public void onStart()
304 Util.debug("Main: onStart");
308 public void onResume()
311 Util.debug("Main: onResume");
315 public void onPause()
318 Util.debug("Main: onPause");
325 Util.debug("Main: onStop");
329 public void onRestart()
332 Util.debug("Main: onRestart");
336 public void onDestroy()
339 Util.debug("Main: onDestroy");
343 public boolean onCreateOptionsMenu(Menu menu)
345 MenuInflater inflater = getMenuInflater();
346 inflater.inflate(R.menu.main, menu);
351 public boolean onPrepareOptionsMenu(Menu menu)
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);
360 public boolean onOptionsItemSelected(MenuItem item)
362 switch (item.getItemId()) {
366 case R.id.disconnect:
378 class MainHandler extends Handler
381 public void handleMessage(android.os.Message msg)
383 CMD.Response resp = CMD.Response.values()[msg.what];
386 Main.this.onRegister((Task)msg.obj);
388 case REGISTER_SENSORS:
389 Main.this.onRegister((Sensors)msg.obj);
392 Main.this.onNotify((String)msg.obj);
395 Main.this.onShowData((float[])msg.obj);
398 Util.debug("Main: unknown message - " + resp);