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