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