1 /* 2 * Copyright (C) 2022 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.util.AttributeSet 21 import com.android.systemui.R 22 23 class DelayableMarqueeTextView @JvmOverloads constructor( 24 context: Context, 25 attrs: AttributeSet? = null, 26 defStyleAttr: Int = 0, 27 defStyleRes: Int = 0 28 ) : SafeMarqueeTextView(context, attrs, defStyleAttr, defStyleRes) { 29 30 var marqueeDelay: Long = DEFAULT_MARQUEE_DELAY 31 private var wantsMarquee = false 32 private var marqueeBlocked = true 33 34 private val enableMarquee = Runnable { 35 if (wantsMarquee) { 36 marqueeBlocked = false 37 startMarquee() 38 } 39 } 40 41 init { 42 val typedArray = context.theme.obtainStyledAttributes( 43 attrs, 44 R.styleable.DelayableMarqueeTextView, 45 defStyleAttr, 46 defStyleRes 47 ) 48 marqueeDelay = typedArray.getInteger( 49 R.styleable.DelayableMarqueeTextView_marqueeDelay, 50 DEFAULT_MARQUEE_DELAY.toInt() 51 ).toLong() 52 typedArray.recycle() 53 } 54 55 override fun startMarquee() { 56 if (!isSelected) { 57 return 58 } 59 wantsMarquee = true 60 if (marqueeBlocked) { 61 if (handler?.hasCallbacks(enableMarquee) == false) { 62 postDelayed(enableMarquee, marqueeDelay) 63 } 64 return 65 } 66 super.startMarquee() 67 } 68 69 override fun stopMarquee() { 70 handler?.removeCallbacks(enableMarquee) 71 wantsMarquee = false 72 marqueeBlocked = true 73 super.stopMarquee() 74 } 75 76 companion object { 77 const val DEFAULT_MARQUEE_DELAY = 2000L 78 } 79 } 80