blob: 6fe5756ea9f8f63c97a279a7a545579308b6359d [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) {
44 if (otherState instanceof TextViewTransformState) {
45 TextViewTransformState otherTvs = (TextViewTransformState) otherState;
Selim Cinek049f6ad2016-03-01 12:06:57 -080046 if(TextUtils.equals(otherTvs.mText.getText(), mText.getText())) {
47 int ownEllipsized = getEllipsisCount();
48 int otherEllipsized = otherTvs.getEllipsisCount();
Selim Cinek2268bcf2016-03-10 18:06:54 -080049 return ownEllipsized == otherEllipsized
Adrian Roos237ffa42016-06-02 13:15:09 -070050 && getInnerHeight(mText) == getInnerHeight(otherTvs.mText);
Selim Cinek049f6ad2016-03-01 12:06:57 -080051 }
Selim Cinek4ffd6362015-12-29 15:12:23 +010052 }
53 return super.sameAs(otherState);
54 }
55
Adrian Roos237ffa42016-06-02 13:15:09 -070056 private int getInnerHeight(TextView text) {
57 return text.getHeight() - text.getPaddingTop() - text.getPaddingBottom();
58 }
59
Selim Cinek049f6ad2016-03-01 12:06:57 -080060 private int getEllipsisCount() {
61 Layout l = mText.getLayout();
62 if (l != null) {
63 int lines = l.getLineCount();
64 if (lines > 0) {
65 // we only care about the first line
66 return l.getEllipsisCount(0);
67 }
68 }
69 return 0;
70 }
71
Selim Cinek4ffd6362015-12-29 15:12:23 +010072 public static TextViewTransformState obtain() {
73 TextViewTransformState instance = sInstancePool.acquire();
74 if (instance != null) {
75 return instance;
76 }
77 return new TextViewTransformState();
78 }
79
80 @Override
81 public void recycle() {
Selim Cinek0ffbda62016-01-01 20:29:12 +010082 super.recycle();
Selim Cinek4ffd6362015-12-29 15:12:23 +010083 sInstancePool.release(this);
84 }
85
86 @Override
87 protected void reset() {
88 super.reset();
89 mText = null;
90 }
91}