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;
36 private TabHost window;
37 private TabWidget tabs;
38 private LinearLayout chat;
40 private EditText input;
42 private TextView spades;
43 private TextView debug;
45 private ScrollView lscroll;
46 private ScrollView dscroll;
48 /* Private handler methods */
49 private void onRegister(Object obj)
51 Os.debug("Main: onRegister");
52 this.task = (Task)obj;
55 private void onMessage(Object obj)
57 Message msg = (Message)obj;
60 this.debug.append("> " + msg.line + "\n");
61 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
66 this.log.append(msg.from + ": " + msg.msg + "\n");
69 if (!msg.txt.equals(this.topic))
70 this.log.append("** Topic for " + msg.arg + ": " + msg.txt + " **\n");
74 if (!msg.txt.equals(this.names))
75 this.log.append("** Users in " + msg.arg + ": " + msg.txt + " **\n");
79 this.lscroll.smoothScrollTo(0, this.log.getBottom());
82 private void onNotify(String text)
84 Os.debug("Main: onNotify - " + text);
85 this.log.append("** " + text + " **\n");
86 this.toast.setText(text);
90 /* Private service methods */
91 private void startService()
93 Os.debug("Main: startService");
94 startService(new Intent(this, Task.class)
95 .putExtra("Messenger", this.messenger));
98 private void stopService()
100 Os.debug("Main: stopService");
101 stopService(new Intent(this, Task.class));
104 /* Widget callback functions */
105 public void onSend(View btn)
107 if (this.task == null)
109 String txt = this.input.getText().toString();
110 Message msg = this.task.send(txt);
113 this.input.setText("");
116 /* Activity Methods */
118 public void onCreate(Bundle savedInstanceState)
121 super.onCreate(savedInstanceState);
122 Os.debug("Main: onCreate");
125 this.setContentView(R.layout.main);
128 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
130 // Setup communication
131 this.handler = new MainHandler();
132 this.messenger = new Messenger(this.handler);
135 this.window = (TabHost) findViewById(android.R.id.tabhost);
136 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
137 this.chat = (LinearLayout) findViewById(R.id.chat);
138 this.log = (TextView) findViewById(R.id.log);
139 this.input = (EditText) findViewById(R.id.input);
140 this.send = (Button) findViewById(R.id.send);
141 this.spades = (TextView) findViewById(R.id.spades);
142 this.debug = (TextView) findViewById(R.id.debug);
144 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
145 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
150 this.window.addTab(this.window
152 .setIndicator("Chat")
153 .setContent(R.id.chat));
154 this.window.addTab(this.window
155 .newTabSpec("spades")
156 .setIndicator("Spades")
157 .setContent(R.id.spades));
158 this.window.addTab(this.window
160 .setIndicator("Debug")
161 .setContent(R.id.debug));
162 } catch (Exception e) {
163 Os.debug("Error setting content view", e);
169 public void onStart()
172 Os.debug("Main: onStart");
176 public void onResume()
179 Os.debug("Main: onResume");
183 public void onPause()
186 Os.debug("Main: onPause");
193 Os.debug("Main: onStop");
197 public void onRestart()
200 Os.debug("Main: onRestart");
204 public void onDestroy()
207 Os.debug("Main: onDestroy");
211 public boolean onCreateOptionsMenu(Menu menu)
213 MenuInflater inflater = getMenuInflater();
214 inflater.inflate(R.menu.main, menu);
219 public boolean onPrepareOptionsMenu(Menu menu)
221 menu.findItem(R.id.connect).setVisible(!this.ready);
222 menu.findItem(R.id.disconnect).setVisible(this.ready);
227 public boolean onOptionsItemSelected(MenuItem item)
229 switch (item.getItemId()) {
233 case R.id.disconnect:
246 class MainHandler extends Handler
248 public void handleMessage(android.os.Message msg)
252 Main.this.onRegister(msg.obj);
255 Main.this.onMessage(msg.obj);
258 Main.this.ready = true;
260 case Task.DISCONNECT:
261 Main.this.ready = false;
264 Main.this.onNotify((String)msg.obj);
267 Os.debug("Main: unknown message - " + msg.what);