]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Main.java
Add support for SASL PLAIN authentication
[~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.method.ScrollingMovementMethod;
9 import android.view.Menu;
10 import android.view.MenuInflater;
11 import android.view.MenuItem;
12 import android.view.View;
13 import android.widget.Button;
14 import android.widget.EditText;
15 import android.widget.LinearLayout;
16 import android.widget.ScrollView;
17 import android.widget.TabHost;
18 import android.widget.TabWidget;
19 import android.widget.TextView;
20 import android.widget.Toast;
21
22 import android.preference.PreferenceActivity;
23
24 public class Main extends Activity
25 {
26         /* Static data */
27         private Handler      handler;
28         private Messenger    messenger;
29
30         /* Private data */
31         private Task         task;
32         private Toast        toast;
33         private boolean      ready;
34         private String       topic;
35         private String       names;
36
37         /* Widgets */
38         private TabHost      window;
39         private TabWidget    tabs;
40         private LinearLayout chat;
41         private TextView     log;
42         private EditText     input;
43         private Button       send;
44         private TextView     spades;
45         private TextView     debug;
46
47         private ScrollView   lscroll;
48         private ScrollView   dscroll;
49
50         /* Private helper methods */
51         private void notice(String text)
52         {
53                 this.log.append("*** " + text + "\n");
54         }
55
56         /* Private handler methods */
57         private void onRegister(Object obj)
58         {
59                 Os.debug("Main: onRegister");
60                 this.task = (Task)obj;
61         }
62
63         private void onMessage(Object obj)
64         {
65                 Message msg = (Message)obj;
66
67                 // Debug
68                 this.debug.append("> " + msg.line + "\n");
69                 this.dscroll.smoothScrollTo(0, this.debug.getBottom());
70
71                 // Chat
72                 switch (msg.type) {
73                         case PRIVMSG:
74                                 this.log.append(msg.from + ": " + msg.msg + "\n");
75                                 break;
76                         case TOPIC:
77                                 if (!msg.txt.equals(this.topic))
78                                         this.notice("Topic for " + msg.arg + ": " + msg.txt);
79                                 this.topic = msg.txt;
80                                 break;
81                         case NAMES:
82                                 if (!msg.txt.equals(this.names))
83                                         this.notice("Users in " + msg.arg + ": " + msg.txt);
84                                 this.names = msg.txt;
85                                 break;
86                         case ERROR:
87                                 this.notice("Error: " + msg.txt);
88                                 break;
89                         case AUTHOK:
90                                 this.notice("Authentication succeeded: " + msg.txt);
91                                 break;
92                         case AUTHFAIL:
93                                 this.notice("Authentication failed: " + msg.txt);
94                                 break;
95                 }
96                 this.lscroll.smoothScrollTo(0, this.log.getBottom());
97         }
98
99         private void onNotify(String text)
100         {
101                 Os.debug("Main: onNotify - " + text);
102                 this.notice(text);
103                 this.toast.setText(text);
104                 this.toast.show();
105         }
106
107         /* Private service methods */
108         private void startService()
109         {
110                 Os.debug("Main: startService");
111                 startService(new Intent(this, Task.class)
112                                 .putExtra("Messenger", this.messenger));
113         }
114
115         private void stopService()
116         {
117                 Os.debug("Main: stopService");
118                 stopService(new Intent(this, Task.class));
119         }
120
121         /* Widget callback functions */
122         public void onSend(View btn)
123         {
124                 if (this.task == null)
125                         return;
126                 String  txt = this.input.getText().toString();
127                 Message msg = this.task.send(txt);
128                 if (msg == null)
129                         return;
130                 this.input.setText("");
131         }
132
133         /* Activity Methods */
134         @Override
135         public void onCreate(Bundle savedInstanceState)
136         {
137                 try {
138                         super.onCreate(savedInstanceState);
139                         Os.debug("Main: onCreate");
140
141                         // Setup main layout
142                         this.setContentView(R.layout.main);
143
144                         // Setup toast
145                         this.toast     = Toast.makeText(this, "", Toast.LENGTH_SHORT);
146
147                         // Setup communication
148                         this.handler   = new MainHandler();
149                         this.messenger = new Messenger(this.handler);
150
151                         // Find widgets
152                         this.window    = (TabHost)      findViewById(android.R.id.tabhost);
153                         this.tabs      = (TabWidget)    findViewById(android.R.id.tabs);
154                         this.chat      = (LinearLayout) findViewById(R.id.chat);
155                         this.log       = (TextView)     findViewById(R.id.log);
156                         this.input     = (EditText)     findViewById(R.id.input);
157                         this.send      = (Button)       findViewById(R.id.send);
158                         this.spades    = (TextView)     findViewById(R.id.spades);
159                         this.debug     = (TextView)     findViewById(R.id.debug);
160
161                         this.lscroll   = (ScrollView)   findViewById(R.id.log_scroll);
162                         this.dscroll   = (ScrollView)   findViewById(R.id.debug_scroll);
163
164                         // Add window tabs
165                         this.window.setup();
166
167                         this.window.addTab(this.window
168                                         .newTabSpec("chat")
169                                         .setIndicator("Chat")
170                                         .setContent(R.id.chat));
171                         this.window.addTab(this.window
172                                         .newTabSpec("spades")
173                                         .setIndicator("Spades")
174                                         .setContent(R.id.spades));
175                         this.window.addTab(this.window
176                                         .newTabSpec("debug")
177                                         .setIndicator("Debug")
178                                         .setContent(R.id.debug));
179                 } catch (Exception e) {
180                         Os.debug("Error setting content view", e);
181                         return;
182                 }
183         }
184
185         @Override
186         public void onStart()
187         {
188                 super.onStart();
189                 Os.debug("Main: onStart");
190         }
191
192         @Override
193         public void onResume()
194         {
195                 super.onResume();
196                 Os.debug("Main: onResume");
197         }
198
199         @Override
200         public void onPause()
201         {
202                 super.onPause();
203                 Os.debug("Main: onPause");
204         }
205
206         @Override
207         public void onStop()
208         {
209                 super.onStop();
210                 Os.debug("Main: onStop");
211         }
212
213         @Override
214         public void onRestart()
215         {
216                 super.onRestart();
217                 Os.debug("Main: onRestart");
218         }
219
220         @Override
221         public void onDestroy()
222         {
223                 super.onDestroy();
224                 Os.debug("Main: onDestroy");
225         }
226
227         @Override
228         public boolean onCreateOptionsMenu(Menu menu)
229         {
230                 MenuInflater inflater = getMenuInflater();
231                 inflater.inflate(R.menu.main, menu);
232                 return true;
233         }
234
235         @Override
236         public boolean onPrepareOptionsMenu(Menu menu)
237         {
238                 menu.findItem(R.id.connect).setVisible(!this.ready);
239                 menu.findItem(R.id.disconnect).setVisible(this.ready);
240                 return true;
241         }
242
243         @Override
244         public boolean onOptionsItemSelected(MenuItem item)
245         {
246                 switch (item.getItemId()) {
247                         case R.id.connect:
248                                 this.startService();
249                                 return true;
250                         case R.id.disconnect:
251                                 this.stopService();
252                                 return true;
253                         case R.id.settings:
254                                 this.startActivity(new Intent(this, Prefs.class));
255                                 return true;
256                         case R.id.exit:
257                                 this.stopService();
258                                 this.finish();
259                                 return true;
260                         default:
261                                 return false;
262                 }
263         }
264
265         /* Handler class */
266         class MainHandler extends Handler
267         {
268                 public void handleMessage(android.os.Message msg)
269                 {
270                         switch (msg.what) {
271                                 case Task.REGISTER:
272                                         Main.this.onRegister(msg.obj);
273                                         break;
274                                 case Task.MESSAGE:
275                                         Main.this.onMessage(msg.obj);
276                                         break;
277                                 case Task.CONNECT:
278                                         Main.this.ready = true;
279                                         break;
280                                 case Task.DISCONNECT:
281                                         Main.this.ready = false;
282                                         break;
283                                 case Task.NOTIFY:
284                                         Main.this.onNotify((String)msg.obj);
285                                         break;
286                                 default:
287                                         Os.debug("Main: unknown message - " + msg.what);
288                                         break;
289                         }
290                 }
291         }
292 }