]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Main.java
Clear text windows before replaying log
[~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 int hsv2rgb(int hsv)
60         {
61                 int h  = (hsv & 0xff0000) >> 16;
62                 int s  = (hsv & 0x00ff00) >>  8;
63                 int v  = (hsv & 0x0000ff) >>  0;
64
65                 int c  = (v * s) / 256;
66                 int h1 = (h * 6) / 256;
67                 int x  = c * (1 - Math.abs((h1%2)-1));
68                 int m  = v - c;
69
70                 int rgb = 0;
71
72                 if (0 <= h1 && h1 <= 1) rgb = (c << 16) | (x << 8) | 0;
73                 if (1 <= h1 && h1 <= 2) rgb = (x << 16) | (c << 8) | 0;
74                 if (2 <= h1 && h1 <= 3) rgb = (0 << 16) | (c << 8) | x;
75                 if (3 <= h1 && h1 <= 4) rgb = (0 << 16) | (x << 8) | c;
76                 if (4 <= h1 && h1 <= 5) rgb = (x << 16) | (0 << 8) | c;
77                 if (5 <= h1 && h1 <= 6) rgb = (c << 16) | (0 << 8) | x;
78
79                 return rgb + (m << 16) + (m << 8) + m;
80         }
81
82         private void notice(String text)
83         {
84                 String    msg  = "*** " + text + "\n";
85                 Spannable span = new SpannableString(msg);
86                 span.setSpan(new StyleSpan(Typeface.BOLD), 0, msg.length(), 0);
87                 this.log.append(span);
88         }
89
90         private void display(Message msg)
91         {
92                 String date = DateFormat.format("hh:mm:ss", msg.time).toString();
93                 String text = String.format("(%s) %s: %s\n", date, msg.from, msg.msg);
94                 Spannable span = new SpannableString(text);
95
96                 // Determin positions
97                 int de  = 1 + date.length() + 1;
98                 int ne  = de + 1 + msg.from.length() + 1;
99                 int pos = ne + 1;
100
101                 // Get user color
102                 int hash  = msg.from.hashCode();
103                 int color = this.hsv2rgb(hash | 0x8080) | 0xff000000;
104
105                 // Format date and name
106                 span.setSpan(new ForegroundColorSpan(0xffffff88), 0,    de, 0);
107                 span.setSpan(new ForegroundColorSpan(color),      de+1, ne, 0);
108
109                 // Format IRC Colors
110                 for (Message.Format fmt : msg.parts) {
111                         int len = fmt.txt.length();
112
113                         // Bold/italics
114                         if (fmt.bold && fmt.italic)
115                                 span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), pos, pos+len, 0);
116                         else if (fmt.bold)
117                                 span.setSpan(new StyleSpan(Typeface.BOLD), pos, pos+len, 0);
118                         else if (fmt.italic)
119                                 span.setSpan(new StyleSpan(Typeface.ITALIC), pos, pos+len, 0);
120
121                         // Striketrough / underline
122                         if (fmt.strike)
123                                 span.setSpan(new StrikethroughSpan(), pos, pos+len, 0);
124                         if (fmt.underline)
125                                 span.setSpan(new UnderlineSpan(), pos, pos+len, 0);
126
127                         // Colors (reverse not supported)
128                         if (fmt.fg!=null)
129                                 span.setSpan(new ForegroundColorSpan(fmt.fg.color), pos, pos+len, 0);
130                         if (fmt.bg!=null)
131                                 span.setSpan(new BackgroundColorSpan(fmt.bg.color), pos, pos+len, 0);
132
133                         pos += len;
134                 }
135
136                 // Append the message
137                 this.log.append(span);
138         }
139
140         /* Private handler methods */
141         private void onRegister(Task task)
142         {
143                 Os.debug("Main: onRegister");
144                 this.task      = task;
145                 this.game.task = task;
146                 this.running   = this.task.isRunning();
147                 this.log.setText("");
148                 this.debug.setText("");
149                 for (Object obj : this.task.getLog()) {
150                         if (String.class.isInstance(obj))
151                                 this.notice((String)obj);
152                         if (Message.class.isInstance(obj))
153                                 this.onMessage((Message)obj);
154                 }
155         }
156
157         private void onMessage(Message msg)
158         {
159                 // Debug
160                 this.debug.append("> " + msg.line + "\n");
161                 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
162
163                 // Chat
164                 switch (msg.type) {
165                         case PRIVMSG:
166                                 this.display(msg);
167                                 this.game.onMessage(msg);
168                                 break;
169                         case TOPIC:
170                                 if (!msg.txt.equals(this.topic))
171                                         this.notice("Topic for " + msg.arg + ": " + msg.txt);
172                                 this.topic = msg.txt;
173                                 break;
174                         case NAMES:
175                                 if (!msg.txt.equals(this.names))
176                                         this.notice("Users in " + msg.arg + ": " + msg.txt);
177                                 this.names = msg.txt;
178                                 break;
179                         case ERROR:
180                                 this.notice("Error: " + msg.txt);
181                                 break;
182                         case AUTHOK:
183                                 this.notice("Authentication succeeded: " + msg.txt);
184                                 break;
185                         case AUTHFAIL:
186                                 this.notice("Authentication failed: " + msg.txt);
187                                 break;
188                 }
189                 this.lscroll.smoothScrollTo(0, this.log.getBottom());
190         }
191
192         private void onNotify(String text)
193         {
194                 Os.debug("Main: onNotify - " + text);
195                 this.notice(text);
196                 this.toast.setText(text);
197                 this.toast.show();
198         }
199
200         /* Private service methods */
201         private void register()
202         {
203                 Os.debug("Main: register");
204                 startService(new Intent(this, Task.class)
205                                 .putExtra("Command",   Task.REGISTER)
206                                 .putExtra("Messenger", this.messenger));
207         }
208
209         private void connect()
210         {
211                 Os.debug("Main: connect");
212                 startService(new Intent(this, Task.class)
213                                 .putExtra("Command", Task.CONNECT));
214                 this.running = true;
215         }
216
217         private void disconnect()
218         {
219                 Os.debug("Main: disconnect");
220                 startService(new Intent(this, Task.class)
221                                 .putExtra("Command", Task.DISCONNECT));
222                 this.running = false;
223         }
224
225         private void quit()
226         {
227                 this.log.setText("");
228                 this.debug.setText("");
229                 stopService(new Intent(this, Task.class));
230                 Intent intent = new Intent(Intent.ACTION_MAIN);
231                 intent.addCategory(Intent.CATEGORY_HOME);
232                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
233                 startActivity(intent);
234         }
235
236         /* Widget callback functions */
237         public void onSend(View btn)
238         {
239                 if (this.task == null)
240                         return;
241                 String  txt = this.input.getText().toString();
242                 Message msg = this.task.send(txt);
243                 if (msg == null)
244                         return;
245                 this.input.setText("");
246         }
247
248         /* Activity Methods */
249         @Override
250         public void onCreate(Bundle savedInstanceState)
251         {
252                 try {
253                         super.onCreate(savedInstanceState);
254                         Os.debug("Main: onCreate");
255
256                         // Setup preferences
257                         PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
258
259                         // Setup main layout
260                         this.setContentView(R.layout.main);
261
262                         // Setup toast
263                         this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
264
265                         // Setup communication
266                         this.handler   = new MainHandler();
267                         this.messenger = new Messenger(this.handler);
268
269                         // Find widgets
270                         this.window    = (TabHost)      findViewById(android.R.id.tabhost);
271                         this.tabs      = (TabWidget)    findViewById(android.R.id.tabs);
272                         this.chat      = (LinearLayout) findViewById(R.id.chat);
273                         this.log       = (TextView)     findViewById(R.id.log);
274                         this.input     = (EditText)     findViewById(R.id.input);
275                         this.send      = (Button)       findViewById(R.id.send);
276                         this.spades    = (LinearLayout) findViewById(R.id.spades);
277                         this.debug     = (TextView)     findViewById(R.id.debug);
278
279                         this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
280                         this.dscroll   = (ScrollView)   findViewById(R.id.debug_scroll);
281
282                         // Add window tabs
283                         this.window.setup();
284
285                         this.window.addTab(this.window
286                                         .newTabSpec("chat")
287                                         .setIndicator("Chat")
288                                         .setContent(R.id.chat));
289                         this.window.addTab(this.window
290                                         .newTabSpec("spades")
291                                         .setIndicator("Spades")
292                                         .setContent(R.id.spades));
293                         this.window.addTab(this.window
294                                         .newTabSpec("debug")
295                                         .setIndicator("Debug")
296                                         .setContent(R.id.debug));
297
298                         // Setup Spades game and cards view
299                         this.game  = new Spades(PreferenceManager
300                                         .getDefaultSharedPreferences(this)
301                                         .getString("pref_referee", "rhawk"));
302                         this.cards = new Cards(this);
303
304                         this.game.cards = this.cards;
305                         this.cards.game = this.game;
306
307                         this.spades.addView(cards);
308
309                         // Attach to background service
310                         this.register();
311
312                 } catch (Exception e) {
313                         Os.debug("Error setting content view", e);
314                         return;
315                 }
316         }
317
318         @Override
319         public void onStart()
320         {
321                 super.onStart();
322                 this.register();
323                 Os.debug("Main: onStart");
324         }
325
326         @Override
327         public void onResume()
328         {
329                 super.onResume();
330                 Os.debug("Main: onResume");
331         }
332
333         @Override
334         public void onPause()
335         {
336                 super.onPause();
337                 Os.debug("Main: onPause");
338         }
339
340         @Override
341         public void onStop()
342         {
343                 super.onStop();
344                 Os.debug("Main: onStop");
345         }
346
347         @Override
348         public void onRestart()
349         {
350                 super.onRestart();
351                 Os.debug("Main: onRestart");
352         }
353
354         @Override
355         public void onDestroy()
356         {
357                 super.onDestroy();
358                 Os.debug("Main: onDestroy");
359         }
360
361         @Override
362         public boolean onCreateOptionsMenu(Menu menu)
363         {
364                 MenuInflater inflater = getMenuInflater();
365                 inflater.inflate(R.menu.main, menu);
366                 return true;
367         }
368
369         @Override
370         public boolean onPrepareOptionsMenu(Menu menu)
371         {
372                 menu.findItem(R.id.connect).setVisible(!this.running);
373                 menu.findItem(R.id.disconnect).setVisible(this.running);
374                 return true;
375         }
376
377         @Override
378         public boolean onOptionsItemSelected(MenuItem item)
379         {
380                 switch (item.getItemId()) {
381                         case R.id.connect:
382                                 this.connect();
383                                 return true;
384                         case R.id.disconnect:
385                                 this.disconnect();
386                                 return true;
387                         case R.id.settings:
388                                 this.startActivity(new Intent(this, Prefs.class));
389                                 return true;
390                         case R.id.quit:
391                                 this.quit();
392                                 return true;
393                         default:
394                                 return false;
395                 }
396         }
397
398         /* Handler class */
399         class MainHandler extends Handler
400         {
401                 public void handleMessage(android.os.Message msg)
402                 {
403                         switch (msg.what) {
404                                 case Task.REGISTER:
405                                         Main.this.onRegister((Task)msg.obj);
406                                         break;
407                                 case Task.MESSAGE:
408                                         Main.this.onMessage((Message)msg.obj);
409                                         break;
410                                 case Task.CONNECT:
411                                         Main.this.running = true;
412                                         break;
413                                 case Task.DISCONNECT:
414                                         Main.this.running = false;
415                                         break;
416                                 case Task.NOTIFY:
417                                         Main.this.onNotify((String)msg.obj);
418                                         break;
419                                 default:
420                                         Os.debug("Main: unknown message - " + msg.what);
421                                         break;
422                         }
423                 }
424         }
425 }