Home | History | Annotate | Download | only in notification
      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 
     19 import android.text.Layout;
     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);
     32     private TextView mText;
     33 
     34     @Override
     35     public void initFrom(View view) {
     36         super.initFrom(view);
     37         if (view instanceof TextView) {
     38             mText = (TextView) view;
     39         }
     40     }
     41 
     42     @Override
     43     protected boolean sameAs(TransformState otherState) {
     44         if (otherState instanceof TextViewTransformState) {
     45             TextViewTransformState otherTvs = (TextViewTransformState) otherState;
     46             if(TextUtils.equals(otherTvs.mText.getText(), mText.getText())) {
     47                 int ownEllipsized = getEllipsisCount();
     48                 int otherEllipsized = otherTvs.getEllipsisCount();
     49                 return ownEllipsized == otherEllipsized
     50                         && getInnerHeight(mText) == getInnerHeight(otherTvs.mText);
     51             }
     52         }
     53         return super.sameAs(otherState);
     54     }
     55 
     56     private int getInnerHeight(TextView text) {
     57         return text.getHeight() - text.getPaddingTop() - text.getPaddingBottom();
     58     }
     59 
     60     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 
     72     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() {
     82         super.recycle();
     83         sInstancePool.release(this);
     84     }
     85 
     86     @Override
     87     protected void reset() {
     88         super.reset();
     89         mText = null;
     90     }
     91 }
     92