blob: fb4e2eccadc0d61d49442bbe7c5512b4269b1693 [file] [log] [blame]
Selim Cinek4ffd6362015-12-29 15:12:23 +01001/*
2 * Copyright (C) 2016 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
17package com.android.systemui.statusbar.notification;
18
Selim Cinek049f6ad2016-03-01 12:06:57 -080019import android.text.Layout;
Selim Cinek4ffd6362015-12-29 15:12:23 +010020import android.text.TextUtils;
21import android.util.Pools;
22import android.view.View;
23import android.widget.TextView;
24
25/**
26 * A transform state of a mText view.
27*/
28public class TextViewTransformState extends TransformState {
29
30 private static Pools.SimplePool<TextViewTransformState> sInstancePool
31 = new Pools.SimplePool<>(40);
Selim Cinek049f6ad2016-03-01 12:06:57 -080032 private TextView mText;
Selim Cinek4ffd6362015-12-29 15:12:23 +010033
34 @Override
35 public void initFrom(View view) {
36 super.initFrom(view);
37 if (view instanceof TextView) {
Selim Cinek049f6ad2016-03-01 12:06:57 -080038 mText = (TextView) view;
Selim Cinek4ffd6362015-12-29 15:12:23 +010039 }
40 }
41
42 @Override
43 protected boolean sameAs(TransformState otherState) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -070044 if (super.sameAs(otherState)) {
45 return true;
46 }
Selim Cinek4ffd6362015-12-29 15:12:23 +010047 if (otherState instanceof TextViewTransformState) {
48 TextViewTransformState otherTvs = (TextViewTransformState) otherState;
Selim Cinek049f6ad2016-03-01 12:06:57 -080049 if(TextUtils.equals(otherTvs.mText.getText(), mText.getText())) {
50 int ownEllipsized = getEllipsisCount();
51 int otherEllipsized = otherTvs.getEllipsisCount();
Selim Cinek2268bcf2016-03-10 18:06:54 -080052 return ownEllipsized == otherEllipsized
Adrian Roos237ffa42016-06-02 13:15:09 -070053 && getInnerHeight(mText) == getInnerHeight(otherTvs.mText);
Selim Cinek049f6ad2016-03-01 12:06:57 -080054 }
Selim Cinek4ffd6362015-12-29 15:12:23 +010055 }
Selim Cinek5d6ef8d2017-05-18 22:16:00 -070056 return false;
Selim Cinek4ffd6362015-12-29 15:12:23 +010057 }
58
Adrian Roos237ffa42016-06-02 13:15:09 -070059 private int getInnerHeight(TextView text) {
60 return text.getHeight() - text.getPaddingTop() - text.getPaddingBottom();
61 }
62
Selim Cinek049f6ad2016-03-01 12:06:57 -080063 private int getEllipsisCount() {
64 Layout l = mText.getLayout();
65 if (l != null) {
66 int lines = l.getLineCount();
67 if (lines > 0) {
68 // we only care about the first line
69 return l.getEllipsisCount(0);
70 }
71 }
72 return 0;
73 }
74
Selim Cinek4ffd6362015-12-29 15:12:23 +010075 public static TextViewTransformState obtain() {
76 TextViewTransformState instance = sInstancePool.acquire();
77 if (instance != null) {
78 return instance;
79 }
80 return new TextViewTransformState();
81 }
82
83 @Override
84 public void recycle() {
Selim Cinek0ffbda62016-01-01 20:29:12 +010085 super.recycle();
Selim Cinek4ffd6362015-12-29 15:12:23 +010086 sInstancePool.release(this);
87 }
88
89 @Override
90 protected void reset() {
91 super.reset();
92 mText = null;
93 }
94}