blob: d3393b33035585a9ad3fd7348439e66d1e4db211 [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();
49 return ownEllipsized == otherEllipsized;
50 }
Selim Cinek4ffd6362015-12-29 15:12:23 +010051 }
52 return super.sameAs(otherState);
53 }
54
Selim Cinek049f6ad2016-03-01 12:06:57 -080055 private int getEllipsisCount() {
56 Layout l = mText.getLayout();
57 if (l != null) {
58 int lines = l.getLineCount();
59 if (lines > 0) {
60 // we only care about the first line
61 return l.getEllipsisCount(0);
62 }
63 }
64 return 0;
65 }
66
Selim Cinek4ffd6362015-12-29 15:12:23 +010067 public static TextViewTransformState obtain() {
68 TextViewTransformState instance = sInstancePool.acquire();
69 if (instance != null) {
70 return instance;
71 }
72 return new TextViewTransformState();
73 }
74
75 @Override
76 public void recycle() {
Selim Cinek0ffbda62016-01-01 20:29:12 +010077 super.recycle();
Selim Cinek4ffd6362015-12-29 15:12:23 +010078 sInstancePool.release(this);
79 }
80
81 @Override
82 protected void reset() {
83 super.reset();
84 mText = null;
85 }
86}