allow creating new files and directories

This commit is contained in:
tibbi 2016-07-14 11:35:42 +02:00
parent 25a09dbeec
commit 9c3d5e4222
4 changed files with 121 additions and 4 deletions

View File

@ -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();
}
}

View File

@ -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<FileDirItem> 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<FileDirItem> newItems = getItems(path);
mPath = getArguments().getString(Constants.PATH);
final List<FileDirItem> 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<FileDirItem> getItems(String path) {
private List<FileDirItem> getItems() {
final List<FileDirItem> 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

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/dialog_holder"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin">
<EditText
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:inputType="textCapSentences"
android:singleLine="true"/>
<RadioGroup
android:id="@+id/dialog_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/dialog_radio_directory">
<RadioButton
android:id="@+id/dialog_radio_directory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/medium_margin"
android:text="@string/directory"/>
<RadioButton
android:id="@+id/dialog_radio_file"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/file"/>
</RadioGroup>
</LinearLayout>

View File

@ -2,6 +2,11 @@
<string name="app_name">Simple File Manager</string>
<string name="no_permissions">We need the permission to access your storage</string>
<string name="no_app_found">No app for opening this type of files is available</string>
<string name="create_new">Create new item</string>
<string name="directory">Directory</string>
<string name="file">File</string>
<string name="invalid_name">The name contains invalid characters</string>
<string name="error_occurred">An unknown error occurred</string>
<!-- About -->
<string name="about">About</string>