1 package edu.ucla.iBeaconNav;
4 import java.util.LinkedList;
5 import java.util.Collection;
6 import java.util.concurrent.locks.Lock;
7 import java.util.concurrent.locks.ReentrantLock;
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;
21 public class Task extends Service
24 private Messenger messenger;
25 private Beacon beacon;
26 private Sensors sensors;
27 private boolean running;
30 public void tellMain(CMD.Response cmd, Object value)
33 android.os.Message msg = android.os.Message.obtain();
34 msg.what = cmd.ordinal();
36 this.messenger.send(msg);
37 } catch (Exception e) {
38 Util.debug("Task: error sending message", e);
42 public void notify(String text, int icon)
45 this.tellMain(CMD.Response.NOTIFY, text);
48 Notification note = new Notification.Builder(this)
49 .setContentTitle("iBeaconNav!")
50 .setContentText("iBeaconNav!")
54 this.startForeground(1, note);
57 public boolean isRunning()
63 private void handle(CMD.Command cmd, Messenger mgr)
66 if (cmd != CMD.Command.REGISTER && mgr != null && mgr != this.messenger)
67 Util.debug("Task: handle - invalid messenger");
71 // Setup communication with Main
73 Util.debug("Task: handle - register");
75 this.tellMain(CMD.Response.REGISTER, this);
78 // Create client thread
80 Util.debug("Task: handle - connect");
82 this.notify("Connected", android.R.drawable.presence_online);
83 //this.ibeacon.setBackgroundMode(this, false);
88 Util.debug("Task: handle - register");
89 //this.ibeacon.setBackgroundMode(this, true);
91 this.stopForeground(true);
96 this.sensors.reset_heading();
101 this.sensors.reset_distance();
107 /* Service Methods */
109 public void onCreate()
111 Util.debug("Task: onCreate");
113 this.beacon = new Beacon(this);
114 this.sensors = new Sensors(this);
118 public void onDestroy()
120 Util.debug("Task: onDestroy");
125 public int onStartCommand(Intent intent, int flags, int startId)
127 Util.debug("Task: onStartCommand");
128 int rval = super.onStartCommand(intent, flags, startId);
129 CMD.Command cmd = (CMD.Command)intent.getExtras().get("Command");
130 Messenger mgr = (Messenger)intent.getExtras().get("Messenger");
131 this.handle(cmd, mgr);
136 public IBinder onBind(Intent intent)
138 Util.debug("Task: onBind");