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 Intent intent = new Intent(this, Main.class);
62 PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
64 Notification.Builder nb = new Notification.Builder(this)
65 .setContentIntent(pend)
67 .setContentTitle("Spades!")
69 .setContentText(text);
71 Notification note = nb.build();
72 this.startForeground(1, note);
75 private void handle(int cmd, Messenger mgr)
78 if (cmd != REGISTER && mgr != null && mgr != this.messenger) {
79 Os.debug("Task: handle - invalid messenger");
82 // Setup communication with Main
83 if (cmd == REGISTER) {
84 Os.debug("Task: handle - register");
86 this.command(REGISTER, this);
89 // Create client thread
90 if (cmd == CONNECT && this.thread == null) {
91 Os.debug("Task: handle - connect");
92 this.thread = new Thread(this);
97 if (cmd == DISCONNECT && this.thread != null) {
98 Os.debug("Task: handle - register");
102 } catch (Exception e) {
103 Os.debug("Task: error stopping service", e);
109 public Message send(String dst, String txt)
111 if (this.client == null)
113 Message msg = this.client.send(dst, txt);
115 this.command(MESSAGE, msg);
119 public Message send(String txt)
121 if (this.client == null)
123 Message msg = this.client.send(txt);
125 this.command(MESSAGE, msg);
129 public List<Object> getLog()
132 LinkedList<Object> out = new LinkedList<Object>(this.log);
137 public boolean isRunning()
139 return this.thread != null;
142 /* Runnable methods */
145 Os.debug("Task: thread run");
147 // Setup notification bar
148 this.notify("Connecting..", android.R.drawable.presence_invisible);
151 String server = this.prefs.getString ("pref_server", this.client.server);
152 String port = this.prefs.getString ("pref_port", this.client.port + "");
153 String nickname = this.prefs.getString ("pref_nickname", this.client.nickname);
154 String channel = this.prefs.getString ("pref_channel", this.client.channel);
155 boolean usesasl = this.prefs.getBoolean("pref_usesasl", this.client.usesasl);
156 String authname = this.prefs.getString ("pref_authname", this.client.authname);
157 String password = this.prefs.getString ("pref_password", this.client.password);
159 // Update client settings
160 this.client.setServer(server, Integer.parseInt(port));
161 this.client.setUser(nickname, channel);
162 this.client.setAuth(usesasl, authname, password);
165 if (!this.client.connect()) {
166 this.command(DISCONNECT, null);
167 this.notify("Unable to connect", android.R.drawable.presence_offline);
173 while (this.client.state == Client.State.SETUP) {
174 Message msg = this.client.recv();
177 this.command(MESSAGE, msg);
180 // Notify connection status
181 if (this.client.state == Client.State.READY) {
182 this.command(CONNECT, null);
183 this.notify("Connected", android.R.drawable.presence_online);
185 this.command(DISCONNECT, null);
186 this.notify("Connetion aborted", android.R.drawable.presence_offline);
190 while (this.client.state == Client.State.READY) {
191 Message msg = this.client.recv();
194 this.command(MESSAGE, msg);
197 // Notify disconnect disconnected
198 this.notify("Disconnected", android.R.drawable.presence_offline);
199 this.command(DISCONNECT, null);
201 // Shutdown the client
205 Os.debug("Task: thread exit");
208 /* Service Methods */
210 public void onCreate()
212 Os.debug("Task: onCreate");
215 this.log = new LinkedList<Object>();
216 this.lock = new ReentrantLock();
217 this.client = new Client();
218 this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
222 public void onDestroy()
224 Os.debug("Task: onDestroy");
225 this.handle(DISCONNECT, null);
229 public int onStartCommand(Intent intent, int flags, int startId)
231 Os.debug("Task: onStart");
232 super.onStartCommand(intent, flags, startId);
233 int cmd = intent.getExtras().getInt("Command");
234 Messenger mgr = (Messenger)intent.getExtras().get("Messenger");
235 this.handle(cmd, mgr);
240 public IBinder onBind(Intent intent)
242 Os.debug("Task: onBind");