]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Main.java
Change Exit to Quit and return to home screen
[~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 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 }