Java/Android stuff (me coding dirty \o/)
[saartuer.git] / AndTuer / src / de / hacksaar / andtuer / DoorActivity.java
1 package de.hacksaar.andtuer;
2
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.app.Dialog;
6 import android.content.DialogInterface;
7 import android.content.Intent;
8 import android.content.SharedPreferences;
9 import android.os.Bundle;
10 import android.preference.PreferenceManager;
11 import android.text.InputType;
12 import android.util.Log;
13 import android.view.Menu;
14 import android.view.MenuItem;
15 import android.view.View;
16 import android.widget.EditText;
17 import android.widget.TextView;
18
19 public class DoorActivity extends Activity implements View.OnClickListener {
20
21         private static final String TAG = "DoorActivity";
22         private static final int DIALOG_BOOLEAN = 7;
23         private static final int DIALOG_STRING = 14;
24         private static final int TEXT_ID = 42;
25         private final AsyncTyshell.Prompter prompter = new DialogPrompter();
26         private AsyncTyshell task;
27         private String pendingMessage;
28
29         private Dialog askBooleanDialog() {
30                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
31                 builder.setTitle(null);
32                 builder.setMessage(pendingMessage);
33
34                 builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
35                         @Override
36                         public void onClick(DialogInterface dialog, int whichButton) {
37                                 assert task != null;
38                                 task.promptResult(true);
39                         }
40                 });
41
42                 builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
43                         @Override
44                         public void onClick(DialogInterface dialog, int which) {
45                                 assert task != null;
46                                 task.promptResult(false);
47                         }
48                 });
49
50                 return builder.create();
51         }
52
53         private Dialog askStringDialog() {
54                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
55                 builder.setTitle(null);
56                 builder.setMessage(pendingMessage);
57
58                 final EditText input = new EditText(this);
59                 input.setId(TEXT_ID);
60                 input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
61                 builder.setView(input);
62
63                 builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
64                         @Override
65                         public void onClick(DialogInterface dialog, int whichButton) {
66                                 assert task != null;
67                                 String value = input.getText().toString();
68                                 task.promptResult(value);
69                         }
70                 });
71
72                 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
73                         @Override
74                         public void onClick(DialogInterface dialog, int whichButton) {
75                                 assert task != null;
76                                 task.promptResult(null);
77                         }
78                 });
79
80                 return builder.create();
81         }
82
83         private void onBuzzClick() {
84                 if (task != null) {
85                         task.sendCommand("buzz");
86                 }
87         }
88
89         @Override
90         public void onClick(View view) {
91                 switch (view.getId()) {
92                         case R.id.connect_button:
93                                 onConnectClick();
94                                 break;
95                         case R.id.buzz_button:
96                                 onBuzzClick();
97                                 break;
98                         case R.id.open_button:
99                                 onOpenClick();
100                                 break;
101                         case R.id.close_button:
102                                 onCloseClick();
103                                 break;
104                         case R.id.disconnect_button:
105                                 onDisconnectClick();
106                                 break;
107                 }
108         }
109
110         private void onCloseClick() {
111                 if (task != null) {
112                         task.sendCommand("close");
113                 }
114         }
115
116         private void onConnectClick() {
117                 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
118                 task = new AsyncTyshell(preferences.getString(DoorSettings.PREF_SERVER_HOSTNAME, DoorSettings.DEFAULT_HOST),
119                                                                 preferences.getInt(DoorSettings.PREF_SERVER_PORT, DoorSettings.DEFAULT_PORT),
120                                                                 preferences.getString(DoorSettings.PREF_USER_USERNAME, null),
121                                                                 preferences.getString(DoorSettings.PREF_USER_KEYFILE, DoorSettings.DEFAULT_KEYFILE),
122                                                                 prompter);
123                 task.execute();
124                 findViewById(R.id.open_button).setEnabled(true);
125                 findViewById(R.id.close_button).setEnabled(true);
126                 findViewById(R.id.buzz_button).setEnabled(true);
127                 findViewById(R.id.disconnect_button).setEnabled(true);
128         }
129
130         @Override
131         public void onCreate(Bundle savedInstanceState) {
132                 super.onCreate(savedInstanceState);
133                 setContentView(R.layout.door);
134                 findViewById(R.id.open_button).setOnClickListener(this);
135                 findViewById(R.id.close_button).setOnClickListener(this);
136                 findViewById(R.id.buzz_button).setOnClickListener(this);
137                 findViewById(R.id.disconnect_button).setOnClickListener(this);
138                 findViewById(R.id.connect_button).setOnClickListener(this);
139         }
140
141         @Override
142         protected Dialog onCreateDialog(int id) {
143                 switch (id) {
144                         case DIALOG_BOOLEAN:
145                                 return askBooleanDialog();
146                         case DIALOG_STRING:
147                                 return askStringDialog();
148                         default:
149                                 return super.onCreateDialog(id);
150                 }
151         }
152
153         @Override
154         public boolean onCreateOptionsMenu(Menu menu) {
155                 getMenuInflater().inflate(R.menu.main, menu);
156                 return super.onCreateOptionsMenu(menu);
157         }
158
159         private void onDisconnectClick() {
160                 if (task != null) {
161                         task.sendCommand("exit");
162                         task.disconnect();
163                 }
164                 findViewById(R.id.open_button).setEnabled(false);
165                 findViewById(R.id.close_button).setEnabled(false);
166                 findViewById(R.id.buzz_button).setEnabled(false);
167                 findViewById(R.id.disconnect_button).setEnabled(false);
168         }
169
170         @Override
171         public boolean onMenuItemSelected(int featureId, MenuItem item) {
172                 switch (item.getItemId()) {
173                         case R.id.menu_settings:
174                                 startActivity(new Intent(this, DoorSettings.class));
175                                 return true;
176                         default:
177                                 return super.onMenuItemSelected(featureId, item);
178                 }
179         }
180
181         private void onOpenClick() {
182                 if (task != null) {
183                         task.sendCommand("open");
184                 }
185         }
186
187         @Override
188         protected void onPrepareDialog(int id, Dialog dialog) {
189                 switch (id) {
190                         case DIALOG_STRING:
191                                 ((TextView) dialog.findViewById(TEXT_ID)).setText("");
192                         case DIALOG_BOOLEAN:
193                                 ((AlertDialog) dialog).setMessage(pendingMessage);
194                                 break;
195                         default:
196                                 super.onPrepareDialog(id, dialog);
197                 }
198
199         }
200
201         private class DialogPrompter implements AsyncTyshell.Prompter {
202                 DialogPrompter() {
203                 }
204
205                 @Override
206                 public void promptBoolean(String message) {
207                         pendingMessage = message;
208                         runOnUiThread(new Runnable() {
209                                 @Override
210                                 public void run() {
211                                         showDialog(DIALOG_BOOLEAN);
212                                 }
213                         });
214                 }
215
216                 @Override
217                 public void promptString(String message) {
218                         pendingMessage = message;
219                         runOnUiThread(new Runnable() {
220                                 @Override
221                                 public void run() {
222                                         showDialog(DIALOG_STRING);
223                                 }
224                         });
225                 }
226
227                 @Override
228                 public void sendMessage(final String message) {
229                         runOnUiThread(new Runnable() {
230                                 @Override
231                                 public void run() {
232                                         Log.d(TAG, "Message: " + message);
233                                 }
234                         });
235                 }
236         }
237 }