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