Browse Source

The folder chooser dialog can create folders. Resolves #872.

Aidan Follestad 8 years ago
parent
commit
aecb567e2f

+ 13 - 1
README.md

@@ -1237,7 +1237,6 @@ The Builder is used like this:
 ```java
 // Pass AppCompatActivity which implements FileCallback
 new FileChooserDialog.Builder(this)
-    .chooseButton(R.string.md_choose_label)  // changes label of the choose button
     .initialPath("/sdcard/Download")  // changes initial path, defaults to external storage directory
     .mimeType("image/*") // Optional MIME type filter
     .tag("optional-identifier")
@@ -1291,6 +1290,19 @@ public class MyActivity implements FolderChooserDialog.FolderCallback {
 
 ---
 
+Optionally, you can allow users to have the ability to create new folders from this dialog:
+
+```java
+new FolderChooserDialog.Builder(this)
+    .chooseButton(R.string.md_choose_label)  // changes label of the choose button
+    .initialPath("/sdcard/Download")  // changes initial path, defaults to external storage directory
+    .tag("optional-identifier")
+    .allowNewFolder(true, R.string.new_folder)  // pass 0 in the second parameter to use default button label
+    .show();
+```
+
+---
+
 # Simple List Dialogs
 
 Simple List Dialogs are a specific style of list dialogs taken from the Material Design Guidelines: https://www.google.com/design/spec/components/dialogs.html#dialogs-simple-dialogs

+ 44 - 3
commons/src/main/java/com/afollestad/materialdialogs/folderselector/FolderChooserDialog.java

@@ -16,6 +16,7 @@ import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentActivity;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
+import android.widget.Toast;
 
 import com.afollestad.materialdialogs.DialogAction;
 import com.afollestad.materialdialogs.MaterialDialog;
@@ -93,7 +94,7 @@ public class FolderChooserDialog extends DialogFragment implements MaterialDialo
             getArguments().putString("current_path", getBuilder().mInitialPath);
         parentFolder = new File(getArguments().getString("current_path"));
         parentContents = listFiles();
-        return new MaterialDialog.Builder(getActivity())
+        MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity())
                 .title(parentFolder.getAbsolutePath())
                 .items(getContentsArray())
                 .itemsCallback(this)
@@ -112,8 +113,32 @@ public class FolderChooserDialog extends DialogFragment implements MaterialDialo
                 })
                 .autoDismiss(false)
                 .positiveText(getBuilder().mChooseButton)
-                .negativeText(getBuilder().mCancelButton)
-                .build();
+                .negativeText(getBuilder().mCancelButton);
+        if (getBuilder().mAllowNewFolder) {
+            builder.neutralText(getBuilder().mNewFolderButton);
+            builder.onNeutral(new MaterialDialog.SingleButtonCallback() {
+                @Override
+                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
+                    createNewFolder();
+                }
+            });
+        }
+        return builder.build();
+    }
+
+    private void createNewFolder() {
+        new MaterialDialog.Builder(getActivity())
+                .title(getBuilder().mNewFolderButton)
+                .input(0, 0, false, new MaterialDialog.InputCallback() {
+                    @Override
+                    public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
+                        //noinspection ResultOfMethodCallIgnored
+                        final File newFi = new File(parentFolder, input.toString());
+                        if (!newFi.mkdir())
+                            Toast.makeText(getActivity(), "Unable to create folder " + newFi.getAbsolutePath() + ", make sure you have the WRITE_EXTERNAL_STORAGE permission.", Toast.LENGTH_SHORT).show();
+                        else reload();
+                    }
+                }).show();
     }
 
     @Override
@@ -129,6 +154,10 @@ public class FolderChooserDialog extends DialogFragment implements MaterialDialo
             if (parentFolder.getAbsolutePath().equals("/storage/emulated"))
                 parentFolder = Environment.getExternalStorageDirectory();
         }
+        reload();
+    }
+
+    private void reload() {
         parentContents = listFiles();
         MaterialDialog dialog = (MaterialDialog) getDialog();
         dialog.setTitle(parentFolder.getAbsolutePath());
@@ -163,6 +192,9 @@ public class FolderChooserDialog extends DialogFragment implements MaterialDialo
         protected int mCancelButton;
         protected String mInitialPath;
         protected String mTag;
+        protected boolean mAllowNewFolder;
+        @StringRes
+        protected int mNewFolderButton;
 
         public <ActivityType extends AppCompatActivity & FolderCallback> Builder(@NonNull ActivityType context) {
             mContext = context;
@@ -183,6 +215,15 @@ public class FolderChooserDialog extends DialogFragment implements MaterialDialo
             return this;
         }
 
+        @NonNull
+        public Builder allowNewFolder(boolean allow, @StringRes int buttonLabel) {
+            mAllowNewFolder = allow;
+            if (buttonLabel == 0)
+                buttonLabel = R.string.new_folder;
+            mNewFolderButton = buttonLabel;
+            return this;
+        }
+
         @NonNull
         public Builder initialPath(@Nullable String initialPath) {
             if (initialPath == null)

+ 1 - 0
commons/src/main/res/values/strings.xml

@@ -8,4 +8,5 @@
     <string name="md_storage_perm_error">Your app has not been granted the READ_EXTERNAL_STORAGE permission.</string>
     <string name="md_custom_label">Custom</string>
     <string name="md_presets_label">Presets</string>
+    <string name="new_folder">New Folder</string>
 </resources>

+ 1 - 1
sample/src/main/AndroidManifest.xml

@@ -2,7 +2,7 @@
     xmlns:tools="http://schemas.android.com/tools"
     package="com.afollestad.materialdialogssample">
 
-    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
     <application
         android:allowBackup="false"

+ 3 - 2
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -661,13 +661,14 @@ public class MainActivity extends AppCompatActivity implements
     @OnClick(R.id.folder_chooser)
     public void showFolderChooser() {
         chooserDialog = R.id.folder_chooser;
-        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
+        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                 PackageManager.PERMISSION_GRANTED) {
-            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_RC);
+            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_RC);
             return;
         }
         new FolderChooserDialog.Builder(MainActivity.this)
                 .chooseButton(R.string.md_choose_label)
+                .allowNewFolder(true, 0)
                 .show();
     }