1 /*
2  * 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.wm.shell.common.bubbles
17 
18 import android.content.Context
19 import android.graphics.Rect
20 import android.util.AttributeSet
21 import android.widget.LinearLayout
22 
23 /** A popup container view that uses [BubblePopupDrawable] as a background */
24 open class BubblePopupView
25 @JvmOverloads
26 constructor(
27     context: Context,
28     attrs: AttributeSet? = null,
29     defStyleAttr: Int = 0,
30     defStyleRes: Int = 0
31 ) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
32     var popupDrawable: BubblePopupDrawable? = null
33         private set
34 
35     /**
36      * Sets up the popup drawable with the config provided. Required to remove dependency on local
37      * resources
38      */
setupBackgroundnull39     fun setupBackground(config: BubblePopupDrawable.Config) {
40         popupDrawable = BubblePopupDrawable(config)
41         background = popupDrawable
42         forceLayout()
43     }
44 
45     /**
46      * Sets the arrow direction for the background drawable and updates the padding to fit the
47      * content inside of the popup drawable
48      */
setArrowDirectionnull49     fun setArrowDirection(direction: BubblePopupDrawable.ArrowDirection) {
50         popupDrawable?.let {
51             it.arrowDirection = direction
52             val padding = Rect()
53             if (it.getPadding(padding)) {
54                 setPadding(padding.left, padding.top, padding.right, padding.bottom)
55             }
56         }
57     }
58 
59     /** Sets the arrow position for the background drawable and triggers redraw */
setArrowPositionnull60     fun setArrowPosition(position: BubblePopupDrawable.ArrowPosition) {
61         popupDrawable?.let {
62             it.arrowPosition = position
63             invalidate()
64         }
65     }
66 }
67