]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Main.java
Display IRC colors in text view
[~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.graphics.Color;
6 import android.graphics.Typeface;
7 import android.os.Bundle;
8 import android.os.Handler;
9 import android.os.Messenger;
10 import android.preference.PreferenceManager;
11 import android.text.Spannable;
12 import android.text.SpannableString;
13 import android.text.format.DateFormat;
14 import android.text.style.BackgroundColorSpan;
15 import android.text.style.ForegroundColorSpan;
16 import android.text.style.StrikethroughSpan;
17 import android.text.style.StyleSpan;
18 import android.text.style.UnderlineSpan;
19 import android.view.Menu;
20 import android.view.MenuInflater;
21 import android.view.MenuItem;
22 import android.view.View;
23 import android.widget.Button;
24 import android.widget.EditText;
25 import android.widget.LinearLayout;
26 import android.widget.ScrollView;
27 import android.widget.TabHost;
28 import android.widget.TabWidget;
29 import android.widget.TextView;
30 import android.widget.Toast;
31
32 public class Main extends Activity
33 {
34         /* Private data */
35         private Handler      handler;
36         private Messenger    messenger;
37         private Task         task;
38         private Toast        toast;
39         private boolean      running;
40         private String       topic;
41         private String       names;
42         private Cards        cards;
43         private Spades       game;
44
45         /* Widgets */
46         private TabHost      window;
47         private TabWidget    tabs;
48         private LinearLayout chat;
49         private TextView     log;
50         private EditText     input;
51         private Button       send;
52         private LinearLayout spades;
53         private TextView     debug;
54
55         private ScrollView   lscroll;
56         private ScrollView   dscroll;
57
58         /* Private helper methods */
59         private void notice(String text)
60         {
61                 String    msg  = "*** " + text + "\n";
62                 Spannable span = new SpannableString(msg);
63                 span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0);
64                 this.log.append(span);
65         }
66
67         private void display(Message msg)
68         {
69                 String date = DateFormat.format("hh:mm:ss", msg.time).toString();
70                 String text = String.format("(%s) %s: %s\n", date, msg.from, msg.msg);
71                 Spannable span = new SpannableString(text);
72
73                 // Determin positions
74                 int de  = 1 + date.length() + 1;
75                 int ne  = de + 1 + msg.from.length() + 1;
76                 int pos = ne + 1;
77
78                 // Format date and name
79                 span.setSpan(new ForegroundColorSpan(0xffffff88), 0,    de, 0);
80                 span.setSpan(new ForegroundColorSpan(0xffff88ff), de+1, ne, 0);
81
82                 // Format IRC Colors
83                 for (Message.Format fmt : msg.parts) {
84                         int len = fmt.txt.length();
85
86                         // Bold/italics
87                         if (fmt.bold && fmt.italic)
88                                 span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), pos, pos+len, 0);
89                         else if (fmt.bold)
90                                 span.setSpan(new StyleSpan(Typeface.BOLD), pos, pos+len, 0);
91                         else if (fmt.italic)
92                                 span.setSpan(new StyleSpan(Typeface.ITALIC), pos, pos+len, 0);
93
94                         // Striketrough / underline
95                         if (fmt.strike)
96                                 span.setSpan(new StrikethroughSpan(), pos, pos+len, 0);
97                         if (fmt.underline)
98                                 span.setSpan(new UnderlineSpan(), pos, pos+len, 0);
99
100                         // Colors (reverse not supported)
101                         if (fmt.fg!=null)
102                                 span.setSpan(new ForegroundColorSpan(fmt.fg.color), pos, pos+len, 0);
103                         if (fmt.bg!=null)
104                                 span.setSpan(new BackgroundColorSpan(fmt.bg.color), pos, pos+len, 0);
105
106                         pos += len;
107                 }
108
109                 // Append the message
110                 this.log.append(span);
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                 this.running = false;
194         }
195
196         private void quit()
197         {
198                 stopService(new Intent(this, Task.class));
199                 Intent intent = new Intent(Intent.ACTION_MAIN);
200                 intent.addCategory(Intent.CATEGORY_HOME);
201                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
202                 startActivity(intent);
203         }
204
205         /* Widget callback functions */
206         public void onSend(View btn)
207         {
208                 if (this.task == null)
209                         return;
210                 String  txt = this.input.getText().toString();
211                 Message msg = this.task.send(txt);
212                 if (msg == null)
213                         return;
214                 this.input.setText("");
215         }
216
217         /* Activity Methods */
218         @Override
219         public void onCreate(Bundle savedInstanceState)
220         {
221                 try {
222                         super.onCreate(savedInstanceState);
223                         Os.debug("Main: onCreate");
224
225                         // Setup preferences
226                         PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
227
228                         // Setup main layout
229                         this.setContentView(R.layout.main);
230
231                         // Setup toast
232                         this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
233
234                         // Setup communication
235                         this.handler   = new MainHandler();
236                         this.messenger = new Messenger(this.handler);
237
238                         // Find widgets
239                         this.window    = (TabHost)      findViewById(android.R.id.tabhost);
240                         this.tabs      = (TabWidget)    findViewById(android.R.id.tabs);
241                         this.chat      = (LinearLayout) findViewById(R.id.chat);
242                         this.log       = (TextView)     findViewById(R.id.log);
243                         this.input     = (EditText)     findViewById(R.id.input);
244                         this.send      = (Button)       findViewById(R.id.send);
245                         this.spades    = (LinearLayout) findViewById(R.id.spades);
246                         this.debug     = (TextView)     findViewById(R.id.debug);
247
248                         this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
249                         this.dscroll   = (ScrollView)   findViewById(R.id.debug_scroll);
250
251                         // Add window tabs
252                         this.window.setup();
253
254                         this.window.addTab(this.window
255                                         .newTabSpec("chat")
256                                         .setIndicator("Chat")
257                                         .setContent(R.id.chat));
258                         this.window.addTab(this.window
259                                         .newTabSpec("spades")
260                                         .setIndicator("Spades")
261                                         .setContent(R.id.spades));
262                         this.window.addTab(this.window
263                                         .newTabSpec("debug")
264                                         .setIndicator("Debug")
265                                         .setContent(R.id.debug));
266
267                         // Setup Spades game and cards view
268                         this.game  = new Spades(PreferenceManager
269                                         .getDefaultSharedPreferences(this)
270                                         .getString("pref_referee", "rhawk"));
271                         this.cards = new Cards(this);
272
273                         this.game.cards = this.cards;
274                         this.cards.game = this.game;
275
276                         this.spades.addView(cards);
277
278                         // Attach to background service
279                         this.register();
280
281                 } catch (Exception e) {
282                         Os.debug("Error setting content view", e);
283                         return;
284                 }
285         }
286
287         @Override
288         public void onStart()
289         {
290                 super.onStart();
291                 Os.debug("Main: onStart");
292         }
293
294         @Override
295         public void onResume()
296         {
297                 super.onResume();
298                 Os.debug("Main: onResume");
299         }
300
301         @Override
302         public void onPause()
303         {
304                 super.onPause();
305                 Os.debug("Main: onPause");
306         }
307
308         @Override
309         public void onStop()
310         {
311                 super.onStop();
312                 Os.debug("Main: onStop");
313         }
314
315         @Override
316         public void onRestart()
317         {
318                 super.onRestart();
319                 Os.debug("Main: onRestart");
320         }
321
322         @Override
323         public void onDestroy()
324         {
325                 super.onDestroy();
326                 Os.debug("Main: onDestroy");
327         }
328
329         @Override
330         public boolean onCreateOptionsMenu(Menu menu)
331         {
332                 MenuInflater inflater = getMenuInflater();
333                 inflater.inflate(R.menu.main, menu);
334                 return true;
335         }
336
337         @Override
338         public boolean onPrepareOptionsMenu(Menu menu)
339         {
340                 menu.findItem(R.id.connect).setVisible(!this.running);
341                 menu.findItem(R.id.disconnect).setVisible(this.running);
342                 return true;
343         }
344
345         @Override
346         public boolean onOptionsItemSelected(MenuItem item)
347         {
348                 switch (item.getItemId()) {
349                         case R.id.connect:
350                                 this.connect();
351                                 return true;
352                         case R.id.disconnect:
353                                 this.disconnect();
354                                 return true;
355                         case R.id.settings:
356                                 this.startActivity(new Intent(this, Prefs.class));
357                                 return true;
358                         case R.id.quit:
359                                 this.quit();
360                                 return true;
361                         default:
362                                 return false;
363                 }
364         }
365
366         /* Handler class */
367         class MainHandler extends Handler
368         {
369                 public void handleMessage(android.os.Message msg)
370                 {
371                         switch (msg.what) {
372                                 case Task.REGISTER:
373                                         Main.this.onRegister((Task)msg.obj);
374                                         break;
375                                 case Task.MESSAGE:
376                                         Main.this.onMessage((Message)msg.obj);
377                                         break;
378                                 case Task.CONNECT:
379                                         Main.this.running = true;
380                                         break;
381                                 case Task.DISCONNECT:
382                                         Main.this.running = false;
383                                         break;
384                                 case Task.NOTIFY:
385                                         Main.this.onNotify((String)msg.obj);
386                                         break;
387                                 default:
388                                         Os.debug("Main: unknown message - " + msg.what);
389                                         break;
390                         }
391                 }
392         }
393 }