]> Pileus Git - ~andy/iBeaconNav/blob - src/edu/ucla/iBeaconNav/Task.java
6d8c777d33960792e38be0f87f0fd13b0aa21968
[~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         /* Private data */
28         private Messenger      messenger;
29         private IBeaconManager ibeacon;
30
31         /* Private methods */
32         private void tellMain(CMD.Response cmd, Object value)
33         {
34                 try {
35                         android.os.Message msg = android.os.Message.obtain();
36                         msg.what = cmd.ordinal();
37                         msg.obj  = value;
38                         this.messenger.send(msg);
39                 } catch (Exception e) {
40                         Util.debug("Task: error sending message", e);
41                 }
42         }
43
44         private void notify(String text, int icon)
45         {
46                 // Notify Main
47                 this.tellMain(CMD.Response.NOTIFY, text);
48
49                 // Notification bar
50                 //Notification  note   = new Notification(icon, null, 0);
51                 //Intent        intent = new Intent(this, Main.class);
52                 //PendingIntent pend   = PendingIntent.getActivity(this, 0, intent, 0);
53                 //note.setLatestEventInfo(this, "iBeaconNav!", text, pend);
54                 //PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
55
56                 Notification  note = new Notification.Builder(this)
57                         .setContentTitle("iBeaconNav!")
58                         .setContentText("iBeaconNav!")
59                         .setSmallIcon(icon)
60                         .build();
61
62                 this.startForeground(1, note);
63         }
64
65         private void handle(CMD.Command cmd, Messenger mgr)
66         {
67                 // Validate messenger
68                 if (cmd != CMD.Command.REGISTER && mgr != null && mgr != this.messenger)
69                         Util.debug("Task: handle - invalid messenger");
70
71                 // Handle the command
72                 switch (cmd) {
73                         // Setup communication with Main
74                         case REGISTER:
75                                 Util.debug("Task: handle - register");
76                                 this.messenger = mgr;
77                                 break;
78
79                         // Create client thread
80                         case CONNECT:
81                                 Util.debug("Task: handle - connect");
82                                 this.notify("Connected", android.R.drawable.presence_online);
83                                 this.ibeacon = IBeaconManager.getInstanceForApplication(this);
84                                 this.ibeacon.bind(this);
85                                 //this.ibeacon.setBackgroundMode(this, false);
86                                 break;
87
88                         // Stop client thread
89                         case DISCONNECT:
90                                 Util.debug("Task: handle - register");
91                                 //this.ibeacon.setBackgroundMode(this, true);
92                                 this.ibeacon.unBind(this);
93                                 this.ibeacon = null;
94                                 this.stopForeground(true);
95                                 break;
96                 }
97         }
98
99         /* Public methods */
100         public boolean isRunning()
101         {
102                 return this.ibeacon != null;
103         }
104
105         /* Service Methods */
106         @Override
107         public void onCreate()
108         {
109                 Util.debug("Task: onCreate");
110                 super.onCreate();
111                 IBeaconManager.LOG_DEBUG = true;
112         }
113
114         @Override
115         public void onDestroy()
116         {
117                 Util.debug("Task: onDestroy");
118                 super.onDestroy();
119         }
120
121         @Override
122         public int onStartCommand(Intent intent, int flags, int startId)
123         {
124                 Util.debug("Task: onStartCommand");
125                 int         rval = super.onStartCommand(intent, flags, startId);
126                 CMD.Command cmd  = (CMD.Command)intent.getExtras().get("Command");
127                 Messenger   mgr  = (Messenger)intent.getExtras().get("Messenger");
128                 this.handle(cmd, mgr);
129                 return rval;
130         }
131
132         @Override
133         public IBinder onBind(Intent intent)
134         {
135                 Util.debug("Task: onBind");
136                 return null;
137         }
138
139         /* IBeaconConsumer Methods */
140         @Override
141         public void onIBeaconServiceConnect() {
142                 Util.debug("Task: onIBeaconServiceConnect");
143
144                 this.ibeacon.setRangeNotifier(this);
145                 this.ibeacon.setMonitorNotifier(this);
146
147                 try {
148                         Region region = new Region("iBeaconNavMonitoringId", null, null, null);
149                         this.ibeacon.startRangingBeaconsInRegion(region);
150                         this.ibeacon.startMonitoringBeaconsInRegion(region);
151                 } catch (RemoteException e) {
152                         Util.debug("Task: onIBeaconServiceConnect - error");
153                 }
154         }
155
156         /* RangeNotifier Methods */
157         @Override
158         public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
159                 Util.debug("Task: didRangeBeaconsInRegion");
160         }
161
162         /* MonitorNotifier Methods */
163         @Override
164         public void didEnterRegion(Region region) {
165                 Util.debug("Task: didEnterRegion");
166         }
167
168         @Override
169         public void didExitRegion(Region region) {
170                 Util.debug("Task: didExitRegion");
171         }
172
173         @Override
174         public void didDetermineStateForRegion(int state, Region region) {
175                 Util.debug("Task: didDetermineStateForRegion");
176         }
177 }