]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Task.java
Add simple menu
[~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.Intent;
7 import android.os.IBinder;
8 import android.os.Messenger;
9
10 public class Task extends Service implements Runnable
11 {
12         /* Commands */
13         public static final int REGISTER = 0;
14         public static final int MESSAGE  = 1;
15
16         /* Configuration */
17         private String    server    = "irc.freenode.net";
18         private String    nickname  = "andydroid";
19         private String    channel   = "#rhnoise";
20
21         /* Private data */
22         private Messenger messenger = null;
23         private Thread    thread    = null;
24         private Client    client    = null;
25
26         /* Private methods */
27         private void command(int cmd, Object value)
28         {
29                 try {
30                         android.os.Message msg = android.os.Message.obtain();
31                         msg.what = cmd;
32                         msg.obj  = value;
33                         this.messenger.send(msg);
34                 } catch (Exception e) {
35                         Os.debug("Task: error sending message");
36                 }
37         }
38
39         /* Public methods */
40         public Message send(String txt)
41         {
42                 if (this.client == null)
43                         return null;
44                 return this.client.send(txt);
45         }
46
47         /* Runnable methods */
48         @Override
49         public void run()
50         {
51                 Os.debug("Task: thread run");
52
53                 if (!client.connect(server, nickname, channel))
54                         return;
55
56                 while (true) {
57                         Message msg = client.recv();
58                         if (msg == null)
59                                 break;
60                         this.command(MESSAGE, msg);
61                 }
62
63                 if (!client.abort())
64                         return;
65
66                 Os.debug("Task: thread exit");
67         }
68
69         /* Service Methods */
70         @Override
71         public void onCreate()
72         {
73                 Os.debug("Task: onCreate");
74                 super.onCreate();
75
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);
80
81                 note.setLatestEventInfo(this, "Spades Title", "Spades Message", pend);
82                 startForeground(1, note);
83
84                 /* Start client thread */
85                 client = new Client();
86                 thread = new Thread(this);
87                 thread.start();
88         }
89         
90         @Override
91         public void onDestroy()
92         {
93                 Os.debug("Task: onDestroy");
94                 try {
95                         this.client.abort();
96                         this.thread.join();
97                 } catch (Exception e) {
98                         Os.debug("Task: error stopping service", e);
99                 }
100         }
101         
102         @Override
103         public void onStart(Intent intent, int startId)
104         {
105                 Os.debug("Task: onStart");
106                 super.onStart(intent, startId);
107                 this.messenger = (Messenger)intent.getExtras().get("Messenger");
108                 this.command(REGISTER, this);
109         }
110
111         @Override
112         public IBinder onBind(Intent intent)
113         {
114                 Os.debug("Task: onBind");
115                 return null;
116         }
117 }