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;
11 import android.widget.Toast;
13 public class Task extends Service implements Runnable
16 public static final int REGISTER = 0;
17 public static final int MESSAGE = 1;
18 public static final int CONNECT = 2;
19 public static final int DISCONNECT = 3;
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;
30 private Toast toast = null;
33 private void command(int cmd, Object value)
36 android.os.Message msg = android.os.Message.obtain();
39 this.messenger.send(msg);
40 } catch (Exception e) {
41 Os.debug("Task: error sending message");
45 private void notify(String text, int icon)
48 Os.debug("Task: notify - " + text);
51 this.toast.setText(text);
55 Notification note = new Notification(icon, null, 0);
56 Intent intent = new Intent(this, Main.class);
57 PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
59 note.setLatestEventInfo(this, "Spades!", text, pend);
60 this.startForeground(1, note);
64 public Message send(String txt)
66 if (this.client == null)
68 Message msg = this.client.send(txt);
70 this.command(MESSAGE, msg);
74 /* Runnable methods */
78 Os.debug("Task: thread run");
80 // Android Toast setup
83 // Setup notification bar
84 this.notify("Connecting..", android.R.drawable.presence_invisible);
87 if (!this.client.connect(server, nickname, channel)) {
88 this.command(DISCONNECT, null);
89 this.notify("Unable to connect", android.R.drawable.presence_offline);
95 while (!this.client.ready) {
96 Message msg = this.client.recv();
99 this.command(MESSAGE, msg);
102 // Notify connection status
103 if (this.client.ready) {
104 this.command(CONNECT, null);
105 this.notify("Connected", android.R.drawable.presence_online);
107 this.command(DISCONNECT, null);
108 this.notify("Connetion aborted", android.R.drawable.presence_offline);
112 while (this.client.ready) {
113 Message msg = this.client.recv();
116 this.command(MESSAGE, msg);
119 // Notify disconnect disconnected
120 this.notify("Disconnected", android.R.drawable.presence_offline);
121 this.command(DISCONNECT, null);
123 // Shutdown the client
127 Os.debug("Task: thread exit");
130 /* Service Methods */
132 public void onCreate()
134 Os.debug("Task: onCreate");
138 Context context = this.getApplicationContext();
139 this.toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
142 this.client = new Client();
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");