X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Forg%2Fpileus%2Fspades%2FMain.java;h=568d617afe0f1121d3699447dfb464a205d8d142;hb=0faf3ef54ec1c06b23dc5c5a9bf0912e9e7b4395;hp=3855eb92cbc94b5a6c3a8fc91513677058c2aa04;hpb=444f43d4b3f1859dd940eb141dd7d0ee7f705de2;p=~andy%2Fspades diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index 3855eb9..568d617 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -5,27 +5,35 @@ import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Messenger; +import android.preference.PreferenceManager; +import android.text.Html; import android.text.method.ScrollingMovementMethod; -import android.view.View; +import android.text.format.DateFormat; 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; @@ -34,43 +42,164 @@ public class Main extends Activity private TextView log; private EditText input; 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 void notice(String text) { - Os.debug("Main: onRegister"); - this.task = (Task)obj; + this.log.append(Html.fromHtml("*** " + text + "
")); } - public void onMessage(Object obj) + private void display(Message msg) { - Message msg = (Message)obj; + String when = DateFormat.format("hh:mm:ss", msg.time).toString(); + String from = String.format("%s", msg.from); + String text = msg.msg; + String fmt = null; + + // Do IRC Colors - only partly works + String fg = "\">"; + text = text + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">"); + text = text + .replaceAll("\\002", "") // bold + .replaceAll("\\011", "") // italic + .replaceAll("\\025", ""); // underline + text = text + .replaceAll("\\003(\\d+)(,\\d+)?", fg) // color + .replaceAll("\\013(\\d+)(,\\d+)?", fg); // color + text = text + .replaceAll("<0?0>", "#000000") // White + .replaceAll("<0?1>", "#000000") // Black + .replaceAll("<0?2>", "#000080") // Navy Blue + .replaceAll("<0?3>", "#008000") // Green + .replaceAll("<0?4>", "#FF0000") // Red + .replaceAll("<0?5>", "#804040") // Brown + .replaceAll("<0?6>", "#8000FF") // Purple + .replaceAll("<0?7>", "#808000") // Olive + .replaceAll("<0?8>", "#FFFF00") // Yellow + .replaceAll("<0?9>", "#00FF00") // Lime Green + .replaceAll("<10>", "#008080") // Teal + .replaceAll("<11>", "#00FFFF") // Aqua Light + .replaceAll("<12>", "#0000FF") // Royal Blue + .replaceAll("<13>", "#FF00FF") // Hot Pink + .replaceAll("<14>", "#808080") // Dark Gray + .replaceAll("<15>", "#C0C0C0"); // Light Gray + + // Message formatting + switch (msg.how) { + case DIRECT: + case MENTION: + case PRIVMSG: + fmt = "(%s) %s: %s"; + break; + case SENT: + fmt = "(%s) %s: %s"; + break; + default: + fmt = "(%s) %s: %s"; + break; + } + + String html = String.format(fmt, when, from, text); + this.log.append(Html.fromHtml(html + "
")); + } + /* Private handler methods */ + private void onRegister(Task task) + { + Os.debug("Main: onRegister"); + this.task = task; + this.game.task = task; + this.running = this.task.isRunning(); + 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 startService() + private void onNotify(String text) { - Os.debug("Main: startService"); + Os.debug("Main: onNotify - " + text); + this.notice(text); + this.toast.setText(text); + this.toast.show(); + } + + /* Private service methods */ + private void register() + { + 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: connect"); + startService(new Intent(this, Task.class) + .putExtra("Command", Task.CONNECT)); + this.running = true; + } + + private void disconnect() + { + Os.debug("Main: disconnect"); + startService(new Intent(this, Task.class) + .putExtra("Command", Task.DISCONNECT)); + } + + private void quit() { - Os.debug("Main: stopService"); 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 */ @@ -83,7 +212,6 @@ public class Main extends Activity if (msg == null) return; this.input.setText(""); - this.log.append(msg.from + ": " + msg.msg + "\n"); } /* Activity Methods */ @@ -94,9 +222,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); @@ -108,7 +242,7 @@ public class Main extends Activity 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.spades = (LinearLayout) findViewById(R.id.spades); this.debug = (TextView) findViewById(R.id.debug); this.lscroll = (ScrollView) findViewById(R.id.log_scroll); @@ -129,6 +263,21 @@ public class Main extends Activity .newTabSpec("debug") .setIndicator("Debug") .setContent(R.id.debug)); + + // 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); return; @@ -185,18 +334,29 @@ public class Main extends Activity 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.startService(); + this.connect(); return true; case R.id.disconnect: - this.stopService(); + this.disconnect(); return true; - case R.id.help: - Os.debug("Main: Help!"); + case R.id.settings: + this.startActivity(new Intent(this, Prefs.class)); + return true; + case R.id.quit: + this.quit(); return true; default: return false; @@ -210,10 +370,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.running = true; + break; + case Task.DISCONNECT: + Main.this.running = false; + break; + case Task.NOTIFY: + Main.this.onNotify((String)msg.obj); break; default: Os.debug("Main: unknown message - " + msg.what); @@ -222,4 +391,3 @@ public class Main extends Activity } } } -