]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Main.java
Add cards display from IRC messages
[~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.Html;
9 import android.text.method.ScrollingMovementMethod;
10 import android.text.format.DateFormat;
11 import android.view.Menu;
12 import android.view.MenuInflater;
13 import android.view.MenuItem;
14 import android.view.View;
15 import android.widget.Button;
16 import android.widget.EditText;
17 import android.widget.LinearLayout;
18 import android.widget.ScrollView;
19 import android.widget.TabHost;
20 import android.widget.TabWidget;
21 import android.widget.TextView;
22 import android.widget.Toast;
23
24 public class Main extends Activity
25 {
26         /* Private data */
27         private Handler      handler;
28         private Messenger    messenger;
29         private Task         task;
30         private Toast        toast;
31         private boolean      running;
32         private String       topic;
33         private String       names;
34         private Cards        cards;
35         private Spades       game;
36
37         /* Widgets */
38         private TabHost      window;
39         private TabWidget    tabs;
40         private LinearLayout chat;
41         private TextView     log;
42         private EditText     input;
43         private Button       send;
44         private LinearLayout spades;
45         private TextView     debug;
46
47         private ScrollView   lscroll;
48         private ScrollView   dscroll;
49
50         /* Private helper methods */
51         private void notice(String text)
52         {
53                 this.log.append(Html.fromHtml("<b>*** " + text + "</b><br />"));
54         }
55
56         private void display(Message msg)
57         {
58                 String when = DateFormat.format("hh:mm:ss", msg.time).toString();
59                 String from = String.format("<font color=\"#ff88ff\">%s</font>", msg.from);
60                 String text = msg.msg;
61                 String fmt  = null;
62
63                 // Do IRC Colors - only partly works
64                 String fg = "<font color=\"<$1>\">";
65                 text = text
66                         .replaceAll("&", "&amp;")
67                         .replaceAll("<", "&lt;")
68                         .replaceAll(">", "&gt;");
69                 text = text
70                         .replaceAll("\\002", "<b>")             // bold
71                         .replaceAll("\\011", "<i>")             // italic
72                         .replaceAll("\\025", "<u>");            // underline
73                 text = text
74                         .replaceAll("\\003(\\d+)(,\\d+)?", fg)  // color
75                         .replaceAll("\\013(\\d+)(,\\d+)?", fg); // color
76                 text = text
77                         .replaceAll("<0?0>", "#000000")         // White
78                         .replaceAll("<0?1>", "#000000")         // Black
79                         .replaceAll("<0?2>", "#000080")         // Navy Blue
80                         .replaceAll("<0?3>", "#008000")         // Green
81                         .replaceAll("<0?4>", "#FF0000")         // Red
82                         .replaceAll("<0?5>", "#804040")         // Brown
83                         .replaceAll("<0?6>", "#8000FF")         // Purple
84                         .replaceAll("<0?7>", "#808000")         // Olive
85                         .replaceAll("<0?8>", "#FFFF00")         // Yellow
86                         .replaceAll("<0?9>", "#00FF00")         // Lime Green
87                         .replaceAll("<10>",  "#008080")         // Teal
88                         .replaceAll("<11>",  "#00FFFF")         // Aqua Light
89                         .replaceAll("<12>",  "#0000FF")         // Royal Blue
90                         .replaceAll("<13>",  "#FF00FF")         // Hot Pink
91                         .replaceAll("<14>",  "#808080")         // Dark Gray
92                         .replaceAll("<15>",  "#C0C0C0");        // Light Gray
93
94                 // Message formatting
95                 switch (msg.how) {
96                         case DIRECT:
97                         case MENTION:
98                         case PRIVMSG:
99                                 fmt  = "<b>(%s) %s: %s</b>";
100                                 break;
101                         case SENT:
102                                 fmt  = "(%s) <b>%s</b>: %s";
103                                 break;
104                         default:
105                                 fmt  = "(%s) %s: %s";
106                                 break;
107                 }
108
109                 String html = String.format(fmt, when, from, text);
110                 this.log.append(Html.fromHtml(html + "<br />"));
111         }
112
113         /* Private handler methods */
114         private void onRegister(Task task)
115         {
116                 Os.debug("Main: onRegister");
117                 this.task      = task;
118                 this.game.task = task;
119                 this.running = this.task.isRunning();
120                 for (Object obj : this.task.getLog()) {
121                         if (String.class.isInstance(obj))
122                                 this.notice((String)obj);
123                         if (Message.class.isInstance(obj))
124                                 this.onMessage((Message)obj);
125                 }
126         }
127
128         private void onMessage(Message msg)
129         {
130                 // Debug
131                 this.debug.append("> " + msg.line + "\n");
132                 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
133
134                 // Chat
135                 switch (msg.type) {
136                         case PRIVMSG:
137                                 this.display(msg);
138                                 this.game.onMessage(msg);
139                                 break;
140                         case TOPIC:
141                                 if (!msg.txt.equals(this.topic))
142                                         this.notice("Topic for " + msg.arg + ": " + msg.txt);
143                                 this.topic = msg.txt;
144                                 break;
145                         case NAMES:
146                                 if (!msg.txt.equals(this.names))
147                                         this.notice("Users in " + msg.arg + ": " + msg.txt);
148                                 this.names = msg.txt;
149                                 break;
150                         case ERROR:
151                                 this.notice("Error: " + msg.txt);
152                                 break;
153                         case AUTHOK:
154                                 this.notice("Authentication succeeded: " + msg.txt);
155                                 break;
156                         case AUTHFAIL:
157                                 this.notice("Authentication failed: " + msg.txt);
158                                 break;
159                 }
160                 this.lscroll.smoothScrollTo(0, this.log.getBottom());
161         }
162
163         private void onNotify(String text)
164         {
165                 Os.debug("Main: onNotify - " + text);
166                 this.notice(text);
167                 this.toast.setText(text);
168                 this.toast.show();
169         }
170
171         /* Private service methods */
172         private void register()
173         {
174                 Os.debug("Main: register");
175                 startService(new Intent(this, Task.class)
176                                 .putExtra("Command",   Task.REGISTER)
177                                 .putExtra("Messenger", this.messenger));
178         }
179
180         private void connect()
181         {
182                 Os.debug("Main: connect");
183                 startService(new Intent(this, Task.class)
184                                 .putExtra("Command", Task.CONNECT));
185                 this.running = true;
186         }
187
188         private void disconnect()
189         {
190                 Os.debug("Main: disconnect");
191                 startService(new Intent(this, Task.class)
192                                 .putExtra("Command", Task.DISCONNECT));
193         }
194
195         private void exit()
196         {
197                 stopService(new Intent(this, Task.class));
198         }
199
200         /* Widget callback functions */
201         public void onSend(View btn)
202         {
203                 if (this.task == null)
204                         return;
205                 String  txt = this.input.getText().toString();
206                 Message msg = this.task.send(txt);
207                 if (msg == null)
208                         return;
209                 this.input.setText("");
210         }
211
212         /* Activity Methods */
213         @Override
214         public void onCreate(Bundle savedInstanceState)
215         {
216                 try {
217                         super.onCreate(savedInstanceState);
218                         Os.debug("Main: onCreate");
219
220                         // Setup main layout
221                         this.setContentView(R.layout.main);
222
223                         // Setup toast
224                         this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
225
226                         // Setup communication
227                         this.handler   = new MainHandler();
228                         this.messenger = new Messenger(this.handler);
229
230                         // Find widgets
231                         this.window    = (TabHost)      findViewById(android.R.id.tabhost);
232                         this.tabs      = (TabWidget)    findViewById(android.R.id.tabs);
233                         this.chat      = (LinearLayout) findViewById(R.id.chat);
234                         this.log       = (TextView)     findViewById(R.id.log);
235                         this.input     = (EditText)     findViewById(R.id.input);
236                         this.send      = (Button)       findViewById(R.id.send);
237                         this.spades    = (LinearLayout) findViewById(R.id.spades);
238                         this.debug     = (TextView)     findViewById(R.id.debug);
239
240                         this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
241                         this.dscroll   = (ScrollView)   findViewById(R.id.debug_scroll);
242
243                         // Add window tabs
244                         this.window.setup();
245
246                         this.window.addTab(this.window
247                                         .newTabSpec("chat")
248                                         .setIndicator("Chat")
249                                         .setContent(R.id.chat));
250                         this.window.addTab(this.window
251                                         .newTabSpec("spades")
252                                         .setIndicator("Spades")
253                                         .setContent(R.id.spades));
254                         this.window.addTab(this.window
255                                         .newTabSpec("debug")
256                                         .setIndicator("Debug")
257                                         .setContent(R.id.debug));
258
259                         // Setup Spades game and cards view
260                         this.game  = new Spades("rhawk");
261                         this.cards = new Cards(this);
262
263                         this.game.cards = this.cards;
264                         this.cards.game = this.game;
265
266                         this.spades.addView(cards);
267
268                         // Attach to background service
269                         this.register();
270
271                 } catch (Exception e) {
272                         Os.debug("Error setting content view", e);
273                         return;
274                 }
275         }
276
277         @Override
278         public void onStart()
279         {
280                 super.onStart();
281                 Os.debug("Main: onStart");
282         }
283
284         @Override
285         public void onResume()
286         {
287                 super.onResume();
288                 Os.debug("Main: onResume");
289         }
290
291         @Override
292         public void onPause()
293         {
294                 super.onPause();
295                 Os.debug("Main: onPause");
296         }
297
298         @Override
299         public void onStop()
300         {
301                 super.onStop();
302                 Os.debug("Main: onStop");
303         }
304
305         @Override
306         public void onRestart()
307         {
308                 super.onRestart();
309                 Os.debug("Main: onRestart");
310         }
311
312         @Override
313         public void onDestroy()
314         {
315                 super.onDestroy();
316                 Os.debug("Main: onDestroy");
317         }
318
319         @Override
320         public boolean onCreateOptionsMenu(Menu menu)
321         {
322                 MenuInflater inflater = getMenuInflater();
323                 inflater.inflate(R.menu.main, menu);
324                 return true;
325         }
326
327         @Override
328         public boolean onPrepareOptionsMenu(Menu menu)
329         {
330                 menu.findItem(R.id.connect).setVisible(!this.running);
331                 menu.findItem(R.id.disconnect).setVisible(this.running);
332                 return true;
333         }
334
335         @Override
336         public boolean onOptionsItemSelected(MenuItem item)
337         {
338                 switch (item.getItemId()) {
339                         case R.id.connect:
340                                 this.connect();
341                                 return true;
342                         case R.id.disconnect:
343                                 this.disconnect();
344                                 return true;
345                         case R.id.settings:
346                                 this.startActivity(new Intent(this, Prefs.class));
347                                 return true;
348                         case R.id.exit:
349                                 this.exit();
350                                 this.finish();
351                                 return true;
352                         default:
353                                 return false;
354                 }
355         }
356
357         /* Handler class */
358         class MainHandler extends Handler
359         {
360                 public void handleMessage(android.os.Message msg)
361                 {
362                         switch (msg.what) {
363                                 case Task.REGISTER:
364                                         Main.this.onRegister((Task)msg.obj);
365                                         break;
366                                 case Task.MESSAGE:
367                                         Main.this.onMessage((Message)msg.obj);
368                                         break;
369                                 case Task.CONNECT:
370                                         Main.this.running = true;
371                                         break;
372                                 case Task.DISCONNECT:
373                                         Main.this.running = false;
374                                         break;
375                                 case Task.NOTIFY:
376                                         Main.this.onNotify((String)msg.obj);
377                                         break;
378                                 default:
379                                         Os.debug("Main: unknown message - " + msg.what);
380                                         break;
381                         }
382                 }
383         }
384 }