]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Main.java
Add initial OpenGL example
[~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      ready;
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(Object obj)
114         {
115                 Os.debug("Main: onRegister");
116                 this.task = (Task)obj;
117         }
118
119         private void onMessage(Object obj)
120         {
121                 Message msg = (Message)obj;
122
123                 // Debug
124                 this.debug.append("> " + msg.line + "\n");
125                 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
126
127                 // Chat
128                 switch (msg.type) {
129                         case PRIVMSG:
130                                 this.display(msg);
131                                 break;
132                         case TOPIC:
133                                 if (!msg.txt.equals(this.topic))
134                                         this.notice("Topic for " + msg.arg + ": " + msg.txt);
135                                 this.topic = msg.txt;
136                                 break;
137                         case NAMES:
138                                 if (!msg.txt.equals(this.names))
139                                         this.notice("Users in " + msg.arg + ": " + msg.txt);
140                                 this.names = msg.txt;
141                                 break;
142                         case ERROR:
143                                 this.notice("Error: " + msg.txt);
144                                 break;
145                         case AUTHOK:
146                                 this.notice("Authentication succeeded: " + msg.txt);
147                                 break;
148                         case AUTHFAIL:
149                                 this.notice("Authentication failed: " + msg.txt);
150                                 break;
151                 }
152                 this.lscroll.smoothScrollTo(0, this.log.getBottom());
153         }
154
155         private void onNotify(String text)
156         {
157                 Os.debug("Main: onNotify - " + text);
158                 this.notice(text);
159                 this.toast.setText(text);
160                 this.toast.show();
161         }
162
163         /* Private service methods */
164         private void startService()
165         {
166                 Os.debug("Main: startService");
167                 startService(new Intent(this, Task.class)
168                                 .putExtra("Messenger", this.messenger));
169         }
170
171         private void stopService()
172         {
173                 Os.debug("Main: stopService");
174                 stopService(new Intent(this, Task.class));
175         }
176
177         /* Widget callback functions */
178         public void onSend(View btn)
179         {
180                 if (this.task == null)
181                         return;
182                 String  txt = this.input.getText().toString();
183                 Message msg = this.task.send(txt);
184                 if (msg == null)
185                         return;
186                 this.input.setText("");
187         }
188
189         /* Activity Methods */
190         @Override
191         public void onCreate(Bundle savedInstanceState)
192         {
193                 try {
194                         super.onCreate(savedInstanceState);
195                         Os.debug("Main: onCreate");
196
197                         // Setup main layout
198                         this.setContentView(R.layout.main);
199
200                         // Setup toast
201                         this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
202
203                         // Setup communication
204                         this.handler   = new MainHandler();
205                         this.messenger = new Messenger(this.handler);
206
207                         // Find widgets
208                         this.window    = (TabHost)      findViewById(android.R.id.tabhost);
209                         this.tabs      = (TabWidget)    findViewById(android.R.id.tabs);
210                         this.chat      = (LinearLayout) findViewById(R.id.chat);
211                         this.log       = (TextView)     findViewById(R.id.log);
212                         this.input     = (EditText)     findViewById(R.id.input);
213                         this.send      = (Button)       findViewById(R.id.send);
214                         this.spades    = (LinearLayout) findViewById(R.id.spades);
215                         this.debug     = (TextView)     findViewById(R.id.debug);
216
217                         this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
218                         this.dscroll   = (ScrollView)   findViewById(R.id.debug_scroll);
219
220                         // Add window tabs
221                         this.window.setup();
222
223                         this.window.addTab(this.window
224                                         .newTabSpec("chat")
225                                         .setIndicator("Chat")
226                                         .setContent(R.id.chat));
227                         this.window.addTab(this.window
228                                         .newTabSpec("spades")
229                                         .setIndicator("Spades")
230                                         .setContent(R.id.spades));
231                         this.window.addTab(this.window
232                                         .newTabSpec("debug")
233                                         .setIndicator("Debug")
234                                         .setContent(R.id.debug));
235
236                         // Setup OpenGL view
237                         this.cards = new Cards(this);
238                         this.spades.addView(cards);
239                         
240                 } catch (Exception e) {
241                         Os.debug("Error setting content view", e);
242                         return;
243                 }
244         }
245
246         @Override
247         public void onStart()
248         {
249                 super.onStart();
250                 Os.debug("Main: onStart");
251         }
252
253         @Override
254         public void onResume()
255         {
256                 super.onResume();
257                 Os.debug("Main: onResume");
258         }
259
260         @Override
261         public void onPause()
262         {
263                 super.onPause();
264                 Os.debug("Main: onPause");
265         }
266
267         @Override
268         public void onStop()
269         {
270                 super.onStop();
271                 Os.debug("Main: onStop");
272         }
273
274         @Override
275         public void onRestart()
276         {
277                 super.onRestart();
278                 Os.debug("Main: onRestart");
279         }
280
281         @Override
282         public void onDestroy()
283         {
284                 super.onDestroy();
285                 Os.debug("Main: onDestroy");
286         }
287
288         @Override
289         public boolean onCreateOptionsMenu(Menu menu)
290         {
291                 MenuInflater inflater = getMenuInflater();
292                 inflater.inflate(R.menu.main, menu);
293                 return true;
294         }
295
296         @Override
297         public boolean onPrepareOptionsMenu(Menu menu)
298         {
299                 menu.findItem(R.id.connect).setVisible(!this.ready);
300                 menu.findItem(R.id.disconnect).setVisible(this.ready);
301                 return true;
302         }
303
304         @Override
305         public boolean onOptionsItemSelected(MenuItem item)
306         {
307                 switch (item.getItemId()) {
308                         case R.id.connect:
309                                 this.startService();
310                                 return true;
311                         case R.id.disconnect:
312                                 this.stopService();
313                                 return true;
314                         case R.id.settings:
315                                 this.startActivity(new Intent(this, Prefs.class));
316                                 return true;
317                         case R.id.exit:
318                                 this.stopService();
319                                 this.finish();
320                                 return true;
321                         default:
322                                 return false;
323                 }
324         }
325
326         /* Handler class */
327         class MainHandler extends Handler
328         {
329                 public void handleMessage(android.os.Message msg)
330                 {
331                         switch (msg.what) {
332                                 case Task.REGISTER:
333                                         Main.this.onRegister(msg.obj);
334                                         break;
335                                 case Task.MESSAGE:
336                                         Main.this.onMessage(msg.obj);
337                                         break;
338                                 case Task.CONNECT:
339                                         Main.this.ready = true;
340                                         break;
341                                 case Task.DISCONNECT:
342                                         Main.this.ready = false;
343                                         break;
344                                 case Task.NOTIFY:
345                                         Main.this.onNotify((String)msg.obj);
346                                         break;
347                                 default:
348                                         Os.debug("Main: unknown message - " + msg.what);
349                                         break;
350                         }
351                 }
352         }
353 }