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