correct deleting files on SD card

This commit is contained in:
tibbi 2016-11-05 18:06:55 +01:00
parent 20fb58ddbe
commit 4b56761751
2 changed files with 52 additions and 44 deletions

View File

@ -11,44 +11,46 @@ import android.widget.Toast
import com.simplemobiletools.filepicker.extensions.getSDCardPath import com.simplemobiletools.filepicker.extensions.getSDCardPath
import java.util.regex.Pattern import java.util.regex.Pattern
object Utils { class Utils {
fun getFilename(path: String): String { companion object {
return path.substring(path.lastIndexOf("/") + 1) fun getFilename(path: String): String {
} return path.substring(path.lastIndexOf("/") + 1)
}
fun getFileExtension(fileName: String): String {
return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase() fun getFileExtension(fileName: String): String {
} return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase()
}
fun showToast(context: Context, resId: Int) {
Toast.makeText(context, context.resources.getString(resId), Toast.LENGTH_SHORT).show() fun showToast(context: Context, resId: Int) {
} Toast.makeText(context, context.resources.getString(resId), Toast.LENGTH_SHORT).show()
}
fun hasStoragePermission(cxt: Context): Boolean {
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED fun hasStoragePermission(cxt: Context): Boolean {
} return ContextCompat.checkSelfPermission(cxt, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
}
fun isNameValid(name: String): Boolean {
val pattern = Pattern.compile("^[-_.A-Za-z0-9 ]+$") fun isNameValid(name: String): Boolean {
val matcher = pattern.matcher(name) val pattern = Pattern.compile("^[-_.A-Za-z0-9 ]+$")
return matcher.matches() val matcher = pattern.matcher(name)
} return matcher.matches()
}
fun needsStupidWritePermissions(context: Context, path: String) = isPathOnSD(context, path) && isKitkat()
fun needsStupidWritePermissions(context: Context, path: String) = isPathOnSD(context, path) && isKitkat()
fun isPathOnSD(context: Context, path: String): Boolean {
return path.startsWith(context.getSDCardPath()) fun isPathOnSD(context: Context, path: String): Boolean {
} return path.startsWith(context.getSDCardPath())
}
fun isKitkat() = Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT
fun isKitkat() = Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT
fun getFileDocument(context: Context, path: String): DocumentFile {
val relativePath = path.substring(context.getSDCardPath().length + 1) fun getFileDocument(context: Context, path: String): DocumentFile {
var document = DocumentFile.fromTreeUri(context, Uri.parse(Config.newInstance(context).treeUri)) val relativePath = path.substring(context.getSDCardPath().length + 1)
val parts = relativePath.split("/") var document = DocumentFile.fromTreeUri(context, Uri.parse(Config.newInstance(context).treeUri))
for (part in parts) { val parts = relativePath.split("/")
document = document.findFile(part) for (part in parts) {
document = document.findFile(part)
}
return document
} }
return document
} }
} }

View File

@ -11,6 +11,7 @@ import android.os.Parcelable;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
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.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;
@ -137,7 +138,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
if (files != null) { if (files != null) {
for (File file : files) { for (File file : files) {
final String curPath = file.getAbsolutePath(); final String curPath = file.getAbsolutePath();
final String curName = Utils.INSTANCE.getFilename(curPath); final String curName = Utils.Companion.getFilename(curPath);
if (!mShowHidden && curName.startsWith(".")) if (!mShowHidden && curName.startsWith("."))
continue; continue;
@ -181,7 +182,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
} else { } else {
final String path = item.getPath(); final String path = item.getPath();
final File file = new File(path); final File file = new File(path);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(Utils.INSTANCE.getFileExtension(path)); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(Utils.Companion.getFileExtension(path));
if (mimeType == null) if (mimeType == null)
mimeType = "text/plain"; mimeType = "text/plain";
@ -192,7 +193,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
startActivity(intent); startActivity(intent);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
if (!tryGenericMimeType(intent, mimeType, file)) { if (!tryGenericMimeType(intent, mimeType, file)) {
Utils.INSTANCE.showToast(getContext(), R.string.no_app_found); Utils.Companion.showToast(getContext(), R.string.no_app_found);
} }
} }
} }
@ -302,7 +303,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
} }
if (uris.isEmpty()) { if (uris.isEmpty()) {
Utils.INSTANCE.showToast(getContext(), R.string.no_files_selected); Utils.Companion.showToast(getContext(), R.string.no_files_selected);
return; return;
} }
@ -467,7 +468,12 @@ public class ItemsFragment extends android.support.v4.app.Fragment
} }
} }
item.delete(); if (Utils.Companion.needsStupidWritePermissions(getContext(), item.getAbsolutePath())) {
final DocumentFile document = Utils.Companion.getFileDocument(getContext(), item.getAbsolutePath());
document.delete();
} else {
item.delete();
}
MediaScannerConnection.scanFile(getContext(), new String[]{item.getAbsolutePath()}, null, null); MediaScannerConnection.scanFile(getContext(), new String[]{item.getAbsolutePath()}, null, null);
} }
@ -493,7 +499,7 @@ public class ItemsFragment extends android.support.v4.app.Fragment
@Override @Override
public void copyFailed() { public void copyFailed() {
Utils.INSTANCE.showToast(getContext(), R.string.copy_failed); Utils.Companion.showToast(getContext(), R.string.copy_failed);
} }
public interface ItemInteractionListener { public interface ItemInteractionListener {