DirectoryChooser can now create new folders

This commit is contained in:
daniel oeh 2012-12-23 12:03:53 +01:00
parent 5d3f10575f
commit 6f8a582710
3 changed files with 57 additions and 3 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/new_folder_item" android:title="@string/new_folder_label" android:showAsAction="ifRoom|withText"></item>
<item android:id="@+id/new_folder_item" android:title="@string/create_folder_label" android:showAsAction="ifRoom|withText"></item>
</menu>

View File

@ -217,7 +217,11 @@
<!-- Directory chooser -->
<string name="selected_folder_label">Selected folder:</string>
<string name="new_folder_label">New folder</string>
<string name="create_folder_label">Create folder</string>
<string name="choose_data_directory">Choose data folder</string>
<string name="create_folder_msg">Create new folder with name "%1$s"?</string>
<string name="create_folder_success">Created new folder</string>
<string name="create_folder_error_no_write_access">Cannot write to this folder</string>
<string name="create_folder_error_already_exists">Folder already exists</string>
<string name="create_folder_error">Could not create folder</string>
</resources>

View File

@ -5,6 +5,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileObserver;
@ -19,6 +22,7 @@ import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
@ -33,6 +37,8 @@ import de.danoeh.antennapod.R;
public class DirectoryChooserActivity extends SherlockActivity {
private static final String TAG = "DirectoryChooserActivity";
private static final String CREATE_DIRECTORY_NAME = "AntennaPod";
private Button butConfirm;
private Button butCancel;
private ImageButton butNavUp;
@ -215,7 +221,51 @@ public class DirectoryChooserActivity extends SherlockActivity {
}
private void openNewFolderDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.create_folder_label);
builder.setMessage(String.format(getString(R.string.create_folder_msg),
CREATE_DIRECTORY_NAME));
builder.setNegativeButton(R.string.cancel_label,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setPositiveButton(R.string.confirm_label,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
int msg = createFolder();
Toast t = Toast.makeText(DirectoryChooserActivity.this,
msg, Toast.LENGTH_SHORT);
t.show();
}
});
builder.create().show();
}
private int createFolder() {
if (selectedDir != null && selectedDir.canWrite()) {
File newDir = new File(selectedDir, CREATE_DIRECTORY_NAME);
if (!newDir.exists()) {
boolean result = newDir.mkdir();
if (result) {
return R.string.create_folder_success;
} else {
return R.string.create_folder_error;
}
} else {
return R.string.create_folder_error_already_exists;
}
} else if (selectedDir.canWrite() == false) {
return R.string.create_folder_error_no_write_access;
} else {
return R.string.create_folder_error;
}
}
}