1 /*
2  * Copyright (C) 2019 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.util;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 import android.util.AttributeSet;
22 import android.widget.TextView;
23 
24 /**
25  * TextView that changes its ellipsize value with its visibility.
26  *
27  * The View responds to changes in user-visibility to change its ellipsize from MARQUEE to END
28  * and back. Useful for TextView that need to marquee forever.
29  */
30 public class AutoMarqueeTextView extends TextView {
31 
32     private boolean mAggregatedVisible = false;
33 
AutoMarqueeTextView(Context context)34     public AutoMarqueeTextView(Context context) {
35         super(context);
36     }
37 
AutoMarqueeTextView(Context context, AttributeSet attrs)38     public AutoMarqueeTextView(Context context, AttributeSet attrs) {
39         super(context, attrs);
40     }
41 
AutoMarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr)42     public AutoMarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
43         super(context, attrs, defStyleAttr);
44     }
45 
AutoMarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)46     public AutoMarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr,
47             int defStyleRes) {
48         super(context, attrs, defStyleAttr, defStyleRes);
49     }
50 
51     @Override
onFinishInflate()52     protected void onFinishInflate() {
53         onVisibilityAggregated(isVisibleToUser());
54     }
55 
56     @Override
onAttachedToWindow()57     protected void onAttachedToWindow() {
58         super.onAttachedToWindow();
59         setSelected(true);
60     }
61 
62     @Override
onDetachedFromWindow()63     protected void onDetachedFromWindow() {
64         super.onDetachedFromWindow();
65         setSelected(false);
66     }
67 
68     @Override
onVisibilityAggregated(boolean isVisible)69     public void onVisibilityAggregated(boolean isVisible) {
70         super.onVisibilityAggregated(isVisible);
71         if (isVisible == mAggregatedVisible) return;
72 
73         mAggregatedVisible = isVisible;
74         if (mAggregatedVisible) {
75             setEllipsize(TextUtils.TruncateAt.MARQUEE);
76         } else {
77             setEllipsize(TextUtils.TruncateAt.END);
78         }
79     }
80 }
81