package edu.ucla.iBeaconNav; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; import android.os.Messenger; import android.preference.PreferenceManager; import android.text.Spannable; import android.text.SpannableString; import android.text.format.DateFormat; import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; import android.text.style.UnderlineSpan; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TabHost; import android.widget.TabWidget; import android.widget.TextView; import android.widget.Toast; import android.os.Bundle; import com.google.android.gms.maps.MapView; public class Main extends Activity { /* Private data */ private Handler handler; private Messenger messenger; private Task task; private Sensors sensors; private Toast toast; /* Widgets */ private TabHost window; private TabWidget tabs; private LinearLayout map; private LinearLayout state; private TextView debug; private ScrollView scroll; private Button rstHdBttn; private Button rstDstBttn; /* Private helper methods */ private void notice(String text) { String msg = "*** " + text + "\n"; Spannable span = new SpannableString(msg); span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0); this.debug.append(span); } /* Private handler methods */ private void onRegister(Task task) { Util.debug("Main: onRegister_task"); this.task = task; } private void onRegister(Sensors sensors) { Util.debug("Main: onRegister_sensors"); this.sensors = sensors; } private void onPosition() { Util.debug("Main: onPosition"); } private void onNotify(String text) { Util.debug("Main: onNotify - " + text); this.notice(text); this.toast.setText(text); this.toast.show(); } private void onShowData(float[] data){ TextView textView1 = null; TextView textView2 = null; TextView textView3 = null; int displayNum = 3; int dataId = (int)(data[0]+0.5); CMD.Data dataType = CMD.Data.values()[dataId]; switch (dataType){ case ACC: textView1 = (TextView)findViewById(R.id.accText1); textView2 = (TextView)findViewById(R.id.accText2); textView3 = (TextView)findViewById(R.id.accText3); break; case MAG: return; /*textView1 = (TextView)findViewById(R.id.magText1); textView2 = (TextView)findViewById(R.id.magText2); textView3 = (TextView)findViewById(R.id.magText3); break;*/ case GYR: textView1 = (TextView)findViewById(R.id.gyrText1); textView2 = (TextView)findViewById(R.id.gyrText2); textView3 = (TextView)findViewById(R.id.gyrText3); break; case GYR1HZ: textView1 = (TextView)findViewById(R.id.gyr1hzx); textView2 = (TextView)findViewById(R.id.gyr1hzy); textView3 = (TextView)findViewById(R.id.gyr1hzz); break; case ORIENT: textView1 = (TextView)findViewById(R.id.orientText1); textView2 = (TextView)findViewById(R.id.orientText2); textView3 = (TextView)findViewById(R.id.orientText3); break; case WRDACC: textView1 = (TextView)findViewById(R.id.wrdAccText1); textView2 = (TextView)findViewById(R.id.wrdAccText2); textView3 = (TextView)findViewById(R.id.wrdAccText3); break; case STPCNT: textView1 = (TextView)findViewById(R.id.stepCntText); textView2 = (TextView)findViewById(R.id.calGrvText); displayNum = 2; break; case POSITION: textView1 = (TextView)findViewById(R.id.curPosXText); textView2 = (TextView)findViewById(R.id.curPosYText); displayNum = 2; break; case ROTATION: textView1 = (TextView)findViewById(R.id.roll); textView2 = (TextView)findViewById(R.id.pitch); textView3 = (TextView)findViewById(R.id.yaw); break; case HEADING: textView1 = (TextView)findViewById(R.id.headingText); displayNum = 1; break; case WRDGYR: textView1 = (TextView)findViewById(R.id.wrdGyrText1); displayNum = 1; break; case STABLE: textView1 = (TextView)findViewById(R.id.stableText); displayNum = 1; break; default: Util.debug("Main: Nothing Matches"); } textView1.setText(Float.toString(data[1])); if (displayNum >1){ textView2.setText(Float.toString(data[2])); } if (displayNum >2){ textView3.setText(Float.toString(data[3])); } } /* Private service methods */ private void register() { Util.debug("Main: register"); startService(new Intent(this, Task.class) .putExtra("Command", CMD.Command.REGISTER) .putExtra("Messenger", this.messenger)); startService(new Intent(this, Sensors.class) .putExtra("Command", CMD.Command.REGISTER) .putExtra("Messenger", this.messenger)); } private void connect() { Util.debug("Main: connect"); startService(new Intent(this, Task.class) .putExtra("Command", CMD.Command.CONNECT)); startService(new Intent(this, Sensors.class) .putExtra("Command", CMD.Command.CONNECT)); } private void disconnect() { Util.debug("Main: disconnect"); startService(new Intent(this, Task.class) .putExtra("Command", CMD.Command.DISCONNECT)); startService(new Intent(this, Sensors.class) .putExtra("Command", CMD.Command.DISCONNECT)); } private void quit() { this.debug.setText(""); stopService(new Intent(this, Task.class)); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } /* Activity Methods */ @Override public void onCreate(Bundle savedInstanceState) { try { super.onCreate(savedInstanceState); Util.debug("Main: onCreate"); // Setup toast this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT); // Setup communication this.handler = new MainHandler(); this.messenger = new Messenger(this.handler); // Setup main layout this.setContentView(R.layout.main); // Find widgets this.window = (TabHost) findViewById(android.R.id.tabhost); this.tabs = (TabWidget) findViewById(android.R.id.tabs); this.map = (LinearLayout) findViewById(R.id.map); this.state = (LinearLayout) findViewById(R.id.state); this.debug = (TextView) findViewById(R.id.debug); this.scroll = (ScrollView) findViewById(R.id.debug_scroll); this.rstHdBttn = (Button) findViewById(R.id.rstHdBttn); this.rstDstBttn= (Button) findViewById(R.id.rstDstBttn); // TODO - remove these rstHdBttn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startService(new Intent(Main.this, Sensors.class) .putExtra("Command", CMD.Command.RSTHEAD)); } }); rstDstBttn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startService(new Intent(Main.this, Sensors.class) .putExtra("Command", CMD.Command.RSTDST)); } }); // Get a handle to the Map Fragment //GoogleMap map = ((MapFragment)getFragmentManager() // .findFragmentById(R.id.map_fragment)).getMap(); //LatLng sydney = new LatLng(-33.867, 151.206); //map.setMyLocationEnabled(true); //map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13)); //map.addMarker(new MarkerOptions() // .title("Sydney") // .snippet("The most populous city in Australia.") // .position(sydney)); // Add window tabs this.window.setup(); this.window.addTab(this.window .newTabSpec("state") .setIndicator("State") .setContent(R.id.state)); this.window.addTab(this.window .newTabSpec("map") .setIndicator("Map") .setContent(R.id.map)); this.window.addTab(this.window .newTabSpec("debug") .setIndicator("Debug") .setContent(R.id.debug)); // Attach to background service this.register(); } catch (Exception e) { Util.debug("Error setting content view", e); return; } } @Override public void onStart() { super.onStart(); this.register(); Util.debug("Main: onStart"); } @Override public void onResume() { super.onResume(); Util.debug("Main: onResume"); } @Override public void onPause() { super.onPause(); Util.debug("Main: onPause"); } @Override public void onStop() { super.onStop(); Util.debug("Main: onStop"); } @Override public void onRestart() { super.onRestart(); Util.debug("Main: onRestart"); } @Override public void onDestroy() { super.onDestroy(); Util.debug("Main: onDestroy"); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { boolean running = this.task != null && this.task.isRunning(); menu.findItem(R.id.connect).setVisible(!running); menu.findItem(R.id.disconnect).setVisible(running); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.connect: this.connect(); return true; case R.id.disconnect: this.disconnect(); return true; case R.id.quit: this.quit(); return true; default: return false; } } /* Handler class */ class MainHandler extends Handler { @Override public void handleMessage(android.os.Message msg) { CMD.Response resp = CMD.Response.values()[msg.what]; switch (resp) { case REGISTER_TASK: Main.this.onRegister((Task)msg.obj); break; case REGISTER_SENSORS: Main.this.onRegister((Sensors)msg.obj); break; case NOTIFY: Main.this.onNotify((String)msg.obj); break; case SHOWDATA: Main.this.onShowData((float[])msg.obj); break; default: Util.debug("Main: unknown message - " + resp); break; } } } }