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