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     private var popupDrawable: BubblePopupDrawable? = null
33 
34     /**
35      * Sets up the popup drawable with the config provided. Required to remove dependency on local
36      * resources
37      */
38     fun setupBackground(config: BubblePopupDrawable.Config) {
39         popupDrawable = BubblePopupDrawable(config)
40         background = popupDrawable
41         forceLayout()
42     }
43 
44     /**
45      * Sets the arrow direction for the background drawable and updates the padding to fit the
46      * content inside of the popup drawable
47      */
48     fun setArrowDirection(direction: BubblePopupDrawable.ArrowDirection) {
49         popupDrawable?.let {
50             it.arrowDirection = direction
51             val padding = Rect()
52             if (it.getPadding(padding)) {
53                 setPadding(padding.left, padding.top, padding.right, padding.bottom)
54             }
55         }
56     }
57 
58     /** Sets the arrow position for the background drawable and triggers redraw */
59     fun setArrowPosition(position: BubblePopupDrawable.ArrowPosition) {
60         popupDrawable?.let {
61             it.arrowPosition = position
62             invalidate()
63         }
64     }
65 }
66