mirror of
https://github.com/moparisthebest/k-9
synced 2024-12-25 09:08:49 -05:00
Make attachment saving respect the new preference. Allow long-press to
choose a folder. (new constant renamed from CHOOSE_FOLDER to CHOOSE directory for clarity)
This commit is contained in:
parent
96842b3ce6
commit
ab4bae214f
@ -17,12 +17,16 @@ import com.fsck.k9.*;
|
|||||||
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.crypto.PgpData;
|
import com.fsck.k9.crypto.PgpData;
|
||||||
|
import com.fsck.k9.helper.FileBrowserHelper;
|
||||||
|
import com.fsck.k9.helper.FileBrowserHelper.FileBrowserFailOverCallback;
|
||||||
import com.fsck.k9.mail.*;
|
import com.fsck.k9.mail.*;
|
||||||
import com.fsck.k9.mail.store.StorageManager;
|
import com.fsck.k9.mail.store.StorageManager;
|
||||||
import com.fsck.k9.view.AttachmentView;
|
import com.fsck.k9.view.AttachmentView;
|
||||||
import com.fsck.k9.view.ToggleScrollView;
|
import com.fsck.k9.view.ToggleScrollView;
|
||||||
import com.fsck.k9.view.SingleMessageView;
|
import com.fsck.k9.view.SingleMessageView;
|
||||||
|
import com.fsck.k9.view.AttachmentView.AttachmentFileDownloadCallback;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class MessageView extends K9Activity implements OnClickListener {
|
public class MessageView extends K9Activity implements OnClickListener {
|
||||||
@ -33,7 +37,7 @@ public class MessageView extends K9Activity implements OnClickListener {
|
|||||||
private static final String STATE_PGP_DATA = "pgpData";
|
private static final String STATE_PGP_DATA = "pgpData";
|
||||||
private static final int ACTIVITY_CHOOSE_FOLDER_MOVE = 1;
|
private static final int ACTIVITY_CHOOSE_FOLDER_MOVE = 1;
|
||||||
private static final int ACTIVITY_CHOOSE_FOLDER_COPY = 2;
|
private static final int ACTIVITY_CHOOSE_FOLDER_COPY = 2;
|
||||||
|
private static final int ACTIVITY_CHOOSE_DIRECTORY = 3;
|
||||||
|
|
||||||
private SingleMessageView mMessageView;
|
private SingleMessageView mMessageView;
|
||||||
|
|
||||||
@ -61,6 +65,12 @@ public class MessageView extends K9Activity implements OnClickListener {
|
|||||||
private MessageViewHandler mHandler = new MessageViewHandler();
|
private MessageViewHandler mHandler = new MessageViewHandler();
|
||||||
private StorageManager.StorageListener mStorageListener = new StorageListenerImplementation();
|
private StorageManager.StorageListener mStorageListener = new StorageListenerImplementation();
|
||||||
|
|
||||||
|
/** this variable is used to save the calling AttachmentView
|
||||||
|
* until the onActivityResult is called.
|
||||||
|
* => with this reference we can identity the caller
|
||||||
|
*/
|
||||||
|
private AttachmentView attachmentTmpStore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to temporarily store the destination folder for refile operations if a confirmation
|
* Used to temporarily store the destination folder for refile operations if a confirmation
|
||||||
* dialog is shown.
|
* dialog is shown.
|
||||||
@ -295,6 +305,32 @@ public class MessageView extends K9Activity implements OnClickListener {
|
|||||||
mTopView = mToggleScrollView = (ToggleScrollView) findViewById(R.id.top_view);
|
mTopView = mToggleScrollView = (ToggleScrollView) findViewById(R.id.top_view);
|
||||||
mMessageView = (SingleMessageView) findViewById(R.id.message_view);
|
mMessageView = (SingleMessageView) findViewById(R.id.message_view);
|
||||||
|
|
||||||
|
//set a callback for the attachment view. With this callback the attachmentview
|
||||||
|
//request the start of a filebrowser activity.
|
||||||
|
mMessageView.setAttachmentCallback(new AttachmentFileDownloadCallback() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showFileBrowser(final AttachmentView caller) {
|
||||||
|
FileBrowserHelper.getInstance()
|
||||||
|
.showFileBrowserActivity(MessageView.this,
|
||||||
|
null,
|
||||||
|
MessageView.ACTIVITY_CHOOSE_DIRECTORY,
|
||||||
|
callback);
|
||||||
|
attachmentTmpStore = caller;
|
||||||
|
}
|
||||||
|
FileBrowserFailOverCallback callback = new FileBrowserFailOverCallback() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPathEntered(String path) {
|
||||||
|
attachmentTmpStore.writeFile(new File(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCancel() {
|
||||||
|
// canceled, do nothing
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
mMessageView.initialize(this);
|
mMessageView.initialize(this);
|
||||||
|
|
||||||
setTitle("");
|
setTitle("");
|
||||||
@ -712,6 +748,19 @@ public class MessageView extends K9Activity implements OnClickListener {
|
|||||||
if (resultCode != RESULT_OK)
|
if (resultCode != RESULT_OK)
|
||||||
return;
|
return;
|
||||||
switch (requestCode) {
|
switch (requestCode) {
|
||||||
|
case ACTIVITY_CHOOSE_DIRECTORY:
|
||||||
|
if (resultCode == RESULT_OK && data != null) {
|
||||||
|
// obtain the filename
|
||||||
|
Uri fileUri = data.getData();
|
||||||
|
if (fileUri != null) {
|
||||||
|
String filePath = fileUri.getPath();
|
||||||
|
if (filePath != null) {
|
||||||
|
attachmentTmpStore.writeFile(new File(filePath));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
case ACTIVITY_CHOOSE_FOLDER_MOVE:
|
case ACTIVITY_CHOOSE_FOLDER_MOVE:
|
||||||
case ACTIVITY_CHOOSE_FOLDER_COPY:
|
case ACTIVITY_CHOOSE_FOLDER_COPY:
|
||||||
if (data == null)
|
if (data == null)
|
||||||
|
@ -53,6 +53,8 @@ public class AttachmentView extends FrameLayout {
|
|||||||
public long size;
|
public long size;
|
||||||
public ImageView iconView;
|
public ImageView iconView;
|
||||||
|
|
||||||
|
private AttachmentFileDownloadCallback callback;
|
||||||
|
|
||||||
public AttachmentView(Context context, AttributeSet attrs, int defStyle) {
|
public AttachmentView(Context context, AttributeSet attrs, int defStyle) {
|
||||||
super(context, attrs, defStyle);
|
super(context, attrs, defStyle);
|
||||||
mContext = context;
|
mContext = context;
|
||||||
@ -67,7 +69,18 @@ public class AttachmentView extends FrameLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public interface AttachmentFileDownloadCallback {
|
||||||
|
/**
|
||||||
|
* this method i called by the attachmentview when
|
||||||
|
* he wants to show a filebrowser
|
||||||
|
* the provider should show the filebrowser activity
|
||||||
|
* and save the reference to the attachment view for later.
|
||||||
|
* in his onActivityResult he can get the saved reference and
|
||||||
|
* call the saveFile method of AttachmentView
|
||||||
|
* @param view
|
||||||
|
*/
|
||||||
|
public void showFileBrowser(AttachmentView caller);
|
||||||
|
}
|
||||||
public boolean populateFromPart(Part inputPart, Message message, Account account, MessagingController controller, MessagingListener listener) {
|
public boolean populateFromPart(Part inputPart, Message message, Account account, MessagingController controller, MessagingListener listener) {
|
||||||
try {
|
try {
|
||||||
part = (LocalAttachmentBodyPart) inputPart;
|
part = (LocalAttachmentBodyPart) inputPart;
|
||||||
@ -124,6 +137,14 @@ public class AttachmentView extends FrameLayout {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
downloadButton.setOnLongClickListener(new OnLongClickListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onLongClick(View v) {
|
||||||
|
callback.showFileBrowser(AttachmentView.this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
attachmentName.setText(name);
|
attachmentName.setText(name);
|
||||||
attachmentInfo.setText(SizeFormatter.formatSize(mContext, size));
|
attachmentInfo.setText(SizeFormatter.formatSize(mContext, size));
|
||||||
@ -169,9 +190,13 @@ public class AttachmentView extends FrameLayout {
|
|||||||
saveFile();
|
saveFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeFile() {
|
/**
|
||||||
|
* Writes the attachment onto the given path
|
||||||
|
* @param directory: the base dir where the file should be saved.
|
||||||
|
*/
|
||||||
|
public void writeFile(File directory) {
|
||||||
try {
|
try {
|
||||||
File file = Utility.createUniqueFile(Environment.getExternalStorageDirectory(), name);
|
File file = Utility.createUniqueFile(directory, name);
|
||||||
Uri uri = AttachmentProvider.getAttachmentUri(mAccount, part.getAttachmentId());
|
Uri uri = AttachmentProvider.getAttachmentUri(mAccount, part.getAttachmentId());
|
||||||
InputStream in = mContext.getContentResolver().openInputStream(uri);
|
InputStream in = mContext.getContentResolver().openInputStream(uri);
|
||||||
OutputStream out = new FileOutputStream(file);
|
OutputStream out = new FileOutputStream(file);
|
||||||
@ -179,14 +204,22 @@ public class AttachmentView extends FrameLayout {
|
|||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
out.close();
|
||||||
in.close();
|
in.close();
|
||||||
attachmentSaved(file.getName());
|
attachmentSaved(file.toString());
|
||||||
new MediaScannerNotifier(mContext, file);
|
new MediaScannerNotifier(mContext, file);
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
attachmentNotSaved();
|
attachmentNotSaved();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* saves the file to the defaultpath setting in the config, or if the config
|
||||||
|
* is not set => to the Environment
|
||||||
|
*/
|
||||||
|
public void writeFile() {
|
||||||
|
writeFile(new File(K9.getAttachmentDefaultPath()));
|
||||||
|
}
|
||||||
|
|
||||||
public void saveFile() {
|
public void saveFile() {
|
||||||
|
//TODO: Can the user save attachments on the internal filesystem or sd card only?
|
||||||
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
||||||
/*
|
/*
|
||||||
* Abort early if there's no place to save the attachment. We don't want to spend
|
* Abort early if there's no place to save the attachment. We don't want to spend
|
||||||
@ -259,4 +292,11 @@ public class AttachmentView extends FrameLayout {
|
|||||||
mContext.getString(R.string.message_view_status_attachment_not_saved),
|
mContext.getString(R.string.message_view_status_attachment_not_saved),
|
||||||
Toast.LENGTH_LONG).show();
|
Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
|
public AttachmentFileDownloadCallback getCallback() {
|
||||||
|
return callback;
|
||||||
|
}
|
||||||
|
public void setCallback(AttachmentFileDownloadCallback callback) {
|
||||||
|
this.callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ public class SingleMessageView extends LinearLayout {
|
|||||||
private Button mDownloadRemainder;
|
private Button mDownloadRemainder;
|
||||||
private LayoutInflater mInflater;
|
private LayoutInflater mInflater;
|
||||||
private Contacts mContacts;
|
private Contacts mContacts;
|
||||||
|
private AttachmentView.AttachmentFileDownloadCallback attachmentCallback;
|
||||||
|
|
||||||
public void initialize(Activity activity) {
|
public void initialize(Activity activity) {
|
||||||
mMessageContentView = (MessageWebView) findViewById(R.id.message_content);
|
mMessageContentView = (MessageWebView) findViewById(R.id.message_content);
|
||||||
@ -265,6 +265,7 @@ public class SingleMessageView extends LinearLayout {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
AttachmentView view = (AttachmentView)mInflater.inflate(R.layout.message_view_attachment, null);
|
AttachmentView view = (AttachmentView)mInflater.inflate(R.layout.message_view_attachment, null);
|
||||||
|
view.setCallback(attachmentCallback);
|
||||||
if (view.populateFromPart(part, message, account, controller, listener)) {
|
if (view.populateFromPart(part, message, account, controller, listener)) {
|
||||||
addAttachment(view);
|
addAttachment(view);
|
||||||
}
|
}
|
||||||
@ -299,4 +300,14 @@ public class SingleMessageView extends LinearLayout {
|
|||||||
mMessageContentView.clearView();
|
mMessageContentView.clearView();
|
||||||
mAttachments.removeAllViews();
|
mAttachments.removeAllViews();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AttachmentView.AttachmentFileDownloadCallback getAttachmentCallback() {
|
||||||
|
return attachmentCallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttachmentCallback(
|
||||||
|
AttachmentView.AttachmentFileDownloadCallback attachmentCallback) {
|
||||||
|
this.attachmentCallback = attachmentCallback;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user