]> Pileus Git - ~andy/spades/blobdiff - src/org/pileus/spades/Main.java
Add IRC Service and update UI
[~andy/spades] / src / org / pileus / spades / Main.java
index 5424e5d559dcc0a512257b6ba3b923ce6a48dabe..10f44e50282891a0e639afda3f92199fc726dc51 100644 (file)
 package org.pileus.spades;
 
-import java.io.*;
-import java.net.*;
-
 import android.app.Activity;
+import android.content.Intent;
 import android.os.Bundle;
-import android.util.Log;
+import android.os.Handler;
+import android.os.Messenger;
+import android.text.method.ScrollingMovementMethod;
+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;
 
 public class Main extends Activity
 {
-       /* Configuration */
-       private String server   = "irc.freenode.net";
-       private String nickname = "andydroid";
-       private String channel  = "#rhnoise";
-       private int    port     = 6667;
+       /* Static data */
+       private Handler      handler; 
+       private Messenger    messenger; 
 
        /* Private data */
-       private Socket socket = null;
-       private Client client = null;
+       private Task         task;
 
-       /* Public Methods */
-       @Override
-       public void onCreate(Bundle savedInstanceState)
+       /* Widgets */
+       private TabHost      window;
+       private TabWidget    tabs;
+       private LinearLayout chat;
+       private TextView     log;
+       private EditText     input;
+       private Button       send;
+       private TextView     spades;
+       private TextView     debug;
+
+       private ScrollView   lscroll;
+       private ScrollView   dscroll;
+
+       /* Private methods */
+       public void onRegister(Object obj)
        {
-               super.onCreate(savedInstanceState);
-               setContentView(R.layout.main);
+               Os.debug("Main: onRegister");
+               this.task = (Task)obj;
+       }
 
-               try {
-                       this.socket = new Socket(server, port);
-                       this.client = new Client(server, nickname, channel);
-                       Log.d("Spades", "Socket and client created");
-               } catch(Exception e) {
-                       Log.d("Spades", "Failed to create socket: " + e);
-                       return;
+       public void onMessage(Object obj)
+       {
+               Message msg = (Message)obj;
+
+               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());
                }
+       }
 
+       private void startService()
+       {
+               Os.debug("Main: startService");
+               startService(new Intent(this, Task.class)
+                               .putExtra("Messenger", this.messenger));
+       }
+
+       private void stopService()
+       {
+               Os.debug("Main: stopService");
+               stopService(new Intent(this, Task.class));
+       }
+
+       /* Widget callback functions */
+       public void onSend(View btn)
+       {
+               if (this.task == null)
+                       return;
+               String  txt = this.input.getText().toString();
+               Message msg = this.task.send(txt);
+               if (msg == null)
+                       return;
+               this.input.setText("");
+               this.log.append(msg.from + ": " + msg.msg + "\n");
+       }
+
+       /* Activity Methods */
+       @Override
+       public void onCreate(Bundle savedInstanceState)
+       {
                try {
-                       BufferedReader input  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
-                       PrintWriter    output = new PrintWriter(socket.getOutputStream());
-                       this.client.connect(input, output);
-                       Log.d("Spades", "Client connected");
+                       super.onCreate(savedInstanceState);
+                       Os.debug("Main: onCreate");
+
+                       // Setup main layout
+                       this.setContentView(R.layout.main);
+
+                       // Setup communication
+                       this.handler   = new MainHandler();
+                       this.messenger = new Messenger(this.handler);
+
+                       // Find widgets
+                       this.window    = (TabHost)      findViewById(android.R.id.tabhost);
+                       this.tabs      = (TabWidget)    findViewById(android.R.id.tabs);
+                       this.chat      = (LinearLayout) findViewById(R.id.chat);
+                       this.log       = (TextView)     findViewById(R.id.log);
+                       this.input     = (EditText)     findViewById(R.id.input);
+                       this.send      = (Button)       findViewById(R.id.send);
+                       this.spades    = (TextView)     findViewById(R.id.spades);
+                       this.debug     = (TextView)     findViewById(R.id.debug);
+
+                       this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
+                       this.dscroll   = (ScrollView)   findViewById(R.id.debug_scroll);
+
+                       // Add window tabs
+                       this.window.setup();
+
+                       this.window.addTab(this.window
+                                       .newTabSpec("chat")
+                                       .setIndicator("Chat")
+                                       .setContent(R.id.chat));
+                       this.window.addTab(this.window
+                                       .newTabSpec("spades")
+                                       .setIndicator("Spades")
+                                       .setContent(R.id.spades));
+                       this.window.addTab(this.window
+                                       .newTabSpec("debug")
+                                       .setIndicator("Debug")
+                                       .setContent(R.id.debug));
+
+                       // Start IRC service
+                       this.startService();
+
                } catch (Exception e) {
-                       Log.d("Spades", "Failed to create readers writers: " + e);
+                       Os.debug("Error setting content view", e);
                        return;
                }
+       }
+
+       @Override
+       public void onStart()
+       {
+               super.onStart();
+               Os.debug("Main: onStart");
+       }
+
+       @Override
+       public void onResume()
+       {
+               super.onResume();
+               Os.debug("Main: onResume");
+       }
+
+       @Override
+       public void onPause()
+       {
+               super.onPause();
+               Os.debug("Main: onPause");
+       }
+
+       @Override
+       public void onStop()
+       {
+               super.onStop();
+               Os.debug("Main: onStop");
+       }
 
-               TextView text = (TextView)findViewById(R.id.textview);
-               while (client.running) {
-                       Message msg = client.recv();
-                       if (msg == null)
-                               continue;
+       @Override
+       public void onRestart()
+       {
+               super.onRestart();
+               Os.debug("Main: onRestart");
+       }
+
+       @Override
+       public void onDestroy()
+       {
+               super.onDestroy();
+               Os.debug("Main: onDestroy");
+       }
+
+       /* Handler class */
+       class MainHandler extends Handler
+       {
+               public void handleMessage(android.os.Message msg)
+               {
+                       switch (msg.what) {
+                               case Task.REGISTER:
+                                       Main.this.onRegister(msg.obj);
+                                       break;
+                               case Task.MESSAGE:
+                                       Main.this.onMessage(msg.obj);
+                                       break;
+                               default:
+                                       Os.debug("Main: unknown message - " + msg.what);
+                                       break;
+                       }
                }
        }
 }
+