From 0f0f02776d0ad014225c7cbcf3056532fa69b17f Mon Sep 17 00:00:00 2001 From: Thialfihar Date: Sat, 17 Apr 2010 19:10:39 +0000 Subject: [PATCH] wrapped file dialogs into class in preparation for proper file picking --- .../thialfihar/android/apg/FileDialog.java | 42 +++++++ .../android/apg/PublicKeyListActivity.java | 84 ++++---------- .../android/apg/SecretKeyListActivity.java | 105 ++++-------------- 3 files changed, 90 insertions(+), 141 deletions(-) create mode 100644 src/org/thialfihar/android/apg/FileDialog.java diff --git a/src/org/thialfihar/android/apg/FileDialog.java b/src/org/thialfihar/android/apg/FileDialog.java new file mode 100644 index 000000000..ab1a6fc01 --- /dev/null +++ b/src/org/thialfihar/android/apg/FileDialog.java @@ -0,0 +1,42 @@ +package org.thialfihar.android.apg; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.widget.EditText; + +public class FileDialog { + + public static interface OnClickListener { + public void onCancelClick(); + public void onOkClick(String filename); + } + + public static AlertDialog build(Context context, String title, String message, + String defaultFile, OnClickListener onClickListener) { + AlertDialog.Builder alert = new AlertDialog.Builder(context); + + alert.setTitle(title); + alert.setMessage(message); + + final EditText input = new EditText(context); + input.setText(defaultFile); + alert.setView(input); + + final OnClickListener clickListener = onClickListener; + + alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + clickListener.onOkClick(input.getText().toString()); + } + }); + + alert.setNegativeButton(android.R.string.cancel, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + clickListener.onCancelClick(); + } + }); + return alert.create(); + } +} diff --git a/src/org/thialfihar/android/apg/PublicKeyListActivity.java b/src/org/thialfihar/android/apg/PublicKeyListActivity.java index d858908c9..50fe7422a 100644 --- a/src/org/thialfihar/android/apg/PublicKeyListActivity.java +++ b/src/org/thialfihar/android/apg/PublicKeyListActivity.java @@ -36,16 +36,13 @@ import android.os.Environment; import android.os.Handler; import android.os.Message; import android.view.ContextMenu; -import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo; -import android.view.View.OnKeyListener; import android.widget.BaseExpandableListAdapter; -import android.widget.EditText; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.TextView; @@ -302,50 +299,23 @@ public class PublicKeyListActivity extends ExpandableListActivity } case DIALOG_IMPORT_KEYS: { - AlertDialog.Builder alert = new AlertDialog.Builder(this); + return FileDialog.build(this, "Import Keys", + "Please specify which file to import from.", + Environment.getExternalStorageDirectory() + "/pubring.gpg", + new FileDialog.OnClickListener() { - alert.setTitle("Import Keys"); - alert.setMessage("Please specify which file to import from."); - - final EditText input = new EditText(this); - // TODO: default file? - input.setText(Environment.getExternalStorageDirectory() + "/pubring.gpg"); - input.setOnKeyListener(new OnKeyListener() { - public boolean onKey(View v, int keyCode, KeyEvent event) { - // TODO: this doesn't actually work yet - // If the event is a key-down event on the "enter" - // button - if ((event.getAction() == KeyEvent.ACTION_DOWN) && - (keyCode == KeyEvent.KEYCODE_ENTER)) { - try { - ((AlertDialog) v.getParent()) - .getButton(AlertDialog.BUTTON_POSITIVE) - .performClick(); - } catch (ClassCastException e) { - // don't do anything if we're not in that dialog - } - return true; - } - return false; - } - }); - alert.setView(input); - - alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { + @Override + public void onOkClick(String filename) { removeDialog(DIALOG_IMPORT_KEYS); - mImportFilename = input.getText().toString(); + mImportFilename = filename; importKeys(); } - }); - alert.setNegativeButton(android.R.string.cancel, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { + @Override + public void onCancelClick() { removeDialog(DIALOG_IMPORT_KEYS); } }); - return alert.create(); } case DIALOG_EXPORT_KEY: { @@ -354,38 +324,32 @@ public class PublicKeyListActivity extends ExpandableListActivity } case DIALOG_EXPORT_KEYS: { - AlertDialog.Builder alert = new AlertDialog.Builder(this); + String title = "Export Key"; - if (singleKeyExport) { - alert.setTitle("Export Key"); - } else { - alert.setTitle("Export Keys"); - mSelectedItem = -1; + if (!singleKeyExport) { + // plural "Keys" + title += "s"; } - final int thisDialogId = (singleKeyExport ? DIALOG_DELETE_KEY : DIALOG_EXPORT_KEYS); - alert.setMessage("Please specify which file to export to.\n" + - "WARNING! File will be overwritten if it exists."); + final int thisDialogId = (singleKeyExport ? DIALOG_EXPORT_KEY : DIALOG_EXPORT_KEYS); - final EditText input = new EditText(this); - // TODO: default file? - input.setText(Environment.getExternalStorageDirectory() + "/pubexport.asc"); - alert.setView(input); + return FileDialog.build(this, title, + "Please specify which file to export to.\n" + + "WARNING! File will be overwritten if it exists.", + Environment.getExternalStorageDirectory() + "/pubexport.asc", + new FileDialog.OnClickListener() { - alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { + @Override + public void onOkClick(String filename) { removeDialog(thisDialogId); - mExportFilename = input.getText().toString(); + mExportFilename = filename; exportKeys(); } - }); - alert.setNegativeButton(android.R.string.cancel, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { + @Override + public void onCancelClick() { removeDialog(thisDialogId); } }); - return alert.create(); } case DIALOG_IMPORTING: { diff --git a/src/org/thialfihar/android/apg/SecretKeyListActivity.java b/src/org/thialfihar/android/apg/SecretKeyListActivity.java index 69e8c8c7a..1560546d7 100644 --- a/src/org/thialfihar/android/apg/SecretKeyListActivity.java +++ b/src/org/thialfihar/android/apg/SecretKeyListActivity.java @@ -36,18 +36,14 @@ import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; -import android.util.Log; import android.view.ContextMenu; -import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo; -import android.view.View.OnKeyListener; import android.widget.BaseExpandableListAdapter; -import android.widget.EditText; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.TextView; @@ -337,51 +333,23 @@ public class SecretKeyListActivity extends ExpandableListActivity } case DIALOG_IMPORT_KEYS: { - AlertDialog.Builder alert = new AlertDialog.Builder(this); + return FileDialog.build(this, "Import Keys", + "Please specify which file to import from.", + Environment.getExternalStorageDirectory() + "/secring.gpg", + new FileDialog.OnClickListener() { - alert.setTitle("Import Keys"); - alert.setMessage("Please specify which file to import from."); - - final EditText input = new EditText(this); - // TODO: default file? - input.setText(Environment.getExternalStorageDirectory() + "/secring.gpg"); - input.setOnKeyListener(new OnKeyListener() { - public boolean onKey(View v, int keyCode, KeyEvent event) { - // TODO: this doesn't actually work yet - // If the event is a key-down event on the "enter" - // button - if ((event.getAction() == KeyEvent.ACTION_DOWN) && - (keyCode == KeyEvent.KEYCODE_ENTER)) { - try { - ((AlertDialog) v.getParent()) - .getButton(AlertDialog.BUTTON_POSITIVE) - .performClick(); - } catch (ClassCastException e) { - // don't do anything if we're not in that dialog - } - return true; - } - return false; - } - }); - alert.setView(input); - - alert.setPositiveButton(android.R.string.ok, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { + @Override + public void onOkClick(String filename) { removeDialog(DIALOG_IMPORT_KEYS); - mImportFilename = input.getText().toString(); + mImportFilename = filename; importKeys(); } - }); - alert.setNegativeButton(android.R.string.cancel, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { + @Override + public void onCancelClick() { removeDialog(DIALOG_IMPORT_KEYS); } }); - return alert.create(); } case DIALOG_EXPORT_KEY: { @@ -390,58 +358,33 @@ public class SecretKeyListActivity extends ExpandableListActivity } case DIALOG_EXPORT_KEYS: { - AlertDialog.Builder alert = new AlertDialog.Builder(this); + String title = "Export Key"; - if (singleKeyExport) { - alert.setTitle("Export Key"); - } else { - alert.setTitle("Export Keys"); - mSelectedItem = -1; + if (!singleKeyExport) { + // plural "Keys" + title += "s"; } final int thisDialogId = (singleKeyExport ? DIALOG_DELETE_KEY : DIALOG_EXPORT_KEYS); - alert.setMessage("Please specify which file to export to.\n" + - "WARNING! You are about to export a SECRET key.\n" + - "WARNING! File will be overwritten if it exists."); - final EditText input = new EditText(this); - // TODO: default file? - input.setText(Environment.getExternalStorageDirectory() + "/secexport.asc"); - input.setOnKeyListener(new OnKeyListener() { - public boolean onKey(View v, int keyCode, KeyEvent event) { - // TODO: this doesn't actually work yet - // If the event is a key-down event on the "enter" - // button - if ((event.getAction() == KeyEvent.ACTION_DOWN) && - (keyCode == KeyEvent.KEYCODE_ENTER)) { - try { - ((AlertDialog) v.getParent()) - .getButton(AlertDialog.BUTTON_POSITIVE) - .performClick(); - } catch (ClassCastException e) { - // don't do anything if we're not in that dialog - } - return true; - } - return false; - } - }); - alert.setView(input); + return FileDialog.build(this, title, + "Please specify which file to export to.\n" + + "WARNING! You are about to export SECRET keys.\n" + + "WARNING! File will be overwritten if it exists.", + Environment.getExternalStorageDirectory() + "/secexport.asc", + new FileDialog.OnClickListener() { - alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { + @Override + public void onOkClick(String filename) { removeDialog(thisDialogId); - mExportFilename = input.getText().toString(); + mExportFilename = filename; exportKeys(); } - }); - alert.setNegativeButton(android.R.string.cancel, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { + @Override + public void onCancelClick() { removeDialog(thisDialogId); } }); - return alert.create(); } case DIALOG_IMPORTING: {