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.internal.widget;
18 
19 import android.annotation.AttrRes;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.StyleRes;
23 import android.app.Notification;
24 import android.content.Context;
25 import android.text.Layout;
26 import android.util.AttributeSet;
27 import android.util.Pools;
28 import android.view.LayoutInflater;
29 import android.widget.RemoteViews;
30 
31 import com.android.internal.R;
32 
33 /**
34  * A message of a {@link MessagingLayout}.
35  */
36 @RemoteViews.RemoteView
37 public class MessagingTextMessage extends ImageFloatingTextView implements MessagingMessage {
38 
39     private static Pools.SimplePool<MessagingTextMessage> sInstancePool
40             = new Pools.SynchronizedPool<>(20);
41     private final MessagingMessageState mState = new MessagingMessageState(this);
42 
MessagingTextMessage(@onNull Context context)43     public MessagingTextMessage(@NonNull Context context) {
44         super(context);
45     }
46 
MessagingTextMessage(@onNull Context context, @Nullable AttributeSet attrs)47     public MessagingTextMessage(@NonNull Context context, @Nullable AttributeSet attrs) {
48         super(context, attrs);
49     }
50 
MessagingTextMessage(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr)51     public MessagingTextMessage(@NonNull Context context, @Nullable AttributeSet attrs,
52             @AttrRes int defStyleAttr) {
53         super(context, attrs, defStyleAttr);
54     }
55 
MessagingTextMessage(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes)56     public MessagingTextMessage(@NonNull Context context, @Nullable AttributeSet attrs,
57             @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
58         super(context, attrs, defStyleAttr, defStyleRes);
59     }
60 
61     @Override
getState()62     public MessagingMessageState getState() {
63         return mState;
64     }
65 
66     @Override
setMessage(Notification.MessagingStyle.Message message)67     public boolean setMessage(Notification.MessagingStyle.Message message) {
68         MessagingMessage.super.setMessage(message);
69         setText(message.getText());
70         return true;
71     }
72 
createMessage(IMessagingLayout layout, Notification.MessagingStyle.Message m)73     static MessagingMessage createMessage(IMessagingLayout layout,
74             Notification.MessagingStyle.Message m) {
75         MessagingLinearLayout messagingLinearLayout = layout.getMessagingLinearLayout();
76         MessagingTextMessage createdMessage = sInstancePool.acquire();
77         if (createdMessage == null) {
78             createdMessage = (MessagingTextMessage) LayoutInflater.from(
79                     layout.getContext()).inflate(
80                             R.layout.notification_template_messaging_text_message,
81                             messagingLinearLayout,
82                             false);
83             createdMessage.addOnLayoutChangeListener(MessagingLayout.MESSAGING_PROPERTY_ANIMATOR);
84         }
85         createdMessage.setMessage(m);
86         return createdMessage;
87     }
88 
recycle()89     public void recycle() {
90         MessagingMessage.super.recycle();
91         sInstancePool.release(this);
92     }
93 
dropCache()94     public static void dropCache() {
95         sInstancePool = new Pools.SynchronizedPool<>(10);
96     }
97 
98     @Override
getMeasuredType()99     public int getMeasuredType() {
100         boolean measuredTooSmall = getMeasuredHeight()
101                 < getLayoutHeight() + getPaddingTop() + getPaddingBottom();
102         if (measuredTooSmall && getLineCount() <= 1) {
103             return MEASURED_TOO_SMALL;
104         } else {
105             Layout layout = getLayout();
106             if (layout == null) {
107                 return MEASURED_TOO_SMALL;
108             }
109             if (layout.getEllipsisCount(layout.getLineCount() - 1) > 0) {
110                 return MEASURED_SHORTENED;
111             } else {
112                 return MEASURED_NORMAL;
113             }
114         }
115     }
116 
117     @Override
118     public void setMaxDisplayedLines(int lines) {
119         setMaxLines(lines);
120     }
121 
122     @Override
123     public int getConsumedLines() {
124         return getLineCount();
125     }
126 
127     public int getLayoutHeight() {
128         Layout layout = getLayout();
129         if (layout == null) {
130             return 0;
131         }
132         return layout.getHeight();
133     }
134 
135     @Override
136     public void setColor(int color) {
137         setTextColor(color);
138     }
139 }
140