Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 93d27d0a authored by wangqi's avatar wangqi Committed by Copybara-Service
Browse files

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
parent 93a51cc5
Loading
Loading
Loading
Loading
+22 −2
Original line number Diff line number Diff line
@@ -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);
  }
}
+11 −1
Original line number Diff line number Diff line
@@ -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();
    }
+40 −1
Original line number Diff line number Diff line
@@ -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() {}
}