]> Pileus Git - ~andy/iBeaconNav/blob - src/edu/ucla/iBeaconNav/Task.java
58ef0031c4324135b66f273cae858215286cc81e
[~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.concurrent.locks.Lock;
6 import java.util.concurrent.locks.ReentrantLock;
7
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;
18
19 public class Task extends Service implements Runnable
20 {
21         /* Commands */
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;
27
28         /* Private data */
29         private Messenger  messenger;
30         private Thread     thread;
31         private boolean    active;
32
33         /* Private methods */
34         private void tellMain(int cmd, Object value)
35         {
36                 try {
37                         android.os.Message msg = android.os.Message.obtain();
38                         msg.what = cmd;
39                         msg.obj  = value;
40                         this.messenger.send(msg);
41                 } catch (Exception e) {
42                         Util.debug("Task: error sending message", e);
43                 }
44         }
45
46         private void notify(String text, int icon)
47         {
48                 // Notify Main
49                 this.tellMain(NOTIFY, text);
50
51                 // Notification bar
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);
55
56                 //note.setLatestEventInfo(this, "iBeaconNav!", text, pend);
57                 //this.startForeground(1, note);
58         }
59
60         private void handle(int cmd, Messenger mgr)
61         {
62                 // Validate messenger
63                 if (cmd != REGISTER && mgr != null && mgr != this.messenger) {
64                         Util.debug("Task: handle - invalid messenger");
65                 }
66
67                 // Setup communication with Main
68                 if (cmd == REGISTER) {
69                         Util.debug("Task: handle - register");
70                         this.messenger = mgr;
71                         this.tellMain(REGISTER, this);
72                 }
73
74                 // Create client thread
75                 if (cmd == CONNECT && this.thread == null) {
76                         Util.debug("Task: handle - connect");
77                         this.thread = new Thread(this);
78                         this.thread.start();
79                 }
80
81                 // Stop client thread
82                 if (cmd == DISCONNECT && this.thread != null) {
83                         Util.debug("Task: handle - register");
84                         try {
85                                 this.thread.join();
86                         } catch (Exception e) {
87                                 Util.debug("Task: error stopping service", e);
88                         }
89                 }
90         }
91
92         /* Public methods */
93         public boolean isRunning()
94         {
95                 return this.thread != null;
96         }
97
98         /* Runnable methods */
99         @Override
100         public void run()
101         {
102                 Util.debug("Task: thread run");
103
104                 // Run nav algorithm
105                 while (this.active) {
106                         // Read sensor data
107                         this.tellMain(POSITION, 0);
108                 }
109
110                 Util.debug("Task: thread exit");
111         }
112
113         /* Service Methods */
114         @Override
115         public void onCreate()
116         {
117                 Util.debug("Task: onCreate");
118                 super.onCreate();
119         }
120
121         @Override
122         public void onDestroy()
123         {
124                 Util.debug("Task: onDestroy");
125                 this.handle(DISCONNECT, null);
126         }
127
128         @Override
129         public int onStartCommand(Intent intent, int flags, int startId)
130         {
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);
136                 return rval;
137         }
138
139         @Override
140         public IBinder onBind(Intent intent)
141         {
142                 Util.debug("Task: onBind");
143                 return null;
144         }
145 }