]> Pileus Git - ~andy/iBeaconNav/blob - src/edu/ucla/iBeaconNav/Main.java
Add Google Maps API template
[~andy/iBeaconNav] / src / edu / ucla / iBeaconNav / Main.java
1 package edu.ucla.iBeaconNav;
2
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;
31
32 import android.os.Bundle;
33  
34 import com.google.android.gms.maps.MapView;
35
36 public class Main extends Activity
37 {
38         /* Private data */
39         private Handler      handler;
40         private Messenger    messenger;
41         private Task         task;
42         private Toast        toast;
43         private boolean      running;
44
45         /* Widgets */
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
53         /* Private helper methods */
54         private void notice(String text)
55         {
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);
60         }
61
62         /* Private handler methods */
63         private void onRegister(Task task)
64         {
65                 Util.debug("Main: onRegister");
66                 this.task    = task;
67                 this.running = this.task.isRunning();
68         }
69
70         private void onPosition()
71         {
72                 Util.debug("Main: onPosition");
73         }
74
75         private void onNotify(String text)
76         {
77                 Util.debug("Main: onNotify - " + text);
78                 this.notice(text);
79                 this.toast.setText(text);
80                 this.toast.show();
81         }
82
83         /* Private service methods */
84         private void register()
85         {
86                 Util.debug("Main: register");
87                 startService(new Intent(this, Task.class)
88                                 .putExtra("Command",   Task.REGISTER)
89                                 .putExtra("Messenger", this.messenger));
90         }
91
92         private void connect()
93         {
94                 Util.debug("Main: connect");
95                 startService(new Intent(this, Task.class)
96                                 .putExtra("Command", Task.CONNECT));
97                 this.running = true;
98         }
99
100         private void disconnect()
101         {
102                 Util.debug("Main: disconnect");
103                 startService(new Intent(this, Task.class)
104                                 .putExtra("Command", Task.DISCONNECT));
105                 this.running = false;
106         }
107
108         private void quit()
109         {
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);
116         }
117
118         /* Activity Methods */
119         @Override
120         public void onCreate(Bundle savedInstanceState)
121         {
122                 try {
123                         super.onCreate(savedInstanceState);
124                         Util.debug("Main: onCreate");
125
126                         // Setup toast
127                         this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
128
129                         // Setup communication
130                         this.handler   = new MainHandler();
131                         this.messenger = new Messenger(this.handler);
132
133                         // Setup main layout
134                         this.setContentView(R.layout.main);
135
136                         // Find widgets
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);
143
144                         // Get a handle to the Map Fragment
145                         //GoogleMap map = ((MapFragment)getFragmentManager()
146                         //      .findFragmentById(R.id.map_fragment)).getMap();
147
148                         //LatLng sydney = new LatLng(-33.867, 151.206);
149
150                         //map.setMyLocationEnabled(true);
151                         //map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
152
153                         //map.addMarker(new MarkerOptions()
154                         //      .title("Sydney")
155                         //      .snippet("The most populous city in Australia.")
156                         //      .position(sydney));
157
158                         // Add window tabs
159                         this.window.setup();
160
161                         this.window.addTab(this.window
162                                         .newTabSpec("map")
163                                         .setIndicator("Map")
164                                         .setContent(R.id.map));
165                         this.window.addTab(this.window
166                                         .newTabSpec("state")
167                                         .setIndicator("State")
168                                         .setContent(R.id.state));
169                         this.window.addTab(this.window
170                                         .newTabSpec("debug")
171                                         .setIndicator("Debug")
172                                         .setContent(R.id.debug));
173
174                         // Attach to background service
175                         this.register();
176
177                 } catch (Exception e) {
178                         Util.debug("Error setting content view", e);
179                         return;
180                 }
181         }
182
183         @Override
184         public void onStart()
185         {
186                 super.onStart();
187                 this.register();
188                 Util.debug("Main: onStart");
189         }
190
191         @Override
192         public void onResume()
193         {
194                 super.onResume();
195                 Util.debug("Main: onResume");
196         }
197
198         @Override
199         public void onPause()
200         {
201                 super.onPause();
202                 Util.debug("Main: onPause");
203         }
204
205         @Override
206         public void onStop()
207         {
208                 super.onStop();
209                 Util.debug("Main: onStop");
210         }
211
212         @Override
213         public void onRestart()
214         {
215                 super.onRestart();
216                 Util.debug("Main: onRestart");
217         }
218
219         @Override
220         public void onDestroy()
221         {
222                 super.onDestroy();
223                 Util.debug("Main: onDestroy");
224         }
225
226         @Override
227         public boolean onCreateOptionsMenu(Menu menu)
228         {
229                 MenuInflater inflater = getMenuInflater();
230                 inflater.inflate(R.menu.main, menu);
231                 return true;
232         }
233
234         @Override
235         public boolean onPrepareOptionsMenu(Menu menu)
236         {
237                 menu.findItem(R.id.connect).setVisible(!this.running);
238                 menu.findItem(R.id.disconnect).setVisible(this.running);
239                 return true;
240         }
241
242         @Override
243         public boolean onOptionsItemSelected(MenuItem item)
244         {
245                 switch (item.getItemId()) {
246                         case R.id.connect:
247                                 this.connect();
248                                 return true;
249                         case R.id.disconnect:
250                                 this.disconnect();
251                                 return true;
252                         case R.id.quit:
253                                 this.quit();
254                                 return true;
255                         default:
256                                 return false;
257                 }
258         }
259
260         /* Handler class */
261         class MainHandler extends Handler
262         {
263                 public void handleMessage(android.os.Message msg)
264                 {
265                         switch (msg.what) {
266                                 case Task.REGISTER:
267                                         Main.this.onRegister((Task)msg.obj);
268                                         break;
269                                 case Task.POSITION:
270                                         Main.this.onPosition();
271                                         break;
272                                 case Task.CONNECT:
273                                         Main.this.running = true;
274                                         break;
275                                 case Task.DISCONNECT:
276                                         Main.this.running = false;
277                                         break;
278                                 case Task.NOTIFY:
279                                         Main.this.onNotify((String)msg.obj);
280                                         break;
281                                 default:
282                                         Util.debug("Main: unknown message - " + msg.what);
283                                         break;
284                         }
285                 }
286         }
287 }