delete files from main screen more carefully, and not recursively

This commit is contained in:
tibbi 2016-11-06 21:32:36 +01:00
parent c7408fb9fe
commit a5da407ed3

View file

@ -13,6 +13,7 @@ import android.provider.MediaStore;
import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.provider.DocumentFile;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.util.SparseBooleanArray; import android.util.SparseBooleanArray;
import android.view.ActionMode; import android.view.ActionMode;
@ -232,11 +233,14 @@ public class MainActivity extends SimpleActivity
if (dir.exists()) { if (dir.exists()) {
final File[] files = dir.listFiles(); final File[] files = dir.listFiles();
for (File f : files) { for (File f : files) {
if (f.isFile()) {
updatedFiles.add(f.getAbsolutePath()); updatedFiles.add(f.getAbsolutePath());
f.delete(); deleteItem(f);
}
} }
updatedFiles.add(dir.getAbsolutePath()); updatedFiles.add(dir.getAbsolutePath());
dir.delete(); if (dir.listFiles().length == 0)
deleteItem(dir);
} }
} }
@ -245,6 +249,15 @@ public class MainActivity extends SimpleActivity
mToBeDeleted.clear(); mToBeDeleted.clear();
} }
private void deleteItem(File file) {
if (Utils.Companion.needsStupidWritePermissions(this, file.getAbsolutePath())) {
final DocumentFile document = Utils.Companion.getFileDocument(this, file.getAbsolutePath());
document.delete();
} else {
file.delete();
}
}
private View.OnClickListener undoDeletion = new View.OnClickListener() { private View.OnClickListener undoDeletion = new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {