1 /*
<lambda>null2  * Copyright (C) 2023 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 package com.android.systemui.shared.animation
17 
18 import android.graphics.Paint
19 import android.view.ViewGroup
20 import android.widget.TextView
21 import androidx.core.view.forEach
22 import com.android.app.tracing.traceSection
23 import com.android.systemui.unfold.UnfoldTransitionProgressProvider.TransitionProgressListener
24 import java.lang.ref.WeakReference
25 
26 /**
27  * A listener which disables subpixel flag for all TextView children of a given parent ViewGroup
28  * during fold/unfold transitions.
29  */
30 class DisableSubpixelTextTransitionListener(private val rootView: ViewGroup?) :
31     TransitionProgressListener {
32     private val childrenTextViews: MutableList<WeakReference<TextView>> = mutableListOf()
33     private var isTransitionInProgress: Boolean = false
34 
35     override fun onTransitionStarted() {
36         isTransitionInProgress = true
37         traceSection("subpixelFlagSetForTextView") {
38             traceSection("subpixelFlagTraverseHierarchy") {
39                 getAllChildTextView(rootView, childrenTextViews)
40             }
41             traceSection("subpixelFlagDisableForTextView") {
42                 childrenTextViews.forEach { child ->
43                     val childTextView = child.get() ?: return@forEach
44                     childTextView.paintFlags = childTextView.paintFlags or Paint.SUBPIXEL_TEXT_FLAG
45                 }
46             }
47         }
48     }
49 
50     override fun onTransitionFinished() {
51         if (!isTransitionInProgress) return
52         isTransitionInProgress = false
53         traceSection("subpixelFlagEnableForTextView") {
54             childrenTextViews.forEach { child ->
55                 val childTextView = child.get() ?: return@forEach
56                 childTextView.paintFlags =
57                     childTextView.paintFlags and Paint.SUBPIXEL_TEXT_FLAG.inv()
58             }
59             childrenTextViews.clear()
60         }
61     }
62 
63     /**
64      * Populates a list of all TextView children of a given parent ViewGroup
65      *
66      * @param parent the root ViewGroup for which to retrieve TextView children
67      * @param childrenTextViews the list to store the retrieved TextView children
68      */
69     private fun getAllChildTextView(
70         parent: ViewGroup?,
71         childrenTextViews: MutableList<WeakReference<TextView>>
72     ) {
73         parent?.forEach { child ->
74             when (child) {
75                 is ViewGroup -> getAllChildTextView(child, childrenTextViews)
76                 is TextView -> {
77                     if ((child.paintFlags and Paint.SUBPIXEL_TEXT_FLAG) <= 0) {
78                         childrenTextViews.add(WeakReference(child))
79                     }
80                 }
81             }
82         }
83     }
84 }
85