1 /*
2  * Copyright (C) 2021 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.launcher3.taskbar;
17 
18 import androidx.annotation.NonNull;
19 
20 import com.android.launcher3.BubbleTextView;
21 import com.android.launcher3.R;
22 import com.android.launcher3.model.data.ItemInfo;
23 import com.android.launcher3.popup.PopupContainerWithArrow;
24 import com.android.launcher3.popup.PopupDataProvider;
25 import com.android.launcher3.popup.SystemShortcut;
26 import com.android.launcher3.util.ComponentKey;
27 import com.android.launcher3.views.ActivityContext;
28 
29 import java.util.HashMap;
30 import java.util.Objects;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33 
34 /**
35  * Implements interfaces required to show and allow interacting with a PopupContainerWithArrow.
36  */
37 public class TaskbarPopupController {
38 
39     private static final SystemShortcut.Factory<TaskbarActivityContext>
40             APP_INFO = SystemShortcut.AppInfo::new;
41 
42     private final PopupDataProvider mPopupDataProvider;
43 
TaskbarPopupController()44     public TaskbarPopupController() {
45         // TODO (b/198438631): add notifications dots change listener
46         mPopupDataProvider = new PopupDataProvider(packageUserKey -> {});
47     }
48 
49     @NonNull
getPopupDataProvider()50     public PopupDataProvider getPopupDataProvider() {
51         return mPopupDataProvider;
52     }
53 
setDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy)54     public void setDeepShortcutMap(HashMap<ComponentKey, Integer> deepShortcutMapCopy) {
55         mPopupDataProvider.setDeepShortcutMap(deepShortcutMapCopy);
56     }
57 
58     /**
59      * Shows the notifications and deep shortcuts associated with a Taskbar {@param icon}.
60      * @return the container if shown or null.
61      */
showForIcon(BubbleTextView icon)62     public PopupContainerWithArrow<TaskbarActivityContext> showForIcon(BubbleTextView icon) {
63         TaskbarActivityContext context = ActivityContext.lookupContext(icon.getContext());
64         if (PopupContainerWithArrow.getOpen(context) != null) {
65             // There is already an items container open, so don't open this one.
66             icon.clearFocus();
67             return null;
68         }
69         ItemInfo item = (ItemInfo) icon.getTag();
70         if (!PopupContainerWithArrow.canShow(icon, item)) {
71             return null;
72         }
73 
74         final PopupContainerWithArrow<TaskbarActivityContext> container =
75                 (PopupContainerWithArrow) context.getLayoutInflater().inflate(
76                         R.layout.popup_container, context.getDragLayer(), false);
77         // TODO (b/198438631): configure for taskbar/context
78 
79         container.populateAndShow(icon,
80                 mPopupDataProvider.getShortcutCountForItem(item),
81                 mPopupDataProvider.getNotificationKeysForItem(item),
82                 // TODO (b/198438631): add support for INSTALL shortcut factory
83                 Stream.of(APP_INFO)
84                         .map(s -> s.getShortcut(context, item))
85                         .filter(Objects::nonNull)
86                         .collect(Collectors.toList()));
87         container.requestFocus();
88         return container;
89     }
90 }
91