wrapped file dialogs into class in preparation for proper file picking

This commit is contained in:
Thialfihar 2010-04-17 19:10:39 +00:00
parent 4fc97b90fc
commit 0f0f02776d
3 changed files with 90 additions and 141 deletions

View File

@ -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();
}
}

View File

@ -36,16 +36,13 @@ import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo; import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnKeyListener;
import android.widget.BaseExpandableListAdapter; import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView; import android.widget.ExpandableListView;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@ -302,50 +299,23 @@ public class PublicKeyListActivity extends ExpandableListActivity
} }
case DIALOG_IMPORT_KEYS: { 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"); @Override
alert.setMessage("Please specify which file to import from."); public void onOkClick(String filename) {
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) {
removeDialog(DIALOG_IMPORT_KEYS); removeDialog(DIALOG_IMPORT_KEYS);
mImportFilename = input.getText().toString(); mImportFilename = filename;
importKeys(); importKeys();
} }
});
alert.setNegativeButton(android.R.string.cancel, @Override
new DialogInterface.OnClickListener() { public void onCancelClick() {
public void onClick(DialogInterface dialog, int id) {
removeDialog(DIALOG_IMPORT_KEYS); removeDialog(DIALOG_IMPORT_KEYS);
} }
}); });
return alert.create();
} }
case DIALOG_EXPORT_KEY: { case DIALOG_EXPORT_KEY: {
@ -354,38 +324,32 @@ public class PublicKeyListActivity extends ExpandableListActivity
} }
case DIALOG_EXPORT_KEYS: { case DIALOG_EXPORT_KEYS: {
AlertDialog.Builder alert = new AlertDialog.Builder(this); String title = "Export Key";
if (singleKeyExport) { if (!singleKeyExport) {
alert.setTitle("Export Key"); // plural "Keys"
} else { title += "s";
alert.setTitle("Export Keys");
mSelectedItem = -1;
} }
final int thisDialogId = (singleKeyExport ? DIALOG_DELETE_KEY : DIALOG_EXPORT_KEYS); final int thisDialogId = (singleKeyExport ? DIALOG_EXPORT_KEY : DIALOG_EXPORT_KEYS);
alert.setMessage("Please specify which file to export to.\n" +
"WARNING! File will be overwritten if it exists.");
final EditText input = new EditText(this); return FileDialog.build(this, title,
// TODO: default file? "Please specify which file to export to.\n" +
input.setText(Environment.getExternalStorageDirectory() + "/pubexport.asc"); "WARNING! File will be overwritten if it exists.",
alert.setView(input); Environment.getExternalStorageDirectory() + "/pubexport.asc",
new FileDialog.OnClickListener() {
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int id) { public void onOkClick(String filename) {
removeDialog(thisDialogId); removeDialog(thisDialogId);
mExportFilename = input.getText().toString(); mExportFilename = filename;
exportKeys(); exportKeys();
} }
});
alert.setNegativeButton(android.R.string.cancel, @Override
new DialogInterface.OnClickListener() { public void onCancelClick() {
public void onClick(DialogInterface dialog, int id) {
removeDialog(thisDialogId); removeDialog(thisDialogId);
} }
}); });
return alert.create();
} }
case DIALOG_IMPORTING: { case DIALOG_IMPORTING: {

View File

@ -36,18 +36,14 @@ import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo; import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnKeyListener;
import android.widget.BaseExpandableListAdapter; import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView; import android.widget.ExpandableListView;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@ -337,51 +333,23 @@ public class SecretKeyListActivity extends ExpandableListActivity
} }
case DIALOG_IMPORT_KEYS: { 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"); @Override
alert.setMessage("Please specify which file to import from."); public void onOkClick(String filename) {
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) {
removeDialog(DIALOG_IMPORT_KEYS); removeDialog(DIALOG_IMPORT_KEYS);
mImportFilename = input.getText().toString(); mImportFilename = filename;
importKeys(); importKeys();
} }
});
alert.setNegativeButton(android.R.string.cancel, @Override
new DialogInterface.OnClickListener() { public void onCancelClick() {
public void onClick(DialogInterface dialog, int id) {
removeDialog(DIALOG_IMPORT_KEYS); removeDialog(DIALOG_IMPORT_KEYS);
} }
}); });
return alert.create();
} }
case DIALOG_EXPORT_KEY: { case DIALOG_EXPORT_KEY: {
@ -390,58 +358,33 @@ public class SecretKeyListActivity extends ExpandableListActivity
} }
case DIALOG_EXPORT_KEYS: { case DIALOG_EXPORT_KEYS: {
AlertDialog.Builder alert = new AlertDialog.Builder(this); String title = "Export Key";
if (singleKeyExport) { if (!singleKeyExport) {
alert.setTitle("Export Key"); // plural "Keys"
} else { title += "s";
alert.setTitle("Export Keys");
mSelectedItem = -1;
} }
final int thisDialogId = (singleKeyExport ? DIALOG_DELETE_KEY : DIALOG_EXPORT_KEYS); 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); return FileDialog.build(this, title,
// TODO: default file? "Please specify which file to export to.\n" +
input.setText(Environment.getExternalStorageDirectory() + "/secexport.asc"); "WARNING! You are about to export SECRET keys.\n" +
input.setOnKeyListener(new OnKeyListener() { "WARNING! File will be overwritten if it exists.",
public boolean onKey(View v, int keyCode, KeyEvent event) { Environment.getExternalStorageDirectory() + "/secexport.asc",
// TODO: this doesn't actually work yet new FileDialog.OnClickListener() {
// 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() { @Override
public void onClick(DialogInterface dialog, int id) { public void onOkClick(String filename) {
removeDialog(thisDialogId); removeDialog(thisDialogId);
mExportFilename = input.getText().toString(); mExportFilename = filename;
exportKeys(); exportKeys();
} }
});
alert.setNegativeButton(android.R.string.cancel, @Override
new DialogInterface.OnClickListener() { public void onCancelClick() {
public void onClick(DialogInterface dialog, int id) {
removeDialog(thisDialogId); removeDialog(thisDialogId);
} }
}); });
return alert.create();
} }
case DIALOG_IMPORTING: { case DIALOG_IMPORTING: {