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.beacon.connect();
83 this.sensors.connect();
84 this.notify("Connected", android.R.drawable.presence_online);
89 Util.debug("Task: handle - register");
91 this.beacon.disconnect();
92 this.sensors.disconnect();
93 this.stopForeground(true);
98 this.sensors.reset_heading();
103 this.sensors.reset_distance();
109 /* Service Methods */
111 public void onCreate()
113 Util.debug("Task: onCreate");
115 this.beacon = new Beacon(this);
116 this.sensors = new Sensors(this);
120 public void onDestroy()
122 Util.debug("Task: onDestroy");
127 public int onStartCommand(Intent intent, int flags, int startId)
129 Util.debug("Task: onStartCommand");
130 int rval = super.onStartCommand(intent, flags, startId);
131 CMD.Command cmd = (CMD.Command)intent.getExtras().get("Command");
132 Messenger mgr = (Messenger)intent.getExtras().get("Messenger");
133 this.handle(cmd, mgr);
138 public IBinder onBind(Intent intent)
140 Util.debug("Task: onBind");