]> Pileus Git - ~andy/iBeaconNav/blob - src/edu/ucla/iBeaconNav/Beacon.java
Switch to a single service design
[~andy/iBeaconNav] / src / edu / ucla / iBeaconNav / Beacon.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.ServiceConnection;
15 import android.content.SharedPreferences;
16 import android.os.IBinder;
17 import android.os.Looper;
18 import android.os.Messenger;
19 import android.os.RemoteException;
20 import android.preference.PreferenceManager;
21
22 import com.radiusnetworks.ibeacon.*;
23 import com.radiusnetworks.ibeacon.client.*;
24 import com.radiusnetworks.ibeacon.service.*;
25
26 public class Beacon implements IBeaconConsumer, RangeNotifier, MonitorNotifier
27 {
28         /* Private data */
29         private Task           task;
30
31         private IBeaconManager ibeacon;
32
33         /* Public methods */
34         public Beacon(Task task) {
35                 this.task = task;
36                 IBeaconManager.LOG_DEBUG = true;
37         }
38
39         public void connect() {
40                 Util.debug("Task: handle - connect");
41                 this.task.notify("Connected", android.R.drawable.presence_online);
42                 this.ibeacon = IBeaconManager.getInstanceForApplication(this.task);
43                 this.ibeacon.bind(this);
44                 //this.ibeacon.setBackgroundMode(this, false);
45         }
46
47         public void disconnect() {
48                 Util.debug("Task: handle - register");
49                 this.ibeacon.unBind(this);
50                 //this.ibeacon.setBackgroundMode(this, true);
51         }
52
53         /* IBeaconConsumer Methods
54          *   Pass most of these off to Task,
55          *   I don't even know why they're here */
56         @Override
57         public boolean bindService(Intent intent, ServiceConnection connection, int mode) {
58                 return this.task.bindService(intent, connection, mode);
59         }
60
61         @Override
62         public Context getApplicationContext() {
63                 return this.task.getApplicationContext();
64         }
65
66         @Override
67         public void unbindService(ServiceConnection connection)  {
68                 this.task.unbindService(connection);
69         }
70
71         @Override
72         public void onIBeaconServiceConnect() {
73                 Util.debug("Task: onIBeaconServiceConnect");
74
75                 this.ibeacon.setRangeNotifier(this);
76                 this.ibeacon.setMonitorNotifier(this);
77
78                 try {
79                         Region region = new Region("iBeaconNavMonitoringId", null, null, null);
80                         this.ibeacon.startRangingBeaconsInRegion(region);
81                         this.ibeacon.startMonitoringBeaconsInRegion(region);
82                 } catch (RemoteException e) {
83                         Util.debug("Task: onIBeaconServiceConnect - error");
84                 }
85         }
86
87         /* RangeNotifier Methods */
88         @Override
89         public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
90                 Util.debug("Task: didRangeBeaconsInRegion");
91         }
92
93         /* MonitorNotifier Methods */
94         @Override
95         public void didEnterRegion(Region region) {
96                 Util.debug("Task: didEnterRegion");
97         }
98
99         @Override
100         public void didExitRegion(Region region) {
101                 Util.debug("Task: didExitRegion");
102         }
103
104         @Override
105         public void didDetermineStateForRegion(int state, Region region) {
106                 Util.debug("Task: didDetermineStateForRegion");
107         }
108 }