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;
45 private TabHost window;
46 private TabWidget tabs;
47 private LinearLayout map;
48 private LinearLayout state;
49 private TextView debug;
50 private ScrollView scroll;
52 /* Private helper methods */
53 private void notice(String text)
55 String msg = "*** " + text + "\n";
56 Spannable span = new SpannableString(msg);
57 span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0);
58 this.debug.append(span);
61 /* Private handler methods */
62 private void onRegister(Task task)
64 Util.debug("Main: onRegister");
68 private void onPosition()
70 Util.debug("Main: onPosition");
73 private void onNotify(String text)
75 Util.debug("Main: onNotify - " + text);
77 this.toast.setText(text);
81 /* Private service methods */
82 private void register()
84 Util.debug("Main: register");
85 startService(new Intent(this, Task.class)
86 .putExtra("Command", CMD.Command.REGISTER)
87 .putExtra("Messenger", this.messenger));
90 private void connect()
92 Util.debug("Main: connect");
93 startService(new Intent(this, Task.class)
94 .putExtra("Command", CMD.Command.CONNECT));
97 private void disconnect()
99 Util.debug("Main: disconnect");
100 startService(new Intent(this, Task.class)
101 .putExtra("Command", CMD.Command.DISCONNECT));
106 this.debug.setText("");
107 stopService(new Intent(this, Task.class));
108 Intent intent = new Intent(Intent.ACTION_MAIN);
109 intent.addCategory(Intent.CATEGORY_HOME);
110 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
111 startActivity(intent);
114 /* Activity Methods */
116 public void onCreate(Bundle savedInstanceState)
119 super.onCreate(savedInstanceState);
120 Util.debug("Main: onCreate");
123 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
125 // Setup communication
126 this.handler = new MainHandler();
127 this.messenger = new Messenger(this.handler);
130 this.setContentView(R.layout.main);
133 this.window = (TabHost) findViewById(android.R.id.tabhost);
134 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
135 this.map = (LinearLayout) findViewById(R.id.map);
136 this.state = (LinearLayout) findViewById(R.id.state);
137 this.debug = (TextView) findViewById(R.id.debug);
138 this.scroll = (ScrollView) findViewById(R.id.debug_scroll);
140 // Get a handle to the Map Fragment
141 //GoogleMap map = ((MapFragment)getFragmentManager()
142 // .findFragmentById(R.id.map_fragment)).getMap();
144 //LatLng sydney = new LatLng(-33.867, 151.206);
146 //map.setMyLocationEnabled(true);
147 //map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
149 //map.addMarker(new MarkerOptions()
151 // .snippet("The most populous city in Australia.")
152 // .position(sydney));
157 this.window.addTab(this.window
160 .setContent(R.id.map));
161 this.window.addTab(this.window
163 .setIndicator("State")
164 .setContent(R.id.state));
165 this.window.addTab(this.window
167 .setIndicator("Debug")
168 .setContent(R.id.debug));
170 // Attach to background service
173 } catch (Exception e) {
174 Util.debug("Error setting content view", e);
180 public void onStart()
184 Util.debug("Main: onStart");
188 public void onResume()
191 Util.debug("Main: onResume");
195 public void onPause()
198 Util.debug("Main: onPause");
205 Util.debug("Main: onStop");
209 public void onRestart()
212 Util.debug("Main: onRestart");
216 public void onDestroy()
219 Util.debug("Main: onDestroy");
223 public boolean onCreateOptionsMenu(Menu menu)
225 MenuInflater inflater = getMenuInflater();
226 inflater.inflate(R.menu.main, menu);
231 public boolean onPrepareOptionsMenu(Menu menu)
233 boolean running = this.task != null && this.task.isRunning();
234 menu.findItem(R.id.connect).setVisible(!running);
235 menu.findItem(R.id.disconnect).setVisible(running);
240 public boolean onOptionsItemSelected(MenuItem item)
242 switch (item.getItemId()) {
246 case R.id.disconnect:
258 class MainHandler extends Handler
260 public void handleMessage(android.os.Message msg)
262 CMD.Response resp = CMD.Response.values()[msg.what];
265 Main.this.onPosition();
268 Main.this.onNotify((String)msg.obj);
271 Util.debug("Main: unknown message - " + resp);