]> Pileus Git - ~andy/spades/commitdiff
Add simple menu
authorAndy Spencer <andy753421@gmail.com>
Sat, 13 Apr 2013 00:08:23 +0000 (00:08 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 13 Apr 2013 00:08:23 +0000 (00:08 +0000)
makefile
res/menu/main.xml [new file with mode: 0644]
src/org/pileus/spades/Client.java
src/org/pileus/spades/Main.java
src/org/pileus/spades/Message.java
src/org/pileus/spades/Task.java

index 0e800dcca1c57b1ccab160ab387ade52e8e9fb28..dfcff733052689c5eb68f351ccc8ddbccbd47de7 100644 (file)
--- 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 (file)
index 0000000..aaf037f
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+       <item android:id="@+id/connect"
+               android:title="Connect" />
+       <item android:id="@+id/disconnect"
+               android:title="Disconnect" />
+       <item android:id="@+id/help"
+               android:title="Help" />
+</menu>
index 4b1b4f9f09a939512fd76064fca9fba0dc2cdf87..3ec0b8d516d8c6e155747c480babc994ccd2d31f 100644 (file)
 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);
                }
        }
 }
index 10f44e50282891a0e639afda3f92199fc726dc51..3855eb92cbc94b5a6c3a8fc91513677058c2aa04 100644 (file)
@@ -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
        {
index ba1573a4ad2bcbf9726136e90a5e4034e2e15648..73a8fabdba04ef3a82206c91cdf8296dd6b009e0 100644 (file)
@@ -75,8 +75,6 @@ public class Message
                        this.txt  = notnull(this.msg);
                else
                        this.txt  = notnull(mrTo.group(3));
-
-               this.debug();
        }
 
        public void debug()
index e7026e079718be8de9dd448c744f18da27e58c49..5aad096554237ec13cc761ccf385cdb2ec994b08 100644 (file)
@@ -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