private BufferedReader input = null;
private PrintWriter output = null;
+ /* Public data */
+ public boolean ready = false;
+
/* Public Methods */
public Client(String username, String hostname)
{
this.channel = channel;
try {
- socket = new Socket(this.server, this.port);
- input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- output = new PrintWriter(socket.getOutputStream());
+ this.socket = new Socket(this.server, this.port);
+ this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
+ this.output = new PrintWriter(this.socket.getOutputStream());
} catch (Exception e) {
Os.debug("Client: failed to create connection: " + e);
return false;
}
Os.debug("Client: connected");
- raw("USER "+username+" "+hostname+" "+server+" :"+nickname);
- raw("NICK "+nickname);
+ this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.nickname);
+ this.raw("NICK "+this.nickname);
return true;
}
Os.debug("Client: abort");
try {
this.socket.close();
+ this.ready = false;
return true;
} catch (Exception e) {
Os.debug("Client: error closing socket", e);
{
try {
Os.debug("< " + line);
- output.println(line);
- output.flush();
+ this.output.println(line);
+ this.output.flush();
} catch (Exception e) {
Os.debug("Client: error writing line", e);
}
public Message send(String txt)
{
- Message msg = new Message(channel, nickname, txt);
- raw(msg.line);
+ Message msg = new Message(this.channel, this.nickname, txt);
+ this.raw(msg.line);
return msg;
}
return null;
Os.debug("> " + line);
Message msg = new Message(line);
- process(msg);
+ this.process(msg);
return msg;
} catch (SocketException e) {
+ this.ready = false;
return null;
} catch (Exception e) {
+ this.ready = false;
Os.debug("Client: error in recv", e);
return null;
}
private void process(Message msg)
{
if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
- raw("JOIN " + channel);
- raw("TOPIC " + channel);
+ this.raw("JOIN " + this.channel);
+ this.raw("TOPIC " + this.channel);
+ this.ready = true;
}
if (msg.cmd.equals("PING")) {
- raw("PING " + msg.msg);
+ this.raw("PING " + msg.msg);
}
}
}
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
+import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
+import android.os.Looper;
import android.os.Messenger;
+import android.widget.Toast;
public class Task extends Service implements Runnable
{
/* Commands */
- public static final int REGISTER = 0;
- public static final int MESSAGE = 1;
+ public static final int REGISTER = 0;
+ public static final int MESSAGE = 1;
+ public static final int CONNECT = 2;
+ public static final int DISCONNECT = 3;
/* Configuration */
private String server = "irc.freenode.net";
private Messenger messenger = null;
private Thread thread = null;
private Client client = null;
+ private Toast toast = null;
/* Private methods */
private void command(int cmd, Object value)
}
}
+ private void notify(String text, int icon)
+ {
+ // Log
+ Os.debug("Task: notify - " + text);
+
+ // Toast
+ this.toast.setText(text);
+ this.toast.show();
+
+ // Notify
+ Notification note = new Notification(icon, null, 0);
+ Intent intent = new Intent(this, Main.class);
+ PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
+
+ note.setLatestEventInfo(this, "Spades!", text, pend);
+ this.startForeground(1, note);
+ }
+
/* Public methods */
public Message send(String txt)
{
if (this.client == null)
return null;
- return this.client.send(txt);
+ Message msg = this.client.send(txt);
+ if (msg != null)
+ this.command(MESSAGE, msg);
+ return msg;
}
/* Runnable methods */
{
Os.debug("Task: thread run");
- if (!client.connect(server, nickname, channel))
+ // Android Toast setup
+ Looper.prepare();
+
+ // Setup notification bar
+ this.notify("Connecting..", android.R.drawable.presence_invisible);
+
+ // Start connecting
+ if (!this.client.connect(server, nickname, channel)) {
+ this.command(DISCONNECT, null);
+ this.notify("Unable to connect", android.R.drawable.presence_offline);
+ this.thread = null;
return;
+ }
- while (true) {
- Message msg = client.recv();
+ // Wait for login
+ while (!this.client.ready) {
+ Message msg = this.client.recv();
if (msg == null)
break;
this.command(MESSAGE, msg);
}
- if (!client.abort())
- return;
+ // Notify connection status
+ if (this.client.ready) {
+ this.command(CONNECT, null);
+ this.notify("Connected", android.R.drawable.presence_online);
+ } else {
+ this.command(DISCONNECT, null);
+ this.notify("Connetion aborted", android.R.drawable.presence_offline);
+ }
+
+ // Process messages
+ while (this.client.ready) {
+ Message msg = this.client.recv();
+ if (msg == null)
+ break;
+ this.command(MESSAGE, msg);
+ }
+
+ // Notify disconnect disconnected
+ this.notify("Disconnected", android.R.drawable.presence_offline);
+ this.command(DISCONNECT, null);
+
+ // Shutdown the client
+ this.client.abort();
+ this.thread = null;
Os.debug("Task: thread exit");
}
Os.debug("Task: onCreate");
super.onCreate();
- /* Setup notification bar */
- Notification note = new Notification(android.R.drawable.presence_online, null, 0);
- Intent intent = new Intent(this, Main.class);
- PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
-
- note.setLatestEventInfo(this, "Spades Title", "Spades Message", pend);
- startForeground(1, note);
+ // Setup toast
+ Context context = this.getApplicationContext();
+ this.toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
- /* Start client thread */
- client = new Client();
- thread = new Thread(this);
- thread.start();
+ // Create the client
+ this.client = new Client();
}
@Override
{
Os.debug("Task: onStart");
super.onStart(intent, startId);
+
+ // Setup communication with Main
this.messenger = (Messenger)intent.getExtras().get("Messenger");
this.command(REGISTER, this);
+
+ // Create client thread
+ if (this.thread == null) {
+ this.thread = new Thread(this);
+ this.thread.start();
+ }
}
@Override