X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Forg%2Fpileus%2Fspades%2FMain.java;h=568d617afe0f1121d3699447dfb464a205d8d142;hb=0faf3ef54ec1c06b23dc5c5a9bf0912e9e7b4395;hp=3c3941499f9f0a22e9e1c453f63b9e788d0ab9c7;hpb=3da596612cb845a8a6bd6015b0345fa695923d3e;p=~andy%2Fspades diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index 3c39414..568d617 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -5,6 +5,7 @@ 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.text.format.DateFormat; @@ -28,9 +29,11 @@ public class Main extends Activity private Messenger messenger; private Task task; private Toast toast; - private boolean ready; + private boolean running; private String topic; private String names; + private Cards cards; + private Spades game; /* Widgets */ private TabHost window; @@ -39,7 +42,7 @@ 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; @@ -109,16 +112,22 @@ public class Main extends Activity } /* Private handler methods */ - private void onRegister(Object obj) + private void onRegister(Task task) { Os.debug("Main: onRegister"); - this.task = (Task)obj; + 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(Object obj) + private void onMessage(Message msg) { - Message msg = (Message)obj; - // Debug this.debug.append("> " + msg.line + "\n"); this.dscroll.smoothScrollTo(0, this.debug.getBottom()); @@ -127,6 +136,7 @@ public class Main extends Activity switch (msg.type) { case PRIVMSG: this.display(msg); + this.game.onMessage(msg); break; case TOPIC: if (!msg.txt.equals(this.topic)) @@ -160,17 +170,36 @@ public class Main extends Activity } /* Private service methods */ - private void startService() + 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: 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 */ @@ -193,6 +222,9 @@ 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); @@ -210,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); @@ -231,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; @@ -290,8 +337,8 @@ public class Main extends Activity @Override public boolean onPrepareOptionsMenu(Menu menu) { - menu.findItem(R.id.connect).setVisible(!this.ready); - menu.findItem(R.id.disconnect).setVisible(this.ready); + menu.findItem(R.id.connect).setVisible(!this.running); + menu.findItem(R.id.disconnect).setVisible(this.running); return true; } @@ -300,17 +347,16 @@ public class Main extends Activity { 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.settings: this.startActivity(new Intent(this, Prefs.class)); return true; - case R.id.exit: - this.stopService(); - this.finish(); + case R.id.quit: + this.quit(); return true; default: return false; @@ -324,16 +370,16 @@ 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.ready = true; + Main.this.running = true; break; case Task.DISCONNECT: - Main.this.ready = false; + Main.this.running = false; break; case Task.NOTIFY: Main.this.onNotify((String)msg.obj);