From ef4d505b6f502a0ea248643d3dc3077c15935688 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 14 Jul 2016 17:53:33 +0200 Subject: [PATCH] implement file and directory renaming --- .../filemanager/fragments/ItemsFragment.java | 74 +++++++++++++++++- app/src/main/res/layout/rename_item.xml | 17 ++++ app/src/main/res/menu/cab.xml | 5 ++ app/src/main/res/mipmap-hdpi/edit.png | Bin 0 -> 399 bytes app/src/main/res/mipmap-mdpi/edit.png | Bin 0 -> 211 bytes app/src/main/res/mipmap-xhdpi/edit.png | Bin 0 -> 318 bytes app/src/main/res/mipmap-xxhdpi/edit.png | Bin 0 -> 625 bytes app/src/main/res/mipmap-xxxhdpi/edit.png | Bin 0 -> 355 bytes app/src/main/res/values/strings.xml | 3 + 9 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/layout/rename_item.xml create mode 100644 app/src/main/res/mipmap-hdpi/edit.png create mode 100644 app/src/main/res/mipmap-mdpi/edit.png create mode 100644 app/src/main/res/mipmap-xhdpi/edit.png create mode 100644 app/src/main/res/mipmap-xxhdpi/edit.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/edit.png 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 c31d2be9..87557c00 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/fragments/ItemsFragment.java @@ -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(); diff --git a/app/src/main/res/layout/rename_item.xml b/app/src/main/res/layout/rename_item.xml new file mode 100644 index 00000000..2efc527e --- /dev/null +++ b/app/src/main/res/layout/rename_item.xml @@ -0,0 +1,17 @@ + + + + + + diff --git a/app/src/main/res/menu/cab.xml b/app/src/main/res/menu/cab.xml index 6e774a7c..d8d59597 100644 --- a/app/src/main/res/menu/cab.xml +++ b/app/src/main/res/menu/cab.xml @@ -1,6 +1,11 @@ + gW(CcpbXho6?K0UPOhYT~rV^1^*5`g?dOz68kn3@@)A$Ki(&YNL{+@K!8)= z4mfVxB47aIERq5JwlQz~%^Wyu19J$xP@MVJCk6C?G4NA@%7CM7Kn6exSU4U7-zBJ9 zZ_Ii4fIop7z*Z&|qcY&c6SKOT2&hrG7T5O*B;J^KHvxPBqo}^AH)dH=S>pN*JTY-i z>riQxclQ2+k@P5OzryVwlBP}9s)89-oD3{>)6f%>)WEp!rX(y4nJ z@ErkR#;MwT(m=sdPZ!4!kIuIjHu5$o2)HF~KWtjS$0fd@ATU{VYNc7a3Wwpv-N!zJ zhuml0weD<{1OJ)>DXs47&faP~P7v+jKTD`Z3#2cWW}6=k_Q2 z2c!8O=u<5X=vX`tW(PZ!6KjK;TD4)Qhy2(&!xZ0$Ju;$Yqumgoz7Imeo99fZ|nj&n<> zG;S{_D1UtBW8u$#Pd+mEUf!}_{-o?l^^?<{^gcOuJ0-3?|Ht0yt`5$cz78h4I+l~g zjPsQqCH3s6ie{`?cQ&ykt+S$XX8VPI=eDc#FV0`u(LQ}QdtdzxRYB8}Yp2(~+q%)^ zii)=W?x!o~baV92@3u%i=--e$Vvj zQDZ2huY}D83k5N`2oV8({u#_kj~Ig;Vr&9Zio?Wkq zQc5YMlu{`J7QiX+1pEb#fv=&j>r(*sfD2&7MhRR3-$GwsP}Rw~s0L{&fh*uk=<5uk z8t0~Qa++4vNO_$kOI9{{<<^r`*GPHf2K)g2jTonvM{d9l@Y8XmI=8eR0gRD`dNP`7QVE;^yCLq&3;G`dr{s1HEJNMfWDI)?dc)Z>!pv z+{)_YL_N-!-&Up}xn+@+OOIz3 z6*G(jnpLN3B0o8EXSE1@kIvAn?3h2Rs85b&WdkhHtlF{3>i-PmXjT@QRgVJA%1X1c zC|Qpl^*Bp3t9EEs-O#MM%`gtco|S2af#T1~bLK zS(zpdRFGm;rU?V3o0Z97p!BmcISf>gdRC^91LcvGQc5YMl*-#H<#&UJa#*1O00000 LNkvXXu0mjf@5ULJ literal 0 HcmV?d00001 diff --git a/app/src/main/res/mipmap-xxxhdpi/edit.png b/app/src/main/res/mipmap-xxxhdpi/edit.png new file mode 100644 index 0000000000000000000000000000000000000000..d6668a051cd53b2ef454e8e09872339348c2ee22 GIT binary patch literal 355 zcmV-p0i6DcP)o)4CwERtj4{R- zW9;vcYqq-8KB2^1t{_z z0h;{m09AfwfG$5PK$)Krpv|uhQ0Fto7-NhzVt3qk;nT+k=6L`B002ovPDHLkV1hVj BpML-V literal 0 HcmV?d00001 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b8b6b629..317ed33e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,6 +3,8 @@ We need the permission to access your storage No app for opening this type of files is available Create new item + Rename directory + Rename file Directory File Directory or file with that name already exists @@ -10,6 +12,7 @@ An unknown error occurred Delete Undo + Rename 1 item deleted