1 package org.pileus.spades;
3 import android.app.Notification;
4 import android.app.PendingIntent;
5 import android.app.Service;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.content.SharedPreferences;
9 import android.os.IBinder;
10 import android.os.Looper;
11 import android.os.Messenger;
12 import android.preference.PreferenceManager;
14 public class Task extends Service implements Runnable
17 public static final int REGISTER = 0;
18 public static final int MESSAGE = 1;
19 public static final int CONNECT = 2;
20 public static final int DISCONNECT = 3;
21 public static final int NOTIFY = 4;
24 private SharedPreferences prefs;
25 private Messenger messenger;
26 private Thread thread;
27 private Client client;
30 private void command(int cmd, Object value)
33 android.os.Message msg = android.os.Message.obtain();
36 this.messenger.send(msg);
37 } catch (Exception e) {
38 Os.debug("Task: error sending message");
42 private void notify(String text, int icon)
45 this.command(NOTIFY, text);
48 Notification note = new Notification(icon, null, 0);
49 Intent intent = new Intent(this, Main.class);
50 PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
52 note.setLatestEventInfo(this, "Spades!", text, pend);
53 this.startForeground(1, note);
57 public Message send(String txt)
59 if (this.client == null)
61 Message msg = this.client.send(txt);
63 this.command(MESSAGE, msg);
67 /* Runnable methods */
71 Os.debug("Task: thread run");
73 // Setup notification bar
74 this.notify("Connecting..", android.R.drawable.presence_invisible);
77 String server = this.prefs.getString ("pref_server", this.client.server);
78 String port = this.prefs.getString ("pref_port", this.client.port + "");
79 String nickname = this.prefs.getString ("pref_nickname", this.client.nickname);
80 String channel = this.prefs.getString ("pref_channel", this.client.channel);
81 boolean usesasl = this.prefs.getBoolean("pref_usesasl", this.client.usesasl);
82 String authname = this.prefs.getString ("pref_authname", this.client.authname);
83 String password = this.prefs.getString ("pref_password", this.client.password);
85 // Update client settings
86 this.client.setServer(server, Integer.parseInt(port));
87 this.client.setUser(nickname, channel);
88 this.client.setAuth(usesasl, authname, password);
91 if (!this.client.connect()) {
92 this.command(DISCONNECT, null);
93 this.notify("Unable to connect", android.R.drawable.presence_offline);
99 while (!this.client.ready) {
100 Message msg = this.client.recv();
103 this.command(MESSAGE, msg);
106 // Notify connection status
107 if (this.client.ready) {
108 this.command(CONNECT, null);
109 this.notify("Connected", android.R.drawable.presence_online);
111 this.command(DISCONNECT, null);
112 this.notify("Connetion aborted", android.R.drawable.presence_offline);
116 while (this.client.ready) {
117 Message msg = this.client.recv();
120 this.command(MESSAGE, msg);
123 // Notify disconnect disconnected
124 this.notify("Disconnected", android.R.drawable.presence_offline);
125 this.command(DISCONNECT, null);
127 // Shutdown the client
131 Os.debug("Task: thread exit");
134 /* Service Methods */
136 public void onCreate()
138 Os.debug("Task: onCreate");
141 this.client = new Client();
142 this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
146 public void onDestroy()
148 Os.debug("Task: onDestroy");
152 } catch (Exception e) {
153 Os.debug("Task: error stopping service", e);
158 public void onStart(Intent intent, int startId)
160 Os.debug("Task: onStart");
161 super.onStart(intent, startId);
163 // Setup communication with Main
164 this.messenger = (Messenger)intent.getExtras().get("Messenger");
165 this.command(REGISTER, this);
167 // Create client thread
168 if (this.thread == null) {
169 this.thread = new Thread(this);
175 public IBinder onBind(Intent intent)
177 Os.debug("Task: onBind");