mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-27 11:42:16 -05:00
Use TemporaryAttachmentStore when viewing attachments using file:// URI
This commit is contained in:
parent
34cfd8e5b4
commit
87ca0d3d2a
59
src/com/fsck/k9/cache/TemporaryAttachmentStore.java
vendored
Normal file
59
src/com/fsck/k9/cache/TemporaryAttachmentStore.java
vendored
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package com.fsck.k9.cache;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.fsck.k9.K9;
|
||||||
|
import com.fsck.k9.helper.FileHelper;
|
||||||
|
|
||||||
|
|
||||||
|
public class TemporaryAttachmentStore {
|
||||||
|
private static String TEMPORARY_ATTACHMENT_DIRECTORY = "attachments";
|
||||||
|
private static long MAX_FILE_AGE = 12 * 60 * 60 * 1000; // 12h
|
||||||
|
|
||||||
|
public static File getFile(Context context, String attachmentName) throws IOException {
|
||||||
|
File directory = createOrCleanAttachmentDirectory(context);
|
||||||
|
String filename = FileHelper.sanitizeFilename(attachmentName);
|
||||||
|
return new File(directory, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static File createOrCleanAttachmentDirectory(Context context) throws IOException {
|
||||||
|
File directory = getTemporaryAttachmentDirectory(context);
|
||||||
|
if (directory.exists()) {
|
||||||
|
cleanOldFiles(directory);
|
||||||
|
} else {
|
||||||
|
if (!directory.mkdir()) {
|
||||||
|
throw new IOException("Couldn't create temporary attachment store: " + directory.getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static File getTemporaryAttachmentDirectory(Context context) {
|
||||||
|
return new File(context.getExternalCacheDir(), TEMPORARY_ATTACHMENT_DIRECTORY);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void cleanOldFiles(File directory) {
|
||||||
|
File[] files = directory.listFiles();
|
||||||
|
if (files == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
long cutOffTime = System.currentTimeMillis() - MAX_FILE_AGE;
|
||||||
|
for (File file : files) {
|
||||||
|
if (file.lastModified() < cutOffTime) {
|
||||||
|
if (file.delete()) {
|
||||||
|
if (K9.DEBUG) {
|
||||||
|
Log.d(K9.LOG_TAG, "Deleted from temporary attachment store: " + file.getName());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.w(K9.LOG_TAG, "Couldn't delete from temporary attachment store: " + file.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -32,6 +32,7 @@ import android.widget.Toast;
|
|||||||
import com.fsck.k9.Account;
|
import com.fsck.k9.Account;
|
||||||
import com.fsck.k9.K9;
|
import com.fsck.k9.K9;
|
||||||
import com.fsck.k9.R;
|
import com.fsck.k9.R;
|
||||||
|
import com.fsck.k9.cache.TemporaryAttachmentStore;
|
||||||
import com.fsck.k9.controller.MessagingController;
|
import com.fsck.k9.controller.MessagingController;
|
||||||
import com.fsck.k9.controller.MessagingListener;
|
import com.fsck.k9.controller.MessagingListener;
|
||||||
import com.fsck.k9.helper.FileHelper;
|
import com.fsck.k9.helper.FileHelper;
|
||||||
@ -223,7 +224,7 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo
|
|||||||
*/
|
*/
|
||||||
public void writeFile(File directory) {
|
public void writeFile(File directory) {
|
||||||
try {
|
try {
|
||||||
File file = writeAttachmentToStorage(directory);
|
File file = saveAttachmentWithUniqueFileName(directory);
|
||||||
|
|
||||||
displayAttachmentSavedMessage(file.toString());
|
displayAttachmentSavedMessage(file.toString());
|
||||||
|
|
||||||
@ -236,10 +237,16 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private File writeAttachmentToStorage(File directory) throws IOException {
|
private File saveAttachmentWithUniqueFileName(File directory) throws IOException {
|
||||||
String filename = FileHelper.sanitizeFilename(name);
|
String filename = FileHelper.sanitizeFilename(name);
|
||||||
File file = FileHelper.createUniqueFile(directory, filename);
|
File file = FileHelper.createUniqueFile(directory, filename);
|
||||||
|
|
||||||
|
writeAttachmentToStorage(file);
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeAttachmentToStorage(File file) throws IOException {
|
||||||
Uri uri = AttachmentProvider.getAttachmentUri(account, part.getAttachmentId());
|
Uri uri = AttachmentProvider.getAttachmentUri(account, part.getAttachmentId());
|
||||||
InputStream in = context.getContentResolver().openInputStream(uri);
|
InputStream in = context.getContentResolver().openInputStream(uri);
|
||||||
try {
|
try {
|
||||||
@ -253,8 +260,6 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo
|
|||||||
} finally {
|
} finally {
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return file;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showFile() {
|
public void showFile() {
|
||||||
@ -270,8 +275,9 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo
|
|||||||
IntentAndResolvedActivitiesCount resultForFileUri = getBestViewIntentForFileUri();
|
IntentAndResolvedActivitiesCount resultForFileUri = getBestViewIntentForFileUri();
|
||||||
if (resultForFileUri.getActivitiesCount() > 0) {
|
if (resultForFileUri.getActivitiesCount() > 0) {
|
||||||
try {
|
try {
|
||||||
File file = writeAttachmentToStorage(new File(K9.getAttachmentDefaultPath()));
|
File tempFile = TemporaryAttachmentStore.getFile(context, name);
|
||||||
return createViewIntentForFileUri(resultForFileUri.getMimeType(), Uri.fromFile(file));
|
writeAttachmentToStorage(tempFile);
|
||||||
|
return createViewIntentForFileUri(resultForFileUri.getMimeType(), Uri.fromFile(tempFile));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (K9.DEBUG) {
|
if (K9.DEBUG) {
|
||||||
Log.e(K9.LOG_TAG, "Error while saving attachment to use file:// URI with ACTION_VIEW Intent", e);
|
Log.e(K9.LOG_TAG, "Error while saving attachment to use file:// URI with ACTION_VIEW Intent", e);
|
||||||
|
Loading…
Reference in New Issue
Block a user