2011-04-24 23:17:18 -04:00
|
|
|
package com.fsck.k9.helper;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.ActivityNotFoundException;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.text.InputType;
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.R;
|
|
|
|
|
|
|
|
public class FileBrowserHelper {
|
2011-05-04 20:52:47 -04:00
|
|
|
/**
|
|
|
|
* A string array that specifies the name of the intent to use, and the scheme to use with it
|
|
|
|
* when setting the data for the intent.
|
|
|
|
*/
|
2011-06-01 16:03:56 -04:00
|
|
|
private static final String[][] PICK_DIRECTORY_INTENTS = {
|
|
|
|
{ "org.openintents.action.PICK_DIRECTORY", "file://" }, // OI File Manager (maybe others)
|
|
|
|
{ "com.estrongs.action.PICK_DIRECTORY", "file://" }, // ES File Explorer
|
|
|
|
{ Intent.ACTION_PICK, "folder://" }, // Blackmoon File Browser (maybe others)
|
|
|
|
{ "com.androidworkz.action.PICK_DIRECTORY", "file://" }
|
|
|
|
}; // SystemExplorer
|
2011-05-04 20:52:47 -04:00
|
|
|
|
2011-04-24 23:17:18 -04:00
|
|
|
private static FileBrowserHelper sInstance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* callback class to provide the result of the fallback textedit path dialog
|
|
|
|
*/
|
|
|
|
public interface FileBrowserFailOverCallback {
|
|
|
|
/**
|
|
|
|
* the user has entered a path
|
|
|
|
* @param path the path as String
|
|
|
|
*/
|
|
|
|
public void onPathEntered(String path);
|
|
|
|
/**
|
|
|
|
* the user has cancel the inputtext dialog
|
|
|
|
*/
|
|
|
|
public void onCancel();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* factory method
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private FileBrowserHelper() {
|
|
|
|
}
|
|
|
|
public synchronized static FileBrowserHelper getInstance() {
|
|
|
|
if (sInstance == null) {
|
|
|
|
sInstance = new FileBrowserHelper();
|
|
|
|
}
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tries to open known filebrowsers.
|
|
|
|
* If no filebrowser is found and fallback textdialog is shown
|
2011-05-04 12:10:50 -04:00
|
|
|
* @param c the context as activity
|
2011-04-24 23:17:18 -04:00
|
|
|
* @param startPath: the default value, where the filebrowser will start.
|
|
|
|
* if startPath = null => the default path is used
|
|
|
|
* @param requestcode: the int you will get as requestcode in onActivityResult
|
|
|
|
* (only used if there is a filebrowser installed)
|
|
|
|
* @param callback: the callback (only used when no filebrowser is installed.
|
|
|
|
* if a filebrowser is installed => override the onActivtyResult Method
|
|
|
|
*
|
|
|
|
* @return true: if a filebrowser has been found (the result will be in the onActivityResult
|
|
|
|
* false: a fallback textinput has been shown. The Result will be sent with the callback method
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public boolean showFileBrowserActivity(Activity c, File startPath, int requestcode, FileBrowserFailOverCallback callback) {
|
|
|
|
boolean success = false;
|
2011-05-04 12:10:50 -04:00
|
|
|
|
|
|
|
if (startPath == null) {
|
2011-04-24 23:17:18 -04:00
|
|
|
startPath = new File(K9.getAttachmentDefaultPath());
|
2011-05-04 12:10:50 -04:00
|
|
|
}
|
2011-04-24 23:17:18 -04:00
|
|
|
|
2011-05-04 12:10:50 -04:00
|
|
|
int listIndex = 0;
|
|
|
|
do {
|
2011-05-04 20:52:47 -04:00
|
|
|
String intentAction = PICK_DIRECTORY_INTENTS[listIndex][0];
|
|
|
|
String uriPrefix = PICK_DIRECTORY_INTENTS[listIndex][1];
|
2011-06-01 16:03:56 -04:00
|
|
|
Intent intent = new Intent(intentAction);
|
|
|
|
intent.setData(Uri.parse(uriPrefix + startPath.getPath()));
|
2011-05-04 20:54:20 -04:00
|
|
|
|
2011-06-01 16:03:56 -04:00
|
|
|
try {
|
2011-04-24 23:17:18 -04:00
|
|
|
c.startActivityForResult(intent, requestcode);
|
|
|
|
success = true;
|
2011-05-04 12:10:50 -04:00
|
|
|
} catch (ActivityNotFoundException e) {
|
2011-06-01 16:03:56 -04:00
|
|
|
// Try the next intent in the list
|
|
|
|
listIndex++;
|
2011-12-25 17:45:10 -05:00
|
|
|
}
|
2011-05-04 20:52:47 -04:00
|
|
|
} while (!success && (listIndex < PICK_DIRECTORY_INTENTS.length));
|
2011-05-04 12:10:50 -04:00
|
|
|
|
2011-05-04 20:52:47 -04:00
|
|
|
if (listIndex == PICK_DIRECTORY_INTENTS.length) {
|
2011-05-04 12:10:50 -04:00
|
|
|
//No Filebrowser is installed => show a fallback textdialog
|
|
|
|
showPathTextInput(c, startPath, callback);
|
|
|
|
success = false;
|
2011-04-24 23:17:18 -04:00
|
|
|
}
|
2011-05-04 12:10:50 -04:00
|
|
|
|
2011-04-24 23:17:18 -04:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showPathTextInput(final Activity c, final File startPath, final FileBrowserFailOverCallback callback) {
|
|
|
|
AlertDialog.Builder alert = new AlertDialog.Builder(c);
|
|
|
|
|
|
|
|
alert.setTitle(c.getString(R.string.attachment_save_title));
|
|
|
|
alert.setMessage(c.getString(R.string.attachment_save_desc));
|
|
|
|
final EditText input = new EditText(c);
|
|
|
|
input.setInputType(InputType.TYPE_CLASS_TEXT);
|
|
|
|
if (startPath != null)
|
|
|
|
input.setText(startPath.toString());
|
|
|
|
alert.setView(input);
|
|
|
|
|
|
|
|
alert.setPositiveButton(c.getString(R.string.okay_action), new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int whichButton) {
|
|
|
|
String path = input.getText().toString();
|
|
|
|
callback.onPathEntered(path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
alert.setNegativeButton(c.getString(R.string.cancel_action),
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int whichButton) {
|
|
|
|
callback.onCancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
alert.show();
|
|
|
|
}
|
|
|
|
}
|