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