]> Pileus Git - ~andy/iBeaconNav/blobdiff - src/edu/ucla/iBeaconNav/Task.java
Switch to a single service design
[~andy/iBeaconNav] / src / edu / ucla / iBeaconNav / Task.java
index 6d8c777d33960792e38be0f87f0fd13b0aa21968..c3def77fb4084f7bf983312466f94ab97626fe79 100644 (file)
@@ -18,18 +18,16 @@ import android.os.Messenger;
 import android.os.RemoteException;
 import android.preference.PreferenceManager;
 
-import com.radiusnetworks.ibeacon.*;
-import com.radiusnetworks.ibeacon.client.*;
-import com.radiusnetworks.ibeacon.service.*;
-
-public class Task extends Service implements IBeaconConsumer, RangeNotifier, MonitorNotifier
+public class Task extends Service
 {
        /* Private data */
        private Messenger      messenger;
-       private IBeaconManager ibeacon;
+       private Beacon         beacon;
+       private Sensors        sensors;
+       private boolean        running;
 
-       /* Private methods */
-       private void tellMain(CMD.Response cmd, Object value)
+       /* Public methods */
+       public void tellMain(CMD.Response cmd, Object value)
        {
                try {
                        android.os.Message msg = android.os.Message.obtain();
@@ -41,18 +39,12 @@ public class Task extends Service implements IBeaconConsumer, RangeNotifier, Mon
                }
        }
 
-       private void notify(String text, int icon)
+       public void notify(String text, int icon)
        {
                // Notify Main
                this.tellMain(CMD.Response.NOTIFY, text);
 
                // Notification bar
-               //Notification  note   = new Notification(icon, null, 0);
-               //Intent        intent = new Intent(this, Main.class);
-               //PendingIntent pend   = PendingIntent.getActivity(this, 0, intent, 0);
-               //note.setLatestEventInfo(this, "iBeaconNav!", text, pend);
-               //PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
-
                Notification  note = new Notification.Builder(this)
                        .setContentTitle("iBeaconNav!")
                        .setContentText("iBeaconNav!")
@@ -62,6 +54,12 @@ public class Task extends Service implements IBeaconConsumer, RangeNotifier, Mon
                this.startForeground(1, note);
        }
 
+       public boolean isRunning()
+       {
+               return this.running;
+       }
+
+       /* Private methods */
        private void handle(CMD.Command cmd, Messenger mgr)
        {
                // Validate messenger
@@ -74,14 +72,14 @@ public class Task extends Service implements IBeaconConsumer, RangeNotifier, Mon
                        case REGISTER:
                                Util.debug("Task: handle - register");
                                this.messenger = mgr;
+                               this.tellMain(CMD.Response.REGISTER, this);
                                break;
 
                        // Create client thread
                        case CONNECT:
                                Util.debug("Task: handle - connect");
+                               this.running = true;
                                this.notify("Connected", android.R.drawable.presence_online);
-                               this.ibeacon = IBeaconManager.getInstanceForApplication(this);
-                               this.ibeacon.bind(this);
                                //this.ibeacon.setBackgroundMode(this, false);
                                break;
 
@@ -89,17 +87,21 @@ public class Task extends Service implements IBeaconConsumer, RangeNotifier, Mon
                        case DISCONNECT:
                                Util.debug("Task: handle - register");
                                //this.ibeacon.setBackgroundMode(this, true);
-                               this.ibeacon.unBind(this);
-                               this.ibeacon = null;
+                               this.running = false;
                                this.stopForeground(true);
                                break;
-               }
-       }
 
-       /* Public methods */
-       public boolean isRunning()
-       {
-               return this.ibeacon != null;
+                       // Reset heading
+                       case RSTHEAD:
+                               this.sensors.reset_heading();
+                               break;
+
+                       // Reset distance
+                       case RSTDST:
+                               this.sensors.reset_distance();
+                               break;
+
+               }
        }
 
        /* Service Methods */
@@ -108,7 +110,8 @@ public class Task extends Service implements IBeaconConsumer, RangeNotifier, Mon
        {
                Util.debug("Task: onCreate");
                super.onCreate();
-               IBeaconManager.LOG_DEBUG = true;
+               this.beacon  = new Beacon(this);
+               this.sensors = new Sensors(this);
        }
 
        @Override
@@ -135,43 +138,4 @@ public class Task extends Service implements IBeaconConsumer, RangeNotifier, Mon
                Util.debug("Task: onBind");
                return null;
        }
-
-       /* IBeaconConsumer Methods */
-       @Override
-       public void onIBeaconServiceConnect() {
-               Util.debug("Task: onIBeaconServiceConnect");
-
-               this.ibeacon.setRangeNotifier(this);
-               this.ibeacon.setMonitorNotifier(this);
-
-               try {
-                       Region region = new Region("iBeaconNavMonitoringId", null, null, null);
-                       this.ibeacon.startRangingBeaconsInRegion(region);
-                       this.ibeacon.startMonitoringBeaconsInRegion(region);
-               } catch (RemoteException e) {
-                       Util.debug("Task: onIBeaconServiceConnect - error");
-               }
-       }
-
-       /* RangeNotifier Methods */
-        @Override
-        public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
-               Util.debug("Task: didRangeBeaconsInRegion");
-        }
-
-       /* MonitorNotifier Methods */
-       @Override
-       public void didEnterRegion(Region region) {
-               Util.debug("Task: didEnterRegion");
-       }
-
-       @Override
-       public void didExitRegion(Region region) {
-               Util.debug("Task: didExitRegion");
-       }
-
-       @Override
-       public void didDetermineStateForRegion(int state, Region region) {
-               Util.debug("Task: didDetermineStateForRegion");
-       }
 }