Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.notification; |
| 18 | |
Selim Cinek | 049f6ad | 2016-03-01 12:06:57 -0800 | [diff] [blame] | 19 | import android.text.Layout; |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 20 | import android.text.TextUtils; |
| 21 | import android.util.Pools; |
| 22 | import android.view.View; |
| 23 | import android.widget.TextView; |
| 24 | |
| 25 | /** |
| 26 | * A transform state of a mText view. |
| 27 | */ |
| 28 | public class TextViewTransformState extends TransformState { |
| 29 | |
| 30 | private static Pools.SimplePool<TextViewTransformState> sInstancePool |
| 31 | = new Pools.SimplePool<>(40); |
Selim Cinek | 049f6ad | 2016-03-01 12:06:57 -0800 | [diff] [blame] | 32 | private TextView mText; |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 33 | |
| 34 | @Override |
| 35 | public void initFrom(View view) { |
| 36 | super.initFrom(view); |
| 37 | if (view instanceof TextView) { |
Selim Cinek | 049f6ad | 2016-03-01 12:06:57 -0800 | [diff] [blame] | 38 | mText = (TextView) view; |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | protected boolean sameAs(TransformState otherState) { |
| 44 | if (otherState instanceof TextViewTransformState) { |
| 45 | TextViewTransformState otherTvs = (TextViewTransformState) otherState; |
Selim Cinek | 049f6ad | 2016-03-01 12:06:57 -0800 | [diff] [blame] | 46 | if(TextUtils.equals(otherTvs.mText.getText(), mText.getText())) { |
| 47 | int ownEllipsized = getEllipsisCount(); |
| 48 | int otherEllipsized = otherTvs.getEllipsisCount(); |
Selim Cinek | 2268bcf | 2016-03-10 18:06:54 -0800 | [diff] [blame^] | 49 | return ownEllipsized == otherEllipsized |
| 50 | && mText.getHeight() == otherTvs.mText.getHeight(); |
Selim Cinek | 049f6ad | 2016-03-01 12:06:57 -0800 | [diff] [blame] | 51 | } |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 52 | } |
| 53 | return super.sameAs(otherState); |
| 54 | } |
| 55 | |
Selim Cinek | 049f6ad | 2016-03-01 12:06:57 -0800 | [diff] [blame] | 56 | private int getEllipsisCount() { |
| 57 | Layout l = mText.getLayout(); |
| 58 | if (l != null) { |
| 59 | int lines = l.getLineCount(); |
| 60 | if (lines > 0) { |
| 61 | // we only care about the first line |
| 62 | return l.getEllipsisCount(0); |
| 63 | } |
| 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 68 | public static TextViewTransformState obtain() { |
| 69 | TextViewTransformState instance = sInstancePool.acquire(); |
| 70 | if (instance != null) { |
| 71 | return instance; |
| 72 | } |
| 73 | return new TextViewTransformState(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void recycle() { |
Selim Cinek | 0ffbda6 | 2016-01-01 20:29:12 +0100 | [diff] [blame] | 78 | super.recycle(); |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 79 | sInstancePool.release(this); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void reset() { |
| 84 | super.reset(); |
| 85 | mText = null; |
| 86 | } |
| 87 | } |