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