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