diff options
author | wangqi <wangqi@google.com> | 2018-03-09 10:14:47 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-03-09 11:10:35 -0800 |
commit | 93d27d0a5db53bc7cb7b03c58889667887d5816b (patch) | |
tree | a8fe62c062534b95caeeb29cdfe74e5c39dd686b | |
parent | 93a51cc5d641c45bcc4d8bcd47b715919935b918 (diff) |
Save RTT chat history during the call.
Thus putting call into background won't lose the history.
This is only stored in memory by making RttChatMessage Parcelable and saved
along with fragment's life cycle.
Bug: 67596257
Test: manual
PiperOrigin-RevId: 188500104
Change-Id: I11e8e55f0475defd9c3b9a8cc10db4186392ddd8
3 files changed, 73 insertions, 4 deletions
diff --git a/java/com/android/incallui/rtt/impl/RttChatAdapter.java b/java/com/android/incallui/rtt/impl/RttChatAdapter.java index 912314945..7e2b571c1 100644 --- a/java/com/android/incallui/rtt/impl/RttChatAdapter.java +++ b/java/com/android/incallui/rtt/impl/RttChatAdapter.java @@ -17,6 +17,9 @@ package com.android.incallui.rtt.impl; import android.content.Context; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.view.LayoutInflater; @@ -33,15 +36,26 @@ public class RttChatAdapter extends RecyclerView.Adapter<RttChatMessageViewHolde void newMessageAdded(); } + private static final String KEY_MESSAGE_DATA = "key_message_data"; + private static final String KEY_LAST_REMOTE_MESSAGE = "key_last_remote_message"; + private static final String KEY_LAST_LOCAL_MESSAGE = "key_last_local_message"; + private final Context context; - private final List<RttChatMessage> rttMessages = new ArrayList<>(); + private final List<RttChatMessage> rttMessages; private int lastIndexOfLocalMessage = -1; private int lastIndexOfRemoteMessage = -1; private final MessageListener messageListener; - RttChatAdapter(Context context, MessageListener listener) { + RttChatAdapter(Context context, MessageListener listener, @Nullable Bundle savedInstanceState) { this.context = context; this.messageListener = listener; + if (savedInstanceState == null) { + rttMessages = new ArrayList<>(); + } else { + rttMessages = savedInstanceState.getParcelableArrayList(KEY_MESSAGE_DATA); + lastIndexOfRemoteMessage = savedInstanceState.getInt(KEY_LAST_REMOTE_MESSAGE); + lastIndexOfLocalMessage = savedInstanceState.getInt(KEY_LAST_LOCAL_MESSAGE); + } } @Override @@ -161,4 +175,10 @@ public class RttChatAdapter extends RecyclerView.Adapter<RttChatMessageViewHolde messageListener.newMessageAdded(); } } + + void onSaveInstanceState(@NonNull Bundle bundle) { + bundle.putParcelableArrayList(KEY_MESSAGE_DATA, (ArrayList<RttChatMessage>) rttMessages); + bundle.putInt(KEY_LAST_REMOTE_MESSAGE, lastIndexOfRemoteMessage); + bundle.putInt(KEY_LAST_LOCAL_MESSAGE, lastIndexOfLocalMessage); + } } diff --git a/java/com/android/incallui/rtt/impl/RttChatFragment.java b/java/com/android/incallui/rtt/impl/RttChatFragment.java index f9c91e5fb..9e8a24a48 100644 --- a/java/com/android/incallui/rtt/impl/RttChatFragment.java +++ b/java/com/android/incallui/rtt/impl/RttChatFragment.java @@ -124,6 +124,8 @@ public class RttChatFragment extends Fragment if (savedInstanceState != null) { inCallButtonUiDelegate.onRestoreInstanceState(savedInstanceState); } + // Prevent updating local message until UI is ready. + isClearingInput = true; } @Override @@ -158,7 +160,7 @@ public class RttChatFragment extends Fragment layoutManager.setStackFromEnd(true); recyclerView.setLayoutManager(layoutManager); recyclerView.setHasFixedSize(false); - adapter = new RttChatAdapter(getContext(), this); + adapter = new RttChatAdapter(getContext(), this, savedInstanceState); recyclerView.setAdapter(adapter); recyclerView.addOnScrollListener(onScrollListener); submitButton = view.findViewById(R.id.rtt_chat_submit_button); @@ -242,13 +244,21 @@ public class RttChatFragment extends Fragment public void onStart() { LogUtil.enterBlock("RttChatFragment.onStart"); super.onStart(); + isClearingInput = false; onRttScreenStart(); } @Override + public void onSaveInstanceState(@NonNull Bundle bundle) { + super.onSaveInstanceState(bundle); + adapter.onSaveInstanceState(bundle); + } + + @Override public void onStop() { LogUtil.enterBlock("RttChatFragment.onStop"); super.onStop(); + isClearingInput = true; if (overflowMenu.isShowing()) { overflowMenu.dismiss(); } diff --git a/java/com/android/incallui/rtt/impl/RttChatMessage.java b/java/com/android/incallui/rtt/impl/RttChatMessage.java index 6c852cf92..fd83fb82f 100644 --- a/java/com/android/incallui/rtt/impl/RttChatMessage.java +++ b/java/com/android/incallui/rtt/impl/RttChatMessage.java @@ -16,6 +16,8 @@ package com.android.incallui.rtt.impl; +import android.os.Parcel; +import android.os.Parcelable; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.android.incallui.rtt.protocol.Constants; @@ -25,7 +27,7 @@ import java.util.Iterator; import java.util.List; /** Message class that holds one RTT chat content. */ -final class RttChatMessage { +final class RttChatMessage implements Parcelable { private static final Splitter SPLITTER = Splitter.on(Constants.BUBBLE_BREAKER); @@ -119,4 +121,41 @@ final class RttChatMessage { return messageList; } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(getContent()); + boolean[] values = new boolean[2]; + values[0] = isRemote; + values[1] = isFinished; + dest.writeBooleanArray(values); + } + + public static final Parcelable.Creator<RttChatMessage> CREATOR = + new Parcelable.Creator<RttChatMessage>() { + @Override + public RttChatMessage createFromParcel(Parcel in) { + return new RttChatMessage(in); + } + + @Override + public RttChatMessage[] newArray(int size) { + return new RttChatMessage[size]; + } + }; + + private RttChatMessage(Parcel in) { + content.append(in.readString()); + boolean[] values = new boolean[2]; + in.readBooleanArray(values); + isRemote = values[0]; + isFinished = values[1]; + } + + RttChatMessage() {} } |