blob: 024dde477f50e4eaa3f390939bb9dd1630b26037 [file] [log] [blame]
Winson Chung3d503fb2011-07-13 17:25:49 -07001/*
2 * Copyright (C) 2011 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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung3d503fb2011-07-13 17:25:49 -070018
Liran Binyamina833af32023-08-02 16:30:27 -040019import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_X;
20import static com.android.launcher3.util.MultiTranslateDelegate.INDEX_BUBBLE_ADJUSTMENT_ANIM;
21
22import android.animation.AnimatorSet;
23import android.animation.ObjectAnimator;
24import android.animation.ValueAnimator;
Winson Chung3d503fb2011-07-13 17:25:49 -070025import android.content.Context;
Tony Wickhamd6b40372015-09-23 18:37:57 -070026import android.graphics.Rect;
Winson Chung3d503fb2011-07-13 17:25:49 -070027import android.util.AttributeSet;
Sunny Goyal07b69292018-01-08 14:19:34 -080028import android.view.Gravity;
Sunny Goyalc373e1c2021-03-09 17:27:53 -080029import android.view.LayoutInflater;
Sunny Goyald1580972019-04-09 11:52:49 -070030import android.view.MotionEvent;
Tony Wickham39938cb2021-02-26 09:15:31 -080031import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080032import android.view.ViewDebug;
Sunny Goyal07b69292018-01-08 14:19:34 -080033import android.view.ViewGroup;
Winson Chung3d503fb2011-07-13 17:25:49 -070034import android.widget.FrameLayout;
35
Liran Binyamin69f49742024-03-04 09:39:53 -050036import androidx.annotation.IntDef;
37
Liran Binyamina833af32023-08-02 16:30:27 -040038import com.android.launcher3.util.HorizontalInsettableView;
Liran Binyamin69f49742024-03-04 09:39:53 -050039import com.android.launcher3.util.MultiPropertyFactory.MultiProperty;
Liran Binyamina833af32023-08-02 16:30:27 -040040import com.android.launcher3.util.MultiTranslateDelegate;
Liran Binyamin69f49742024-03-04 09:39:53 -050041import com.android.launcher3.util.MultiValueAlpha;
Liran Binyamina833af32023-08-02 16:30:27 -040042import com.android.launcher3.views.ActivityContext;
43
Liran Binyamin69f49742024-03-04 09:39:53 -050044import java.io.PrintWriter;
45import java.lang.annotation.Retention;
46import java.lang.annotation.RetentionPolicy;
47
Hyunyoung Song95786e02020-09-15 00:34:10 -070048/**
49 * View class that represents the bottom row of the home screen.
50 */
51public class Hotseat extends CellLayout implements Insettable {
Winson Chung3d503fb2011-07-13 17:25:49 -070052
Liran Binyamin69f49742024-03-04 09:39:53 -050053 public static final int ALPHA_CHANNEL_TASKBAR_ALIGNMENT = 0;
54 public static final int ALPHA_CHANNEL_PREVIEW_RENDERER = 1;
55 public static final int ALPHA_CHANNEL_TASKBAR_STASH = 2;
56 public static final int ALPHA_CHANNEL_CHANNELS_COUNT = 3;
57
58 @Retention(RetentionPolicy.RUNTIME)
59 @IntDef({ALPHA_CHANNEL_TASKBAR_ALIGNMENT, ALPHA_CHANNEL_PREVIEW_RENDERER,
60 ALPHA_CHANNEL_TASKBAR_STASH})
61 public @interface HotseatQsbAlphaId {
62 }
63
Sunny Goyalc373e1c2021-03-09 17:27:53 -080064 // Ratio of empty space, qsb should take up to appear visually centered.
65 public static final float QSB_CENTER_FACTOR = .325f;
Liran Binyamina833af32023-08-02 16:30:27 -040066 private static final int BUBBLE_BAR_ADJUSTMENT_ANIMATION_DURATION_MS = 250;
Sunny Goyalc373e1c2021-03-09 17:27:53 -080067
Sunny Goyal4ffec482016-02-09 11:28:52 -080068 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal12069e62018-01-16 14:17:20 -080069 private boolean mHasVerticalHotseat;
Shikha Malhotraf78da1b2022-04-11 10:23:18 +000070 private Workspace<?> mWorkspace;
Adam Cohenb66675a2020-05-15 16:16:17 -070071 private boolean mSendTouchToWorkspace;
Liran Binyamin69f49742024-03-04 09:39:53 -050072 private final MultiValueAlpha mIconsAlphaChannels;
73 private final MultiValueAlpha mQsbAlphaChannels;
Winson Chung3d503fb2011-07-13 17:25:49 -070074
Sunny Goyalc373e1c2021-03-09 17:27:53 -080075 private final View mQsb;
Tony Wickhamae72b462021-03-10 16:18:40 -080076
Winson Chung3d503fb2011-07-13 17:25:49 -070077 public Hotseat(Context context) {
78 this(context, null);
79 }
80
81 public Hotseat(Context context, AttributeSet attrs) {
82 this(context, attrs, 0);
83 }
84
85 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
86 super(context, attrs, defStyle);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080087 mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat, this, false);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080088 addView(mQsb);
Liran Binyamin69f49742024-03-04 09:39:53 -050089 mIconsAlphaChannels = new MultiValueAlpha(getShortcutsAndWidgets(),
90 ALPHA_CHANNEL_CHANNELS_COUNT);
91 mQsbAlphaChannels = new MultiValueAlpha(mQsb, ALPHA_CHANNEL_CHANNELS_COUNT);
Winson Chung3d503fb2011-07-13 17:25:49 -070092 }
93
Samuel Fufaaed008d2019-12-19 10:57:40 -080094 /**
95 * Returns orientation specific cell X given invariant order in the hotseat
96 */
97 public int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070098 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070099 }
Hyunyoung Song31178b82015-02-24 14:12:51 -0800100
Samuel Fufaaed008d2019-12-19 10:57:40 -0800101 /**
102 * Returns orientation specific cell Y given invariant order in the hotseat
103 */
104 public int getCellYFromOrder(int rank) {
Sunny Goyal876e4622018-11-02 13:50:40 -0700105 return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -0700106 }
107
Sihua Maa44f4ac2024-06-03 18:35:39 +0000108 boolean isHasVerticalHotseat() {
109 return mHasVerticalHotseat;
110 }
111
Sunny Goyalab770a12018-11-14 15:17:26 -0800112 public void resetLayout(boolean hasVerticalHotseat) {
Liran Binyamina833af32023-08-02 16:30:27 -0400113 ActivityContext activityContext = ActivityContext.lookupContext(getContext());
114 boolean bubbleBarEnabled = activityContext.isBubbleBarEnabled();
115 boolean hasBubbles = activityContext.hasBubbles();
Sunny Goyal876e4622018-11-02 13:50:40 -0700116 removeAllViewsInLayout();
Sunny Goyal11e96492018-03-23 17:06:00 -0700117 mHasVerticalHotseat = hasVerticalHotseat;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700118 DeviceProfile dp = mActivity.getDeviceProfile();
Liran Binyamina833af32023-08-02 16:30:27 -0400119
120 if (bubbleBarEnabled) {
121 float adjustedBorderSpace = dp.getHotseatAdjustedBorderSpaceForBubbleBar(getContext());
122 if (hasBubbles && Float.compare(adjustedBorderSpace, 0f) != 0) {
Liran Binyamin891e9e32024-06-21 16:33:45 -0400123 getShortcutsAndWidgets().setTranslationProvider(cellX -> {
Liran Binyamina833af32023-08-02 16:30:27 -0400124 float borderSpaceDelta = adjustedBorderSpace - dp.hotseatBorderSpace;
Liran Binyamin891e9e32024-06-21 16:33:45 -0400125 return dp.iconSizePx + cellX * borderSpaceDelta;
Liran Binyamina833af32023-08-02 16:30:27 -0400126 });
127 if (mQsb instanceof HorizontalInsettableView) {
128 HorizontalInsettableView insettableQsb = (HorizontalInsettableView) mQsb;
129 final float insetFraction = (float) dp.iconSizePx / dp.hotseatQsbWidth;
130 // post this to the looper so that QSB has a chance to redraw itself, e.g.
131 // after device rotation
132 mQsb.post(() -> insettableQsb.setHorizontalInsets(insetFraction));
133 }
134 } else {
135 getShortcutsAndWidgets().setTranslationProvider(null);
136 if (mQsb instanceof HorizontalInsettableView) {
137 ((HorizontalInsettableView) mQsb).setHorizontalInsets(0);
138 }
139 }
140 }
141
Thales Lima8cd020b2022-03-15 20:15:14 +0000142 resetCellSize(dp);
Sunny Goyal11e96492018-03-23 17:06:00 -0700143 if (hasVerticalHotseat) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700144 setGridSize(1, dp.numShownHotseatIcons);
Sunny Goyal11e96492018-03-23 17:06:00 -0700145 } else {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700146 setGridSize(dp.numShownHotseatIcons, 1);
Sunny Goyal11e96492018-03-23 17:06:00 -0700147 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700148 }
Adam Cohene25af792013-06-06 23:08:25 -0700149
Liran Binyamina833af32023-08-02 16:30:27 -0400150 /**
151 * Adjust the hotseat icons for the bubble bar.
152 *
153 * <p>When the bubble bar becomes visible, if needed, this method animates the hotseat icons
154 * to reduce the spacing between them and make room for the bubble bar. The QSB width is
155 * animated as well to align with the hotseat icons.
156 *
157 * <p>When the bubble bar goes away, any adjustments that were previously made are reversed.
158 */
159 public void adjustForBubbleBar(boolean isBubbleBarVisible) {
160 DeviceProfile dp = mActivity.getDeviceProfile();
161 float adjustedBorderSpace = dp.getHotseatAdjustedBorderSpaceForBubbleBar(getContext());
162 if (Float.compare(adjustedBorderSpace, 0f) == 0) {
163 return;
164 }
165
166 ShortcutAndWidgetContainer icons = getShortcutsAndWidgets();
167 AnimatorSet animatorSet = new AnimatorSet();
168 float borderSpaceDelta = adjustedBorderSpace - dp.hotseatBorderSpace;
169
170 // update the translation provider for future layout passes of hotseat icons.
171 if (isBubbleBarVisible) {
Liran Binyamin891e9e32024-06-21 16:33:45 -0400172 icons.setTranslationProvider(cellX -> dp.iconSizePx + cellX * borderSpaceDelta);
Liran Binyamina833af32023-08-02 16:30:27 -0400173 } else {
174 icons.setTranslationProvider(null);
175 }
176
177 for (int i = 0; i < icons.getChildCount(); i++) {
178 View child = icons.getChildAt(i);
179 float tx = isBubbleBarVisible ? dp.iconSizePx + i * borderSpaceDelta : 0;
180 if (child instanceof Reorderable) {
181 MultiTranslateDelegate mtd = ((Reorderable) child).getTranslateDelegate();
182 animatorSet.play(
183 mtd.getTranslationX(INDEX_BUBBLE_ADJUSTMENT_ANIM).animateToValue(tx));
184 } else {
185 animatorSet.play(ObjectAnimator.ofFloat(child, VIEW_TRANSLATE_X, tx));
186 }
187 }
Liran Binyamin2a47a4a2024-07-24 12:25:09 -0400188 if (mQsb instanceof HorizontalInsettableView horizontalInsettableQsb) {
189 final float currentInsetFraction = horizontalInsettableQsb.getHorizontalInsets();
190 final float targetInsetFraction =
191 isBubbleBarVisible ? (float) dp.iconSizePx / dp.hotseatQsbWidth : 0;
192 ValueAnimator qsbAnimator =
193 ValueAnimator.ofFloat(currentInsetFraction, targetInsetFraction);
Liran Binyamina833af32023-08-02 16:30:27 -0400194 qsbAnimator.addUpdateListener(animation -> {
Liran Binyamin2a47a4a2024-07-24 12:25:09 -0400195 float insetFraction = (float) animation.getAnimatedValue();
Liran Binyamina833af32023-08-02 16:30:27 -0400196 horizontalInsettableQsb.setHorizontalInsets(insetFraction);
197 });
198 animatorSet.play(qsbAnimator);
199 }
200 animatorSet.setDuration(BUBBLE_BAR_ADJUSTMENT_ANIMATION_DURATION_MS).start();
201 }
202
Adam Cohena5f4e482013-10-11 12:10:28 -0700203 @Override
Sunny Goyal07b69292018-01-08 14:19:34 -0800204 public void setInsets(Rect insets) {
205 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
Sunny Goyalc4d32012020-04-03 17:10:11 -0700206 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal12069e62018-01-16 14:17:20 -0800207
Sunny Goyal11e96492018-03-23 17:06:00 -0700208 if (grid.isVerticalBarLayout()) {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800209 mQsb.setVisibility(View.GONE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800210 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800211 if (grid.isSeascape()) {
Sunny Goyal07b69292018-01-08 14:19:34 -0800212 lp.gravity = Gravity.LEFT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700213 lp.width = grid.hotseatBarSizePx + insets.left;
Sunny Goyal07b69292018-01-08 14:19:34 -0800214 } else {
215 lp.gravity = Gravity.RIGHT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700216 lp.width = grid.hotseatBarSizePx + insets.right;
Sunny Goyal07b69292018-01-08 14:19:34 -0800217 }
218 } else {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800219 mQsb.setVisibility(View.VISIBLE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800220 lp.gravity = Gravity.BOTTOM;
221 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
Thales Limab8c05952022-05-23 16:58:38 +0100222 lp.height = grid.hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800223 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800224
Sunny Goyal57b22792021-05-25 14:35:01 -0700225 Rect padding = grid.getHotseatLayoutPadding(getContext());
226 setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyal07b69292018-01-08 14:19:34 -0800227 setLayoutParams(lp);
228 InsettableFrameLayout.dispatchInsets(this, insets);
229 }
Sunny Goyald1580972019-04-09 11:52:49 -0700230
Shikha Malhotraf78da1b2022-04-11 10:23:18 +0000231 public void setWorkspace(Workspace<?> w) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700232 mWorkspace = w;
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800233 setCellLayoutContainer(w);
Adam Cohenb66675a2020-05-15 16:16:17 -0700234 }
235
236 @Override
237 public boolean onInterceptTouchEvent(MotionEvent ev) {
238 // We allow horizontal workspace scrolling from within the Hotseat. We do this by delegating
239 // touch intercept the Workspace, and if it intercepts, delegating touch to the Workspace
240 // for the remainder of the this input stream.
241 int yThreshold = getMeasuredHeight() - getPaddingBottom();
242 if (mWorkspace != null && ev.getY() <= yThreshold) {
243 mSendTouchToWorkspace = mWorkspace.onInterceptTouchEvent(ev);
244 return mSendTouchToWorkspace;
245 }
246 return false;
247 }
248
Sunny Goyald1580972019-04-09 11:52:49 -0700249 @Override
250 public boolean onTouchEvent(MotionEvent event) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700251 // See comment in #onInterceptTouchEvent
252 if (mSendTouchToWorkspace) {
253 final int action = event.getAction();
254 switch (action & MotionEvent.ACTION_MASK) {
255 case MotionEvent.ACTION_UP:
256 case MotionEvent.ACTION_CANCEL:
257 mSendTouchToWorkspace = false;
258 }
259 return mWorkspace.onTouchEvent(event);
260 }
Alex Chau21698c52021-09-24 14:36:15 +0100261 // Always let touch follow through to Workspace.
262 return false;
Sunny Goyald1580972019-04-09 11:52:49 -0700263 }
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -0700264
265 @Override
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800266 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
267 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
268
Alex Chau206ede92022-08-01 17:46:12 +0100269 DeviceProfile dp = mActivity.getDeviceProfile();
270 mQsb.measure(MeasureSpec.makeMeasureSpec(dp.hotseatQsbWidth, MeasureSpec.EXACTLY),
271 MeasureSpec.makeMeasureSpec(dp.hotseatQsbHeight, MeasureSpec.EXACTLY));
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800272 }
273
274 @Override
275 protected void onLayout(boolean changed, int l, int t, int r, int b) {
276 super.onLayout(changed, l, t, r, b);
277
Alex Chau206ede92022-08-01 17:46:12 +0100278 int qsbMeasuredWidth = mQsb.getMeasuredWidth();
Thales Limaa1012902022-01-13 17:58:42 +0000279 int left;
Alex Chau206ede92022-08-01 17:46:12 +0100280 DeviceProfile dp = mActivity.getDeviceProfile();
281 if (dp.isQsbInline) {
282 int qsbSpace = dp.hotseatBorderSpace;
Thales Lima612230d2022-03-31 18:04:37 +0100283 left = Utilities.isRtl(getResources()) ? r - getPaddingRight() + qsbSpace
Alex Chau206ede92022-08-01 17:46:12 +0100284 : l + getPaddingLeft() - qsbMeasuredWidth - qsbSpace;
Thales Limaa1012902022-01-13 17:58:42 +0000285 } else {
Alex Chau206ede92022-08-01 17:46:12 +0100286 left = (r - l - qsbMeasuredWidth) / 2;
Thales Limaa1012902022-01-13 17:58:42 +0000287 }
Alex Chau206ede92022-08-01 17:46:12 +0100288 int right = left + qsbMeasuredWidth;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800289
Alex Chau206ede92022-08-01 17:46:12 +0100290 int bottom = b - t - dp.getQsbOffsetY();
291 int top = bottom - dp.hotseatQsbHeight;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800292 mQsb.layout(left, top, right, bottom);
293 }
294
Tony Wickham39938cb2021-02-26 09:15:31 -0800295 /**
Liran Binyamin69f49742024-03-04 09:39:53 -0500296 * Sets the alpha value of the specified alpha channel of just our ShortcutAndWidgetContainer.
Tony Wickham9ce3b252021-03-22 14:43:58 -0700297 */
Liran Binyamin69f49742024-03-04 09:39:53 -0500298 public void setIconsAlpha(float alpha, @HotseatQsbAlphaId int channelId) {
299 getIconsAlpha(channelId).setValue(alpha);
Tony Wickhamae72b462021-03-10 16:18:40 -0800300 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800301
Alex Chauc1d26442022-06-09 15:09:26 +0100302 /**
303 * Sets the alpha value of just our QSB.
304 */
Liran Binyamin69f49742024-03-04 09:39:53 -0500305 public void setQsbAlpha(float alpha, @HotseatQsbAlphaId int channelId) {
306 getQsbAlpha(channelId).setValue(alpha);
Alex Chauc1d26442022-06-09 15:09:26 +0100307 }
308
Liran Binyamin69f49742024-03-04 09:39:53 -0500309 /** Returns the alpha channel for ShortcutAndWidgetContainer */
310 public MultiProperty getIconsAlpha(@HotseatQsbAlphaId int channelId) {
311 return mIconsAlphaChannels.get(channelId);
312 }
313
314 /** Returns the alpha channel for Qsb */
315 public MultiProperty getQsbAlpha(@HotseatQsbAlphaId int channelId) {
316 return mQsbAlphaChannels.get(channelId);
Tracy Zhouae881972021-09-30 10:14:34 -0700317 }
318
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800319 /**
320 * Returns the QSB inside hotseat
321 */
322 public View getQsb() {
323 return mQsb;
324 }
Tony Wickham9ce3b252021-03-22 14:43:58 -0700325
Liran Binyamin69f49742024-03-04 09:39:53 -0500326 /** Dumps the Hotseat internal state */
327 public void dump(String prefix, PrintWriter writer) {
328 writer.println(prefix + "Hotseat:");
329 mIconsAlphaChannels.dump(
330 prefix + "\t",
331 writer,
332 "mIconsAlphaChannels",
333 "ALPHA_CHANNEL_TASKBAR_ALIGNMENT",
334 "ALPHA_CHANNEL_PREVIEW_RENDERER",
335 "ALPHA_CHANNEL_TASKBAR_STASH");
336 mQsbAlphaChannels.dump(
337 prefix + "\t",
338 writer,
339 "mQsbAlphaChannels",
340 "ALPHA_CHANNEL_TASKBAR_ALIGNMENT",
341 "ALPHA_CHANNEL_PREVIEW_RENDERER",
342 "ALPHA_CHANNEL_TASKBAR_STASH"
343 );
344 }
345
Winson Chung3d503fb2011-07-13 17:25:49 -0700346}