1 package org.pileus.spades;
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.os.Handler;
7 import android.os.Messenger;
8 import android.text.method.ScrollingMovementMethod;
9 import android.view.View;
10 import android.view.Menu;
11 import android.view.MenuInflater;
12 import android.view.MenuItem;
13 import android.widget.Button;
14 import android.widget.EditText;
15 import android.widget.LinearLayout;
16 import android.widget.TextView;
17 import android.widget.ScrollView;
18 import android.widget.TabHost;
19 import android.widget.TabWidget;
21 public class Main extends Activity
24 private Handler handler;
25 private Messenger messenger;
29 private boolean ready;
32 private TabHost window;
33 private TabWidget tabs;
34 private LinearLayout chat;
36 private EditText input;
38 private TextView spades;
39 private TextView debug;
41 private ScrollView lscroll;
42 private ScrollView dscroll;
45 public void onRegister(Object obj)
47 Os.debug("Main: onRegister");
48 this.task = (Task)obj;
51 public void onMessage(Object obj)
53 Message msg = (Message)obj;
55 this.debug.append("> " + msg.line + "\n");
56 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
58 if (msg.cmd.equals("PRIVMSG")) {
59 this.log.append(msg.from + ": " + msg.msg + "\n");
60 this.lscroll.smoothScrollTo(0, this.log.getBottom());
64 private void startService()
66 Os.debug("Main: startService");
67 startService(new Intent(this, Task.class)
68 .putExtra("Messenger", this.messenger));
71 private void stopService()
73 Os.debug("Main: stopService");
74 stopService(new Intent(this, Task.class));
77 /* Widget callback functions */
78 public void onSend(View btn)
80 if (this.task == null)
82 String txt = this.input.getText().toString();
83 Message msg = this.task.send(txt);
86 this.input.setText("");
89 /* Activity Methods */
91 public void onCreate(Bundle savedInstanceState)
94 super.onCreate(savedInstanceState);
95 Os.debug("Main: onCreate");
98 this.setContentView(R.layout.main);
100 // Setup communication
101 this.handler = new MainHandler();
102 this.messenger = new Messenger(this.handler);
105 this.window = (TabHost) findViewById(android.R.id.tabhost);
106 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
107 this.chat = (LinearLayout) findViewById(R.id.chat);
108 this.log = (TextView) findViewById(R.id.log);
109 this.input = (EditText) findViewById(R.id.input);
110 this.send = (Button) findViewById(R.id.send);
111 this.spades = (TextView) findViewById(R.id.spades);
112 this.debug = (TextView) findViewById(R.id.debug);
114 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
115 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
120 this.window.addTab(this.window
122 .setIndicator("Chat")
123 .setContent(R.id.chat));
124 this.window.addTab(this.window
125 .newTabSpec("spades")
126 .setIndicator("Spades")
127 .setContent(R.id.spades));
128 this.window.addTab(this.window
130 .setIndicator("Debug")
131 .setContent(R.id.debug));
132 } catch (Exception e) {
133 Os.debug("Error setting content view", e);
139 public void onStart()
142 Os.debug("Main: onStart");
146 public void onResume()
149 Os.debug("Main: onResume");
153 public void onPause()
156 Os.debug("Main: onPause");
163 Os.debug("Main: onStop");
167 public void onRestart()
170 Os.debug("Main: onRestart");
174 public void onDestroy()
177 Os.debug("Main: onDestroy");
181 public boolean onCreateOptionsMenu(Menu menu)
183 MenuInflater inflater = getMenuInflater();
184 inflater.inflate(R.menu.main, menu);
189 public boolean onPrepareOptionsMenu(Menu menu)
191 menu.findItem(R.id.connect).setVisible(!this.ready);
192 menu.findItem(R.id.disconnect).setVisible(this.ready);
197 public boolean onOptionsItemSelected(MenuItem item)
199 switch (item.getItemId()) {
203 case R.id.disconnect:
216 class MainHandler extends Handler
218 public void handleMessage(android.os.Message msg)
222 Main.this.onRegister(msg.obj);
225 Main.this.onMessage(msg.obj);
228 Main.this.ready = true;
230 case Task.DISCONNECT:
231 Main.this.ready = false;
234 Os.debug("Main: unknown message - " + msg.what);