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.orientText1);
116 textView2 = (TextView)findViewById(R.id.orientText2);
117 textView3 = (TextView)findViewById(R.id.orientText3);
120 textView1 = (TextView)findViewById(R.id.wrdAccText1);
121 textView2 = (TextView)findViewById(R.id.wrdAccText2);
122 textView3 = (TextView)findViewById(R.id.wrdAccText3);
125 textView1 = (TextView)findViewById(R.id.stepCntText);
126 textView2 = (TextView)findViewById(R.id.calGrvText);
130 textView1 = (TextView)findViewById(R.id.curPosXText);
131 textView2 = (TextView)findViewById(R.id.curPosYText);
135 textView1 = (TextView)findViewById(R.id.headingText);
139 textView1 = (TextView)findViewById(R.id.wrdGyrText1);
143 textView1 = (TextView)findViewById(R.id.stableText);
147 Util.debug("Main: Nothing Matches");
150 textView1.setText(Float.toString(data[1]));
152 textView2.setText(Float.toString(data[2]));
155 textView3.setText(Float.toString(data[3]));
159 /* Private service methods */
160 private void register()
162 Util.debug("Main: register");
163 startService(new Intent(this, Task.class)
164 .putExtra("Command", CMD.Command.REGISTER)
165 .putExtra("Messenger", this.messenger));
166 startService(new Intent(this, Sensors.class)
167 .putExtra("Command", CMD.Command.REGISTER)
168 .putExtra("Messenger", this.messenger));
171 private void connect()
173 Util.debug("Main: connect");
174 startService(new Intent(this, Task.class)
175 .putExtra("Command", CMD.Command.CONNECT));
176 startService(new Intent(this, Sensors.class)
177 .putExtra("Command", CMD.Command.CONNECT));
180 private void disconnect()
182 Util.debug("Main: disconnect");
183 startService(new Intent(this, Task.class)
184 .putExtra("Command", CMD.Command.DISCONNECT));
185 startService(new Intent(this, Sensors.class)
186 .putExtra("Command", CMD.Command.DISCONNECT));
191 this.debug.setText("");
192 stopService(new Intent(this, Task.class));
193 Intent intent = new Intent(Intent.ACTION_MAIN);
194 intent.addCategory(Intent.CATEGORY_HOME);
195 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
196 startActivity(intent);
199 /* Activity Methods */
201 public void onCreate(Bundle savedInstanceState)
204 super.onCreate(savedInstanceState);
205 Util.debug("Main: onCreate");
208 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
210 // Setup communication
211 this.handler = new MainHandler();
212 this.messenger = new Messenger(this.handler);
215 this.setContentView(R.layout.main);
218 this.window = (TabHost) findViewById(android.R.id.tabhost);
219 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
220 this.map = (LinearLayout) findViewById(R.id.map);
221 this.state = (LinearLayout) findViewById(R.id.state);
222 this.debug = (TextView) findViewById(R.id.debug);
223 this.scroll = (ScrollView) findViewById(R.id.debug_scroll);
224 this.rstHdBttn = (Button) findViewById(R.id.rstHdBttn);
225 this.rstDstBttn= (Button) findViewById(R.id.rstDstBttn);
227 // TODO - remove these
228 rstHdBttn.setOnClickListener(new View.OnClickListener() {
230 public void onClick(View v) {
231 // TODO Auto-generated method stub
232 startService(new Intent(Main.this, Sensors.class)
233 .putExtra("Command", CMD.Command.RSTHEAD));
237 rstDstBttn.setOnClickListener(new View.OnClickListener() {
239 public void onClick(View v) {
240 // TODO Auto-generated method stub
241 startService(new Intent(Main.this, Sensors.class)
242 .putExtra("Command", CMD.Command.RSTDST));
246 // Get a handle to the Map Fragment
247 //GoogleMap map = ((MapFragment)getFragmentManager()
248 // .findFragmentById(R.id.map_fragment)).getMap();
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);
374 case REGISTER_SENSORS:
375 Main.this.onRegister((Sensors)msg.obj);
378 Main.this.onNotify((String)msg.obj);
381 Main.this.onShowData((float[])msg.obj);
384 Util.debug("Main: unknown message - " + resp);