implement file and directory renaming

This commit is contained in:
tibbi 2016-07-14 17:53:33 +02:00
parent fe43e9c8f3
commit ef4d505b6f
9 changed files with 95 additions and 4 deletions

View File

@ -195,11 +195,11 @@ public class ItemsFragment extends android.support.v4.app.Fragment
final RadioGroup radio = (RadioGroup) newItemView.findViewById(R.id.dialog_radio_group);
if (radio.getCheckedRadioButtonId() == R.id.dialog_radio_directory) {
if (!createDirectory(file, alertDialog)) {
errorCreatingItem();
errorOccurred();
}
} else {
if (!createFile(file, alertDialog)) {
errorCreatingItem();
errorOccurred();
}
}
} else {
@ -218,7 +218,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
return false;
}
private void errorCreatingItem() {
private void errorOccurred() {
Utils.showToast(getContext(), R.string.error_occurred);
}
@ -272,12 +272,18 @@ public class ItemsFragment extends android.support.v4.app.Fragment
@Override
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
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.cab_rename:
displayRenameDialog();
mode.finish();
return true;
case R.id.cab_delete:
prepareForDeleting();
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() {
mToBeDeleted.clear();
final SparseBooleanArray items = mListView.getCheckedItemPositions();

View 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>

View File

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
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
android:id="@+id/cab_delete"
android:icon="@mipmap/delete"

Binary file not shown.

After

Width:  |  Height:  |  Size: 399 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 211 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 625 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 355 B

View File

@ -3,6 +3,8 @@
<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="rename_directory">Rename directory</string>
<string name="rename_file">Rename file</string>
<string name="directory">Directory</string>
<string name="file">File</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="delete">Delete</string>
<string name="undo">Undo</string>
<string name="rename">Rename</string>
<plurals name="items_deleted">
<item quantity="one">1 item deleted</item>