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;
31 private TabHost window;
32 private TabWidget tabs;
33 private LinearLayout chat;
35 private EditText input;
37 private TextView spades;
38 private TextView debug;
40 private ScrollView lscroll;
41 private ScrollView dscroll;
44 public void onRegister(Object obj)
46 Os.debug("Main: onRegister");
47 this.task = (Task)obj;
50 public void onMessage(Object obj)
52 Message msg = (Message)obj;
54 this.debug.append("> " + msg.line + "\n");
55 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
57 if (msg.cmd.equals("PRIVMSG")) {
58 this.log.append(msg.from + ": " + msg.msg + "\n");
59 this.lscroll.smoothScrollTo(0, this.log.getBottom());
63 private void startService()
65 Os.debug("Main: startService");
66 startService(new Intent(this, Task.class)
67 .putExtra("Messenger", this.messenger));
70 private void stopService()
72 Os.debug("Main: stopService");
73 stopService(new Intent(this, Task.class));
76 /* Widget callback functions */
77 public void onSend(View btn)
79 if (this.task == null)
81 String txt = this.input.getText().toString();
82 Message msg = this.task.send(txt);
85 this.input.setText("");
86 this.log.append(msg.from + ": " + msg.msg + "\n");
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 onOptionsItemSelected(MenuItem item)
191 switch (item.getItemId()) {
195 case R.id.disconnect:
199 Os.debug("Main: Help!");
207 class MainHandler extends Handler
209 public void handleMessage(android.os.Message msg)
213 Main.this.onRegister(msg.obj);
216 Main.this.onMessage(msg.obj);
219 Os.debug("Main: unknown message - " + msg.what);