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