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.Menu;
10 import android.view.MenuInflater;
11 import android.view.MenuItem;
12 import android.view.View;
13 import android.widget.Button;
14 import android.widget.EditText;
15 import android.widget.LinearLayout;
16 import android.widget.ScrollView;
17 import android.widget.TabHost;
18 import android.widget.TabWidget;
19 import android.widget.TextView;
20 import android.widget.Toast;
22 import android.preference.PreferenceActivity;
24 public class Main extends Activity
27 private Handler handler;
28 private Messenger messenger;
33 private boolean ready;
38 private TabHost window;
39 private TabWidget tabs;
40 private LinearLayout chat;
42 private EditText input;
44 private TextView spades;
45 private TextView debug;
47 private ScrollView lscroll;
48 private ScrollView dscroll;
50 /* Private handler methods */
51 private void onRegister(Object obj)
53 Os.debug("Main: onRegister");
54 this.task = (Task)obj;
57 private void onMessage(Object obj)
59 Message msg = (Message)obj;
62 this.debug.append("> " + msg.line + "\n");
63 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
68 this.log.append(msg.from + ": " + msg.msg + "\n");
71 if (!msg.txt.equals(this.topic))
72 this.log.append("** Topic for " + msg.arg + ": " + msg.txt + " **\n");
76 if (!msg.txt.equals(this.names))
77 this.log.append("** Users in " + msg.arg + ": " + msg.txt + " **\n");
81 this.lscroll.smoothScrollTo(0, this.log.getBottom());
84 private void onNotify(String text)
86 Os.debug("Main: onNotify - " + text);
87 this.log.append("** " + text + " **\n");
88 this.toast.setText(text);
92 /* Private service methods */
93 private void startService()
95 Os.debug("Main: startService");
96 startService(new Intent(this, Task.class)
97 .putExtra("Messenger", this.messenger));
100 private void stopService()
102 Os.debug("Main: stopService");
103 stopService(new Intent(this, Task.class));
106 /* Widget callback functions */
107 public void onSend(View btn)
109 if (this.task == null)
111 String txt = this.input.getText().toString();
112 Message msg = this.task.send(txt);
115 this.input.setText("");
118 /* Activity Methods */
120 public void onCreate(Bundle savedInstanceState)
123 super.onCreate(savedInstanceState);
124 Os.debug("Main: onCreate");
127 this.setContentView(R.layout.main);
130 this.toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
132 // Setup communication
133 this.handler = new MainHandler();
134 this.messenger = new Messenger(this.handler);
137 this.window = (TabHost) findViewById(android.R.id.tabhost);
138 this.tabs = (TabWidget) findViewById(android.R.id.tabs);
139 this.chat = (LinearLayout) findViewById(R.id.chat);
140 this.log = (TextView) findViewById(R.id.log);
141 this.input = (EditText) findViewById(R.id.input);
142 this.send = (Button) findViewById(R.id.send);
143 this.spades = (TextView) findViewById(R.id.spades);
144 this.debug = (TextView) findViewById(R.id.debug);
146 this.lscroll = (ScrollView) findViewById(R.id.log_scroll);
147 this.dscroll = (ScrollView) findViewById(R.id.debug_scroll);
152 this.window.addTab(this.window
154 .setIndicator("Chat")
155 .setContent(R.id.chat));
156 this.window.addTab(this.window
157 .newTabSpec("spades")
158 .setIndicator("Spades")
159 .setContent(R.id.spades));
160 this.window.addTab(this.window
162 .setIndicator("Debug")
163 .setContent(R.id.debug));
164 } catch (Exception e) {
165 Os.debug("Error setting content view", e);
171 public void onStart()
174 Os.debug("Main: onStart");
178 public void onResume()
181 Os.debug("Main: onResume");
185 public void onPause()
188 Os.debug("Main: onPause");
195 Os.debug("Main: onStop");
199 public void onRestart()
202 Os.debug("Main: onRestart");
206 public void onDestroy()
209 Os.debug("Main: onDestroy");
213 public boolean onCreateOptionsMenu(Menu menu)
215 MenuInflater inflater = getMenuInflater();
216 inflater.inflate(R.menu.main, menu);
221 public boolean onPrepareOptionsMenu(Menu menu)
223 menu.findItem(R.id.connect).setVisible(!this.ready);
224 menu.findItem(R.id.disconnect).setVisible(this.ready);
229 public boolean onOptionsItemSelected(MenuItem item)
231 switch (item.getItemId()) {
235 case R.id.disconnect:
239 this.startActivity(new Intent(this, Prefs.class));
251 class MainHandler extends Handler
253 public void handleMessage(android.os.Message msg)
257 Main.this.onRegister(msg.obj);
260 Main.this.onMessage(msg.obj);
263 Main.this.ready = true;
265 case Task.DISCONNECT:
266 Main.this.ready = false;
269 Main.this.onNotify((String)msg.obj);
272 Os.debug("Main: unknown message - " + msg.what);