1 /*
2  * Copyright (C) 2020 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.systemui.statusbar.notification.row;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.graphics.drawable.Icon;
22 import android.text.TextUtils;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.FrameLayout;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28 
29 import com.android.internal.widget.ConversationLayout;
30 import com.android.systemui.R;
31 
32 /**
33  * A hybrid view which may contain information about one ore more conversations.
34  */
35 public class HybridConversationNotificationView extends HybridNotificationView {
36 
37     private ImageView mConversationIconView;
38     private TextView mConversationSenderName;
39     private View mConversationFacePile;
40     private int mConversationIconSize;
41     private int mFacePileSize;
42     private int mFacePileProtectionWidth;
43 
HybridConversationNotificationView(Context context)44     public HybridConversationNotificationView(Context context) {
45         this(context, null);
46     }
47 
HybridConversationNotificationView(Context context, @Nullable AttributeSet attrs)48     public HybridConversationNotificationView(Context context, @Nullable AttributeSet attrs) {
49         this(context, attrs, 0);
50     }
51 
HybridConversationNotificationView( Context context, @Nullable AttributeSet attrs, int defStyleAttr)52     public HybridConversationNotificationView(
53             Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
54         this(context, attrs, defStyleAttr, 0);
55     }
56 
HybridConversationNotificationView( Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)57     public HybridConversationNotificationView(
58             Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
59         super(context, attrs, defStyleAttr, defStyleRes);
60     }
61 
62     @Override
onFinishInflate()63     protected void onFinishInflate() {
64         super.onFinishInflate();
65         mConversationIconView = requireViewById(com.android.internal.R.id.conversation_icon);
66         mConversationFacePile = requireViewById(com.android.internal.R.id.conversation_face_pile);
67         mConversationSenderName = requireViewById(R.id.conversation_notification_sender);
68         mFacePileSize = getResources()
69                 .getDimensionPixelSize(R.dimen.conversation_single_line_face_pile_size);
70         mConversationIconSize = getResources()
71                 .getDimensionPixelSize(R.dimen.conversation_single_line_avatar_size);
72         mFacePileProtectionWidth = getResources().getDimensionPixelSize(
73                 R.dimen.conversation_single_line_face_pile_protection_width);
74         mTransformationHelper.addViewTransformingToSimilar(mConversationIconView);
75         mTransformationHelper.addTransformedView(mConversationSenderName);
76     }
77 
78     @Override
bind(@ullable CharSequence title, @Nullable CharSequence text, @Nullable View contentView)79     public void bind(@Nullable CharSequence title, @Nullable CharSequence text,
80             @Nullable View contentView) {
81         if (!(contentView instanceof ConversationLayout)) {
82             super.bind(title, text, contentView);
83             return;
84         }
85 
86         ConversationLayout conversationLayout = (ConversationLayout) contentView;
87         Icon conversationIcon = conversationLayout.getConversationIcon();
88         if (conversationIcon != null) {
89             mConversationFacePile.setVisibility(GONE);
90             mConversationIconView.setVisibility(VISIBLE);
91             mConversationIconView.setImageIcon(conversationIcon);
92         } else {
93             // If there isn't an icon, generate a "face pile" based on the sender avatars
94             mConversationIconView.setVisibility(GONE);
95             mConversationFacePile.setVisibility(VISIBLE);
96 
97             mConversationFacePile =
98                     requireViewById(com.android.internal.R.id.conversation_face_pile);
99             ImageView facePileBottomBg = mConversationFacePile.requireViewById(
100                     com.android.internal.R.id.conversation_face_pile_bottom_background);
101             ImageView facePileBottom = mConversationFacePile.requireViewById(
102                     com.android.internal.R.id.conversation_face_pile_bottom);
103             ImageView facePileTop = mConversationFacePile.requireViewById(
104                     com.android.internal.R.id.conversation_face_pile_top);
105             conversationLayout.bindFacePile(facePileBottomBg, facePileBottom, facePileTop);
106             setSize(mConversationFacePile, mFacePileSize);
107             setSize(facePileBottom, mConversationIconSize);
108             setSize(facePileTop, mConversationIconSize);
109             setSize(facePileBottomBg, mConversationIconSize + 2 * mFacePileProtectionWidth);
110             mTransformationHelper.addViewTransformingToSimilar(facePileTop);
111             mTransformationHelper.addViewTransformingToSimilar(facePileBottom);
112             mTransformationHelper.addViewTransformingToSimilar(facePileBottomBg);
113         }
114         CharSequence conversationTitle = conversationLayout.getConversationTitle();
115         if (TextUtils.isEmpty(conversationTitle)) {
116             conversationTitle = title;
117         }
118         if (conversationLayout.isOneToOne()) {
119             mConversationSenderName.setVisibility(GONE);
120         } else {
121             mConversationSenderName.setVisibility(VISIBLE);
122             mConversationSenderName.setText(conversationLayout.getConversationSenderName());
123         }
124         CharSequence conversationText = conversationLayout.getConversationText();
125         if (TextUtils.isEmpty(conversationText)) {
126             conversationText = text;
127         }
128         super.bind(conversationTitle, conversationText, conversationLayout);
129     }
130 
setSize(View view, int size)131     private static void setSize(View view, int size) {
132         FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) view.getLayoutParams();
133         lp.width = size;
134         lp.height = size;
135         view.setLayoutParams(lp);
136     }
137 }
138