1 package org.pileus.spades;
3 import android.app.Notification;
4 import android.app.PendingIntent;
5 import android.app.Service;
6 import android.content.Intent;
7 import android.os.IBinder;
8 import android.os.Messenger;
10 public class Task extends Service implements Runnable
13 public static final int REGISTER = 0;
14 public static final int MESSAGE = 1;
17 private String server = "irc.freenode.net";
18 private String nickname = "andydroid";
19 private String channel = "#rhnoise";
22 private Messenger messenger = null;
23 private Thread thread = null;
24 private Client client = null;
27 private void command(int cmd, Object value)
30 android.os.Message msg = android.os.Message.obtain();
33 this.messenger.send(msg);
34 } catch (Exception e) {
35 Os.debug("Task: error sending message");
40 public Message send(String txt)
42 if (this.client == null)
44 return this.client.send(txt);
47 /* Runnable methods */
51 Os.debug("Task: thread run");
53 if (!client.connect(server, nickname, channel))
57 Message msg = client.recv();
60 this.command(MESSAGE, msg);
66 Os.debug("Task: thread exit");
71 public void onCreate()
73 Os.debug("Task: onCreate");
76 /* Setup notification bar */
77 Notification note = new Notification(android.R.drawable.presence_online, null, 0);
78 Intent intent = new Intent(this, Main.class);
79 PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
81 note.setLatestEventInfo(this, "Spades Title", "Spades Message", pend);
82 startForeground(1, note);
84 /* Start client thread */
85 client = new Client();
86 thread = new Thread(this);
91 public void onDestroy()
93 Os.debug("Task: onDestroy");
97 } catch (Exception e) {
98 Os.debug("Task: error stopping service", e);
103 public void onStart(Intent intent, int startId)
105 Os.debug("Task: onStart");
106 super.onStart(intent, startId);
107 this.messenger = (Messenger)intent.getExtras().get("Messenger");
108 this.command(REGISTER, this);
112 public IBinder onBind(Intent intent)
114 Os.debug("Task: onBind");