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;
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 onPosition()
73 Util.debug("Main: onPosition");
76 private void onNotify(String text)
78 Util.debug("Main: onNotify - " + text);
80 this.toast.setText(text);
84 private void onShowData(float[] data){
85 TextView textView1 = null;
86 TextView textView2 = null;
87 TextView textView3 = null;
89 int dataId = (int)(data[0]+0.5);
90 CMD.Data dataType = CMD.Data.values()[dataId];
93 textView1 = (TextView)findViewById(R.id.accText1);
94 textView2 = (TextView)findViewById(R.id.accText2);
95 textView3 = (TextView)findViewById(R.id.accText3);
99 /*textView1 = (TextView)findViewById(R.id.magText1);
100 textView2 = (TextView)findViewById(R.id.magText2);
101 textView3 = (TextView)findViewById(R.id.magText3);
104 textView1 = (TextView)findViewById(R.id.gyrText1);
105 textView2 = (TextView)findViewById(R.id.gyrText2);
106 textView3 = (TextView)findViewById(R.id.gyrText3);
109 textView1 = (TextView)findViewById(R.id.gyr1hzx);
110 textView2 = (TextView)findViewById(R.id.gyr1hzy);
111 textView3 = (TextView)findViewById(R.id.gyr1hzz);
114 textView1 = (TextView)findViewById(R.id.orientText1);
115 textView2 = (TextView)findViewById(R.id.orientText2);
116 textView3 = (TextView)findViewById(R.id.orientText3);
119 textView1 = (TextView)findViewById(R.id.wrdAccText1);
120 textView2 = (TextView)findViewById(R.id.wrdAccText2);
121 textView3 = (TextView)findViewById(R.id.wrdAccText3);
124 textView1 = (TextView)findViewById(R.id.stepCntText);
125 textView2 = (TextView)findViewById(R.id.calGrvText);
129 textView1 = (TextView)findViewById(R.id.curPosXText);
130 textView2 = (TextView)findViewById(R.id.curPosYText);
134 textView1 = (TextView)findViewById(R.id.roll);
135 textView2 = (TextView)findViewById(R.id.pitch);
136 textView3 = (TextView)findViewById(R.id.yaw);
139 textView1 = (TextView)findViewById(R.id.headingText);
143 textView1 = (TextView)findViewById(R.id.wrdGyrText1);
147 textView1 = (TextView)findViewById(R.id.stableText);
151 Util.debug("Main: Nothing Matches");
154 textView1.setText(Float.toString(data[1]));
156 textView2.setText(Float.toString(data[2]));
159 textView3.setText(Float.toString(data[3]));
163 /* Private service methods */
164 private void register()
166 Util.debug("Main: register");
167 startService(new Intent(this, Task.class)
168 .putExtra("Command", CMD.Command.REGISTER)
169 .putExtra("Messenger", this.messenger));
172 private void connect()
174 Util.debug("Main: connect");
175 startService(new Intent(this, Task.class)
176 .putExtra("Command", CMD.Command.CONNECT));
179 private void disconnect()
181 Util.debug("Main: disconnect");
182 startService(new Intent(this, Task.class)
183 .putExtra("Command", CMD.Command.DISCONNECT));
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);
196 /* Activity Methods */
198 public void onCreate(Bundle savedInstanceState)
201 super.onCreate(savedInstanceState);
202 Util.debug("Main: onCreate");
205 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
207 // Setup communication
208 this.handler = new MainHandler();
209 this.messenger = new Messenger(this.handler);
212 this.setContentView(R.layout.main);
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);
224 // TODO - remove these
225 rstHdBttn.setOnClickListener(new View.OnClickListener() {
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));
234 rstDstBttn.setOnClickListener(new View.OnClickListener() {
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));
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))
250 //LatLng sydney = new LatLng(-33.867, 151.206);
252 //map.setMyLocationEnabled(true);
253 //map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
255 //map.addMarker(new MarkerOptions()
257 // .snippet("The most populous city in Australia.")
258 // .position(sydney));
263 this.window.addTab(this.window
265 .setIndicator("State")
266 .setContent(R.id.state));
267 this.window.addTab(this.window
270 .setContent(R.id.map));
271 this.window.addTab(this.window
273 .setIndicator("Debug")
274 .setContent(R.id.debug));
276 // Attach to background service
279 } catch (Exception e) {
280 Util.debug("Error setting content view", e);
286 public void onStart()
290 Util.debug("Main: onStart");
294 public void onResume()
297 Util.debug("Main: onResume");
301 public void onPause()
304 Util.debug("Main: onPause");
311 Util.debug("Main: onStop");
315 public void onRestart()
318 Util.debug("Main: onRestart");
322 public void onDestroy()
325 Util.debug("Main: onDestroy");
329 public boolean onCreateOptionsMenu(Menu menu)
331 MenuInflater inflater = getMenuInflater();
332 inflater.inflate(R.menu.main, menu);
337 public boolean onPrepareOptionsMenu(Menu menu)
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);
346 public boolean onOptionsItemSelected(MenuItem item)
348 switch (item.getItemId()) {
352 case R.id.disconnect:
364 class MainHandler extends Handler
367 public void handleMessage(android.os.Message msg)
369 CMD.Response resp = CMD.Response.values()[msg.what];
372 Main.this.onRegister((Task)msg.obj);
375 Main.this.onNotify((String)msg.obj);
378 Main.this.onShowData((float[])msg.obj);
381 Util.debug("Main: unknown message - " + resp);