• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.launcher3.taskbar.bubbles;
17 
18 import static com.android.launcher3.taskbar.TaskbarViewController.ALPHA_INDEX_BUBBLE_BAR;
19 
20 import android.graphics.Rect;
21 import android.view.View;
22 
23 import com.android.launcher3.taskbar.TaskbarControllers;
24 import com.android.launcher3.taskbar.TaskbarSharedState;
25 import com.android.launcher3.taskbar.bubbles.BubbleBarViewController.TaskbarViewPropertiesProvider;
26 import com.android.launcher3.taskbar.bubbles.stashing.BubbleBarLocationOnDemandListener;
27 import com.android.launcher3.taskbar.bubbles.stashing.BubbleStashController;
28 import com.android.launcher3.util.MultiPropertyFactory;
29 import com.android.launcher3.util.RunnableList;
30 
31 import java.io.PrintWriter;
32 import java.util.Optional;
33 
34 /** Hosts various bubble controllers to facilitate passing between one another. */
35 public class BubbleControllers {
36 
37     public final BubbleBarController bubbleBarController;
38     public final BubbleBarViewController bubbleBarViewController;
39     public final BubbleStashController bubbleStashController;
40     public final Optional<BubbleStashedHandleViewController> bubbleStashedHandleViewController;
41     public final BubbleDragController bubbleDragController;
42     public final BubbleDismissController bubbleDismissController;
43     public final BubbleBarPinController bubbleBarPinController;
44     public final BubblePinController bubblePinController;
45     public final Optional<BubbleBarSwipeController> bubbleBarSwipeController;
46     public final BubbleCreator bubbleCreator;
47 
48     private final RunnableList mPostInitRunnables = new RunnableList();
49 
50     /**
51      * Want to add a new controller? Don't forget to:
52      *   * Call init
53      *   * Call onDestroy
54      */
BubbleControllers( BubbleBarController bubbleBarController, BubbleBarViewController bubbleBarViewController, BubbleStashController bubbleStashController, Optional<BubbleStashedHandleViewController> bubbleStashedHandleViewController, BubbleDragController bubbleDragController, BubbleDismissController bubbleDismissController, BubbleBarPinController bubbleBarPinController, BubblePinController bubblePinController, Optional<BubbleBarSwipeController> bubbleBarSwipeController, BubbleCreator bubbleCreator)55     public BubbleControllers(
56             BubbleBarController bubbleBarController,
57             BubbleBarViewController bubbleBarViewController,
58             BubbleStashController bubbleStashController,
59             Optional<BubbleStashedHandleViewController> bubbleStashedHandleViewController,
60             BubbleDragController bubbleDragController,
61             BubbleDismissController bubbleDismissController,
62             BubbleBarPinController bubbleBarPinController,
63             BubblePinController bubblePinController,
64             Optional<BubbleBarSwipeController> bubbleBarSwipeController,
65             BubbleCreator bubbleCreator) {
66         this.bubbleBarController = bubbleBarController;
67         this.bubbleBarViewController = bubbleBarViewController;
68         this.bubbleStashController = bubbleStashController;
69         this.bubbleStashedHandleViewController = bubbleStashedHandleViewController;
70         this.bubbleDragController = bubbleDragController;
71         this.bubbleDismissController = bubbleDismissController;
72         this.bubbleBarPinController = bubbleBarPinController;
73         this.bubblePinController = bubblePinController;
74         this.bubbleBarSwipeController = bubbleBarSwipeController;
75         this.bubbleCreator = bubbleCreator;
76     }
77 
78     /**
79      * Initializes all controllers. Note that controllers can now reference each other through this
80      * BubbleControllers instance, but should be careful to only access things that were created
81      * in constructors for now, as some controllers may still be waiting for init().
82      */
init(TaskbarSharedState taskbarSharedState, TaskbarControllers taskbarControllers)83     public void init(TaskbarSharedState taskbarSharedState, TaskbarControllers taskbarControllers) {
84         BubbleBarLocationCompositeListener bubbleBarLocationListeners =
85                 new BubbleBarLocationCompositeListener(
86                         taskbarControllers.navbarButtonsViewController,
87                         taskbarControllers.taskbarViewController,
88                         new BubbleBarLocationOnDemandListener(() -> taskbarControllers.uiController)
89                 );
90         bubbleBarController.init(this,
91                 bubbleBarLocationListeners,
92                 taskbarSharedState);
93         bubbleStashedHandleViewController.ifPresent(
94                 controller -> controller.init(/* bubbleControllers = */ this));
95         bubbleStashController.init(
96                 taskbarControllers.taskbarInsetsController,
97                 bubbleBarViewController,
98                 bubbleStashedHandleViewController.orElse(null),
99                 taskbarControllers::runAfterInit
100         );
101         bubbleBarViewController.init(taskbarControllers, /* bubbleControllers = */ this,
102                 new TaskbarViewPropertiesProvider() {
103                     @Override
104                     public Rect getTaskbarViewBounds() {
105                         return taskbarControllers.taskbarViewController
106                                 .getTransientTaskbarIconLayoutBoundsInParent();
107                     }
108 
109                     @Override
110                     public MultiPropertyFactory<View>.MultiProperty getIconsAlpha() {
111                         return taskbarControllers.taskbarViewController
112                                 .getTaskbarIconAlpha()
113                                 .get(ALPHA_INDEX_BUBBLE_BAR);
114                     }
115                 });
116         bubbleDragController.init(/* bubbleControllers = */ this);
117         bubbleDismissController.init(/* bubbleControllers = */ this);
118         bubbleBarPinController.init(this, bubbleBarLocationListeners);
119         bubblePinController.init(this);
120         bubbleBarSwipeController.ifPresent(c -> c.init(this));
121 
122         mPostInitRunnables.executeAllAndDestroy();
123     }
124 
125     /**
126      * If all controllers are already initialized, runs the given callback immediately. Otherwise,
127      * queues it to run after calling init() on all controllers. This should likely be used in any
128      * case where one controller is telling another controller to do something inside init().
129      */
runAfterInit(Runnable runnable)130     public void runAfterInit(Runnable runnable) {
131         // If this has been executed in init, it automatically runs adds to it.
132         mPostInitRunnables.add(runnable);
133     }
134 
135     /**
136      * Cleans up all controllers.
137      */
onDestroy()138     public void onDestroy() {
139         bubbleStashedHandleViewController.ifPresent(BubbleStashedHandleViewController::onDestroy);
140         bubbleBarController.onDestroy();
141         bubbleBarViewController.onDestroy();
142     }
143 
144     /** Dumps bubble controllers state. */
dump(PrintWriter pw)145     public void dump(PrintWriter pw) {
146         bubbleBarViewController.dump(pw);
147     }
148 }
149