]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Task.java
2b5be041d5da733cfccf158e4d6a89b2100ab3bd
[~andy/spades] / src / org / pileus / spades / Task.java
1 package org.pileus.spades;
2
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
12 public class Task extends Service implements Runnable
13 {
14         /* Commands */
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;
20
21         /* Configuration */
22         private String    server    = "irc.freenode.net";
23         private String    nickname  = "andydroid";
24         private String    channel   = "#rhnoise";
25
26         /* Private data */
27         private Messenger messenger = null;
28         private Thread    thread    = null;
29         private Client    client    = null;
30
31         /* Private methods */
32         private void command(int cmd, Object value)
33         {
34                 try {
35                         android.os.Message msg = android.os.Message.obtain();
36                         msg.what = cmd;
37                         msg.obj  = value;
38                         this.messenger.send(msg);
39                 } catch (Exception e) {
40                         Os.debug("Task: error sending message");
41                 }
42         }
43
44         private void notify(String text, int icon)
45         {
46                 // Notify Main
47                 this.command(NOTIFY, text);
48
49                 // Notification bar
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);
53
54                 note.setLatestEventInfo(this, "Spades!", text, pend);
55                 this.startForeground(1, note);
56         }
57
58         /* Public methods */
59         public Message send(String txt)
60         {
61                 if (this.client == null)
62                         return null;
63                 Message msg = this.client.send(txt);
64                 if (msg != null)
65                         this.command(MESSAGE, msg);
66                 return msg;
67         }
68
69         /* Runnable methods */
70         @Override
71         public void run()
72         {
73                 Os.debug("Task: thread run");
74
75                 // Setup notification bar
76                 this.notify("Connecting..", android.R.drawable.presence_invisible);
77
78                 // Start connecting
79                 if (!this.client.connect(server, nickname, channel)) {
80                         this.command(DISCONNECT, null);
81                         this.notify("Unable to connect", android.R.drawable.presence_offline);
82                         this.thread = null;
83                         return;
84                 }
85
86                 // Wait for login
87                 while (!this.client.ready) {
88                         Message msg = this.client.recv();
89                         if (msg == null)
90                                 break;
91                         this.command(MESSAGE, msg);
92                 }
93
94                 // Notify connection status
95                 if (this.client.ready) {
96                         this.command(CONNECT, null);
97                         this.notify("Connected", android.R.drawable.presence_online);
98                 } else {
99                         this.command(DISCONNECT, null);
100                         this.notify("Connetion aborted", android.R.drawable.presence_offline);
101                 }
102
103                 // Process messages
104                 while (this.client.ready) {
105                         Message msg = this.client.recv();
106                         if (msg == null)
107                                 break;
108                         this.command(MESSAGE, msg);
109                 }
110
111                 // Notify disconnect disconnected
112                 this.notify("Disconnected", android.R.drawable.presence_offline);
113                 this.command(DISCONNECT, null);
114
115                 // Shutdown the client
116                 this.client.abort();
117                 this.thread = null;
118
119                 Os.debug("Task: thread exit");
120         }
121
122         /* Service Methods */
123         @Override
124         public void onCreate()
125         {
126                 Os.debug("Task: onCreate");
127                 super.onCreate();
128
129                 // Create the client
130                 this.client = new Client();
131         }
132         
133         @Override
134         public void onDestroy()
135         {
136                 Os.debug("Task: onDestroy");
137                 try {
138                         this.client.abort();
139                         this.thread.join();
140                 } catch (Exception e) {
141                         Os.debug("Task: error stopping service", e);
142                 }
143         }
144         
145         @Override
146         public void onStart(Intent intent, int startId)
147         {
148                 Os.debug("Task: onStart");
149                 super.onStart(intent, startId);
150
151                 // Setup communication with Main
152                 this.messenger = (Messenger)intent.getExtras().get("Messenger");
153                 this.command(REGISTER, this);
154
155                 // Create client thread
156                 if (this.thread == null) {
157                         this.thread = new Thread(this);
158                         this.thread.start();
159                 }
160         }
161
162         @Override
163         public IBinder onBind(Intent intent)
164         {
165                 Os.debug("Task: onBind");
166                 return null;
167         }
168 }