]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Task.java
Add IRC Service and update UI
[~andy/spades] / src / org / pileus / spades / Task.java
1 package org.pileus.spades;
2
3 import java.io.*;
4 import java.net.*;
5
6 import android.app.Notification;
7 import android.app.PendingIntent;
8 import android.app.Service;
9 import android.content.Intent;
10 import android.os.IBinder;
11 import android.os.Messenger;
12
13 public class Task extends Service implements Runnable
14 {
15         /* Commands */
16         public static final int REGISTER = 0;
17         public static final int MESSAGE  = 1;
18
19         /* Configuration */
20         private String    server    = "irc.freenode.net";
21         private String    nickname  = "andydroid";
22         private String    channel   = "#rhnoise";
23         private int       port      = 6667;
24
25         /* Private data */
26         private Messenger messenger = null;
27         private Thread    thread    = null;
28         private Socket    socket    = null;
29         private Client    client    = null;
30
31         /* Private methods */
32         private void setup()
33         {
34                 Os.debug("Task: setup");
35                 try {
36                         this.socket = new Socket(server, port);
37                         this.client = new Client(server, nickname, channel);
38                         Os.debug("Task: Socket and client created");
39                 } catch(Exception e) {
40                         Os.debug("Task: Failed to create socket: " + e);
41                         return;
42                 }
43
44                 try {
45                         BufferedReader input  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
46                         PrintWriter    output = new PrintWriter(socket.getOutputStream());
47                         this.client.connect(input, output);
48                         Os.debug("Task: Client connected");
49                 } catch (Exception e) {
50                         Os.debug("Task: Failed to create readers writers: " + e);
51                         return;
52                 }
53         }
54
55         private void command(int cmd, Object value)
56         {
57                 try {
58                         android.os.Message msg = android.os.Message.obtain();
59                         msg.what = cmd;
60                         msg.obj  = value;
61                         this.messenger.send(msg);
62                 } catch (Exception e) {
63                         Os.debug("Task: error sending message");
64                 }
65         }
66
67         /* Public methods */
68         public Message send(String txt)
69         {
70                 if (this.client == null && !this.client.running)
71                         return null;
72                 return this.client.send(txt);
73         }
74
75         /* Runnable methods */
76         @Override
77         public void run()
78         {
79                 Os.debug("Task: thread run");
80                 setup();
81                 while (client.running)
82                         this.command(MESSAGE, client.recv());
83                 Os.debug("Task: thread exit");
84         }
85
86         /* Service Methods */
87         @Override
88         public void onCreate()
89         {
90                 Os.debug("Task: onCreate");
91                 super.onCreate();
92
93                 /* Setup notification bar */
94                 Notification  note   = new Notification(android.R.drawable.presence_online, null, 0);
95                 Intent        intent = new Intent(this, Main.class);
96                 PendingIntent pend   = PendingIntent.getActivity(this, 0, intent, 0);
97
98                 note.setLatestEventInfo(this, "Spades Title", "Spades Message", pend);
99                 startForeground(1, note);
100
101                 /* Start client thread */
102                 thread = new Thread(this);
103                 thread.start();
104         }
105         
106         @Override
107         public void onDestroy()
108         {
109                 Os.debug("Task: onDestroy");
110                 super.onDestroy();
111         }
112         
113         @Override
114         public void onStart(Intent intent, int startId)
115         {
116                 Os.debug("Task: onStart");
117                 super.onStart(intent, startId);
118                 this.messenger = (Messenger)intent.getExtras().get("Messenger");
119                 this.command(REGISTER, this);
120         }
121
122         @Override
123         public IBinder onBind(Intent intent)
124         {
125                 Os.debug("Task: onBind");
126                 return null;
127         }
128 }