]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Main.java
e4bac7fc96d83284d25c7aea8df8254dd6572c07
[~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                 for (Object obj : this.task.getLog()) {
148                         if (String.class.isInstance(obj))
149                                 this.notice((String)obj);
150                         if (Message.class.isInstance(obj))
151                                 this.onMessage((Message)obj);
152                 }
153         }
154
155         private void onMessage(Message msg)
156         {
157                 // Debug
158                 this.debug.append("> " + msg.line + "\n");
159                 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
160
161                 // Chat
162                 switch (msg.type) {
163                         case PRIVMSG:
164                                 this.display(msg);
165                                 this.game.onMessage(msg);
166                                 break;
167                         case TOPIC:
168                                 if (!msg.txt.equals(this.topic))
169                                         this.notice("Topic for " + msg.arg + ": " + msg.txt);
170                                 this.topic = msg.txt;
171                                 break;
172                         case NAMES:
173                                 if (!msg.txt.equals(this.names))
174                                         this.notice("Users in " + msg.arg + ": " + msg.txt);
175                                 this.names = msg.txt;
176                                 break;
177                         case ERROR:
178                                 this.notice("Error: " + msg.txt);
179                                 break;
180                         case AUTHOK:
181                                 this.notice("Authentication succeeded: " + msg.txt);
182                                 break;
183                         case AUTHFAIL:
184                                 this.notice("Authentication failed: " + msg.txt);
185                                 break;
186                 }
187                 this.lscroll.smoothScrollTo(0, this.log.getBottom());
188         }
189
190         private void onNotify(String text)
191         {
192                 Os.debug("Main: onNotify - " + text);
193                 this.notice(text);
194                 this.toast.setText(text);
195                 this.toast.show();
196         }
197
198         /* Private service methods */
199         private void register()
200         {
201                 Os.debug("Main: register");
202                 startService(new Intent(this, Task.class)
203                                 .putExtra("Command",   Task.REGISTER)
204                                 .putExtra("Messenger", this.messenger));
205         }
206
207         private void connect()
208         {
209                 Os.debug("Main: connect");
210                 startService(new Intent(this, Task.class)
211                                 .putExtra("Command", Task.CONNECT));
212                 this.running = true;
213         }
214
215         private void disconnect()
216         {
217                 Os.debug("Main: disconnect");
218                 startService(new Intent(this, Task.class)
219                                 .putExtra("Command", Task.DISCONNECT));
220                 this.running = false;
221         }
222
223         private void quit()
224         {
225                 this.log.setText("");
226                 this.debug.setText("");
227                 stopService(new Intent(this, Task.class));
228                 Intent intent = new Intent(Intent.ACTION_MAIN);
229                 intent.addCategory(Intent.CATEGORY_HOME);
230                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
231                 startActivity(intent);
232         }
233
234         /* Widget callback functions */
235         public void onSend(View btn)
236         {
237                 if (this.task == null)
238                         return;
239                 String  txt = this.input.getText().toString();
240                 Message msg = this.task.send(txt);
241                 if (msg == null)
242                         return;
243                 this.input.setText("");
244         }
245
246         /* Activity Methods */
247         @Override
248         public void onCreate(Bundle savedInstanceState)
249         {
250                 try {
251                         super.onCreate(savedInstanceState);
252                         Os.debug("Main: onCreate");
253
254                         // Setup preferences
255                         PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
256
257                         // Setup main layout
258                         this.setContentView(R.layout.main);
259
260                         // Setup toast
261                         this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
262
263                         // Setup communication
264                         this.handler   = new MainHandler();
265                         this.messenger = new Messenger(this.handler);
266
267                         // Find widgets
268                         this.window    = (TabHost)      findViewById(android.R.id.tabhost);
269                         this.tabs      = (TabWidget)    findViewById(android.R.id.tabs);
270                         this.chat      = (LinearLayout) findViewById(R.id.chat);
271                         this.log       = (TextView)     findViewById(R.id.log);
272                         this.input     = (EditText)     findViewById(R.id.input);
273                         this.send      = (Button)       findViewById(R.id.send);
274                         this.spades    = (LinearLayout) findViewById(R.id.spades);
275                         this.debug     = (TextView)     findViewById(R.id.debug);
276
277                         this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
278                         this.dscroll   = (ScrollView)   findViewById(R.id.debug_scroll);
279
280                         // Add window tabs
281                         this.window.setup();
282
283                         this.window.addTab(this.window
284                                         .newTabSpec("chat")
285                                         .setIndicator("Chat")
286                                         .setContent(R.id.chat));
287                         this.window.addTab(this.window
288                                         .newTabSpec("spades")
289                                         .setIndicator("Spades")
290                                         .setContent(R.id.spades));
291                         this.window.addTab(this.window
292                                         .newTabSpec("debug")
293                                         .setIndicator("Debug")
294                                         .setContent(R.id.debug));
295
296                         // Setup Spades game and cards view
297                         this.game  = new Spades(PreferenceManager
298                                         .getDefaultSharedPreferences(this)
299                                         .getString("pref_referee", "rhawk"));
300                         this.cards = new Cards(this);
301
302                         this.game.cards = this.cards;
303                         this.cards.game = this.game;
304
305                         this.spades.addView(cards);
306
307                         // Attach to background service
308                         this.register();
309
310                 } catch (Exception e) {
311                         Os.debug("Error setting content view", e);
312                         return;
313                 }
314         }
315
316         @Override
317         public void onStart()
318         {
319                 super.onStart();
320                 this.register();
321                 Os.debug("Main: onStart");
322         }
323
324         @Override
325         public void onResume()
326         {
327                 super.onResume();
328                 Os.debug("Main: onResume");
329         }
330
331         @Override
332         public void onPause()
333         {
334                 super.onPause();
335                 Os.debug("Main: onPause");
336         }
337
338         @Override
339         public void onStop()
340         {
341                 super.onStop();
342                 Os.debug("Main: onStop");
343         }
344
345         @Override
346         public void onRestart()
347         {
348                 super.onRestart();
349                 Os.debug("Main: onRestart");
350         }
351
352         @Override
353         public void onDestroy()
354         {
355                 super.onDestroy();
356                 Os.debug("Main: onDestroy");
357         }
358
359         @Override
360         public boolean onCreateOptionsMenu(Menu menu)
361         {
362                 MenuInflater inflater = getMenuInflater();
363                 inflater.inflate(R.menu.main, menu);
364                 return true;
365         }
366
367         @Override
368         public boolean onPrepareOptionsMenu(Menu menu)
369         {
370                 menu.findItem(R.id.connect).setVisible(!this.running);
371                 menu.findItem(R.id.disconnect).setVisible(this.running);
372                 return true;
373         }
374
375         @Override
376         public boolean onOptionsItemSelected(MenuItem item)
377         {
378                 switch (item.getItemId()) {
379                         case R.id.connect:
380                                 this.connect();
381                                 return true;
382                         case R.id.disconnect:
383                                 this.disconnect();
384                                 return true;
385                         case R.id.settings:
386                                 this.startActivity(new Intent(this, Prefs.class));
387                                 return true;
388                         case R.id.quit:
389                                 this.quit();
390                                 return true;
391                         default:
392                                 return false;
393                 }
394         }
395
396         /* Handler class */
397         class MainHandler extends Handler
398         {
399                 public void handleMessage(android.os.Message msg)
400                 {
401                         switch (msg.what) {
402                                 case Task.REGISTER:
403                                         Main.this.onRegister((Task)msg.obj);
404                                         break;
405                                 case Task.MESSAGE:
406                                         Main.this.onMessage((Message)msg.obj);
407                                         break;
408                                 case Task.CONNECT:
409                                         Main.this.running = true;
410                                         break;
411                                 case Task.DISCONNECT:
412                                         Main.this.running = false;
413                                         break;
414                                 case Task.NOTIFY:
415                                         Main.this.onNotify((String)msg.obj);
416                                         break;
417                                 default:
418                                         Os.debug("Main: unknown message - " + msg.what);
419                                         break;
420                         }
421                 }
422         }
423 }