]> Pileus Git - ~andy/spades/blobdiff - src/org/pileus/spades/Main.java
Add support for SASL PLAIN authentication
[~andy/spades] / src / org / pileus / spades / Main.java
index cf0baed4caa470a64dfc3be81ad851bc2ee26d6b..f5118c13f1793f860428616dec2d59cc49390f14 100644 (file)
@@ -6,27 +6,33 @@ import android.os.Bundle;
 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.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.LinearLayout;
-import android.widget.TextView;
 import android.widget.ScrollView;
 import android.widget.TabHost;
 import android.widget.TabWidget;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import android.preference.PreferenceActivity;
 
 public class Main extends Activity
 {
        /* Static data */
-       private Handler      handler; 
-       private Messenger    messenger; 
+       private Handler      handler;
+       private Messenger    messenger;
 
        /* Private data */
        private Task         task;
+       private Toast        toast;
        private boolean      ready;
+       private String       topic;
+       private String       names;
 
        /* Widgets */
        private TabHost      window;
@@ -41,26 +47,64 @@ public class Main extends Activity
        private ScrollView   lscroll;
        private ScrollView   dscroll;
 
-       /* Private methods */
-       public void onRegister(Object obj)
+       /* Private helper methods */
+       private void notice(String text)
+       {
+               this.log.append("*** " + text + "\n");
+       }
+
+       /* Private handler methods */
+       private void onRegister(Object obj)
        {
                Os.debug("Main: onRegister");
                this.task = (Task)obj;
        }
 
-       public void onMessage(Object obj)
+       private void onMessage(Object obj)
        {
                Message msg = (Message)obj;
 
+               // Debug
                this.debug.append("> " + msg.line + "\n");
                this.dscroll.smoothScrollTo(0, this.debug.getBottom());
 
-               if (msg.cmd.equals("PRIVMSG")) {
-                       this.log.append(msg.from + ": " + msg.msg + "\n");
-                       this.lscroll.smoothScrollTo(0, this.log.getBottom());
+               // Chat
+               switch (msg.type) {
+                       case PRIVMSG:
+                               this.log.append(msg.from + ": " + msg.msg + "\n");
+                               break;
+                       case TOPIC:
+                               if (!msg.txt.equals(this.topic))
+                                       this.notice("Topic for " + msg.arg + ": " + msg.txt);
+                               this.topic = msg.txt;
+                               break;
+                       case NAMES:
+                               if (!msg.txt.equals(this.names))
+                                       this.notice("Users in " + msg.arg + ": " + msg.txt);
+                               this.names = msg.txt;
+                               break;
+                       case ERROR:
+                               this.notice("Error: " + msg.txt);
+                               break;
+                       case AUTHOK:
+                               this.notice("Authentication succeeded: " + msg.txt);
+                               break;
+                       case AUTHFAIL:
+                               this.notice("Authentication failed: " + msg.txt);
+                               break;
                }
+               this.lscroll.smoothScrollTo(0, this.log.getBottom());
        }
 
+       private void onNotify(String text)
+       {
+               Os.debug("Main: onNotify - " + text);
+               this.notice(text);
+               this.toast.setText(text);
+               this.toast.show();
+       }
+
+       /* Private service methods */
        private void startService()
        {
                Os.debug("Main: startService");
@@ -97,6 +141,9 @@ public class Main extends Activity
                        // Setup main layout
                        this.setContentView(R.layout.main);
 
+                       // Setup toast
+                       this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
+
                        // Setup communication
                        this.handler   = new MainHandler();
                        this.messenger = new Messenger(this.handler);
@@ -203,6 +250,9 @@ public class Main extends Activity
                        case R.id.disconnect:
                                this.stopService();
                                return true;
+                       case R.id.settings:
+                               this.startActivity(new Intent(this, Prefs.class));
+                               return true;
                        case R.id.exit:
                                this.stopService();
                                this.finish();
@@ -230,6 +280,9 @@ public class Main extends Activity
                                case Task.DISCONNECT:
                                        Main.this.ready = false;
                                        break;
+                               case Task.NOTIFY:
+                                       Main.this.onNotify((String)msg.obj);
+                                       break;
                                default:
                                        Os.debug("Main: unknown message - " + msg.what);
                                        break;
@@ -237,4 +290,3 @@ public class Main extends Activity
                }
        }
 }
-