]> Pileus Git - ~andy/iBeaconNav/blob - src/edu/ucla/iBeaconNav/Task.java
Add connect/disconnect and iBeacon data
[~andy/iBeaconNav] / src / edu / ucla / iBeaconNav / Task.java
1 package edu.ucla.iBeaconNav;
2
3 import java.util.List;
4 import java.util.LinkedList;
5 import java.util.Collection;
6 import java.util.concurrent.locks.Lock;
7 import java.util.concurrent.locks.ReentrantLock;
8
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;
20
21 public class Task extends Service
22 {
23         /* Private data */
24         private Messenger      messenger;
25         private Beacon         beacon;
26         private Sensors        sensors;
27         private boolean        running;
28
29         /* Public methods */
30         public void tellMain(CMD.Response cmd, Object value)
31         {
32                 try {
33                         android.os.Message msg = android.os.Message.obtain();
34                         msg.what = cmd.ordinal();
35                         msg.obj  = value;
36                         this.messenger.send(msg);
37                 } catch (Exception e) {
38                         Util.debug("Task: error sending message", e);
39                 }
40         }
41
42         public void notify(String text, int icon)
43         {
44                 // Notify Main
45                 this.tellMain(CMD.Response.NOTIFY, text);
46
47                 // Notification bar
48                 Notification  note = new Notification.Builder(this)
49                         .setContentTitle("iBeaconNav!")
50                         .setContentText("iBeaconNav!")
51                         .setSmallIcon(icon)
52                         .build();
53
54                 this.startForeground(1, note);
55         }
56
57         public boolean isRunning()
58         {
59                 return this.running;
60         }
61
62         /* Private methods */
63         private void handle(CMD.Command cmd, Messenger mgr)
64         {
65                 // Validate messenger
66                 if (cmd != CMD.Command.REGISTER && mgr != null && mgr != this.messenger)
67                         Util.debug("Task: handle - invalid messenger");
68
69                 // Handle the command
70                 switch (cmd) {
71                         // Setup communication with Main
72                         case REGISTER:
73                                 Util.debug("Task: handle - register");
74                                 this.messenger = mgr;
75                                 this.tellMain(CMD.Response.REGISTER, this);
76                                 break;
77
78                         // Create client thread
79                         case CONNECT:
80                                 Util.debug("Task: handle - connect");
81                                 this.running = true;
82                                 this.beacon.connect();
83                                 this.sensors.connect();
84                                 this.notify("Connected", android.R.drawable.presence_online);
85                                 break;
86
87                         // Stop client thread
88                         case DISCONNECT:
89                                 Util.debug("Task: handle - register");
90                                 this.running = false;
91                                 this.beacon.disconnect();
92                                 this.sensors.disconnect();
93                                 this.stopForeground(true);
94                                 break;
95
96                         // Reset heading
97                         case RSTHEAD:
98                                 this.sensors.reset_heading();
99                                 break;
100
101                         // Reset distance
102                         case RSTDST:
103                                 this.sensors.reset_distance();
104                                 break;
105
106                 }
107         }
108
109         /* Service Methods */
110         @Override
111         public void onCreate()
112         {
113                 Util.debug("Task: onCreate");
114                 super.onCreate();
115                 this.beacon  = new Beacon(this);
116                 this.sensors = new Sensors(this);
117         }
118
119         @Override
120         public void onDestroy()
121         {
122                 Util.debug("Task: onDestroy");
123                 super.onDestroy();
124         }
125
126         @Override
127         public int onStartCommand(Intent intent, int flags, int startId)
128         {
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);
134                 return rval;
135         }
136
137         @Override
138         public IBinder onBind(Intent intent)
139         {
140                 Util.debug("Task: onBind");
141                 return null;
142         }
143 }