1 /*
2  * 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.testing
18 
19 import android.app.Notification
20 import android.content.Context
21 import android.graphics.Bitmap
22 import android.graphics.Canvas
23 import android.graphics.Color
24 import android.graphics.Paint
25 import android.util.PathParser
26 import android.view.LayoutInflater
27 import android.view.ViewGroup
28 import com.android.launcher3.R
29 import com.android.launcher3.taskbar.bubbles.BubbleBarBubble
30 import com.android.launcher3.taskbar.bubbles.BubbleView
31 import com.android.wm.shell.shared.bubbles.BubbleInfo
32 
33 object FakeBubbleViewFactory {
34 
35     /** Inflates a [BubbleView] and adds it to the [parent] view if it is present. */
createBubblenull36     fun createBubble(
37         context: Context,
38         key: String,
39         parent: ViewGroup?,
40         iconSize: Int = 50,
41         iconColor: Int,
42         badgeColor: Int = Color.RED,
43         dotColor: Int = Color.BLUE,
44         suppressNotification: Boolean = false,
45     ): BubbleView {
46         val inflater = LayoutInflater.from(context)
47         // BubbleView uses launcher's badge to icon ratio and expects the badge image to already
48         // have the right size
49         val badgeToIconRatio = 0.444f
50         val badgeRadius = iconSize * badgeToIconRatio / 2
51         val icon = createCircleBitmap(radius = iconSize / 2, color = iconColor)
52         val badge = createCircleBitmap(radius = badgeRadius.toInt(), color = badgeColor)
53 
54         val flags =
55             if (suppressNotification) Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION else 0
56         val bubbleInfo =
57             BubbleInfo(
58                 key,
59                 flags,
60                 null,
61                 null,
62                 0,
63                 context.packageName,
64                 null,
65                 null,
66                 false,
67                 true,
68                 null,
69             )
70         val bubbleView = inflater.inflate(R.layout.bubblebar_item_view, parent, false) as BubbleView
71         val dotPath =
72             PathParser.createPathFromPathData(
73                 context.resources.getString(com.android.internal.R.string.config_icon_mask)
74             )
75         val bubble =
76             BubbleBarBubble(
77                 bubbleInfo,
78                 bubbleView,
79                 badge,
80                 icon,
81                 dotColor,
82                 dotPath,
83                 "test app",
84                 null,
85             )
86         bubbleView.setBubble(bubble)
87         return bubbleView
88     }
89 
createCircleBitmapnull90     private fun createCircleBitmap(radius: Int, color: Int): Bitmap {
91         val bitmap = Bitmap.createBitmap(radius * 2, radius * 2, Bitmap.Config.ARGB_8888)
92         val canvas = Canvas(bitmap)
93         canvas.drawARGB(0, 0, 0, 0)
94         val paint = Paint()
95         paint.color = color
96         canvas.drawCircle(radius.toFloat(), radius.toFloat(), radius.toFloat(), paint)
97         return bitmap
98     }
99 }
100