1 /*
<lambda>null2  * Copyright (C) 2024 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.launcher3.taskbar.bubbles
18 
19 import android.annotation.SuppressLint
20 import android.content.Context
21 import android.graphics.Point
22 import android.view.Gravity.BOTTOM
23 import android.view.Gravity.LEFT
24 import android.view.Gravity.RIGHT
25 import android.view.LayoutInflater
26 import android.view.View
27 import android.widget.FrameLayout
28 import androidx.core.view.updateLayoutParams
29 import com.android.launcher3.R
30 import com.android.wm.shell.common.bubbles.BaseBubblePinController
31 import com.android.wm.shell.common.bubbles.BubbleBarLocation
32 
33 /**
34  * Controller to manage pinning bubble bar to left or right when dragging starts from the bubble bar
35  */
36 class BubbleBarPinController(
37     private val context: Context,
38     private val container: FrameLayout,
39     screenSizeProvider: () -> Point
40 ) : BaseBubblePinController(screenSizeProvider) {
41 
42     private lateinit var bubbleBarViewController: BubbleBarViewController
43     private lateinit var bubbleStashController: BubbleStashController
44     private var exclRectWidth: Float = 0f
45     private var exclRectHeight: Float = 0f
46 
47     private var dropTargetView: View? = null
48 
49     fun init(bubbleControllers: BubbleControllers) {
50         bubbleBarViewController = bubbleControllers.bubbleBarViewController
51         bubbleStashController = bubbleControllers.bubbleStashController
52         exclRectWidth = context.resources.getDimension(R.dimen.bubblebar_dismiss_zone_width)
53         exclRectHeight = context.resources.getDimension(R.dimen.bubblebar_dismiss_zone_height)
54     }
55 
56     override fun getExclusionRectWidth(): Float {
57         return exclRectWidth
58     }
59 
60     override fun getExclusionRectHeight(): Float {
61         return exclRectHeight
62     }
63 
64     override fun getDropTargetView(): View? {
65         return dropTargetView
66     }
67 
68     override fun removeDropTargetView(view: View) {
69         container.removeView(view)
70         dropTargetView = null
71     }
72 
73     override fun createDropTargetView(): View {
74         return LayoutInflater.from(context)
75             .inflate(R.layout.bubble_bar_drop_target, container, false)
76             .also { view ->
77                 dropTargetView = view
78                 container.addView(view)
79             }
80     }
81 
82     @SuppressLint("RtlHardcoded")
83     override fun updateLocation(location: BubbleBarLocation) {
84         val onLeft = location.isOnLeft(container.isLayoutRtl)
85 
86         val bounds = bubbleBarViewController.bubbleBarBounds
87         val horizontalMargin = bubbleBarViewController.horizontalMargin
88         dropTargetView?.updateLayoutParams<FrameLayout.LayoutParams> {
89             width = bounds.width()
90             height = bounds.height()
91             gravity = BOTTOM or (if (onLeft) LEFT else RIGHT)
92             leftMargin = horizontalMargin
93             rightMargin = horizontalMargin
94             bottomMargin = -bubbleStashController.bubbleBarTranslationY.toInt()
95         }
96     }
97 }
98