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.incallui.rtt.impl;
18 
19 import android.content.res.Resources;
20 import android.graphics.drawable.Drawable;
21 import android.support.v7.widget.RecyclerView.ViewHolder;
22 import android.view.Gravity;
23 import android.view.View;
24 import android.widget.ImageView;
25 import android.widget.LinearLayout;
26 import android.widget.LinearLayout.LayoutParams;
27 import android.widget.TextView;
28 import com.android.incallui.rtt.protocol.RttChatMessage;
29 
30 /** ViewHolder class for RTT chat message bubble. */
31 public class RttChatMessageViewHolder extends ViewHolder {
32 
33   private final TextView messageTextView;
34   private final Resources resources;
35   private final ImageView avatarImageView;
36   private final View container;
37 
RttChatMessageViewHolder(View view)38   RttChatMessageViewHolder(View view) {
39     super(view);
40     container = view.findViewById(R.id.rtt_chat_message_container);
41     messageTextView = view.findViewById(R.id.rtt_chat_message);
42     avatarImageView = view.findViewById(R.id.rtt_chat_avatar);
43     resources = view.getResources();
44   }
45 
setMessage(RttChatMessage message, boolean isSameGroup, Drawable imageDrawable)46   void setMessage(RttChatMessage message, boolean isSameGroup, Drawable imageDrawable) {
47     messageTextView.setText(message.getContent());
48     LinearLayout.LayoutParams params = (LayoutParams) container.getLayoutParams();
49     params.gravity = message.isRemote ? Gravity.START : Gravity.END;
50     params.topMargin =
51         isSameGroup
52             ? resources.getDimensionPixelSize(R.dimen.rtt_same_group_message_margin_top)
53             : resources.getDimensionPixelSize(R.dimen.rtt_message_margin_top);
54     container.setLayoutParams(params);
55     messageTextView.setEnabled(message.isRemote);
56     if (message.isRemote) {
57       if (isSameGroup) {
58         avatarImageView.setVisibility(View.INVISIBLE);
59       } else {
60         avatarImageView.setVisibility(View.VISIBLE);
61         avatarImageView.setImageDrawable(imageDrawable);
62       }
63     } else {
64       avatarImageView.setVisibility(View.GONE);
65     }
66   }
67 }
68