]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Main.java
Add simple 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
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                 this.log.append(msg.from + ": " + msg.msg + "\n");
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 onOptionsItemSelected(MenuItem item)
190         {
191                 switch (item.getItemId()) {
192                         case R.id.connect:
193                                 this.startService();
194                                 return true;
195                         case R.id.disconnect:
196                                 this.stopService();
197                                 return true;
198                         case R.id.help:
199                                 Os.debug("Main: Help!");
200                                 return true;
201                         default:
202                                 return false;
203                 }
204         }
205
206         /* Handler class */
207         class MainHandler extends Handler
208         {
209                 public void handleMessage(android.os.Message msg)
210                 {
211                         switch (msg.what) {
212                                 case Task.REGISTER:
213                                         Main.this.onRegister(msg.obj);
214                                         break;
215                                 case Task.MESSAGE:
216                                         Main.this.onMessage(msg.obj);
217                                         break;
218                                 default:
219                                         Os.debug("Main: unknown message - " + msg.what);
220                                         break;
221                         }
222                 }
223         }
224 }
225