use OI File Manager intents to handle open/save file selection

This commit is contained in:
Thialfihar
2010-04-17 23:36:47 +00:00
parent de6743e4f5
commit 09741b0286
9 changed files with 261 additions and 22 deletions

View File

@@ -16,34 +16,66 @@
package org.thialfihar.android.apg;
import org.openintents.intents.FileManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class FileDialog {
public static final int REQUEST_CODE_PICK_FILE_OR_DIRECTORY = 12345;
private static EditText mInput;
private static ImageButton mBrowse;
private static Activity mActivity;
private static String mFileManagerTitle;
private static String mFileManagerButton;
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);
public static AlertDialog build(Activity activity, String title, String message,
String defaultFile, OnClickListener onClickListener,
String fileManagerTitle, String fileManagerButton) {
LayoutInflater inflater =
(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle(title);
alert.setMessage(message);
final EditText input = new EditText(context);
input.setText(defaultFile);
alert.setView(input);
View view = (View) inflater.inflate(R.layout.file_dialog, null);
mActivity = activity;
mInput = (EditText) view.findViewById(R.id.input);
mInput.setText(defaultFile);
mBrowse = (ImageButton) view.findViewById(R.id.btn_browse);
mBrowse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFile();
}
});
mFileManagerTitle = fileManagerTitle;
mFileManagerButton = fileManagerButton;
alert.setView(view);
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());
clickListener.onOkClick(mInput.getText().toString());
}
});
@@ -55,4 +87,31 @@ public class FileDialog {
});
return alert.create();
}
public static void setFilename(String filename) {
if (mInput != null) {
mInput.setText(filename);
}
}
/**
* Opens the file manager to select a file to open.
*/
private static void openFile() {
String fileName = mInput.getText().toString();
Intent intent = new Intent(FileManager.ACTION_PICK_FILE);
intent.setData(Uri.parse("file://" + fileName));
intent.putExtra(FileManager.EXTRA_TITLE, mFileManagerTitle);
intent.putExtra(FileManager.EXTRA_BUTTON_TEXT, mFileManagerButton);
try {
mActivity.startActivityForResult(intent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY);
} catch (ActivityNotFoundException e) {
// No compatible file manager was found.
Toast.makeText(mActivity, R.string.no_filemanager_installed, Toast.LENGTH_SHORT).show();
}
}
}

View File

@@ -72,6 +72,8 @@ public class MainActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Apg.initialize(this);
Button encryptMessageButton = (Button) findViewById(R.id.btn_encryptMessage);
Button decryptMessageButton = (Button) findViewById(R.id.btn_decryptMessage);
mAccounts = (ListView) findViewById(R.id.account_list);
@@ -220,9 +222,7 @@ public class MainActivity extends Activity {
SpannableString info =
new SpannableString("Read the warnings!\n\n" +
"Changes:\n" +
" * display signed-only mails\n" +
" * verify signed-only mails\n" +
" * bug fixes, layout fixes\n" +
" * OI File Manager support\n" +
"\n" +
"WARNING: be careful editing your existing keys, as they " +
"WILL be stripped of certificates right now.\n" +

View File

@@ -31,10 +31,13 @@ import android.app.ExpandableListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
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.LayoutInflater;
import android.view.Menu;
@@ -72,13 +75,14 @@ public class PublicKeyListActivity extends ExpandableListActivity
static final int TASK_EXPORT = 2;
protected int mSelectedItem = -1;
protected String mImportFilename = null;
protected String mExportFilename = null;
protected int mTask = 0;
private ProgressDialog mProgressDialog = null;
private Thread mRunningThread = null;
private String mImportFilename = Environment.getExternalStorageDirectory() + "/pubring.gpg";
private String mExportFilename = Environment.getExternalStorageDirectory() + "/pubexport.asc";
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
@@ -301,7 +305,7 @@ public class PublicKeyListActivity extends ExpandableListActivity
case DIALOG_IMPORT_KEYS: {
return FileDialog.build(this, "Import Keys",
"Please specify which file to import from.",
Environment.getExternalStorageDirectory() + "/pubring.gpg",
mImportFilename,
new FileDialog.OnClickListener() {
@Override
@@ -315,7 +319,9 @@ public class PublicKeyListActivity extends ExpandableListActivity
public void onCancelClick() {
removeDialog(DIALOG_IMPORT_KEYS);
}
});
},
getString(R.string.filemanager_title_open),
getString(R.string.filemanager_btn_open));
}
case DIALOG_EXPORT_KEY: {
@@ -335,7 +341,7 @@ public class PublicKeyListActivity extends ExpandableListActivity
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",
mExportFilename,
new FileDialog.OnClickListener() {
@Override
@@ -349,7 +355,9 @@ public class PublicKeyListActivity extends ExpandableListActivity
public void onCancelClick() {
removeDialog(thisDialogId);
}
});
},
getString(R.string.filemanager_title_save),
getString(R.string.filemanager_btn_save));
}
case DIALOG_IMPORTING: {
@@ -621,4 +629,32 @@ public class PublicKeyListActivity extends ExpandableListActivity
return view;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FileDialog.REQUEST_CODE_PICK_FILE_OR_DIRECTORY: {
if (resultCode == RESULT_OK && data != null) {
String filename = data.getDataString();
if (filename != null) {
// Get rid of URI prefix:
if (filename.startsWith("file://")) {
filename = filename.substring(7);
}
// replace %20 and so on
filename = Uri.decode(filename);
FileDialog.setFilename(filename);
}
}
return;
}
default: {
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}

View File

@@ -32,6 +32,7 @@ import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
@@ -81,13 +82,14 @@ public class SecretKeyListActivity extends ExpandableListActivity
static final int TASK_EXPORT = 2;
protected int mSelectedItem = -1;
protected String mImportFilename = null;
protected String mExportFilename = null;
protected int mTask = 0;
private ProgressDialog mProgressDialog = null;
private Thread mRunningThread = null;
private String mImportFilename = Environment.getExternalStorageDirectory() + "/secring.gpg";
private String mExportFilename = Environment.getExternalStorageDirectory() + "/secexport.asc";
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
@@ -335,7 +337,7 @@ public class SecretKeyListActivity extends ExpandableListActivity
case DIALOG_IMPORT_KEYS: {
return FileDialog.build(this, "Import Keys",
"Please specify which file to import from.",
Environment.getExternalStorageDirectory() + "/secring.gpg",
mImportFilename,
new FileDialog.OnClickListener() {
@Override
@@ -349,7 +351,9 @@ public class SecretKeyListActivity extends ExpandableListActivity
public void onCancelClick() {
removeDialog(DIALOG_IMPORT_KEYS);
}
});
},
getString(R.string.filemanager_title_open),
getString(R.string.filemanager_btn_open));
}
case DIALOG_EXPORT_KEY: {
@@ -370,7 +374,7 @@ public class SecretKeyListActivity extends ExpandableListActivity
"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",
mExportFilename,
new FileDialog.OnClickListener() {
@Override
@@ -384,7 +388,9 @@ public class SecretKeyListActivity extends ExpandableListActivity
public void onCancelClick() {
removeDialog(thisDialogId);
}
});
},
getString(R.string.filemanager_title_save),
getString(R.string.filemanager_btn_save));
}
case DIALOG_IMPORTING: {
@@ -441,6 +447,24 @@ public class SecretKeyListActivity extends ExpandableListActivity
break;
}
case FileDialog.REQUEST_CODE_PICK_FILE_OR_DIRECTORY: {
if (resultCode == RESULT_OK && data != null) {
String filename = data.getDataString();
if (filename != null) {
// Get rid of URI prefix:
if (filename.startsWith("file://")) {
filename = filename.substring(7);
}
// replace %20 and so on
filename = Uri.decode(filename);
FileDialog.setFilename(filename);
}
}
return;
}
default:
break;
}