1 package edu.ucla.iBeaconNav;
4 import java.util.LinkedList;
5 import java.util.Collection;
6 import java.util.concurrent.locks.Lock;
7 import java.util.concurrent.locks.ReentrantLock;
9 import android.app.Notification;
10 import android.app.PendingIntent;
11 import android.app.Service;
12 import android.content.Context;
13 import android.content.Intent;
14 import android.content.SharedPreferences;
15 import android.os.IBinder;
16 import android.os.Looper;
17 import android.os.Messenger;
18 import android.os.RemoteException;
19 import android.preference.PreferenceManager;
21 import com.radiusnetworks.ibeacon.*;
22 import com.radiusnetworks.ibeacon.client.*;
23 import com.radiusnetworks.ibeacon.service.*;
25 public class Task extends Service implements IBeaconConsumer, RangeNotifier, MonitorNotifier
27 /* Main -> Task Messsages */
34 /* Task -> Main messages */
35 static enum Response {
41 private Messenger messenger;
42 private IBeaconManager ibeacon;
45 private void tellMain(Response cmd, Object value)
48 android.os.Message msg = android.os.Message.obtain();
49 msg.what = cmd.ordinal();
51 this.messenger.send(msg);
52 } catch (Exception e) {
53 Util.debug("Task: error sending message", e);
57 private void notify(String text, int icon)
60 this.tellMain(Task.Response.NOTIFY, text);
63 //Notification note = new Notification(icon, null, 0);
64 //Intent intent = new Intent(this, Main.class);
65 //PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
66 //note.setLatestEventInfo(this, "iBeaconNav!", text, pend);
67 //PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
69 Notification note = new Notification.Builder(this)
70 .setContentTitle("iBeaconNav!")
71 .setContentText("iBeaconNav!")
75 this.startForeground(1, note);
78 private void handle(Command cmd, Messenger mgr)
81 if (cmd != Task.Command.REGISTER && mgr != null && mgr != this.messenger)
82 Util.debug("Task: handle - invalid messenger");
86 // Setup communication with Main
88 Util.debug("Task: handle - register");
92 // Create client thread
94 Util.debug("Task: handle - connect");
95 this.notify("Connected", android.R.drawable.presence_online);
96 this.ibeacon = IBeaconManager.getInstanceForApplication(this);
97 this.ibeacon.bind(this);
98 //this.ibeacon.setBackgroundMode(this, false);
101 // Stop client thread
103 Util.debug("Task: handle - register");
104 //this.ibeacon.setBackgroundMode(this, true);
105 this.ibeacon.unBind(this);
107 this.stopForeground(true);
113 public boolean isRunning()
115 return this.ibeacon != null;
118 /* Service Methods */
120 public void onCreate()
122 Util.debug("Task: onCreate");
124 IBeaconManager.LOG_DEBUG = true;
128 public void onDestroy()
130 Util.debug("Task: onDestroy");
135 public int onStartCommand(Intent intent, int flags, int startId)
137 Util.debug("Task: onStartCommand");
138 int rval = super.onStartCommand(intent, flags, startId);
139 Command cmd = (Command)intent.getExtras().get("Command");
140 Messenger mgr = (Messenger)intent.getExtras().get("Messenger");
141 this.handle(cmd, mgr);
146 public IBinder onBind(Intent intent)
148 Util.debug("Task: onBind");
152 /* IBeaconConsumer Methods */
154 public void onIBeaconServiceConnect() {
155 Util.debug("Task: onIBeaconServiceConnect");
157 this.ibeacon.setRangeNotifier(this);
158 this.ibeacon.setMonitorNotifier(this);
161 Region region = new Region("iBeaconNavMonitoringId", null, null, null);
162 this.ibeacon.startRangingBeaconsInRegion(region);
163 this.ibeacon.startMonitoringBeaconsInRegion(region);
164 } catch (RemoteException e) {
165 Util.debug("Task: onIBeaconServiceConnect - error");
169 /* RangeNotifier Methods */
171 public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
172 Util.debug("Task: didRangeBeaconsInRegion");
175 /* MonitorNotifier Methods */
177 public void didEnterRegion(Region region) {
178 Util.debug("Task: didEnterRegion");
182 public void didExitRegion(Region region) {
183 Util.debug("Task: didExitRegion");
187 public void didDetermineStateForRegion(int state, Region region) {
188 Util.debug("Task: didDetermineStateForRegion");