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;
20 import android.widget.Toast;
22 public class Main extends Activity
25 private Handler handler;
26 private Messenger messenger;
31 private boolean ready;
34 private TabHost window;
35 private TabWidget tabs;
36 private LinearLayout chat;
38 private EditText input;
40 private TextView spades;
41 private TextView debug;
43 private ScrollView lscroll;
44 private ScrollView dscroll;
46 /* Private handler methods */
47 private void onRegister(Object obj)
49 Os.debug("Main: onRegister");
50 this.task = (Task)obj;
53 private void onMessage(Object obj)
55 Message msg = (Message)obj;
57 this.debug.append("> " + msg.line + "\n");
58 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
60 if (msg.cmd.equals("PRIVMSG")) {
61 this.log.append(msg.from + ": " + msg.msg + "\n");
62 this.lscroll.smoothScrollTo(0, this.log.getBottom());
66 private void onNotify(String text)
68 Os.debug("Main: onNotify - " + text);
69 this.toast.setText(text);
73 /* Private service methods */
74 private void startService()
76 Os.debug("Main: startService");
77 startService(new Intent(this, Task.class)
78 .putExtra("Messenger", this.messenger));
81 private void stopService()
83 Os.debug("Main: stopService");
84 stopService(new Intent(this, Task.class));
87 /* Widget callback functions */
88 public void onSend(View btn)
90 if (this.task == null)
92 String txt = this.input.getText().toString();
93 Message msg = this.task.send(txt);
96 this.input.setText("");
99 /* Activity Methods */
101 public void onCreate(Bundle savedInstanceState)
104 super.onCreate(savedInstanceState);
105 Os.debug("Main: onCreate");
108 this.setContentView(R.layout.main);
111 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
113 // Setup communication
114 this.handler = new MainHandler();
115 this.messenger = new Messenger(this.handler);
118 this.window = (TabHost) findViewById(android.R.id.tabhost);
119 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
120 this.chat = (LinearLayout) findViewById(R.id.chat);
121 this.log = (TextView) findViewById(R.id.log);
122 this.input = (EditText) findViewById(R.id.input);
123 this.send = (Button) findViewById(R.id.send);
124 this.spades = (TextView) findViewById(R.id.spades);
125 this.debug = (TextView) findViewById(R.id.debug);
127 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
128 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
133 this.window.addTab(this.window
135 .setIndicator("Chat")
136 .setContent(R.id.chat));
137 this.window.addTab(this.window
138 .newTabSpec("spades")
139 .setIndicator("Spades")
140 .setContent(R.id.spades));
141 this.window.addTab(this.window
143 .setIndicator("Debug")
144 .setContent(R.id.debug));
145 } catch (Exception e) {
146 Os.debug("Error setting content view", e);
152 public void onStart()
155 Os.debug("Main: onStart");
159 public void onResume()
162 Os.debug("Main: onResume");
166 public void onPause()
169 Os.debug("Main: onPause");
176 Os.debug("Main: onStop");
180 public void onRestart()
183 Os.debug("Main: onRestart");
187 public void onDestroy()
190 Os.debug("Main: onDestroy");
194 public boolean onCreateOptionsMenu(Menu menu)
196 MenuInflater inflater = getMenuInflater();
197 inflater.inflate(R.menu.main, menu);
202 public boolean onPrepareOptionsMenu(Menu menu)
204 menu.findItem(R.id.connect).setVisible(!this.ready);
205 menu.findItem(R.id.disconnect).setVisible(this.ready);
210 public boolean onOptionsItemSelected(MenuItem item)
212 switch (item.getItemId()) {
216 case R.id.disconnect:
229 class MainHandler extends Handler
231 public void handleMessage(android.os.Message msg)
235 Main.this.onRegister(msg.obj);
238 Main.this.onMessage(msg.obj);
241 Main.this.ready = true;
243 case Task.DISCONNECT:
244 Main.this.ready = false;
247 Main.this.onNotify((String)msg.obj);
250 Os.debug("Main: unknown message - " + msg.what);