]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Main.java
Update preferences
[~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         }
195
196         private void exit()
197         {
198                 stopService(new Intent(this, Task.class));
199         }
200
201         /* Widget callback functions */
202         public void onSend(View btn)
203         {
204                 if (this.task == null)
205                         return;
206                 String  txt = this.input.getText().toString();
207                 Message msg = this.task.send(txt);
208                 if (msg == null)
209                         return;
210                 this.input.setText("");
211         }
212
213         /* Activity Methods */
214         @Override
215         public void onCreate(Bundle savedInstanceState)
216         {
217                 try {
218                         super.onCreate(savedInstanceState);
219                         Os.debug("Main: onCreate");
220
221                         // Setup preferences
222                         PreferenceManager.setDefaultValues(this, R.xml.prefs, false);
223
224                         // Setup main layout
225                         this.setContentView(R.layout.main);
226
227                         // Setup toast
228                         this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
229
230                         // Setup communication
231                         this.handler   = new MainHandler();
232                         this.messenger = new Messenger(this.handler);
233
234                         // Find widgets
235                         this.window    = (TabHost)      findViewById(android.R.id.tabhost);
236                         this.tabs      = (TabWidget)    findViewById(android.R.id.tabs);
237                         this.chat      = (LinearLayout) findViewById(R.id.chat);
238                         this.log       = (TextView)     findViewById(R.id.log);
239                         this.input     = (EditText)     findViewById(R.id.input);
240                         this.send      = (Button)       findViewById(R.id.send);
241                         this.spades    = (LinearLayout) findViewById(R.id.spades);
242                         this.debug     = (TextView)     findViewById(R.id.debug);
243
244                         this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
245                         this.dscroll   = (ScrollView)   findViewById(R.id.debug_scroll);
246
247                         // Add window tabs
248                         this.window.setup();
249
250                         this.window.addTab(this.window
251                                         .newTabSpec("chat")
252                                         .setIndicator("Chat")
253                                         .setContent(R.id.chat));
254                         this.window.addTab(this.window
255                                         .newTabSpec("spades")
256                                         .setIndicator("Spades")
257                                         .setContent(R.id.spades));
258                         this.window.addTab(this.window
259                                         .newTabSpec("debug")
260                                         .setIndicator("Debug")
261                                         .setContent(R.id.debug));
262
263                         // Setup Spades game and cards view
264                         this.game  = new Spades(PreferenceManager
265                                         .getDefaultSharedPreferences(this)
266                                         .getString("pref_referee", "rhawk"));
267                         this.cards = new Cards(this);
268
269                         this.game.cards = this.cards;
270                         this.cards.game = this.game;
271
272                         this.spades.addView(cards);
273
274                         // Attach to background service
275                         this.register();
276
277                 } catch (Exception e) {
278                         Os.debug("Error setting content view", e);
279                         return;
280                 }
281         }
282
283         @Override
284         public void onStart()
285         {
286                 super.onStart();
287                 Os.debug("Main: onStart");
288         }
289
290         @Override
291         public void onResume()
292         {
293                 super.onResume();
294                 Os.debug("Main: onResume");
295         }
296
297         @Override
298         public void onPause()
299         {
300                 super.onPause();
301                 Os.debug("Main: onPause");
302         }
303
304         @Override
305         public void onStop()
306         {
307                 super.onStop();
308                 Os.debug("Main: onStop");
309         }
310
311         @Override
312         public void onRestart()
313         {
314                 super.onRestart();
315                 Os.debug("Main: onRestart");
316         }
317
318         @Override
319         public void onDestroy()
320         {
321                 super.onDestroy();
322                 Os.debug("Main: onDestroy");
323         }
324
325         @Override
326         public boolean onCreateOptionsMenu(Menu menu)
327         {
328                 MenuInflater inflater = getMenuInflater();
329                 inflater.inflate(R.menu.main, menu);
330                 return true;
331         }
332
333         @Override
334         public boolean onPrepareOptionsMenu(Menu menu)
335         {
336                 menu.findItem(R.id.connect).setVisible(!this.running);
337                 menu.findItem(R.id.disconnect).setVisible(this.running);
338                 return true;
339         }
340
341         @Override
342         public boolean onOptionsItemSelected(MenuItem item)
343         {
344                 switch (item.getItemId()) {
345                         case R.id.connect:
346                                 this.connect();
347                                 return true;
348                         case R.id.disconnect:
349                                 this.disconnect();
350                                 return true;
351                         case R.id.settings:
352                                 this.startActivity(new Intent(this, Prefs.class));
353                                 return true;
354                         case R.id.exit:
355                                 this.exit();
356                                 this.finish();
357                                 return true;
358                         default:
359                                 return false;
360                 }
361         }
362
363         /* Handler class */
364         class MainHandler extends Handler
365         {
366                 public void handleMessage(android.os.Message msg)
367                 {
368                         switch (msg.what) {
369                                 case Task.REGISTER:
370                                         Main.this.onRegister((Task)msg.obj);
371                                         break;
372                                 case Task.MESSAGE:
373                                         Main.this.onMessage((Message)msg.obj);
374                                         break;
375                                 case Task.CONNECT:
376                                         Main.this.running = true;
377                                         break;
378                                 case Task.DISCONNECT:
379                                         Main.this.running = false;
380                                         break;
381                                 case Task.NOTIFY:
382                                         Main.this.onNotify((String)msg.obj);
383                                         break;
384                                 default:
385                                         Os.debug("Main: unknown message - " + msg.what);
386                                         break;
387                         }
388                 }
389         }
390 }