be smarter about what files can be deleted

This commit is contained in:
Daniel Gultsch 2022-03-10 15:54:23 +01:00
parent b1ec3a0e29
commit 5c4eccec13
2 changed files with 45 additions and 10 deletions

View file

@ -33,6 +33,7 @@ import androidx.core.content.FileProvider;
import androidx.exifinterface.media.ExifInterface; import androidx.exifinterface.media.ExifInterface;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -83,21 +84,38 @@ public class FileBackend {
private static final float IGNORE_PADDING = 0.15f; private static final float IGNORE_PADDING = 0.15f;
private final XmppConnectionService mXmppConnectionService; private final XmppConnectionService mXmppConnectionService;
private static final List<String> STORAGE_TYPES;
static {
final ImmutableList.Builder<String> builder =
new ImmutableList.Builder<String>()
.add(
Environment.DIRECTORY_DOWNLOADS,
Environment.DIRECTORY_PICTURES,
Environment.DIRECTORY_MOVIES,
Environment.DIRECTORY_DOCUMENTS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.add(Environment.DIRECTORY_RECORDINGS);
}
STORAGE_TYPES = builder.build();
}
public FileBackend(XmppConnectionService service) { public FileBackend(XmppConnectionService service) {
this.mXmppConnectionService = service; this.mXmppConnectionService = service;
} }
public static long getFileSize(Context context, Uri uri) { public static long getFileSize(Context context, Uri uri) {
try { try (final Cursor cursor =
final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); context.getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
long size = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE)); final int index = cursor.getColumnIndex(OpenableColumns.SIZE);
cursor.close(); if (index == -1) {
return size; return -1;
} else { }
return -1; return cursor.getLong(index);
} }
} catch (Exception e) { return -1;
} catch (final Exception ignored) {
return -1; return -1;
} }
} }
@ -861,6 +879,20 @@ public class FileBackend {
return new File(appDirectory, filename); return new File(appDirectory, filename);
} }
public static boolean inConversationsDirectory(final Context context, String path) {
final File fileDirectory = new File(path).getParentFile();
for (final String type : STORAGE_TYPES) {
final File typeDirectory =
new File(
Environment.getExternalStoragePublicDirectory(type),
context.getString(R.string.app_name));
if (typeDirectory.equals(fileDirectory)) {
return true;
}
}
return false;
}
public void setupRelativeFilePath( public void setupRelativeFilePath(
final Message message, final String filename, final String mime) { final Message message, final String filename, final String mime) {
final File file = getStorageLocation(filename, mime); final File file = getStorageLocation(filename, mime);

View file

@ -1184,8 +1184,11 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
cancelTransmission.setVisible(true); cancelTransmission.setVisible(true);
} }
if (m.isFileOrImage() && !deleted && !cancelable) { if (m.isFileOrImage() && !deleted && !cancelable) {
deleteFile.setVisible(true); final String path = m.getRelativeFilePath();
deleteFile.setTitle(activity.getString(R.string.delete_x_file, UIHelper.getFileDescriptionString(activity, m))); if (path == null || !path.startsWith("/") || FileBackend.inConversationsDirectory(requireActivity(), path)) {
deleteFile.setVisible(true);
deleteFile.setTitle(activity.getString(R.string.delete_x_file, UIHelper.getFileDescriptionString(activity, m)));
}
} }
if (showError) { if (showError) {
showErrorMessage.setVisible(true); showErrorMessage.setVisible(true);