]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Main.java
Show topic and current users
[~andy/spades] / src / org / pileus / spades / Main.java
1 package org.pileus.spades;
2
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;
21
22 public class Main extends Activity
23 {
24         /* Static data */
25         private Handler      handler;
26         private Messenger    messenger;
27
28         /* Private data */
29         private Task         task;
30         private Toast        toast;
31         private boolean      ready;
32         private String       topic;
33         private String       names;
34
35         /* Widgets */
36         private TabHost      window;
37         private TabWidget    tabs;
38         private LinearLayout chat;
39         private TextView     log;
40         private EditText     input;
41         private Button       send;
42         private TextView     spades;
43         private TextView     debug;
44
45         private ScrollView   lscroll;
46         private ScrollView   dscroll;
47
48         /* Private handler methods */
49         private void onRegister(Object obj)
50         {
51                 Os.debug("Main: onRegister");
52                 this.task = (Task)obj;
53         }
54
55         private void onMessage(Object obj)
56         {
57                 Message msg = (Message)obj;
58
59                 // Debug
60                 this.debug.append("> " + msg.line + "\n");
61                 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
62
63                 // Chat
64                 switch (msg.type) {
65                         case PRIVMSG:
66                                 this.log.append(msg.from + ": " + msg.msg + "\n");
67                                 break;
68                         case TOPIC:
69                                 if (!msg.txt.equals(this.topic))
70                                         this.log.append("** Topic for " + msg.arg + ": " + msg.txt + " **\n");
71                                 this.topic = msg.txt;
72                                 break;
73                         case NAMES:
74                                 if (!msg.txt.equals(this.names))
75                                         this.log.append("** Users in " + msg.arg + ": " + msg.txt + " **\n");
76                                 this.names = msg.txt;
77                                 break;
78                 }
79                 this.lscroll.smoothScrollTo(0, this.log.getBottom());
80         }
81
82         private void onNotify(String text)
83         {
84                 Os.debug("Main: onNotify - " + text);
85                 this.log.append("** " + text + " **\n");
86                 this.toast.setText(text);
87                 this.toast.show();
88         }
89
90         /* Private service methods */
91         private void startService()
92         {
93                 Os.debug("Main: startService");
94                 startService(new Intent(this, Task.class)
95                                 .putExtra("Messenger", this.messenger));
96         }
97
98         private void stopService()
99         {
100                 Os.debug("Main: stopService");
101                 stopService(new Intent(this, Task.class));
102         }
103
104         /* Widget callback functions */
105         public void onSend(View btn)
106         {
107                 if (this.task == null)
108                         return;
109                 String  txt = this.input.getText().toString();
110                 Message msg = this.task.send(txt);
111                 if (msg == null)
112                         return;
113                 this.input.setText("");
114         }
115
116         /* Activity Methods */
117         @Override
118         public void onCreate(Bundle savedInstanceState)
119         {
120                 try {
121                         super.onCreate(savedInstanceState);
122                         Os.debug("Main: onCreate");
123
124                         // Setup main layout
125                         this.setContentView(R.layout.main);
126
127                         // Setup toast
128                         this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
129
130                         // Setup communication
131                         this.handler   = new MainHandler();
132                         this.messenger = new Messenger(this.handler);
133
134                         // Find widgets
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);
143
144                         this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
145                         this.dscroll   = (ScrollView)   findViewById(R.id.debug_scroll);
146
147                         // Add window tabs
148                         this.window.setup();
149
150                         this.window.addTab(this.window
151                                         .newTabSpec("chat")
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
159                                         .newTabSpec("debug")
160                                         .setIndicator("Debug")
161                                         .setContent(R.id.debug));
162                 } catch (Exception e) {
163                         Os.debug("Error setting content view", e);
164                         return;
165                 }
166         }
167
168         @Override
169         public void onStart()
170         {
171                 super.onStart();
172                 Os.debug("Main: onStart");
173         }
174
175         @Override
176         public void onResume()
177         {
178                 super.onResume();
179                 Os.debug("Main: onResume");
180         }
181
182         @Override
183         public void onPause()
184         {
185                 super.onPause();
186                 Os.debug("Main: onPause");
187         }
188
189         @Override
190         public void onStop()
191         {
192                 super.onStop();
193                 Os.debug("Main: onStop");
194         }
195
196         @Override
197         public void onRestart()
198         {
199                 super.onRestart();
200                 Os.debug("Main: onRestart");
201         }
202
203         @Override
204         public void onDestroy()
205         {
206                 super.onDestroy();
207                 Os.debug("Main: onDestroy");
208         }
209
210         @Override
211         public boolean onCreateOptionsMenu(Menu menu)
212         {
213                 MenuInflater inflater = getMenuInflater();
214                 inflater.inflate(R.menu.main, menu);
215                 return true;
216         }
217
218         @Override
219         public boolean onPrepareOptionsMenu(Menu menu)
220         {
221                 menu.findItem(R.id.connect).setVisible(!this.ready);
222                 menu.findItem(R.id.disconnect).setVisible(this.ready);
223                 return true;
224         }
225
226         @Override
227         public boolean onOptionsItemSelected(MenuItem item)
228         {
229                 switch (item.getItemId()) {
230                         case R.id.connect:
231                                 this.startService();
232                                 return true;
233                         case R.id.disconnect:
234                                 this.stopService();
235                                 return true;
236                         case R.id.exit:
237                                 this.stopService();
238                                 this.finish();
239                                 return true;
240                         default:
241                                 return false;
242                 }
243         }
244
245         /* Handler class */
246         class MainHandler extends Handler
247         {
248                 public void handleMessage(android.os.Message msg)
249                 {
250                         switch (msg.what) {
251                                 case Task.REGISTER:
252                                         Main.this.onRegister(msg.obj);
253                                         break;
254                                 case Task.MESSAGE:
255                                         Main.this.onMessage(msg.obj);
256                                         break;
257                                 case Task.CONNECT:
258                                         Main.this.ready = true;
259                                         break;
260                                 case Task.DISCONNECT:
261                                         Main.this.ready = false;
262                                         break;
263                                 case Task.NOTIFY:
264                                         Main.this.onNotify((String)msg.obj);
265                                         break;
266                                 default:
267                                         Os.debug("Main: unknown message - " + msg.what);
268                                         break;
269                         }
270                 }
271         }
272 }
273