mirror of
https://github.com/SimpleMobileTools/Simple-File-Manager.git
synced 2025-06-05 22:09:15 +02:00
implement file and directory renaming
This commit is contained in:
@ -195,11 +195,11 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
|||||||
final RadioGroup radio = (RadioGroup) newItemView.findViewById(R.id.dialog_radio_group);
|
final RadioGroup radio = (RadioGroup) newItemView.findViewById(R.id.dialog_radio_group);
|
||||||
if (radio.getCheckedRadioButtonId() == R.id.dialog_radio_directory) {
|
if (radio.getCheckedRadioButtonId() == R.id.dialog_radio_directory) {
|
||||||
if (!createDirectory(file, alertDialog)) {
|
if (!createDirectory(file, alertDialog)) {
|
||||||
errorCreatingItem();
|
errorOccurred();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!createFile(file, alertDialog)) {
|
if (!createFile(file, alertDialog)) {
|
||||||
errorCreatingItem();
|
errorOccurred();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -218,7 +218,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void errorCreatingItem() {
|
private void errorOccurred() {
|
||||||
Utils.showToast(getContext(), R.string.error_occurred);
|
Utils.showToast(getContext(), R.string.error_occurred);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,12 +272,18 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
||||||
return false;
|
final MenuItem menuItem = menu.findItem(R.id.cab_rename);
|
||||||
|
menuItem.setVisible(mSelectedItemsCnt == 1);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
|
case R.id.cab_rename:
|
||||||
|
displayRenameDialog();
|
||||||
|
mode.finish();
|
||||||
|
return true;
|
||||||
case R.id.cab_delete:
|
case R.id.cab_delete:
|
||||||
prepareForDeleting();
|
prepareForDeleting();
|
||||||
mode.finish();
|
mode.finish();
|
||||||
@ -287,6 +293,66 @@ public class ItemsFragment extends android.support.v4.app.Fragment
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void displayRenameDialog() {
|
||||||
|
final int itemIndex = getSelectedItemIndex();
|
||||||
|
if (itemIndex == -1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
final FileDirItem item = mItems.get(itemIndex);
|
||||||
|
final View renameView = getActivity().getLayoutInflater().inflate(R.layout.rename_item, null);
|
||||||
|
final EditText itemName = (EditText) renameView.findViewById(R.id.item_name);
|
||||||
|
itemName.setText(item.getName());
|
||||||
|
|
||||||
|
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
||||||
|
int renameString = R.string.rename_file;
|
||||||
|
if (item.getIsDirectory())
|
||||||
|
renameString = R.string.rename_directory;
|
||||||
|
|
||||||
|
builder.setTitle(getResources().getString(renameString));
|
||||||
|
builder.setView(renameView);
|
||||||
|
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 String name = itemName.getText().toString().trim();
|
||||||
|
if (Utils.isNameValid(name)) {
|
||||||
|
final File currFile = new File(mPath, item.getName());
|
||||||
|
final File newFile = new File(mPath, name);
|
||||||
|
|
||||||
|
if (newFile.exists()) {
|
||||||
|
Utils.showToast(getContext(), R.string.name_taken);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currFile.renameTo(newFile)) {
|
||||||
|
alertDialog.dismiss();
|
||||||
|
fillItems();
|
||||||
|
} else {
|
||||||
|
errorOccurred();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Utils.showToast(getContext(), R.string.invalid_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getSelectedItemIndex() {
|
||||||
|
final SparseBooleanArray items = mListView.getCheckedItemPositions();
|
||||||
|
int cnt = items.size();
|
||||||
|
for (int i = 0; i < cnt; i++) {
|
||||||
|
if (items.valueAt(i)) {
|
||||||
|
return items.keyAt(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
private void prepareForDeleting() {
|
private void prepareForDeleting() {
|
||||||
mToBeDeleted.clear();
|
mToBeDeleted.clear();
|
||||||
final SparseBooleanArray items = mListView.getCheckedItemPositions();
|
final SparseBooleanArray items = mListView.getCheckedItemPositions();
|
||||||
|
17
app/src/main/res/layout/rename_item.xml
Normal file
17
app/src/main/res/layout/rename_item.xml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?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:padding="@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"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
@ -1,6 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
<item
|
||||||
|
android:id="@+id/cab_rename"
|
||||||
|
android:icon="@mipmap/edit"
|
||||||
|
android:title="@string/rename"
|
||||||
|
app:showAsAction="ifRoom"/>
|
||||||
<item
|
<item
|
||||||
android:id="@+id/cab_delete"
|
android:id="@+id/cab_delete"
|
||||||
android:icon="@mipmap/delete"
|
android:icon="@mipmap/delete"
|
||||||
|
BIN
app/src/main/res/mipmap-hdpi/edit.png
Normal file
BIN
app/src/main/res/mipmap-hdpi/edit.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 399 B |
BIN
app/src/main/res/mipmap-mdpi/edit.png
Normal file
BIN
app/src/main/res/mipmap-mdpi/edit.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 211 B |
BIN
app/src/main/res/mipmap-xhdpi/edit.png
Normal file
BIN
app/src/main/res/mipmap-xhdpi/edit.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 318 B |
BIN
app/src/main/res/mipmap-xxhdpi/edit.png
Normal file
BIN
app/src/main/res/mipmap-xxhdpi/edit.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 625 B |
BIN
app/src/main/res/mipmap-xxxhdpi/edit.png
Normal file
BIN
app/src/main/res/mipmap-xxxhdpi/edit.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 355 B |
@ -3,6 +3,8 @@
|
|||||||
<string name="no_permissions">We need the permission to access your storage</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="no_app_found">No app for opening this type of files is available</string>
|
||||||
<string name="create_new">Create new item</string>
|
<string name="create_new">Create new item</string>
|
||||||
|
<string name="rename_directory">Rename directory</string>
|
||||||
|
<string name="rename_file">Rename file</string>
|
||||||
<string name="directory">Directory</string>
|
<string name="directory">Directory</string>
|
||||||
<string name="file">File</string>
|
<string name="file">File</string>
|
||||||
<string name="name_taken">Directory or file with that name already exists</string>
|
<string name="name_taken">Directory or file with that name already exists</string>
|
||||||
@ -10,6 +12,7 @@
|
|||||||
<string name="error_occurred">An unknown error occurred</string>
|
<string name="error_occurred">An unknown error occurred</string>
|
||||||
<string name="delete">Delete</string>
|
<string name="delete">Delete</string>
|
||||||
<string name="undo">Undo</string>
|
<string name="undo">Undo</string>
|
||||||
|
<string name="rename">Rename</string>
|
||||||
|
|
||||||
<plurals name="items_deleted">
|
<plurals name="items_deleted">
|
||||||
<item quantity="one">1 item deleted</item>
|
<item quantity="one">1 item deleted</item>
|
||||||
|
Reference in New Issue
Block a user