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;
43 private boolean running;
46 private TabHost window;
47 private TabWidget tabs;
48 private LinearLayout map;
49 private LinearLayout state;
50 private TextView debug;
51 private ScrollView scroll;
53 /* Private helper methods */
54 private void notice(String text)
56 String msg = "*** " + text + "\n";
57 Spannable span = new SpannableString(msg);
58 span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0);
59 this.debug.append(span);
62 /* Private handler methods */
63 private void onRegister(Task task)
65 Util.debug("Main: onRegister");
67 this.running = this.task.isRunning();
70 private void onPosition()
72 Util.debug("Main: onPosition");
75 private void onNotify(String text)
77 Util.debug("Main: onNotify - " + text);
79 this.toast.setText(text);
83 /* Private service methods */
84 private void register()
86 Util.debug("Main: register");
87 startService(new Intent(this, Task.class)
88 .putExtra("Command", Task.REGISTER)
89 .putExtra("Messenger", this.messenger));
92 private void connect()
94 Util.debug("Main: connect");
95 startService(new Intent(this, Task.class)
96 .putExtra("Command", Task.CONNECT));
100 private void disconnect()
102 Util.debug("Main: disconnect");
103 startService(new Intent(this, Task.class)
104 .putExtra("Command", Task.DISCONNECT));
105 this.running = false;
110 this.debug.setText("");
111 stopService(new Intent(this, Task.class));
112 Intent intent = new Intent(Intent.ACTION_MAIN);
113 intent.addCategory(Intent.CATEGORY_HOME);
114 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
115 startActivity(intent);
118 /* Activity Methods */
120 public void onCreate(Bundle savedInstanceState)
123 super.onCreate(savedInstanceState);
124 Util.debug("Main: onCreate");
127 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
129 // Setup communication
130 this.handler = new MainHandler();
131 this.messenger = new Messenger(this.handler);
134 this.setContentView(R.layout.main);
137 this.window = (TabHost) findViewById(android.R.id.tabhost);
138 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
139 this.map = (LinearLayout) findViewById(R.id.map);
140 this.state = (LinearLayout) findViewById(R.id.state);
141 this.debug = (TextView) findViewById(R.id.debug);
142 this.scroll = (ScrollView) findViewById(R.id.debug_scroll);
144 // Get a handle to the Map Fragment
145 //GoogleMap map = ((MapFragment)getFragmentManager()
146 // .findFragmentById(R.id.map_fragment)).getMap();
148 //LatLng sydney = new LatLng(-33.867, 151.206);
150 //map.setMyLocationEnabled(true);
151 //map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
153 //map.addMarker(new MarkerOptions()
155 // .snippet("The most populous city in Australia.")
156 // .position(sydney));
161 this.window.addTab(this.window
164 .setContent(R.id.map));
165 this.window.addTab(this.window
167 .setIndicator("State")
168 .setContent(R.id.state));
169 this.window.addTab(this.window
171 .setIndicator("Debug")
172 .setContent(R.id.debug));
174 // Attach to background service
177 } catch (Exception e) {
178 Util.debug("Error setting content view", e);
184 public void onStart()
188 Util.debug("Main: onStart");
192 public void onResume()
195 Util.debug("Main: onResume");
199 public void onPause()
202 Util.debug("Main: onPause");
209 Util.debug("Main: onStop");
213 public void onRestart()
216 Util.debug("Main: onRestart");
220 public void onDestroy()
223 Util.debug("Main: onDestroy");
227 public boolean onCreateOptionsMenu(Menu menu)
229 MenuInflater inflater = getMenuInflater();
230 inflater.inflate(R.menu.main, menu);
235 public boolean onPrepareOptionsMenu(Menu menu)
237 menu.findItem(R.id.connect).setVisible(!this.running);
238 menu.findItem(R.id.disconnect).setVisible(this.running);
243 public boolean onOptionsItemSelected(MenuItem item)
245 switch (item.getItemId()) {
249 case R.id.disconnect:
261 class MainHandler extends Handler
263 public void handleMessage(android.os.Message msg)
267 Main.this.onRegister((Task)msg.obj);
270 Main.this.onPosition();
273 Main.this.running = true;
275 case Task.DISCONNECT:
276 Main.this.running = false;
279 Main.this.onNotify((String)msg.obj);
282 Util.debug("Main: unknown message - " + msg.what);