]> Pileus Git - ~andy/iBeaconNav/blob - src/edu/ucla/iBeaconNav/Task.java
c3def77fb4084f7bf983312466f94ab97626fe79
[~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.notify("Connected", android.R.drawable.presence_online);
83                                 //this.ibeacon.setBackgroundMode(this, false);
84                                 break;
85
86                         // Stop client thread
87                         case DISCONNECT:
88                                 Util.debug("Task: handle - register");
89                                 //this.ibeacon.setBackgroundMode(this, true);
90                                 this.running = false;
91                                 this.stopForeground(true);
92                                 break;
93
94                         // Reset heading
95                         case RSTHEAD:
96                                 this.sensors.reset_heading();
97                                 break;
98
99                         // Reset distance
100                         case RSTDST:
101                                 this.sensors.reset_distance();
102                                 break;
103
104                 }
105         }
106
107         /* Service Methods */
108         @Override
109         public void onCreate()
110         {
111                 Util.debug("Task: onCreate");
112                 super.onCreate();
113                 this.beacon  = new Beacon(this);
114                 this.sensors = new Sensors(this);
115         }
116
117         @Override
118         public void onDestroy()
119         {
120                 Util.debug("Task: onDestroy");
121                 super.onDestroy();
122         }
123
124         @Override
125         public int onStartCommand(Intent intent, int flags, int startId)
126         {
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);
132                 return rval;
133         }
134
135         @Override
136         public IBinder onBind(Intent intent)
137         {
138                 Util.debug("Task: onBind");
139                 return null;
140         }
141 }