• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content.res.Resources;
19 import android.graphics.Point;
20 import android.graphics.Rect;
21 import android.view.ViewTreeObserver;
22 
23 import com.android.launcher3.DeviceProfile;
24 import com.android.launcher3.R;
25 import com.android.launcher3.anim.AnimatedFloat;
26 import com.android.launcher3.util.DimensionUtils;
27 import com.android.launcher3.util.TouchController;
28 
29 import java.io.PrintWriter;
30 
31 /**
32  * Handles properties/data collection, then passes the results to TaskbarDragLayer to render.
33  */
34 public class TaskbarDragLayerController implements TaskbarControllers.LoggableTaskbarController,
35         TaskbarControllers.BackgroundRendererController {
36 
37     private final TaskbarActivityContext mActivity;
38     private final TaskbarDragLayer mTaskbarDragLayer;
39     private final int mFolderMargin;
40 
41     // Alpha properties for taskbar background.
42     private final AnimatedFloat mBgTaskbar = new AnimatedFloat(this::updateBackgroundAlpha);
43     private final AnimatedFloat mBgNavbar = new AnimatedFloat(this::updateBackgroundAlpha);
44     private final AnimatedFloat mKeyguardBgTaskbar = new AnimatedFloat(this::updateBackgroundAlpha);
45     private final AnimatedFloat mNotificationShadeBgTaskbar = new AnimatedFloat(
46             this::updateBackgroundAlpha);
47     private final AnimatedFloat mImeBgTaskbar = new AnimatedFloat(this::updateBackgroundAlpha);
48     private final AnimatedFloat mAssistantBgTaskbar = new AnimatedFloat(
49             this::updateBackgroundAlpha);
50     // Used to hide our background color when someone else (e.g. ScrimView) is handling it.
51     private final AnimatedFloat mBgOverride = new AnimatedFloat(this::updateBackgroundAlpha);
52 
53     // Translation property for taskbar background.
54     private final AnimatedFloat mBgOffset = new AnimatedFloat(this::updateBackgroundOffset);
55 
56     // Used to fade in/out the entirety of the taskbar, for a smooth transition before/after sysui
57     // changes the inset visibility.
58     private final AnimatedFloat mTaskbarAlpha = new AnimatedFloat(this::updateTaskbarAlpha);
59 
60     // Initialized in init.
61     private TaskbarControllers mControllers;
62     private TaskbarStashViaTouchController mTaskbarStashViaTouchController;
63     private AnimatedFloat mOnBackgroundNavButtonColorIntensity;
64 
65     private float mLastSetBackgroundAlpha;
66 
TaskbarDragLayerController(TaskbarActivityContext activity, TaskbarDragLayer taskbarDragLayer)67     public TaskbarDragLayerController(TaskbarActivityContext activity,
68             TaskbarDragLayer taskbarDragLayer) {
69         mActivity = activity;
70         mTaskbarDragLayer = taskbarDragLayer;
71         final Resources resources = mTaskbarDragLayer.getResources();
72         mFolderMargin = resources.getDimensionPixelSize(R.dimen.taskbar_folder_margin);
73     }
74 
init(TaskbarControllers controllers)75     public void init(TaskbarControllers controllers) {
76         mControllers = controllers;
77         mTaskbarStashViaTouchController = new TaskbarStashViaTouchController(mControllers);
78         mTaskbarDragLayer.init(new TaskbarDragLayerCallbacks());
79 
80         mOnBackgroundNavButtonColorIntensity = mControllers.navbarButtonsViewController
81                 .getOnTaskbarBackgroundNavButtonColorOverride();
82 
83         mBgTaskbar.value = 1;
84         mKeyguardBgTaskbar.value = 1;
85         mNotificationShadeBgTaskbar.value = 1;
86         mImeBgTaskbar.value = 1;
87         mAssistantBgTaskbar.value = 1;
88         mBgOverride.value = 1;
89         updateBackgroundAlpha();
90 
91         mTaskbarAlpha.value = 1;
92         updateTaskbarAlpha();
93     }
94 
onDestroy()95     public void onDestroy() {
96         mTaskbarDragLayer.onDestroy();
97     }
98 
99     /**
100      * @return Bounds (in TaskbarDragLayer coordinates) where an opened Folder can display.
101      */
getFolderBoundingBox()102     public Rect getFolderBoundingBox() {
103         Rect boundingBox = new Rect(0, 0, mTaskbarDragLayer.getWidth(),
104                 mTaskbarDragLayer.getHeight() - mActivity.getDeviceProfile().taskbarHeight);
105         boundingBox.inset(mFolderMargin, mFolderMargin);
106         return boundingBox;
107     }
108 
getTaskbarBackgroundAlpha()109     public AnimatedFloat getTaskbarBackgroundAlpha() {
110         return mBgTaskbar;
111     }
112 
getNavbarBackgroundAlpha()113     public AnimatedFloat getNavbarBackgroundAlpha() {
114         return mBgNavbar;
115     }
116 
getKeyguardBgTaskbar()117     public AnimatedFloat getKeyguardBgTaskbar() {
118         return mKeyguardBgTaskbar;
119     }
120 
getNotificationShadeBgTaskbar()121     public AnimatedFloat getNotificationShadeBgTaskbar() {
122         return mNotificationShadeBgTaskbar;
123     }
124 
getImeBgTaskbar()125     public AnimatedFloat getImeBgTaskbar() {
126         return mImeBgTaskbar;
127     }
128 
getAssistantBgTaskbar()129     public AnimatedFloat getAssistantBgTaskbar() {
130         return mAssistantBgTaskbar;
131     }
132 
getTaskbarBackgroundOffset()133     public AnimatedFloat getTaskbarBackgroundOffset() {
134         return mBgOffset;
135     }
136 
getTaskbarAlpha()137     public AnimatedFloat getTaskbarAlpha() {
138         return mTaskbarAlpha;
139     }
140 
141     /**
142      * Make updates when configuration changes.
143      */
onConfigurationChanged()144     public void onConfigurationChanged() {
145         mTaskbarStashViaTouchController.updateGestureHeight();
146     }
147 
updateBackgroundAlpha()148     private void updateBackgroundAlpha() {
149         final float bgNavbar = mBgNavbar.value;
150         final float bgTaskbar = mBgTaskbar.value * mKeyguardBgTaskbar.value
151                 * mNotificationShadeBgTaskbar.value * mImeBgTaskbar.value
152                 * mAssistantBgTaskbar.value;
153         mLastSetBackgroundAlpha = mBgOverride.value * Math.max(bgNavbar, bgTaskbar);
154         mTaskbarDragLayer.setTaskbarBackgroundAlpha(mLastSetBackgroundAlpha);
155 
156         updateOnBackgroundNavButtonColorIntensity();
157     }
158 
159     /**
160      * Sets the translation of the background during the swipe up gesture.
161      */
setTranslationYForSwipe(float transY)162     public void setTranslationYForSwipe(float transY) {
163         mTaskbarDragLayer.setBackgroundTranslationYForSwipe(transY);
164     }
165 
166     /**
167      * Sets the translation of the background during the spring on stash animation.
168      */
setTranslationYForStash(float transY)169     public void setTranslationYForStash(float transY) {
170         mTaskbarDragLayer.setBackgroundTranslationYForStash(transY);
171     }
172 
updateBackgroundOffset()173     private void updateBackgroundOffset() {
174         mTaskbarDragLayer.setTaskbarBackgroundOffset(mBgOffset.value);
175 
176         updateOnBackgroundNavButtonColorIntensity();
177     }
178 
updateTaskbarAlpha()179     private void updateTaskbarAlpha() {
180         mTaskbarDragLayer.setAlpha(mTaskbarAlpha.value);
181     }
182 
183     @Override
setCornerRoundness(float cornerRoundness)184     public void setCornerRoundness(float cornerRoundness) {
185         mTaskbarDragLayer.setCornerRoundness(cornerRoundness);
186     }
187 
188     /**
189      * Set if another controller is temporarily handling background drawing. In this case we
190      * override our background alpha to be {@code 0}.
191      */
setIsBackgroundDrawnElsewhere(boolean isBackgroundDrawnElsewhere)192     public void setIsBackgroundDrawnElsewhere(boolean isBackgroundDrawnElsewhere) {
193         mBgOverride.updateValue(isBackgroundDrawnElsewhere ? 0 : 1);
194     }
195 
updateOnBackgroundNavButtonColorIntensity()196     private void updateOnBackgroundNavButtonColorIntensity() {
197         mOnBackgroundNavButtonColorIntensity.updateValue(
198                 mLastSetBackgroundAlpha * (1 - mBgOffset.value));
199     }
200 
201     @Override
dumpLogs(String prefix, PrintWriter pw)202     public void dumpLogs(String prefix, PrintWriter pw) {
203         pw.println(prefix + "TaskbarDragLayerController:");
204 
205         pw.println(prefix + "\tmBgOffset=" + mBgOffset.value);
206         pw.println(prefix + "\tmTaskbarAlpha=" + mTaskbarAlpha.value);
207         pw.println(prefix + "\tmFolderMargin=" + mFolderMargin);
208         pw.println(prefix + "\tmLastSetBackgroundAlpha=" + mLastSetBackgroundAlpha);
209         pw.println(prefix + "\t\tmBgOverride=" + mBgOverride.value);
210         pw.println(prefix + "\t\tmBgNavbar=" + mBgNavbar.value);
211         pw.println(prefix + "\t\tmBgTaskbar=" + mBgTaskbar.value);
212         pw.println(prefix + "\t\tmKeyguardBgTaskbar=" + mKeyguardBgTaskbar.value);
213         pw.println(prefix + "\t\tmNotificationShadeBgTaskbar=" + mNotificationShadeBgTaskbar.value);
214         pw.println(prefix + "\t\tmImeBgTaskbar=" + mImeBgTaskbar.value);
215         pw.println(prefix + "\t\tmAssistantBgTaskbar=" + mAssistantBgTaskbar.value);
216     }
217 
218     /**
219      * Callbacks for {@link TaskbarDragLayer} to interact with its controller.
220      */
221     public class TaskbarDragLayerCallbacks {
222 
223         /**
224          * Called to update the touchable insets.
225          * @see ViewTreeObserver.InternalInsetsInfo#setTouchableInsets(int)
226          */
updateInsetsTouchability(ViewTreeObserver.InternalInsetsInfo insetsInfo)227         public void updateInsetsTouchability(ViewTreeObserver.InternalInsetsInfo insetsInfo) {
228             mControllers.taskbarInsetsController.updateInsetsTouchability(insetsInfo);
229         }
230 
231         /**
232          * Called when a child is removed from TaskbarDragLayer.
233          */
onDragLayerViewRemoved()234         public void onDragLayerViewRemoved() {
235             mActivity.onDragEndOrViewRemoved();
236         }
237 
238         /**
239          * Returns how tall the background should be drawn at the bottom of the screen.
240          */
getTaskbarBackgroundHeight()241         public int getTaskbarBackgroundHeight() {
242             DeviceProfile deviceProfile = mActivity.getDeviceProfile();
243             if (TaskbarManager.isPhoneMode(deviceProfile)) {
244                 Resources resources = mActivity.getResources();
245                 Point taskbarDimensions =
246                         DimensionUtils.getTaskbarPhoneDimensions(deviceProfile, resources,
247                                 TaskbarManager.isPhoneMode(deviceProfile));
248                 return taskbarDimensions.y == -1 ?
249                         deviceProfile.getDisplayInfo().currentSize.y :
250                         taskbarDimensions.y;
251             } else {
252                 return deviceProfile.taskbarHeight;
253             }
254         }
255 
256         /**
257          * Returns touch controllers.
258          */
getTouchControllers()259         public TouchController[] getTouchControllers() {
260             return new TouchController[] {
261                     mActivity.getDragController(),
262                     mControllers.taskbarForceVisibleImmersiveController,
263                     mControllers.navbarButtonsViewController.getTouchController(),
264                     mTaskbarStashViaTouchController,
265             };
266         }
267     }
268 }
269