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