1 package edu.ucla.iBeaconNav;
4 import java.util.LinkedList;
5 import java.util.concurrent.locks.Lock;
6 import java.util.concurrent.locks.ReentrantLock;
8 import android.app.Notification;
9 import android.app.PendingIntent;
10 import android.app.Service;
11 import android.content.Context;
12 import android.content.Intent;
13 import android.content.SharedPreferences;
14 import android.os.IBinder;
15 import android.os.Looper;
16 import android.os.Messenger;
17 import android.preference.PreferenceManager;
19 public class Task extends Service implements Runnable
22 public static final int REGISTER = 0;
23 public static final int POSITION = 1;
24 public static final int CONNECT = 2;
25 public static final int DISCONNECT = 3;
26 public static final int NOTIFY = 4;
29 private Messenger messenger;
30 private Thread thread;
31 private boolean active;
34 private void tellMain(int cmd, Object value)
37 android.os.Message msg = android.os.Message.obtain();
40 this.messenger.send(msg);
41 } catch (Exception e) {
42 Util.debug("Task: error sending message", e);
46 private void notify(String text, int icon)
49 this.tellMain(NOTIFY, text);
52 //Notification note = new Notification(icon, null, 0);
53 //Intent intent = new Intent(this, Main.class);
54 //PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
56 //note.setLatestEventInfo(this, "iBeaconNav!", text, pend);
57 //this.startForeground(1, note);
60 private void handle(int cmd, Messenger mgr)
63 if (cmd != REGISTER && mgr != null && mgr != this.messenger) {
64 Util.debug("Task: handle - invalid messenger");
67 // Setup communication with Main
68 if (cmd == REGISTER) {
69 Util.debug("Task: handle - register");
71 this.tellMain(REGISTER, this);
74 // Create client thread
75 if (cmd == CONNECT && this.thread == null) {
76 Util.debug("Task: handle - connect");
77 this.thread = new Thread(this);
82 if (cmd == DISCONNECT && this.thread != null) {
83 Util.debug("Task: handle - register");
86 } catch (Exception e) {
87 Util.debug("Task: error stopping service", e);
93 public boolean isRunning()
95 return this.thread != null;
98 /* Runnable methods */
102 Util.debug("Task: thread run");
105 while (this.active) {
107 this.tellMain(POSITION, 0);
110 Util.debug("Task: thread exit");
113 /* Service Methods */
115 public void onCreate()
117 Util.debug("Task: onCreate");
122 public void onDestroy()
124 Util.debug("Task: onDestroy");
125 this.handle(DISCONNECT, null);
129 public int onStartCommand(Intent intent, int flags, int startId)
131 Util.debug("Task: onStartCommand");
132 int rval = super.onStartCommand(intent, flags, startId);
133 int cmd = intent.getExtras().getInt("Command");
134 Messenger mgr = (Messenger)intent.getExtras().get("Messenger");
135 this.handle(cmd, mgr);
140 public IBinder onBind(Intent intent)
142 Util.debug("Task: onBind");