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 TextView logText;
27 private AsyncTyshell task;
28 private String pendingMessage;
30 private Dialog askBooleanDialog() {
31 AlertDialog.Builder builder = new AlertDialog.Builder(this);
32 builder.setTitle(null);
33 builder.setMessage(pendingMessage);
35 builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
37 public void onClick(DialogInterface dialog, int whichButton) {
39 task.promptResult(true);
43 builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
45 public void onClick(DialogInterface dialog, int which) {
47 task.promptResult(false);
51 return builder.create();
54 private Dialog askStringDialog() {
55 AlertDialog.Builder builder = new AlertDialog.Builder(this);
56 builder.setTitle(null);
57 builder.setMessage(pendingMessage);
59 final EditText input = new EditText(this);
61 input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
62 builder.setView(input);
64 builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
66 public void onClick(DialogInterface dialog, int whichButton) {
68 String value = input.getText().toString();
69 task.promptResult(value);
73 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
75 public void onClick(DialogInterface dialog, int whichButton) {
77 task.promptResult(null);
81 return builder.create();
84 private void onBuzzClick() {
86 task.sendCommand("buzz");
91 public void onClick(View view) {
92 switch (view.getId()) {
93 case R.id.connect_button:
96 case R.id.buzz_button:
99 case R.id.unlock_button:
102 case R.id.disconnect_button:
108 private void onCloseClick() {
110 task.sendCommand("close");
114 private void onConnectClick() {
115 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
116 task = new AsyncTyshell(preferences.getString(DoorSettings.PREF_SERVER_HOSTNAME, DoorSettings.DEFAULT_HOST),
117 preferences.getInt(DoorSettings.PREF_SERVER_PORT, DoorSettings.DEFAULT_PORT),
118 preferences.getString(DoorSettings.PREF_USER_USERNAME, null),
119 preferences.getString(DoorSettings.PREF_USER_KEYFILE, DoorSettings.DEFAULT_KEYFILE),
122 findViewById(R.id.connect_button).setEnabled(false);
126 public void onCreate(Bundle savedInstanceState) {
127 super.onCreate(savedInstanceState);
128 setContentView(R.layout.door);
129 for (int view : new int[]{R.id.unlock_button, R.id.buzz_button, R.id.disconnect_button, R.id.connect_button}) {
130 findViewById(view).setOnClickListener(this);
132 logText = (TextView) findViewById(R.id.logout);
134 logText.setText("Welcome to Hacksaar AndTuer, Version " +
135 getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
136 } catch (Exception e) {
137 logText.setText("Welcome to Hacksaar AndTuer, [unknown version]");
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.unlock_button).setEnabled(false);
165 findViewById(R.id.buzz_button).setEnabled(false);
166 findViewById(R.id.disconnect_button).setEnabled(false);
167 findViewById(R.id.connect_button).setEnabled(true);
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);
182 protected void onPrepareDialog(int id, Dialog dialog) {
185 ((TextView) dialog.findViewById(TEXT_ID)).setText("");
187 ((AlertDialog) dialog).setMessage(pendingMessage);
190 super.onPrepareDialog(id, dialog);
195 private void onUnlockClick() {
197 task.sendCommand("unlock");
201 private void writeLog(String msg) {
202 Log.d(TAG, "Log: " + msg);
203 logText.setText(logText.getText() + "\n" + msg);
205 findViewById(R.id.unlock_button).setEnabled(true);
206 findViewById(R.id.buzz_button).setEnabled(true);
207 findViewById(R.id.disconnect_button).setEnabled(true);
210 private class DialogPrompter implements AsyncTyshell.Prompter {
215 public void promptBoolean(String message) {
216 pendingMessage = message;
217 runOnUiThread(new Runnable() {
220 showDialog(DIALOG_BOOLEAN);
226 public void promptString(String message) {
227 pendingMessage = message;
228 runOnUiThread(new Runnable() {
231 showDialog(DIALOG_STRING);
237 public void sendMessage(final String message) {
238 runOnUiThread(new Runnable() {