From 444f43d4b3f1859dd940eb141dd7d0ee7f705de2 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sat, 13 Apr 2013 00:08:23 +0000 Subject: [PATCH] Add simple menu --- makefile | 2 +- res/menu/main.xml | 9 +++ src/org/pileus/spades/Client.java | 120 +++++++++++++++-------------- src/org/pileus/spades/Main.java | 33 +++++++- src/org/pileus/spades/Message.java | 2 - src/org/pileus/spades/Task.java | 55 ++++++------- 6 files changed, 124 insertions(+), 97 deletions(-) create mode 100644 res/menu/main.xml diff --git a/makefile b/makefile index 0e800dc..dfcff73 100644 --- a/makefile +++ b/makefile @@ -24,7 +24,7 @@ clean: rm -rf bin gen obj logcat: - adb logcat Spades:D '*:S' + adb logcat Spades:D E/AndroidRuntime '*:S' run: bin/install.stamp adb shell am start -W -a android.intent.action.MAIN -n $(PACKAGE)/.Main diff --git a/res/menu/main.xml b/res/menu/main.xml new file mode 100644 index 0000000..aaf037f --- /dev/null +++ b/res/menu/main.xml @@ -0,0 +1,9 @@ + + + + + + diff --git a/src/org/pileus/spades/Client.java b/src/org/pileus/spades/Client.java index 4b1b4f9..3ec0b8d 100644 --- a/src/org/pileus/spades/Client.java +++ b/src/org/pileus/spades/Client.java @@ -1,71 +1,103 @@ package org.pileus.spades; -import java.io.BufferedReader; -import java.io.PrintWriter; +import java.io.*; +import java.net.*; public class Client { /* Private data */ private String server = null; + private int port = 6667; private String nickname = null; private String channel = null; private String username = null; private String hostname = null; + private Socket socket = null; private BufferedReader input = null; private PrintWriter output = null; - /* Public data */ - public boolean running = true; - /* Public Methods */ - public Client(String server, String nickname, String channel, - String username, String hostname) + public Client(String username, String hostname) { - this.server = server; - this.nickname = nickname; - this.channel = channel; this.username = username; this.hostname = hostname; Os.debug("Client: create"); } - public Client(String server, String nickname, String channel) + public Client() { - this(server, nickname, channel, "user", "localhost"); + this("user", "localhost"); } - public void connect(BufferedReader input, PrintWriter output) + public boolean connect(String server, String nickname, String channel) { - this.input = input; - this.output = output; Os.debug("Client: connect"); - putline("USER "+username+" "+hostname+" "+server+" :"+nickname); - putline("NICK "+nickname); + + this.server = server; + this.nickname = nickname; + this.channel = channel; + + try { + socket = new Socket(this.server, this.port); + input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + output = new PrintWriter(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); + + return true; + } + + public boolean abort() + { + Os.debug("Client: abort"); + try { + this.socket.close(); + return true; + } catch (Exception e) { + Os.debug("Client: error closing socket", e); + return false; + } + } + + public void raw(String line) + { + try { + Os.debug("< " + line); + output.println(line); + output.flush(); + } catch (Exception e) { + Os.debug("Client: error writing line", e); + } } public Message send(String txt) { - Message msg = new Message(channel, nickname, txt); - putline(msg.line); + Message msg = new Message(channel, nickname, txt); + raw(msg.line); return msg; } public Message recv() { try { - String line = getline(); - if (line == null) { - this.running = false; + String line = input.readLine(); + if (line == null) return null; - } else { - Message msg = new Message(line); - process(msg); - return msg; - } + Os.debug("> " + line); + Message msg = new Message(line); + process(msg); + return msg; + } catch (SocketException e) { + return null; } catch (Exception e) { Os.debug("Client: error in recv", e); - this.running = false; return null; } } @@ -74,37 +106,11 @@ public class Client private void process(Message msg) { if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) { - putline("JOIN " + channel); - putline("TOPIC " + channel); + raw("JOIN " + channel); + raw("TOPIC " + channel); } if (msg.cmd.equals("PING")) { - putline("PING " + msg.msg); - } - } - - private String getline() - { - try { - String line = input.readLine(); - if (line != null) - Os.debug("> " + line); - return line; - } catch (Exception e) { - Os.debug("Client: error reading line", e); - this.running = false; - return ""; - } - } - - private void putline(String line) - { - try { - Os.debug("< " + line); - output.println(line); - output.flush(); - } catch (Exception e) { - Os.debug("Client: error writing line", e); - this.running = false; + raw("PING " + msg.msg); } } } diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index 10f44e5..3855eb9 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -7,6 +7,9 @@ import android.os.Handler; import android.os.Messenger; import android.text.method.ScrollingMovementMethod; import android.view.View; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; @@ -126,10 +129,6 @@ public class Main extends Activity .newTabSpec("debug") .setIndicator("Debug") .setContent(R.id.debug)); - - // Start IRC service - this.startService(); - } catch (Exception e) { Os.debug("Error setting content view", e); return; @@ -178,6 +177,32 @@ public class Main extends Activity Os.debug("Main: onDestroy"); } + @Override + public boolean onCreateOptionsMenu(Menu menu) + { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) + { + switch (item.getItemId()) { + case R.id.connect: + this.startService(); + return true; + case R.id.disconnect: + this.stopService(); + return true; + case R.id.help: + Os.debug("Main: Help!"); + return true; + default: + return false; + } + } + /* Handler class */ class MainHandler extends Handler { diff --git a/src/org/pileus/spades/Message.java b/src/org/pileus/spades/Message.java index ba1573a..73a8fab 100644 --- a/src/org/pileus/spades/Message.java +++ b/src/org/pileus/spades/Message.java @@ -75,8 +75,6 @@ public class Message this.txt = notnull(this.msg); else this.txt = notnull(mrTo.group(3)); - - this.debug(); } public void debug() diff --git a/src/org/pileus/spades/Task.java b/src/org/pileus/spades/Task.java index e7026e0..5aad096 100644 --- a/src/org/pileus/spades/Task.java +++ b/src/org/pileus/spades/Task.java @@ -1,8 +1,5 @@ package org.pileus.spades; -import java.io.*; -import java.net.*; - import android.app.Notification; import android.app.PendingIntent; import android.app.Service; @@ -20,38 +17,13 @@ public class Task extends Service implements Runnable private String server = "irc.freenode.net"; private String nickname = "andydroid"; private String channel = "#rhnoise"; - private int port = 6667; /* Private data */ private Messenger messenger = null; private Thread thread = null; - private Socket socket = null; private Client client = null; /* Private methods */ - private void setup() - { - Os.debug("Task: setup"); - try { - this.socket = new Socket(server, port); - this.client = new Client(server, nickname, channel); - Os.debug("Task: Socket and client created"); - } catch(Exception e) { - Os.debug("Task: Failed to create socket: " + e); - return; - } - - try { - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - PrintWriter output = new PrintWriter(socket.getOutputStream()); - this.client.connect(input, output); - Os.debug("Task: Client connected"); - } catch (Exception e) { - Os.debug("Task: Failed to create readers writers: " + e); - return; - } - } - private void command(int cmd, Object value) { try { @@ -67,7 +39,7 @@ public class Task extends Service implements Runnable /* Public methods */ public Message send(String txt) { - if (this.client == null && !this.client.running) + if (this.client == null) return null; return this.client.send(txt); } @@ -77,9 +49,20 @@ public class Task extends Service implements Runnable public void run() { Os.debug("Task: thread run"); - setup(); - while (client.running) - this.command(MESSAGE, client.recv()); + + if (!client.connect(server, nickname, channel)) + return; + + while (true) { + Message msg = client.recv(); + if (msg == null) + break; + this.command(MESSAGE, msg); + } + + if (!client.abort()) + return; + Os.debug("Task: thread exit"); } @@ -99,6 +82,7 @@ public class Task extends Service implements Runnable startForeground(1, note); /* Start client thread */ + client = new Client(); thread = new Thread(this); thread.start(); } @@ -107,7 +91,12 @@ public class Task extends Service implements Runnable public void onDestroy() { Os.debug("Task: onDestroy"); - super.onDestroy(); + try { + this.client.abort(); + this.thread.join(); + } catch (Exception e) { + Os.debug("Task: error stopping service", e); + } } @Override -- 2.43.2