From b15777bd3a33711ccb3051971e9fa42f65d88eee Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 12 Sep 2018 22:20:19 +0200 Subject: [PATCH] make media previews survive rotations --- .../ui/ConversationFragment.java | 18 +++++++++- .../ui/adapter/MediaPreviewAdapter.java | 4 +-- .../conversations/ui/util/Attachment.java | 36 ++++++++++++++++++- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index fbc707f9a..2190b61b5 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -139,12 +139,14 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke public static final String RECENTLY_USED_QUICK_ACTION = "recently_used_quick_action"; public static final String STATE_CONVERSATION_UUID = ConversationFragment.class.getName() + ".uuid"; public static final String STATE_SCROLL_POSITION = ConversationFragment.class.getName() + ".scroll_position"; - public static final String STATE_PHOTO_URI = ConversationFragment.class.getName() + ".take_photo_uri"; + public static final String STATE_PHOTO_URI = ConversationFragment.class.getName() + ".media_previews"; + public static final String STATE_MEDIA_PREVIEWS = ConversationFragment.class.getName() + ".take_photo_uri"; private static final String STATE_LAST_MESSAGE_UUID = "state_last_message_uuid"; private final List messageList = new ArrayList<>(); private final PendingItem postponedActivityResult = new PendingItem<>(); private final PendingItem pendingConversationsUuid = new PendingItem<>(); + private final PendingItem> pendingMediaPreviews = new PendingItem<>(); private final PendingItem pendingExtras = new PendingItem<>(); private final PendingItem pendingTakePhotoUri = new PendingItem<>(); private final PendingItem pendingScrollState = new PendingItem<>(); @@ -1773,6 +1775,10 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke if (scrollState != null) { outState.putParcelable(STATE_SCROLL_POSITION, scrollState); } + final ArrayList attachments = mediaPreviewAdapter.getAttachments(); + if (attachments.size() > 0) { + outState.putParcelableArrayList(STATE_MEDIA_PREVIEWS, attachments); + } } } @@ -1783,10 +1789,14 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke return; } String uuid = savedInstanceState.getString(STATE_CONVERSATION_UUID); + ArrayList attachments = savedInstanceState.getParcelableArrayList(STATE_MEDIA_PREVIEWS); pendingLastMessageUuid.push(savedInstanceState.getString(STATE_LAST_MESSAGE_UUID, null)); if (uuid != null) { QuickLoader.set(uuid); this.pendingConversationsUuid.push(uuid); + if (attachments != null && attachments.size() > 0) { + this.pendingMediaPreviews.push(attachments); + } String takePhotoUri = savedInstanceState.getString(STATE_PHOTO_URI); if (takePhotoUri != null) { pendingTakePhotoUri.push(Uri.parse(takePhotoUri)); @@ -2637,9 +2647,15 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke reInit(conversation); ScrollState scrollState = pendingScrollState.pop(); String lastMessageUuid = pendingLastMessageUuid.pop(); + List attachments = pendingMediaPreviews.pop(); if (scrollState != null) { setScrollPosition(scrollState, lastMessageUuid); } + if (attachments != null && attachments.size() > 0) { + Log.d(Config.LOGTAG,"had attachments on restore"); + mediaPreviewAdapter.addMediaPreviews(attachments); + toggleInputMethod(); + } return true; } diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MediaPreviewAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MediaPreviewAdapter.java index 109a3b7d9..73117143e 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MediaPreviewAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MediaPreviewAdapter.java @@ -29,7 +29,7 @@ import eu.siacs.conversations.ui.util.StyledAttributes; public class MediaPreviewAdapter extends RecyclerView.Adapter { - private final List mediaPreviews = new ArrayList<>(); + private final ArrayList mediaPreviews = new ArrayList<>(); private final ConversationFragment conversationFragment; @@ -150,7 +150,7 @@ public class MediaPreviewAdapter extends RecyclerView.Adapter 0; } - public List getAttachments() { + public ArrayList getAttachments() { return mediaPreviews; } diff --git a/src/main/java/eu/siacs/conversations/ui/util/Attachment.java b/src/main/java/eu/siacs/conversations/ui/util/Attachment.java index cc311d9e1..b9726f70f 100644 --- a/src/main/java/eu/siacs/conversations/ui/util/Attachment.java +++ b/src/main/java/eu/siacs/conversations/ui/util/Attachment.java @@ -33,6 +33,8 @@ import android.content.ClipData; import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.os.Parcel; +import android.os.Parcelable; import android.util.Log; import java.util.ArrayList; @@ -44,7 +46,39 @@ import eu.siacs.conversations.Config; import eu.siacs.conversations.utils.MimeUtils; import eu.siacs.conversations.xmpp.stanzas.IqPacket; -public class Attachment { +public class Attachment implements Parcelable { + + Attachment(Parcel in) { + uri = in.readParcelable(Uri.class.getClassLoader()); + mime = in.readString(); + uuid = UUID.fromString(in.readString()); + type = Type.valueOf(in.readString()); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeParcelable(uri, flags); + dest.writeString(mime); + dest.writeString(uuid.toString()); + dest.writeString(type.toString()); + } + + @Override + public int describeContents() { + return 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public Attachment createFromParcel(Parcel in) { + return new Attachment(in); + } + + @Override + public Attachment[] newArray(int size) { + return new Attachment[size]; + } + }; public String getMime() { return mime;