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 
17 package com.android.car.docklib.view
18 
19 import android.car.media.CarMediaManager
20 import android.content.ComponentName
21 import android.content.Context
22 import android.content.res.Resources
23 import android.view.View
24 import androidx.annotation.OpenForTesting
25 import androidx.annotation.VisibleForTesting
26 import com.android.car.carlaunchercommon.shortcuts.AppInfoShortcutItem
27 import com.android.car.carlaunchercommon.shortcuts.ForceStopShortcutItem
28 import com.android.car.carlaunchercommon.shortcuts.PinShortcutItem
29 import com.android.car.docklib.data.DockAppItem
30 import com.android.car.ui.shortcutspopup.CarUiShortcutsPopup
31 
32 /**
33  * [View.OnLongClickListener] for handling long clicks on dock item.
34  * It is responsible to create and show th popup window
35  *
36  * @property dockAppItem the [DockAppItem] to be used on long click.
37  * @property pinItemClickDelegate called when item should be pinned at that position
38  * @property unpinItemClickDelegate called when item should be unpinned at that position
39  * @property component [ComponentName] of the item at this position
40  * @property userContext [Context] for the current running user
41  * @property mediaServiceComponents list of [ComponentName] of the services the adhere to the media
42  * service interface
43  */
44 @OpenForTesting
45 open class DockItemLongClickListener(
46     private var dockAppItem: DockAppItem,
47     private val pinItemClickDelegate: Runnable,
48     private val unpinItemClickDelegate: Runnable,
49     private val component: ComponentName,
50     private val userContext: Context,
51     private val carMediaManager: CarMediaManager?,
52     private val mediaServiceComponents: Set<ComponentName>
53 ) : View.OnLongClickListener {
onLongClicknull54     override fun onLongClick(view: View?): Boolean {
55         if (view == null) return false
56 
57         createCarUiShortcutsPopupBuilder()
58             .addShortcut(ForceStopShortcutItem(
59                 userContext,
60                 component.packageName,
61                 dockAppItem.name,
62                 carMediaManager,
63                 mediaServiceComponents
64             ))
65             .addShortcut(AppInfoShortcutItem(userContext, component.packageName, userContext.user))
66             .addShortcut(
67                 createPinShortcutItem(
68                     view.context.resources,
69                     isItemPinned = (dockAppItem.type == DockAppItem.Type.STATIC),
70                     pinItemClickDelegate,
71                     unpinItemClickDelegate
72                 )
73             )
74             .build(view.context, view)
75             .show()
76         return true
77     }
78 
79     /**
80      * Set the [DockAppItem] to be used on long click.
81      */
setDockAppItemnull82     fun setDockAppItem(dockAppItem: DockAppItem) {
83         this.dockAppItem = dockAppItem
84     }
85 
86     /**
87      * Need to be overridden in test.
88      */
89     @VisibleForTesting
90     @OpenForTesting
createCarUiShortcutsPopupBuildernull91     open fun createCarUiShortcutsPopupBuilder(): CarUiShortcutsPopup.Builder =
92         CarUiShortcutsPopup.Builder()
93 
94     /**
95      * Need to be overridden in test.
96      */
97     @VisibleForTesting
98     fun createPinShortcutItem(
99         resources: Resources,
100         isItemPinned: Boolean,
101         pinItemClickDelegate: Runnable,
102         unpinItemClickDelegate: Runnable
103     ): PinShortcutItem = PinShortcutItem(
104         resources,
105         isItemPinned,
106         pinItemClickDelegate,
107         unpinItemClickDelegate
108     )
109 }
110