1 package org.pileus.spades;
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 MESSAGE = 1;
24 public static final int CONNECT = 2;
25 public static final int DISCONNECT = 3;
26 public static final int NOTIFY = 4;
29 private SharedPreferences prefs;
30 private Messenger messenger;
31 private Thread thread;
32 private Client client;
33 private List<Object> log;
37 private void command(int cmd, Object value)
39 if (cmd == MESSAGE || cmd == NOTIFY) {
45 android.os.Message msg = android.os.Message.obtain();
48 this.messenger.send(msg);
49 } catch (Exception e) {
50 Os.debug("Task: error sending message", e);
54 @SuppressWarnings("deprecation")
55 private void notify(String text, int icon)
58 this.command(NOTIFY, text);
61 Notification note = new Notification(icon, null, 0);
62 Intent intent = new Intent(this, Main.class);
63 PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
65 note.setLatestEventInfo(this, "Spades!", text, pend);
66 this.startForeground(1, note);
69 private void handle(int cmd, Messenger mgr)
72 if (cmd != REGISTER && mgr != null && mgr != this.messenger) {
73 Os.debug("Task: handle - invalid messenger");
76 // Setup communication with Main
77 if (cmd == REGISTER) {
78 Os.debug("Task: handle - register");
80 this.command(REGISTER, this);
83 // Create client thread
84 if (cmd == CONNECT && this.thread == null) {
85 Os.debug("Task: handle - connect");
86 this.thread = new Thread(this);
91 if (cmd == DISCONNECT && this.thread != null) {
92 Os.debug("Task: handle - register");
96 } catch (Exception e) {
97 Os.debug("Task: error stopping service", e);
103 public Message send(String txt)
105 if (this.client == null)
107 Message msg = this.client.send(txt);
109 this.command(MESSAGE, msg);
113 public List<Object> getLog()
116 LinkedList<Object> out = new LinkedList<Object>(this.log);
121 public boolean isRunning()
123 return this.thread != null;
126 /* Runnable methods */
130 Os.debug("Task: thread run");
132 // Setup notification bar
133 this.notify("Connecting..", android.R.drawable.presence_invisible);
136 String server = this.prefs.getString ("pref_server", this.client.server);
137 String port = this.prefs.getString ("pref_port", this.client.port + "");
138 String nickname = this.prefs.getString ("pref_nickname", this.client.nickname);
139 String channel = this.prefs.getString ("pref_channel", this.client.channel);
140 boolean usesasl = this.prefs.getBoolean("pref_usesasl", this.client.usesasl);
141 String authname = this.prefs.getString ("pref_authname", this.client.authname);
142 String password = this.prefs.getString ("pref_password", this.client.password);
144 // Update client settings
145 this.client.setServer(server, Integer.parseInt(port));
146 this.client.setUser(nickname, channel);
147 this.client.setAuth(usesasl, authname, password);
150 if (!this.client.connect()) {
151 this.command(DISCONNECT, null);
152 this.notify("Unable to connect", android.R.drawable.presence_offline);
158 while (this.client.state == Client.State.SETUP) {
159 Message msg = this.client.recv();
162 this.command(MESSAGE, msg);
165 // Notify connection status
166 if (this.client.state == Client.State.READY) {
167 this.command(CONNECT, null);
168 this.notify("Connected", android.R.drawable.presence_online);
170 this.command(DISCONNECT, null);
171 this.notify("Connetion aborted", android.R.drawable.presence_offline);
175 while (this.client.state == Client.State.READY) {
176 Message msg = this.client.recv();
179 this.command(MESSAGE, msg);
182 // Notify disconnect disconnected
183 this.notify("Disconnected", android.R.drawable.presence_offline);
184 this.command(DISCONNECT, null);
186 // Shutdown the client
190 Os.debug("Task: thread exit");
193 /* Service Methods */
195 public void onCreate()
197 Os.debug("Task: onCreate");
200 this.log = new LinkedList<Object>();
201 this.lock = new ReentrantLock();
202 this.client = new Client();
203 this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
207 public void onDestroy()
209 Os.debug("Task: onDestroy");
210 this.handle(DISCONNECT, null);
214 @SuppressWarnings("deprecation")
215 public void onStart(Intent intent, int startId)
217 Os.debug("Task: onStart");
218 super.onStart(intent, startId);
219 int cmd = intent.getExtras().getInt("Command");
220 Messenger mgr = (Messenger)intent.getExtras().get("Messenger");
221 this.handle(cmd, mgr);
225 public IBinder onBind(Intent intent)
227 Os.debug("Task: onBind");