package edu.ucla.iBeaconNav; import java.util.List; import java.util.LinkedList; import java.util.Collection; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.IBinder; import android.os.Looper; 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 { /* Main -> Task Messsages */ static enum Command { REGISTER, CONNECT, DISCONNECT, }; /* Task -> Main messages */ static enum Response { POSITION, NOTIFY, }; /* Private data */ private Messenger messenger; private IBeaconManager ibeacon; /* Private methods */ private void tellMain(Response cmd, Object value) { try { android.os.Message msg = android.os.Message.obtain(); msg.what = cmd.ordinal(); msg.obj = value; this.messenger.send(msg); } catch (Exception e) { Util.debug("Task: error sending message", e); } } private void notify(String text, int icon) { // Notify Main this.tellMain(Task.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!") .setSmallIcon(icon) .build(); this.startForeground(1, note); } private void handle(Command cmd, Messenger mgr) { // Validate messenger if (cmd != Task.Command.REGISTER && mgr != null && mgr != this.messenger) Util.debug("Task: handle - invalid messenger"); // Handle the command switch (cmd) { // Setup communication with Main case REGISTER: Util.debug("Task: handle - register"); this.messenger = mgr; break; // Create client thread case CONNECT: Util.debug("Task: handle - connect"); this.notify("Connected", android.R.drawable.presence_online); this.ibeacon = IBeaconManager.getInstanceForApplication(this); this.ibeacon.bind(this); //this.ibeacon.setBackgroundMode(this, false); break; // Stop client thread case DISCONNECT: Util.debug("Task: handle - register"); //this.ibeacon.setBackgroundMode(this, true); this.ibeacon.unBind(this); this.ibeacon = null; this.stopForeground(true); break; } } /* Public methods */ public boolean isRunning() { return this.ibeacon != null; } /* Service Methods */ @Override public void onCreate() { Util.debug("Task: onCreate"); super.onCreate(); IBeaconManager.LOG_DEBUG = true; } @Override public void onDestroy() { Util.debug("Task: onDestroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Util.debug("Task: onStartCommand"); int rval = super.onStartCommand(intent, flags, startId); Command cmd = (Command)intent.getExtras().get("Command"); Messenger mgr = (Messenger)intent.getExtras().get("Messenger"); this.handle(cmd, mgr); return rval; } @Override public IBinder onBind(Intent intent) { 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 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"); } }