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