]> Pileus Git - ~andy/spades/blobdiff - src/org/pileus/spades/Main.java
Add connect button
[~andy/spades] / src / org / pileus / spades / Main.java
index 10f44e50282891a0e639afda3f92199fc726dc51..231fee0e16994447bbf9a21258c3e6bdf2850ac5 100644 (file)
@@ -2,27 +2,45 @@ package org.pileus.spades;
 
 import android.app.Activity;
 import android.content.Intent;
+import android.graphics.Color;
+import android.graphics.Typeface;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Messenger;
-import android.text.method.ScrollingMovementMethod;
+import android.preference.PreferenceManager;
+import android.text.Spannable;
+import android.text.SpannableString;
+import android.text.format.DateFormat;
+import android.text.style.BackgroundColorSpan;
+import android.text.style.ForegroundColorSpan;
+import android.text.style.StrikethroughSpan;
+import android.text.style.StyleSpan;
+import android.text.style.UnderlineSpan;
+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;
 
 public class Main extends Activity
 {
-       /* Static data */
-       private Handler      handler; 
-       private Messenger    messenger; 
-
        /* Private data */
+       private Handler      handler;
+       private Messenger    messenger;
        private Task         task;
+       private Toast        toast;
+       private boolean      running;
+       private String       topic;
+       private String       names;
+       private Cards        cards;
+       private Spades       game;
 
        /* Widgets */
        private TabHost      window;
@@ -30,47 +48,205 @@ public class Main extends Activity
        private LinearLayout chat;
        private TextView     log;
        private EditText     input;
+       private Button       connect;
        private Button       send;
-       private TextView     spades;
+       private LinearLayout spades;
        private TextView     debug;
 
        private ScrollView   lscroll;
        private ScrollView   dscroll;
 
-       /* Private methods */
-       public void onRegister(Object obj)
+       /* Private helper methods */
+       private int hsv2rgb(int hsv)
        {
-               Os.debug("Main: onRegister");
-               this.task = (Task)obj;
+               int h  = (hsv & 0xff0000) >> 16;
+               int s  = (hsv & 0x00ff00) >>  8;
+               int v  = (hsv & 0x0000ff) >>  0;
+
+               int c  = (v * s) / 256;
+               int h1 = (h * 6) / 256;
+               int x  = c * (1 - Math.abs((h1%2)-1));
+               int m  = v - c;
+
+               int rgb = 0;
+
+               if (0 <= h1 && h1 <= 1) rgb = (c << 16) | (x << 8) | 0;
+               if (1 <= h1 && h1 <= 2) rgb = (x << 16) | (c << 8) | 0;
+               if (2 <= h1 && h1 <= 3) rgb = (0 << 16) | (c << 8) | x;
+               if (3 <= h1 && h1 <= 4) rgb = (0 << 16) | (x << 8) | c;
+               if (4 <= h1 && h1 <= 5) rgb = (x << 16) | (0 << 8) | c;
+               if (5 <= h1 && h1 <= 6) rgb = (c << 16) | (0 << 8) | x;
+
+               return rgb + (m << 16) + (m << 8) + m;
+       }
+
+       private void notice(String text)
+       {
+               String    msg  = "*** " + text + "\n";
+               Spannable span = new SpannableString(msg);
+               span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0);
+               this.log.append(span);
+       }
+
+       private void display(Message msg)
+       {
+               String date = DateFormat.format("hh:mm:ss", msg.time).toString();
+               String text = String.format("(%s) %s: %s\n", date, msg.from, msg.msg);
+               Spannable span = new SpannableString(text);
+
+               // Determin positions
+               int de  = 1 + date.length() + 1;
+               int ne  = de + 1 + msg.from.length() + 1;
+               int pos = ne + 1;
+
+               // Get user color
+               int hash  = msg.from.hashCode();
+               int color = this.hsv2rgb(hash | 0x8080) | 0xff000000;
+
+               // Format date and name
+               span.setSpan(new ForegroundColorSpan(0xffffff88), 0,    de, 0);
+               span.setSpan(new ForegroundColorSpan(color),      de+1, ne, 0);
+
+               // Format IRC Colors
+               for (Message.Format fmt : msg.parts) {
+                       int len = fmt.txt.length();
+
+                       // Bold/italics
+                       if (fmt.bold && fmt.italic)
+                               span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), pos, pos+len, 0);
+                       else if (fmt.bold)
+                               span.setSpan(new StyleSpan(Typeface.BOLD), pos, pos+len, 0);
+                       else if (fmt.italic)
+                               span.setSpan(new StyleSpan(Typeface.ITALIC), pos, pos+len, 0);
+
+                       // Striketrough / underline
+                       if (fmt.strike)
+                               span.setSpan(new StrikethroughSpan(), pos, pos+len, 0);
+                       if (fmt.underline)
+                               span.setSpan(new UnderlineSpan(), pos, pos+len, 0);
+
+                       // Colors (reverse not supported)
+                       if (fmt.fg!=null)
+                               span.setSpan(new ForegroundColorSpan(fmt.fg.color), pos, pos+len, 0);
+                       if (fmt.bg!=null)
+                               span.setSpan(new BackgroundColorSpan(fmt.bg.color), pos, pos+len, 0);
+
+                       pos += len;
+               }
+
+               // Append the message
+               this.log.append(span);
        }
 
-       public void onMessage(Object obj)
+       private void update(boolean running)
        {
-               Message msg = (Message)obj;
+               this.running = running;
+               this.connect.setVisibility(running ? View.GONE : View.VISIBLE);
+               this.send.setVisibility(running ? View.VISIBLE : View.GONE);
+       }
 
+       /* Private handler methods */
+       private void onRegister(Task task)
+       {
+               Os.debug("Main: onRegister");
+               this.task      = task;
+               this.game.task = task;
+               this.update(this.task.isRunning());
+               this.log.setText("");
+               this.debug.setText("");
+               for (Object obj : this.task.getLog()) {
+                       if (String.class.isInstance(obj))
+                               this.notice((String)obj);
+                       if (Message.class.isInstance(obj))
+                               this.onMessage((Message)obj);
+               }
+       }
+
+       private void onMessage(Message msg)
+       {
+               // 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.display(msg);
+                               this.game.onMessage(msg);
+                               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 void startService()
+       /* Private service methods */
+       private void register()
        {
-               Os.debug("Main: startService");
+               Os.debug("Main: register");
                startService(new Intent(this, Task.class)
+                               .putExtra("Command",   Task.REGISTER)
                                .putExtra("Messenger", this.messenger));
        }
 
-       private void stopService()
+       private void connect()
        {
-               Os.debug("Main: stopService");
+               Os.debug("Main: connect");
+               startService(new Intent(this, Task.class)
+                               .putExtra("Command", Task.CONNECT));
+               this.update(true);
+       }
+
+       private void disconnect()
+       {
+               Os.debug("Main: disconnect");
+               startService(new Intent(this, Task.class)
+                               .putExtra("Command", Task.DISCONNECT));
+               this.update(false);
+       }
+
+       private void quit()
+       {
+               this.log.setText("");
+               this.debug.setText("");
                stopService(new Intent(this, Task.class));
+               Intent intent = new Intent(Intent.ACTION_MAIN);
+               intent.addCategory(Intent.CATEGORY_HOME);
+               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+               startActivity(intent);
        }
 
        /* Widget callback functions */
+       public void onConnect(View btn)
+       {
+               this.connect();
+       }
+
        public void onSend(View btn)
        {
                if (this.task == null)
@@ -80,7 +256,6 @@ public class Main extends Activity
                if (msg == null)
                        return;
                this.input.setText("");
-               this.log.append(msg.from + ": " + msg.msg + "\n");
        }
 
        /* Activity Methods */
@@ -91,9 +266,15 @@ public class Main extends Activity
                        super.onCreate(savedInstanceState);
                        Os.debug("Main: onCreate");
 
+                       // Setup preferences
+                       PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
+
                        // 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);
@@ -104,8 +285,9 @@ public class Main extends Activity
                        this.chat      = (LinearLayout) findViewById(R.id.chat);
                        this.log       = (TextView)     findViewById(R.id.log);
                        this.input     = (EditText)     findViewById(R.id.input);
+                       this.connect   = (Button)       findViewById(R.id.connect);
                        this.send      = (Button)       findViewById(R.id.send);
-                       this.spades    = (TextView)     findViewById(R.id.spades);
+                       this.spades    = (LinearLayout) findViewById(R.id.spades);
                        this.debug     = (TextView)     findViewById(R.id.debug);
 
                        this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
@@ -127,8 +309,19 @@ public class Main extends Activity
                                        .setIndicator("Debug")
                                        .setContent(R.id.debug));
 
-                       // Start IRC service
-                       this.startService();
+                       // Setup Spades game and cards view
+                       this.game  = new Spades(PreferenceManager
+                                       .getDefaultSharedPreferences(this)
+                                       .getString("pref_referee", "rhawk"));
+                       this.cards = new Cards(this);
+
+                       this.game.cards = this.cards;
+                       this.cards.game = this.game;
+
+                       this.spades.addView(cards);
+
+                       // Attach to background service
+                       this.register();
 
                } catch (Exception e) {
                        Os.debug("Error setting content view", e);
@@ -140,6 +333,7 @@ public class Main extends Activity
        public void onStart()
        {
                super.onStart();
+               this.register();
                Os.debug("Main: onStart");
        }
 
@@ -178,6 +372,43 @@ 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 onPrepareOptionsMenu(Menu menu)
+       {
+               menu.findItem(R.id.connect).setVisible(!this.running);
+               menu.findItem(R.id.disconnect).setVisible(this.running);
+               return true;
+       }
+
+       @Override
+       public boolean onOptionsItemSelected(MenuItem item)
+       {
+               switch (item.getItemId()) {
+                       case R.id.connect:
+                               this.connect();
+                               return true;
+                       case R.id.disconnect:
+                               this.disconnect();
+                               return true;
+                       case R.id.settings:
+                               this.startActivity(new Intent(this, Prefs.class));
+                               return true;
+                       case R.id.quit:
+                               this.quit();
+                               return true;
+                       default:
+                               return false;
+               }
+       }
+
        /* Handler class */
        class MainHandler extends Handler
        {
@@ -185,10 +416,19 @@ public class Main extends Activity
                {
                        switch (msg.what) {
                                case Task.REGISTER:
-                                       Main.this.onRegister(msg.obj);
+                                       Main.this.onRegister((Task)msg.obj);
                                        break;
                                case Task.MESSAGE:
-                                       Main.this.onMessage(msg.obj);
+                                       Main.this.onMessage((Message)msg.obj);
+                                       break;
+                               case Task.CONNECT:
+                                       Main.this.update(true);
+                                       break;
+                               case Task.DISCONNECT:
+                                       Main.this.update(false);
+                                       break;
+                               case Task.NOTIFY:
+                                       Main.this.onNotify((String)msg.obj);
                                        break;
                                default:
                                        Os.debug("Main: unknown message - " + msg.what);
@@ -197,4 +437,3 @@ public class Main extends Activity
                }
        }
 }
-