1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.dialer.rtt; 18 19 import android.content.Context; 20 import android.support.v7.widget.RecyclerView; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import com.android.dialer.common.LogUtil; 25 import com.android.dialer.glidephotomanager.PhotoInfo; 26 27 /** Adapter class for holding RTT chat data. */ 28 public class RttTranscriptAdapter extends RecyclerView.Adapter<RttTranscriptMessageViewHolder> { 29 30 private PhotoInfo photoInfo; 31 32 private final Context context; 33 private RttTranscript rttTranscript; 34 private int firstPositionToShowTimestamp; 35 RttTranscriptAdapter(Context context)36 RttTranscriptAdapter(Context context) { 37 this.context = context; 38 } 39 40 @Override onCreateViewHolder(ViewGroup parent, int viewType)41 public RttTranscriptMessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 42 LayoutInflater layoutInflater = LayoutInflater.from(context); 43 View view = layoutInflater.inflate(R.layout.rtt_transcript_list_item, parent, false); 44 return new RttTranscriptMessageViewHolder(view); 45 } 46 47 @Override getItemViewType(int position)48 public int getItemViewType(int position) { 49 return super.getItemViewType(position); 50 } 51 52 @Override onBindViewHolder(RttTranscriptMessageViewHolder rttChatMessageViewHolder, int i)53 public void onBindViewHolder(RttTranscriptMessageViewHolder rttChatMessageViewHolder, int i) { 54 boolean isSameGroup = false; 55 boolean hasMoreInSameGroup = false; 56 RttTranscriptMessage rttTranscriptMessage = rttTranscript.getMessages(i); 57 if (i > 0) { 58 isSameGroup = 59 rttTranscriptMessage.getIsRemote() == rttTranscript.getMessages(i - 1).getIsRemote(); 60 } 61 if (i + 1 < getItemCount()) { 62 hasMoreInSameGroup = 63 rttTranscriptMessage.getIsRemote() == rttTranscript.getMessages(i + 1).getIsRemote(); 64 } 65 rttChatMessageViewHolder.setMessage(rttTranscriptMessage, isSameGroup, photoInfo); 66 if (hasMoreInSameGroup) { 67 rttChatMessageViewHolder.hideTimestamp(); 68 } else { 69 rttChatMessageViewHolder.showTimestamp( 70 rttTranscriptMessage.getTimestamp(), 71 rttTranscriptMessage.getIsRemote(), 72 i == firstPositionToShowTimestamp); 73 } 74 } 75 76 @Override getItemCount()77 public int getItemCount() { 78 if (rttTranscript == null) { 79 return 0; 80 } 81 return rttTranscript.getMessagesCount(); 82 } 83 setRttTranscript(RttTranscript rttTranscript)84 void setRttTranscript(RttTranscript rttTranscript) { 85 if (rttTranscript == null) { 86 LogUtil.w("RttTranscriptAdapter.setRttTranscript", "null RttTranscript"); 87 return; 88 } 89 this.rttTranscript = rttTranscript; 90 firstPositionToShowTimestamp = findFirstPositionToShowTimestamp(rttTranscript); 91 92 notifyDataSetChanged(); 93 } 94 95 /** 96 * Returns first position of message that should show time stamp. This is usually the last one of 97 * first grouped messages. 98 */ findFirstPositionToShowTimestamp(RttTranscript rttTranscript)99 protected static int findFirstPositionToShowTimestamp(RttTranscript rttTranscript) { 100 int i = 0; 101 while (i + 1 < rttTranscript.getMessagesCount() 102 && rttTranscript.getMessages(i).getIsRemote() 103 == rttTranscript.getMessages(i + 1).getIsRemote()) { 104 i++; 105 } 106 return i; 107 } 108 setPhotoInfo(PhotoInfo photoInfo)109 void setPhotoInfo(PhotoInfo photoInfo) { 110 this.photoInfo = photoInfo; 111 } 112 } 113