1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-02-17 07:30:16 -05:00

Fix "Show pictures" button functionality

This commit is contained in:
cketti 2015-02-17 03:37:37 +01:00
parent 68147880ce
commit e15cda8504
3 changed files with 107 additions and 59 deletions

View File

@ -32,6 +32,7 @@ import android.widget.Toast;
import com.fsck.k9.R; import com.fsck.k9.R;
import com.fsck.k9.helper.ClipboardManager; import com.fsck.k9.helper.ClipboardManager;
import com.fsck.k9.helper.Contacts; import com.fsck.k9.helper.Contacts;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mailstore.AttachmentViewInfo; import com.fsck.k9.mailstore.AttachmentViewInfo;
@ -66,11 +67,9 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
private LinearLayout mAttachments; private LinearLayout mAttachments;
private Button mShowHiddenAttachments; private Button mShowHiddenAttachments;
private LinearLayout mHiddenAttachments; private LinearLayout mHiddenAttachments;
private View mShowPicturesAction; private boolean showingPictures;
private boolean mShowPictures;
private boolean mHasAttachments; private boolean mHasAttachments;
private LayoutInflater mInflater; private LayoutInflater mInflater;
private Contacts mContacts;
private AttachmentViewCallback attachmentCallback; private AttachmentViewCallback attachmentCallback;
private OpenPgpHeaderViewCallback openPgpHeaderViewCallback; private OpenPgpHeaderViewCallback openPgpHeaderViewCallback;
private View mAttachmentsContainer; private View mAttachmentsContainer;
@ -80,12 +79,14 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
private Map<AttachmentViewInfo, AttachmentView> attachments = new HashMap<AttachmentViewInfo, AttachmentView>(); private Map<AttachmentViewInfo, AttachmentView> attachments = new HashMap<AttachmentViewInfo, AttachmentView>();
private boolean displayPgpHeader; private boolean displayPgpHeader;
private OpenPgpHeaderView openPgpHeaderView; private OpenPgpHeaderView openPgpHeaderView;
private ShowPicturesController showPicturesController;
public void initialize(AttachmentViewCallback attachmentCallback, public void initialize(ShowPicturesController showPicturesController, AttachmentViewCallback attachmentCallback,
OpenPgpHeaderViewCallback openPgpHeaderViewCallback, boolean displayPgpHeader) { OpenPgpHeaderViewCallback openPgpHeaderViewCallback, boolean displayPgpHeader) {
Context context = getContext(); Context context = getContext();
this.showPicturesController = showPicturesController;
this.attachmentCallback = attachmentCallback; this.attachmentCallback = attachmentCallback;
this.openPgpHeaderViewCallback = openPgpHeaderViewCallback; this.openPgpHeaderViewCallback = openPgpHeaderViewCallback;
this.displayPgpHeader = displayPgpHeader; this.displayPgpHeader = displayPgpHeader;
@ -102,11 +103,8 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
mHiddenAttachments.setVisibility(View.GONE); mHiddenAttachments.setVisibility(View.GONE);
mShowHiddenAttachments = (Button) findViewById(R.id.show_hidden_attachments); mShowHiddenAttachments = (Button) findViewById(R.id.show_hidden_attachments);
mShowHiddenAttachments.setVisibility(View.GONE); mShowHiddenAttachments.setVisibility(View.GONE);
mShowPicturesAction = findViewById(R.id.show_pictures);
mShowPictures = false; showingPictures = false;
mContacts = Contacts.getInstance(context);
mInflater = LayoutInflater.from(context); mInflater = LayoutInflater.from(context);
mMessageContentView.setVisibility(View.VISIBLE); mMessageContentView.setVisibility(View.VISIBLE);
@ -124,7 +122,6 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
setBackgroundColor(outValue.data); setBackgroundColor(outValue.data);
mShowHiddenAttachments.setOnClickListener(this); mShowHiddenAttachments.setOnClickListener(this);
// mShowPicturesAction.setOnClickListener(this);
mClipboardManager = ClipboardManager.getInstance(context); mClipboardManager = ClipboardManager.getInstance(context);
} }
@ -352,13 +349,6 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
onShowHiddenAttachments(); onShowHiddenAttachments();
break; break;
} }
case R.id.show_pictures: {
// Allow network access first...
setLoadPictures(true);
// ...then re-populate the WebView with the message text
loadBodyFromText(mText);
break;
}
} }
} }
@ -372,29 +362,18 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
} }
public boolean showPictures() { private boolean isShowingPictures() {
return mShowPictures; return showingPictures;
} }
public void setShowPictures(Boolean show) { private void setLoadPictures(boolean enable) {
mShowPictures = show;
}
/**
* Enable/disable image loading of the WebView. But always hide the
* "Show pictures" button!
*
* @param enable true, if (network) images should be loaded.
* false, otherwise.
*/
public void setLoadPictures(boolean enable) {
mMessageContentView.blockNetworkData(!enable); mMessageContentView.blockNetworkData(!enable);
setShowPictures(enable); showingPictures = enable;
showShowPicturesAction(false);
} }
public void showShowPicturesAction(boolean show) { public void showPictures() {
// mShowPicturesAction.setVisibility(show ? View.VISIBLE : View.GONE); setLoadPictures(true);
loadBodyFromText(mText);
} }
public void enableAttachmentButtons() { public void enableAttachmentButtons() {
@ -409,7 +388,8 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
} }
} }
public void setMessageViewContainer(MessageViewContainer messageViewContainer) throws MessagingException { public void setMessageViewContainer(MessageViewContainer messageViewContainer, boolean automaticallyLoadPictures)
throws MessagingException {
resetView(); resetView();
WebViewClient webViewClient = K9WebViewClient.newInstance(messageViewContainer.rootPart); WebViewClient webViewClient = K9WebViewClient.newInstance(messageViewContainer.rootPart);
@ -433,7 +413,7 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
boolean lookForImages = true; boolean lookForImages = true;
if (mSavedState != null) { if (mSavedState != null) {
if (mSavedState.showPictures) { if (mSavedState.showingPictures) {
setLoadPictures(true); setLoadPictures(true);
lookForImages = false; lookForImages = false;
} }
@ -445,25 +425,15 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
mSavedState = null; mSavedState = null;
} }
/* if (mText != null && lookForImages) {
if (text != null && lookForImages) { if (Utility.hasExternalImages(mText) && !isShowingPictures()) {
// If the message contains external pictures and the "Show pictures" if (automaticallyLoadPictures) {
// button wasn't already pressed, see if the user's preferences has us
// showing them anyway.
if (Utility.hasExternalImages(text) && !showPictures()) {
Address[] from = messageViewContainer.message.getFrom();
if ((account.getShowPictures() == Account.ShowPictures.ALWAYS) ||
((account.getShowPictures() == Account.ShowPictures.ONLY_FROM_CONTACTS) &&
// Make sure we have at least one from address
(from != null && from.length > 0) &&
mContacts.isInContacts(from[0].getAddress()))) {
setLoadPictures(true); setLoadPictures(true);
} else { } else {
showShowPicturesAction(true); showPicturesController.notifyMessageContainerContainsPictures(this);
} }
} }
} }
*/
if (displayPgpHeader) { if (displayPgpHeader) {
openPgpHeaderView.setOpenPgpData(messageViewContainer.signatureResult, messageViewContainer.encrypted, openPgpHeaderView.setOpenPgpData(messageViewContainer.signatureResult, messageViewContainer.encrypted,
@ -527,7 +497,6 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
public void resetView() { public void resetView() {
setLoadPictures(false); setLoadPictures(false);
showShowPicturesAction(false);
mAttachments.removeAllViews(); mAttachments.removeAllViews();
mHiddenAttachments.removeAllViews(); mHiddenAttachments.removeAllViews();
@ -551,7 +520,7 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
mAttachmentsContainer.getVisibility() == View.VISIBLE); mAttachmentsContainer.getVisibility() == View.VISIBLE);
savedState.hiddenAttachmentsVisible = (mHiddenAttachments != null && savedState.hiddenAttachmentsVisible = (mHiddenAttachments != null &&
mHiddenAttachments.getVisibility() == View.VISIBLE); mHiddenAttachments.getVisibility() == View.VISIBLE);
savedState.showPictures = mShowPictures; savedState.showingPictures = showingPictures;
return savedState; return savedState;
} }
@ -595,7 +564,7 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
static class SavedState extends BaseSavedState { static class SavedState extends BaseSavedState {
boolean attachmentViewVisible; boolean attachmentViewVisible;
boolean hiddenAttachmentsVisible; boolean hiddenAttachmentsVisible;
boolean showPictures; boolean showingPictures;
public static final Parcelable.Creator<SavedState> CREATOR = public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() { new Parcelable.Creator<SavedState>() {
@ -619,7 +588,7 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
super(in); super(in);
this.attachmentViewVisible = (in.readInt() != 0); this.attachmentViewVisible = (in.readInt() != 0);
this.hiddenAttachmentsVisible = (in.readInt() != 0); this.hiddenAttachmentsVisible = (in.readInt() != 0);
this.showPictures = (in.readInt() != 0); this.showingPictures = (in.readInt() != 0);
} }
@Override @Override
@ -627,7 +596,7 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
super.writeToParcel(out, flags); super.writeToParcel(out, flags);
out.writeInt((this.attachmentViewVisible) ? 1 : 0); out.writeInt((this.attachmentViewVisible) ? 1 : 0);
out.writeInt((this.hiddenAttachmentsVisible) ? 1 : 0); out.writeInt((this.hiddenAttachmentsVisible) ? 1 : 0);
out.writeInt((this.showPictures) ? 1 : 0); out.writeInt((this.showingPictures) ? 1 : 0);
} }
} }
} }

View File

@ -1,5 +1,8 @@
package com.fsck.k9.ui.messageview; package com.fsck.k9.ui.messageview;
import java.util.ArrayList;
import java.util.List;
import android.app.Fragment; import android.app.Fragment;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
@ -11,8 +14,11 @@ import android.widget.Button;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import com.fsck.k9.Account; import com.fsck.k9.Account;
import com.fsck.k9.Account.ShowPictures;
import com.fsck.k9.K9; import com.fsck.k9.K9;
import com.fsck.k9.R; import com.fsck.k9.R;
import com.fsck.k9.helper.Contacts;
import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.Message; import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
@ -21,7 +27,7 @@ import com.fsck.k9.mailstore.MessageViewInfo.MessageViewContainer;
import com.fsck.k9.view.MessageHeader; import com.fsck.k9.view.MessageHeader;
public class MessageTopView extends LinearLayout { public class MessageTopView extends LinearLayout implements ShowPicturesController {
private MessageHeader mHeaderContainer; private MessageHeader mHeaderContainer;
private LayoutInflater mInflater; private LayoutInflater mInflater;
@ -30,6 +36,9 @@ public class MessageTopView extends LinearLayout {
private Button mDownloadRemainder; private Button mDownloadRemainder;
private AttachmentViewCallback attachmentCallback; private AttachmentViewCallback attachmentCallback;
private OpenPgpHeaderViewCallback openPgpHeaderViewCallback; private OpenPgpHeaderViewCallback openPgpHeaderViewCallback;
private Button showPicturesButton;
private List<MessageContainerView> messageContainerViewsWithPictures = new ArrayList<MessageContainerView>();
public MessageTopView(Context context, AttributeSet attrs) { public MessageTopView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
@ -52,8 +61,27 @@ public class MessageTopView extends LinearLayout {
mDownloadRemainder = (Button) findViewById(R.id.download_remainder); mDownloadRemainder = (Button) findViewById(R.id.download_remainder);
mDownloadRemainder.setVisibility(View.GONE); mDownloadRemainder.setVisibility(View.GONE);
containerViews = (LinearLayout) findViewById(R.id.message_containers); showPicturesButton = (Button) findViewById(R.id.show_pictures);
setShowPicturesButtonListener();
containerViews = (LinearLayout) findViewById(R.id.message_containers);
}
private void setShowPicturesButtonListener() {
showPicturesButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showPicturesInAllContainerViews();
}
});
}
private void showPicturesInAllContainerViews() {
for (MessageContainerView containerView : messageContainerViewsWithPictures) {
containerView.showPictures();
}
hideShowPicturesButton();
} }
public void resetView() { public void resetView() {
@ -65,11 +93,15 @@ public class MessageTopView extends LinearLayout {
throws MessagingException { throws MessagingException {
resetView(); resetView();
ShowPictures showPicturesSetting = account.getShowPictures();
boolean automaticallyLoadPictures =
shouldAutomaticallyLoadPictures(showPicturesSetting, messageViewInfo.message);
for (MessageViewContainer container : messageViewInfo.containers) { for (MessageViewContainer container : messageViewInfo.containers) {
MessageContainerView view = (MessageContainerView) mInflater.inflate(R.layout.message_container, null); MessageContainerView view = (MessageContainerView) mInflater.inflate(R.layout.message_container, null);
boolean displayPgpHeader = !Account.NO_OPENPGP_PROVIDER.equals(account.getOpenPgpProvider()); boolean displayPgpHeader = !Account.NO_OPENPGP_PROVIDER.equals(account.getOpenPgpProvider());
view.initialize(attachmentCallback, openPgpHeaderViewCallback, displayPgpHeader); view.initialize(this, attachmentCallback, openPgpHeaderViewCallback, displayPgpHeader);
view.setMessageViewContainer(container); view.setMessageViewContainer(container, automaticallyLoadPictures);
containerViews.addView(view); containerViews.addView(view);
} }
@ -133,4 +165,45 @@ public class MessageTopView extends LinearLayout {
} }
} }
private void showShowPicturesButton() {
showPicturesButton.setVisibility(View.VISIBLE);
}
private void hideShowPicturesButton() {
showPicturesButton.setVisibility(View.GONE);
}
@Override
public void notifyMessageContainerContainsPictures(MessageContainerView messageContainerView) {
messageContainerViewsWithPictures.add(messageContainerView);
showShowPicturesButton();
}
private boolean shouldAutomaticallyLoadPictures(ShowPictures showPicturesSetting, Message message) {
return showPicturesSetting == ShowPictures.ALWAYS || shouldShowPicturesFromSender(showPicturesSetting, message);
}
private boolean shouldShowPicturesFromSender(ShowPictures showPicturesSetting, Message message) {
if (showPicturesSetting != ShowPictures.ONLY_FROM_CONTACTS) {
return false;
}
String senderEmailAddress = getSenderEmailAddress(message);
if (senderEmailAddress == null) {
return false;
}
Contacts contacts = Contacts.getInstance(getContext());
return contacts.isInContacts(senderEmailAddress);
}
private String getSenderEmailAddress(Message message) {
Address[] from = message.getFrom();
if (from == null || from.length == 0) {
return null;
}
return from[0].getAddress();
}
} }

View File

@ -0,0 +1,6 @@
package com.fsck.k9.ui.messageview;
interface ShowPicturesController {
void notifyMessageContainerContainsPictures(MessageContainerView messageContainerView);
}