TextViewTransformState.java revision 2268bcf00645f2aa8df1cb15e6a6c4097e9b9454
14ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek/*
24ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek * Copyright (C) 2016 The Android Open Source Project
34ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek *
44ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek * Licensed under the Apache License, Version 2.0 (the "License");
54ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek * you may not use this file except in compliance with the License.
64ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek * You may obtain a copy of the License at
74ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek *
84ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek *      http://www.apache.org/licenses/LICENSE-2.0
94ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek *
104ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek * Unless required by applicable law or agreed to in writing, software
114ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek * distributed under the License is distributed on an "AS IS" BASIS,
124ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek * See the License for the specific language governing permissions and
144ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek * limitations under the License
154ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek */
164ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek
174ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinekpackage com.android.systemui.statusbar.notification;
184ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek
19049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinekimport android.text.Layout;
204ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinekimport android.text.TextUtils;
214ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinekimport android.util.Pools;
224ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinekimport android.view.View;
234ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinekimport android.widget.TextView;
244ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek
254ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek/**
264ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek * A transform state of a mText view.
274ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek*/
284ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinekpublic class TextViewTransformState extends TransformState {
294ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek
304ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    private static Pools.SimplePool<TextViewTransformState> sInstancePool
314ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek            = new Pools.SimplePool<>(40);
32049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek    private TextView mText;
334ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek
344ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    @Override
354ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    public void initFrom(View view) {
364ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        super.initFrom(view);
374ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        if (view instanceof TextView) {
38049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek            mText = (TextView) view;
394ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        }
404ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    }
414ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek
424ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    @Override
434ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    protected boolean sameAs(TransformState otherState) {
444ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        if (otherState instanceof TextViewTransformState) {
454ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek            TextViewTransformState otherTvs = (TextViewTransformState) otherState;
46049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek            if(TextUtils.equals(otherTvs.mText.getText(), mText.getText())) {
47049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek                int ownEllipsized = getEllipsisCount();
48049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek                int otherEllipsized = otherTvs.getEllipsisCount();
492268bcf00645f2aa8df1cb15e6a6c4097e9b9454Selim Cinek                return ownEllipsized == otherEllipsized
502268bcf00645f2aa8df1cb15e6a6c4097e9b9454Selim Cinek                        && mText.getHeight() == otherTvs.mText.getHeight();
51049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek            }
524ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        }
534ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        return super.sameAs(otherState);
544ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    }
554ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek
56049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek    private int getEllipsisCount() {
57049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek        Layout l = mText.getLayout();
58049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek        if (l != null) {
59049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek            int lines = l.getLineCount();
60049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek            if (lines > 0) {
61049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek                // we only care about the first line
62049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek                return l.getEllipsisCount(0);
63049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek            }
64049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek        }
65049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek        return 0;
66049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek    }
67049f6adfb8fea4622af5cd3247514ec7a461bf46Selim Cinek
684ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    public static TextViewTransformState obtain() {
694ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        TextViewTransformState instance = sInstancePool.acquire();
704ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        if (instance != null) {
714ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek            return instance;
724ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        }
734ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        return new TextViewTransformState();
744ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    }
754ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek
764ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    @Override
774ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    public void recycle() {
780ffbda62e55ad390e05e6c3ff52e1378e420285cSelim Cinek        super.recycle();
794ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        sInstancePool.release(this);
804ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    }
814ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek
824ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    @Override
834ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    protected void reset() {
844ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        super.reset();
854ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek        mText = null;
864ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek    }
874ffd63611a0d516c9988b37e9c06e6f8390c2a2fSelim Cinek}
88