make media previews survive rotations

This commit is contained in:
Daniel Gultsch 2018-09-12 22:20:19 +02:00
parent fbc1d242ca
commit b15777bd3a
3 changed files with 54 additions and 4 deletions

View file

@ -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 RECENTLY_USED_QUICK_ACTION = "recently_used_quick_action";
public static final String STATE_CONVERSATION_UUID = ConversationFragment.class.getName() + ".uuid"; 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_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 static final String STATE_LAST_MESSAGE_UUID = "state_last_message_uuid";
private final List<Message> messageList = new ArrayList<>(); private final List<Message> messageList = new ArrayList<>();
private final PendingItem<ActivityResult> postponedActivityResult = new PendingItem<>(); private final PendingItem<ActivityResult> postponedActivityResult = new PendingItem<>();
private final PendingItem<String> pendingConversationsUuid = new PendingItem<>(); private final PendingItem<String> pendingConversationsUuid = new PendingItem<>();
private final PendingItem<ArrayList<Attachment>> pendingMediaPreviews = new PendingItem<>();
private final PendingItem<Bundle> pendingExtras = new PendingItem<>(); private final PendingItem<Bundle> pendingExtras = new PendingItem<>();
private final PendingItem<Uri> pendingTakePhotoUri = new PendingItem<>(); private final PendingItem<Uri> pendingTakePhotoUri = new PendingItem<>();
private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>(); private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>();
@ -1773,6 +1775,10 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
if (scrollState != null) { if (scrollState != null) {
outState.putParcelable(STATE_SCROLL_POSITION, scrollState); outState.putParcelable(STATE_SCROLL_POSITION, scrollState);
} }
final ArrayList<Attachment> 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; return;
} }
String uuid = savedInstanceState.getString(STATE_CONVERSATION_UUID); String uuid = savedInstanceState.getString(STATE_CONVERSATION_UUID);
ArrayList<Attachment> attachments = savedInstanceState.getParcelableArrayList(STATE_MEDIA_PREVIEWS);
pendingLastMessageUuid.push(savedInstanceState.getString(STATE_LAST_MESSAGE_UUID, null)); pendingLastMessageUuid.push(savedInstanceState.getString(STATE_LAST_MESSAGE_UUID, null));
if (uuid != null) { if (uuid != null) {
QuickLoader.set(uuid); QuickLoader.set(uuid);
this.pendingConversationsUuid.push(uuid); this.pendingConversationsUuid.push(uuid);
if (attachments != null && attachments.size() > 0) {
this.pendingMediaPreviews.push(attachments);
}
String takePhotoUri = savedInstanceState.getString(STATE_PHOTO_URI); String takePhotoUri = savedInstanceState.getString(STATE_PHOTO_URI);
if (takePhotoUri != null) { if (takePhotoUri != null) {
pendingTakePhotoUri.push(Uri.parse(takePhotoUri)); pendingTakePhotoUri.push(Uri.parse(takePhotoUri));
@ -2637,9 +2647,15 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
reInit(conversation); reInit(conversation);
ScrollState scrollState = pendingScrollState.pop(); ScrollState scrollState = pendingScrollState.pop();
String lastMessageUuid = pendingLastMessageUuid.pop(); String lastMessageUuid = pendingLastMessageUuid.pop();
List<Attachment> attachments = pendingMediaPreviews.pop();
if (scrollState != null) { if (scrollState != null) {
setScrollPosition(scrollState, lastMessageUuid); setScrollPosition(scrollState, lastMessageUuid);
} }
if (attachments != null && attachments.size() > 0) {
Log.d(Config.LOGTAG,"had attachments on restore");
mediaPreviewAdapter.addMediaPreviews(attachments);
toggleInputMethod();
}
return true; return true;
} }

View file

@ -29,7 +29,7 @@ import eu.siacs.conversations.ui.util.StyledAttributes;
public class MediaPreviewAdapter extends RecyclerView.Adapter<MediaPreviewAdapter.MediaPreviewViewHolder> { public class MediaPreviewAdapter extends RecyclerView.Adapter<MediaPreviewAdapter.MediaPreviewViewHolder> {
private final List<Attachment> mediaPreviews = new ArrayList<>(); private final ArrayList<Attachment> mediaPreviews = new ArrayList<>();
private final ConversationFragment conversationFragment; private final ConversationFragment conversationFragment;
@ -150,7 +150,7 @@ public class MediaPreviewAdapter extends RecyclerView.Adapter<MediaPreviewAdapte
return mediaPreviews.size() > 0; return mediaPreviews.size() > 0;
} }
public List<Attachment> getAttachments() { public ArrayList<Attachment> getAttachments() {
return mediaPreviews; return mediaPreviews;
} }

View file

@ -33,6 +33,8 @@ import android.content.ClipData;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log; import android.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
@ -44,7 +46,39 @@ import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.MimeUtils; import eu.siacs.conversations.utils.MimeUtils;
import eu.siacs.conversations.xmpp.stanzas.IqPacket; 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<Attachment> CREATOR = new Creator<Attachment>() {
@Override
public Attachment createFromParcel(Parcel in) {
return new Attachment(in);
}
@Override
public Attachment[] newArray(int size) {
return new Attachment[size];
}
};
public String getMime() { public String getMime() {
return mime; return mime;