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