blob: d6e7a8b28f0cd7e08f64fae13a7f568fa3133ba5 [file] [log] [blame]
Selim Cinek54809622020-04-30 19:04:44 -07001/*
2 * Copyright (C) 2020 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
17package com.android.systemui.util.animation
18
19import android.annotation.SuppressLint
20import android.content.Context
21import android.view.View
Selim Cinek2de5ebb2020-05-20 15:39:03 -070022import android.view.ViewGroup
Selim Cinek54809622020-04-30 19:04:44 -070023import android.widget.FrameLayout
Selim Cinek2de5ebb2020-05-20 15:39:03 -070024import com.android.systemui.R
Selim Cinek54809622020-04-30 19:04:44 -070025
26/**
Selim Cinekf418bb02020-05-04 17:16:58 -070027 * A special view that is designed to host a single "unique object". The unique object is
28 * dynamically added and removed from this view and may transition to other UniqueObjectHostViews
29 * available in the system.
30 * This is useful to share a singular instance of a view that can transition between completely
31 * independent parts of the view hierarchy.
32 * If the view currently hosts the unique object, it's measuring it normally,
33 * but if it's not attached, it will obtain the size by requesting a measure, as if it were
34 * always attached.
Selim Cinek54809622020-04-30 19:04:44 -070035 */
36class UniqueObjectHostView(
37 context: Context
38) : FrameLayout(context) {
Selim Cinek2de5ebb2020-05-20 15:39:03 -070039 lateinit var measurementManager: MeasurementManager
Selim Cinek54809622020-04-30 19:04:44 -070040
41 @SuppressLint("DrawAllocation")
42 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
Selim Cinek54809622020-04-30 19:04:44 -070043 val paddingHorizontal = paddingStart + paddingEnd
44 val paddingVertical = paddingTop + paddingBottom
45 val width = MeasureSpec.getSize(widthMeasureSpec) - paddingHorizontal
46 val widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.getMode(widthMeasureSpec))
47 val height = MeasureSpec.getSize(heightMeasureSpec) - paddingVertical
48 val heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.getMode(heightMeasureSpec))
Selim Cinek2de5ebb2020-05-20 15:39:03 -070049 val measurementInput = MeasurementInput(widthSpec, heightSpec)
50
51 // Let's make sure the measurementManager knows about our size, to ensure that we have
52 // a value available. This might perform a measure internally if we don't have a cached
53 // size.
54 val (cachedWidth, cachedHeight) = measurementManager.onMeasure(measurementInput)
55
Selim Cinekb28ec0a2020-05-01 15:07:42 -070056 if (!isCurrentHost()) {
Selim Cinek2de5ebb2020-05-20 15:39:03 -070057 // We're not currently the host, let's use the dimension from our cache
Selim Cinekf418bb02020-05-04 17:16:58 -070058 // The goal here is that the view will always have a consistent measuring, regardless
59 // if it's attached or not.
60 // The behavior is therefore very similar to the view being persistently attached to
61 // this host, which can prevent flickers. It also makes sure that we always know
62 // the size of the view during transitions even if it has never been attached here
63 // before.
Selim Cinek54809622020-04-30 19:04:44 -070064 setMeasuredDimension(cachedWidth + paddingHorizontal, cachedHeight + paddingVertical)
65 } else {
Selim Cinekb28ec0a2020-05-01 15:07:42 -070066 super.onMeasure(widthMeasureSpec, heightMeasureSpec)
67 // Let's update our cache
Selim Cinek2de5ebb2020-05-20 15:39:03 -070068 getChildAt(0)?.requiresRemeasuring = false
Selim Cinek54809622020-04-30 19:04:44 -070069 }
70 }
71
Selim Cinek2de5ebb2020-05-20 15:39:03 -070072 override fun addView(child: View?, index: Int, params: ViewGroup.LayoutParams?) {
Jeff DeCewafec78f2020-06-12 13:57:23 -040073 if (child == null) {
74 throw IllegalArgumentException("child must be non-null")
75 }
76 if (child.measuredWidth == 0 || measuredWidth == 0 || child.requiresRemeasuring == true) {
Selim Cinek2de5ebb2020-05-20 15:39:03 -070077 super.addView(child, index, params)
78 return
79 }
80 // Suppress layouts when adding a view. The view should already be laid out with the
81 // right size when being attached to this view
82 invalidate()
83 addViewInLayout(child, index, params, true /* preventRequestLayout */)
Jeff DeCewafec78f2020-06-12 13:57:23 -040084 // RTL properties are normally resolved in onMeasure(), which we are intentionally skipping
85 child.resolveRtlPropertiesIfNeeded()
Selim Cinek2de5ebb2020-05-20 15:39:03 -070086 val left = paddingLeft
87 val top = paddingTop
88 val paddingHorizontal = paddingStart + paddingEnd
89 val paddingVertical = paddingTop + paddingBottom
Jeff DeCewafec78f2020-06-12 13:57:23 -040090 child.layout(left,
Selim Cinek2de5ebb2020-05-20 15:39:03 -070091 top,
92 left + measuredWidth - paddingHorizontal,
93 top + measuredHeight - paddingVertical)
94 }
95
Selim Cinek54809622020-04-30 19:04:44 -070096 private fun isCurrentHost() = childCount != 0
Selim Cinek54809622020-04-30 19:04:44 -070097
Selim Cinek2de5ebb2020-05-20 15:39:03 -070098 interface MeasurementManager {
99 fun onMeasure(input: MeasurementInput): MeasurementOutput
Selim Cinek54809622020-04-30 19:04:44 -0700100 }
Selim Cinek54809622020-04-30 19:04:44 -0700101}
102
103/**
Selim Cinek2de5ebb2020-05-20 15:39:03 -0700104 * Does this view require remeasuring currently outside of the regular measure flow?
Selim Cinek54809622020-04-30 19:04:44 -0700105 */
Selim Cinek2de5ebb2020-05-20 15:39:03 -0700106var View.requiresRemeasuring: Boolean
107 get() {
108 val required = getTag(R.id.requires_remeasuring)
109 return required?.equals(true) ?: false
110 }
111 set(value) {
112 setTag(R.id.requires_remeasuring, value)
113 }