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.os.IBinder;
9 import android.os.Looper;
10 import android.os.Messenger;
12 public class Task extends Service implements Runnable
15 public static final int REGISTER = 0;
16 public static final int MESSAGE = 1;
17 public static final int CONNECT = 2;
18 public static final int DISCONNECT = 3;
19 public static final int NOTIFY = 4;
22 private String server = "irc.freenode.net";
23 private String nickname = "andydroid";
24 private String channel = "#rhnoise";
27 private Messenger messenger = null;
28 private Thread thread = null;
29 private Client client = null;
32 private void command(int cmd, Object value)
35 android.os.Message msg = android.os.Message.obtain();
38 this.messenger.send(msg);
39 } catch (Exception e) {
40 Os.debug("Task: error sending message");
44 private void notify(String text, int icon)
47 this.command(NOTIFY, text);
50 Notification note = new Notification(icon, null, 0);
51 Intent intent = new Intent(this, Main.class);
52 PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
54 note.setLatestEventInfo(this, "Spades!", text, pend);
55 this.startForeground(1, note);
59 public Message send(String txt)
61 if (this.client == null)
63 Message msg = this.client.send(txt);
65 this.command(MESSAGE, msg);
69 /* Runnable methods */
73 Os.debug("Task: thread run");
75 // Setup notification bar
76 this.notify("Connecting..", android.R.drawable.presence_invisible);
79 if (!this.client.connect(server, nickname, channel)) {
80 this.command(DISCONNECT, null);
81 this.notify("Unable to connect", android.R.drawable.presence_offline);
87 while (!this.client.ready) {
88 Message msg = this.client.recv();
91 this.command(MESSAGE, msg);
94 // Notify connection status
95 if (this.client.ready) {
96 this.command(CONNECT, null);
97 this.notify("Connected", android.R.drawable.presence_online);
99 this.command(DISCONNECT, null);
100 this.notify("Connetion aborted", android.R.drawable.presence_offline);
104 while (this.client.ready) {
105 Message msg = this.client.recv();
108 this.command(MESSAGE, msg);
111 // Notify disconnect disconnected
112 this.notify("Disconnected", android.R.drawable.presence_offline);
113 this.command(DISCONNECT, null);
115 // Shutdown the client
119 Os.debug("Task: thread exit");
122 /* Service Methods */
124 public void onCreate()
126 Os.debug("Task: onCreate");
130 this.client = new Client();
134 public void onDestroy()
136 Os.debug("Task: onDestroy");
140 } catch (Exception e) {
141 Os.debug("Task: error stopping service", e);
146 public void onStart(Intent intent, int startId)
148 Os.debug("Task: onStart");
149 super.onStart(intent, startId);
151 // Setup communication with Main
152 this.messenger = (Messenger)intent.getExtras().get("Messenger");
153 this.command(REGISTER, this);
155 // Create client thread
156 if (this.thread == null) {
157 this.thread = new Thread(this);
163 public IBinder onBind(Intent intent)
165 Os.debug("Task: onBind");