]> Pileus Git - ~andy/iBeaconNav/blob - src/edu/ucla/iBeaconNav/Task.java
Get iBeacon service running
[~andy/iBeaconNav] / src / edu / ucla / iBeaconNav / Task.java
1 package edu.ucla.iBeaconNav;
2
3 import java.util.List;
4 import java.util.LinkedList;
5 import java.util.Collection;
6 import java.util.concurrent.locks.Lock;
7 import java.util.concurrent.locks.ReentrantLock;
8
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;
20
21 import com.radiusnetworks.ibeacon.*;
22 import com.radiusnetworks.ibeacon.client.*;
23 import com.radiusnetworks.ibeacon.service.*;
24
25 public class Task extends Service implements IBeaconConsumer, RangeNotifier, MonitorNotifier
26 {
27         /* Main -> Task Messsages */
28         static enum Command {
29                 REGISTER,
30                 CONNECT,
31                 DISCONNECT,
32         };
33
34         /* Task -> Main messages */
35         static enum Response {
36                 POSITION,
37                 NOTIFY,
38         };
39
40         /* Private data */
41         private Messenger      messenger;
42         private IBeaconManager ibeacon;
43
44         /* Private methods */
45         private void tellMain(Response cmd, Object value)
46         {
47                 try {
48                         android.os.Message msg = android.os.Message.obtain();
49                         msg.what = cmd.ordinal();
50                         msg.obj  = value;
51                         this.messenger.send(msg);
52                 } catch (Exception e) {
53                         Util.debug("Task: error sending message", e);
54                 }
55         }
56
57         private void notify(String text, int icon)
58         {
59                 // Notify Main
60                 this.tellMain(Task.Response.NOTIFY, text);
61
62                 // Notification bar
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);
68
69                 Notification  note = new Notification.Builder(this)
70                         .setContentTitle("iBeaconNav!")
71                         .setContentText("iBeaconNav!")
72                         .setSmallIcon(icon)
73                         .build();
74
75                 this.startForeground(1, note);
76         }
77
78         private void handle(Command cmd, Messenger mgr)
79         {
80                 // Validate messenger
81                 if (cmd != Task.Command.REGISTER && mgr != null && mgr != this.messenger)
82                         Util.debug("Task: handle - invalid messenger");
83
84                 // Handle the command
85                 switch (cmd) {
86                         // Setup communication with Main
87                         case REGISTER:
88                                 Util.debug("Task: handle - register");
89                                 this.messenger = mgr;
90                                 break;
91
92                         // Create client thread
93                         case CONNECT:
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);
99                                 break;
100
101                         // Stop client thread
102                         case DISCONNECT:
103                                 Util.debug("Task: handle - register");
104                                 //this.ibeacon.setBackgroundMode(this, true);
105                                 this.ibeacon.unBind(this);
106                                 this.ibeacon = null;
107                                 this.stopForeground(true);
108                                 break;
109                 }
110         }
111
112         /* Public methods */
113         public boolean isRunning()
114         {
115                 return this.ibeacon != null;
116         }
117
118         /* Service Methods */
119         @Override
120         public void onCreate()
121         {
122                 Util.debug("Task: onCreate");
123                 super.onCreate();
124                 IBeaconManager.LOG_DEBUG = true;
125         }
126
127         @Override
128         public void onDestroy()
129         {
130                 Util.debug("Task: onDestroy");
131                 super.onDestroy();
132         }
133
134         @Override
135         public int onStartCommand(Intent intent, int flags, int startId)
136         {
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);
142                 return rval;
143         }
144
145         @Override
146         public IBinder onBind(Intent intent)
147         {
148                 Util.debug("Task: onBind");
149                 return null;
150         }
151
152         /* IBeaconConsumer Methods */
153         @Override
154         public void onIBeaconServiceConnect() {
155                 Util.debug("Task: onIBeaconServiceConnect");
156
157                 this.ibeacon.setRangeNotifier(this);
158                 this.ibeacon.setMonitorNotifier(this);
159
160                 try {
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");
166                 }
167         }
168
169         /* RangeNotifier Methods */
170         @Override
171         public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
172                 Util.debug("Task: didRangeBeaconsInRegion");
173         }
174
175         /* MonitorNotifier Methods */
176         @Override
177         public void didEnterRegion(Region region) {
178                 Util.debug("Task: didEnterRegion");
179         }
180
181         @Override
182         public void didExitRegion(Region region) {
183                 Util.debug("Task: didExitRegion");
184         }
185
186         @Override
187         public void didDetermineStateForRegion(int state, Region region) {
188                 Util.debug("Task: didDetermineStateForRegion");
189         }
190 }