1 package de.hacksaar.andtuer;
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;
19 public class DoorActivity extends Activity implements View.OnClickListener {
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;
29 private Dialog askBooleanDialog() {
30 AlertDialog.Builder builder = new AlertDialog.Builder(this);
31 builder.setTitle(null);
32 builder.setMessage(pendingMessage);
34 builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
36 public void onClick(DialogInterface dialog, int whichButton) {
38 task.promptResult(true);
42 builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
44 public void onClick(DialogInterface dialog, int which) {
46 task.promptResult(false);
50 return builder.create();
53 private Dialog askStringDialog() {
54 AlertDialog.Builder builder = new AlertDialog.Builder(this);
55 builder.setTitle(null);
56 builder.setMessage(pendingMessage);
58 final EditText input = new EditText(this);
60 input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
61 builder.setView(input);
63 builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
65 public void onClick(DialogInterface dialog, int whichButton) {
67 String value = input.getText().toString();
68 task.promptResult(value);
72 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
74 public void onClick(DialogInterface dialog, int whichButton) {
76 task.promptResult(null);
80 return builder.create();
83 private void onBuzzClick() {
85 task.sendCommand("buzz");
90 public void onClick(View view) {
91 switch (view.getId()) {
92 case R.id.connect_button:
95 case R.id.buzz_button:
98 case R.id.open_button:
101 case R.id.close_button:
104 case R.id.disconnect_button:
110 private void onCloseClick() {
112 task.sendCommand("close");
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),
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);
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);
142 protected Dialog onCreateDialog(int id) {
145 return askBooleanDialog();
147 return askStringDialog();
149 return super.onCreateDialog(id);
154 public boolean onCreateOptionsMenu(Menu menu) {
155 getMenuInflater().inflate(R.menu.main, menu);
156 return super.onCreateOptionsMenu(menu);
159 private void onDisconnectClick() {
161 task.sendCommand("exit");
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);
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));
177 return super.onMenuItemSelected(featureId, item);
181 private void onOpenClick() {
183 task.sendCommand("open");
188 protected void onPrepareDialog(int id, Dialog dialog) {
191 ((TextView) dialog.findViewById(TEXT_ID)).setText("");
193 ((AlertDialog) dialog).setMessage(pendingMessage);
196 super.onPrepareDialog(id, dialog);
201 private class DialogPrompter implements AsyncTyshell.Prompter {
206 public void promptBoolean(String message) {
207 pendingMessage = message;
208 runOnUiThread(new Runnable() {
211 showDialog(DIALOG_BOOLEAN);
217 public void promptString(String message) {
218 pendingMessage = message;
219 runOnUiThread(new Runnable() {
222 showDialog(DIALOG_STRING);
228 public void sendMessage(final String message) {
229 runOnUiThread(new Runnable() {
232 Log.d(TAG, "Message: " + message);