diff --git a/app/src/main/java/com/simplemobiletools/filemanager/Utils.java b/app/src/main/java/com/simplemobiletools/filemanager/Utils.java index 8b2408b9..0f5d7c69 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/Utils.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/Utils.java @@ -14,6 +14,9 @@ import android.graphics.PorterDuffColorFilter; import android.support.v4.content.ContextCompat; import android.widget.Toast; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class Utils { public static String getFilename(final String path) { return path.substring(path.lastIndexOf("/") + 1); @@ -42,4 +45,10 @@ public class Utils { canvas.drawBitmap(bmp, 0, 0, paint); return bmp; } + + public static boolean isNameValid(String name) { + final Pattern pattern = Pattern.compile("^[-_.A-Za-z0-9 ]+$"); + final Matcher matcher = pattern.matcher(name); + return matcher.matches(); + } } diff --git a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java index b992a47a..f6f0e17d 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java @@ -6,12 +6,16 @@ import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.widget.SwipeRefreshLayout; +import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.WindowManager; import android.webkit.MimeTypeMap; import android.widget.AdapterView; +import android.widget.EditText; import android.widget.ListView; +import android.widget.RadioGroup; import com.simplemobiletools.filemanager.Config; import com.simplemobiletools.filemanager.Constants; @@ -21,6 +25,7 @@ import com.simplemobiletools.filemanager.adapters.ItemsAdapter; import com.simplemobiletools.filemanager.models.FileDirItem; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -36,6 +41,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment private List mItems; private ItemInteractionListener mListener; + private String mPath; private boolean mShowHidden; @@ -66,8 +72,8 @@ public class ItemsFragment extends android.support.v4.app.Fragment } private void fillItems() { - final String path = getArguments().getString(Constants.PATH); - final List newItems = getItems(path); + mPath = getArguments().getString(Constants.PATH); + final List newItems = getItems(); Collections.sort(newItems); if (mItems != null && newItems.toString().equals(mItems.toString())) { return; @@ -84,9 +90,9 @@ public class ItemsFragment extends android.support.v4.app.Fragment mListener = listener; } - private List getItems(String path) { + private List getItems() { final List items = new ArrayList<>(); - final File base = new File(path); + final File base = new File(mPath); File[] files = base.listFiles(); for (File file : files) { final String curPath = file.getAbsolutePath(); @@ -135,7 +141,65 @@ public class ItemsFragment extends android.support.v4.app.Fragment @OnClick(R.id.items_fab) public void fabClicked(View view) { + final View newItemView = getActivity().getLayoutInflater().inflate(R.layout.create_new, null); + final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); + builder.setTitle(getResources().getString(R.string.create_new)); + builder.setView(newItemView); + builder.setPositiveButton("OK", null); + builder.setNegativeButton("Cancel", null); + + final AlertDialog alertDialog = builder.create(); + alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); + alertDialog.show(); + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + final EditText itemName = (EditText) newItemView.findViewById(R.id.item_name); + final String name = itemName.getText().toString().trim(); + if (Utils.isNameValid(name)) { + File file = new File(mPath, name); + final RadioGroup radio = (RadioGroup) newItemView.findViewById(R.id.dialog_radio_group); + if (radio.getCheckedRadioButtonId() == R.id.dialog_radio_directory) { + if (!createDirectory(file, alertDialog)) { + errorCreatingItem(); + } + } else { + if (!createFile(file, alertDialog)) { + errorCreatingItem(); + } + } + } else { + Utils.showToast(getContext(), R.string.invalid_name); + } + } + }); + } + + private boolean createDirectory(File file, AlertDialog alertDialog) { + if (file.mkdirs()) { + alertDialog.dismiss(); + fillItems(); + return true; + } + return false; + } + + private void errorCreatingItem() { + Utils.showToast(getContext(), R.string.error_occurred); + } + + private boolean createFile(File file, AlertDialog alertDialog) { + try { + if (file.createNewFile()) { + alertDialog.dismiss(); + fillItems(); + return true; + } + } catch (IOException ignored) { + + } + return false; } @Override diff --git a/app/src/main/res/layout/create_new.xml b/app/src/main/res/layout/create_new.xml new file mode 100644 index 00000000..d642ee2e --- /dev/null +++ b/app/src/main/res/layout/create_new.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0b491901..c49a5f61 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,6 +2,11 @@ Simple File Manager We need the permission to access your storage No app for opening this type of files is available + Create new item + Directory + File + The name contains invalid characters + An unknown error occurred About