blob: 117c28126a109943a2594989aa47d784804e4599 [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
maxwenf069c2b2022-11-15 22:10:05 +010036import com.android.launcher3.Utilities;
Liran Binyamina833af32023-08-02 16:30:27 -040037import com.android.launcher3.util.HorizontalInsettableView;
38import com.android.launcher3.util.MultiTranslateDelegate;
39import com.android.launcher3.views.ActivityContext;
40
Hyunyoung Song95786e02020-09-15 00:34:10 -070041/**
42 * View class that represents the bottom row of the home screen.
43 */
44public class Hotseat extends CellLayout implements Insettable {
Winson Chung3d503fb2011-07-13 17:25:49 -070045
Sunny Goyalc373e1c2021-03-09 17:27:53 -080046 // Ratio of empty space, qsb should take up to appear visually centered.
47 public static final float QSB_CENTER_FACTOR = .325f;
Liran Binyamina833af32023-08-02 16:30:27 -040048 private static final int BUBBLE_BAR_ADJUSTMENT_ANIMATION_DURATION_MS = 250;
Sunny Goyalc373e1c2021-03-09 17:27:53 -080049
Sunny Goyal4ffec482016-02-09 11:28:52 -080050 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal12069e62018-01-16 14:17:20 -080051 private boolean mHasVerticalHotseat;
Shikha Malhotraf78da1b2022-04-11 10:23:18 +000052 private Workspace<?> mWorkspace;
Adam Cohenb66675a2020-05-15 16:16:17 -070053 private boolean mSendTouchToWorkspace;
Winson Chung3d503fb2011-07-13 17:25:49 -070054
Sunny Goyalc373e1c2021-03-09 17:27:53 -080055 private final View mQsb;
Tony Wickhamae72b462021-03-10 16:18:40 -080056
Winson Chung3d503fb2011-07-13 17:25:49 -070057 public Hotseat(Context context) {
58 this(context, null);
59 }
60
61 public Hotseat(Context context, AttributeSet attrs) {
62 this(context, attrs, 0);
63 }
64
65 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
66 super(context, attrs, defStyle);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080067
maxwenf069c2b2022-11-15 22:10:05 +010068 if (Utilities.showHotseatQsbWidget(context)) {
69 mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat, this, false);
70 } else {
71 mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat_empty, this, false);
72 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -080073 addView(mQsb);
Winson Chung3d503fb2011-07-13 17:25:49 -070074 }
75
Samuel Fufaaed008d2019-12-19 10:57:40 -080076 /**
77 * Returns orientation specific cell X given invariant order in the hotseat
78 */
79 public int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070080 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070081 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080082
Samuel Fufaaed008d2019-12-19 10:57:40 -080083 /**
84 * Returns orientation specific cell Y given invariant order in the hotseat
85 */
86 public int getCellYFromOrder(int rank) {
Sunny Goyal876e4622018-11-02 13:50:40 -070087 return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070088 }
89
Sihua Maa44f4ac2024-06-03 18:35:39 +000090 boolean isHasVerticalHotseat() {
91 return mHasVerticalHotseat;
92 }
93
Sunny Goyalab770a12018-11-14 15:17:26 -080094 public void resetLayout(boolean hasVerticalHotseat) {
Liran Binyamina833af32023-08-02 16:30:27 -040095 ActivityContext activityContext = ActivityContext.lookupContext(getContext());
96 boolean bubbleBarEnabled = activityContext.isBubbleBarEnabled();
97 boolean hasBubbles = activityContext.hasBubbles();
Sunny Goyal876e4622018-11-02 13:50:40 -070098 removeAllViewsInLayout();
Sunny Goyal11e96492018-03-23 17:06:00 -070099 mHasVerticalHotseat = hasVerticalHotseat;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700100 DeviceProfile dp = mActivity.getDeviceProfile();
Liran Binyamina833af32023-08-02 16:30:27 -0400101
102 if (bubbleBarEnabled) {
103 float adjustedBorderSpace = dp.getHotseatAdjustedBorderSpaceForBubbleBar(getContext());
104 if (hasBubbles && Float.compare(adjustedBorderSpace, 0f) != 0) {
105 getShortcutsAndWidgets().setTranslationProvider(child -> {
106 int index = getShortcutsAndWidgets().indexOfChild(child);
107 float borderSpaceDelta = adjustedBorderSpace - dp.hotseatBorderSpace;
108 return dp.iconSizePx + index * borderSpaceDelta;
109 });
110 if (mQsb instanceof HorizontalInsettableView) {
111 HorizontalInsettableView insettableQsb = (HorizontalInsettableView) mQsb;
112 final float insetFraction = (float) dp.iconSizePx / dp.hotseatQsbWidth;
113 // post this to the looper so that QSB has a chance to redraw itself, e.g.
114 // after device rotation
115 mQsb.post(() -> insettableQsb.setHorizontalInsets(insetFraction));
116 }
117 } else {
118 getShortcutsAndWidgets().setTranslationProvider(null);
119 if (mQsb instanceof HorizontalInsettableView) {
120 ((HorizontalInsettableView) mQsb).setHorizontalInsets(0);
121 }
122 }
123 }
124
Thales Lima8cd020b2022-03-15 20:15:14 +0000125 resetCellSize(dp);
Sunny Goyal11e96492018-03-23 17:06:00 -0700126 if (hasVerticalHotseat) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700127 setGridSize(1, dp.numShownHotseatIcons);
Sunny Goyal11e96492018-03-23 17:06:00 -0700128 } else {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700129 setGridSize(dp.numShownHotseatIcons, 1);
Sunny Goyal11e96492018-03-23 17:06:00 -0700130 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700131 }
Adam Cohene25af792013-06-06 23:08:25 -0700132
Liran Binyamina833af32023-08-02 16:30:27 -0400133 /**
134 * Adjust the hotseat icons for the bubble bar.
135 *
136 * <p>When the bubble bar becomes visible, if needed, this method animates the hotseat icons
137 * to reduce the spacing between them and make room for the bubble bar. The QSB width is
138 * animated as well to align with the hotseat icons.
139 *
140 * <p>When the bubble bar goes away, any adjustments that were previously made are reversed.
141 */
142 public void adjustForBubbleBar(boolean isBubbleBarVisible) {
143 DeviceProfile dp = mActivity.getDeviceProfile();
144 float adjustedBorderSpace = dp.getHotseatAdjustedBorderSpaceForBubbleBar(getContext());
145 if (Float.compare(adjustedBorderSpace, 0f) == 0) {
146 return;
147 }
148
149 ShortcutAndWidgetContainer icons = getShortcutsAndWidgets();
150 AnimatorSet animatorSet = new AnimatorSet();
151 float borderSpaceDelta = adjustedBorderSpace - dp.hotseatBorderSpace;
152
153 // update the translation provider for future layout passes of hotseat icons.
154 if (isBubbleBarVisible) {
155 icons.setTranslationProvider(child -> {
156 int index = icons.indexOfChild(child);
157 return dp.iconSizePx + index * borderSpaceDelta;
158 });
159 } else {
160 icons.setTranslationProvider(null);
161 }
162
163 for (int i = 0; i < icons.getChildCount(); i++) {
164 View child = icons.getChildAt(i);
165 float tx = isBubbleBarVisible ? dp.iconSizePx + i * borderSpaceDelta : 0;
166 if (child instanceof Reorderable) {
167 MultiTranslateDelegate mtd = ((Reorderable) child).getTranslateDelegate();
168 animatorSet.play(
169 mtd.getTranslationX(INDEX_BUBBLE_ADJUSTMENT_ANIM).animateToValue(tx));
170 } else {
171 animatorSet.play(ObjectAnimator.ofFloat(child, VIEW_TRANSLATE_X, tx));
172 }
173 }
174 if (mQsb instanceof HorizontalInsettableView) {
175 HorizontalInsettableView horizontalInsettableQsb = (HorizontalInsettableView) mQsb;
176 ValueAnimator qsbAnimator = ValueAnimator.ofFloat(0f, 1f);
177 qsbAnimator.addUpdateListener(animation -> {
178 float fraction = qsbAnimator.getAnimatedFraction();
179 float insetFraction = isBubbleBarVisible
180 ? (float) dp.iconSizePx * fraction / dp.hotseatQsbWidth
181 : (float) dp.iconSizePx * (1 - fraction) / dp.hotseatQsbWidth;
182 horizontalInsettableQsb.setHorizontalInsets(insetFraction);
183 });
184 animatorSet.play(qsbAnimator);
185 }
186 animatorSet.setDuration(BUBBLE_BAR_ADJUSTMENT_ANIMATION_DURATION_MS).start();
187 }
188
Adam Cohena5f4e482013-10-11 12:10:28 -0700189 @Override
Sunny Goyal07b69292018-01-08 14:19:34 -0800190 public void setInsets(Rect insets) {
191 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
Sunny Goyalc4d32012020-04-03 17:10:11 -0700192 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal12069e62018-01-16 14:17:20 -0800193
Sunny Goyal11e96492018-03-23 17:06:00 -0700194 if (grid.isVerticalBarLayout()) {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800195 mQsb.setVisibility(View.GONE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800196 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800197 if (grid.isSeascape()) {
Sunny Goyal07b69292018-01-08 14:19:34 -0800198 lp.gravity = Gravity.LEFT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700199 lp.width = grid.hotseatBarSizePx + insets.left;
Sunny Goyal07b69292018-01-08 14:19:34 -0800200 } else {
201 lp.gravity = Gravity.RIGHT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700202 lp.width = grid.hotseatBarSizePx + insets.right;
Sunny Goyal07b69292018-01-08 14:19:34 -0800203 }
204 } else {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800205 mQsb.setVisibility(View.VISIBLE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800206 lp.gravity = Gravity.BOTTOM;
207 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
Thales Limab8c05952022-05-23 16:58:38 +0100208 lp.height = grid.hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800209 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800210
Sunny Goyal57b22792021-05-25 14:35:01 -0700211 Rect padding = grid.getHotseatLayoutPadding(getContext());
212 setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyal07b69292018-01-08 14:19:34 -0800213 setLayoutParams(lp);
214 InsettableFrameLayout.dispatchInsets(this, insets);
215 }
Sunny Goyald1580972019-04-09 11:52:49 -0700216
Shikha Malhotraf78da1b2022-04-11 10:23:18 +0000217 public void setWorkspace(Workspace<?> w) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700218 mWorkspace = w;
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800219 setCellLayoutContainer(w);
Adam Cohenb66675a2020-05-15 16:16:17 -0700220 }
221
222 @Override
223 public boolean onInterceptTouchEvent(MotionEvent ev) {
224 // We allow horizontal workspace scrolling from within the Hotseat. We do this by delegating
225 // touch intercept the Workspace, and if it intercepts, delegating touch to the Workspace
226 // for the remainder of the this input stream.
227 int yThreshold = getMeasuredHeight() - getPaddingBottom();
228 if (mWorkspace != null && ev.getY() <= yThreshold) {
229 mSendTouchToWorkspace = mWorkspace.onInterceptTouchEvent(ev);
230 return mSendTouchToWorkspace;
231 }
232 return false;
233 }
234
Sunny Goyald1580972019-04-09 11:52:49 -0700235 @Override
236 public boolean onTouchEvent(MotionEvent event) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700237 // See comment in #onInterceptTouchEvent
238 if (mSendTouchToWorkspace) {
239 final int action = event.getAction();
240 switch (action & MotionEvent.ACTION_MASK) {
241 case MotionEvent.ACTION_UP:
242 case MotionEvent.ACTION_CANCEL:
243 mSendTouchToWorkspace = false;
244 }
245 return mWorkspace.onTouchEvent(event);
246 }
Alex Chau21698c52021-09-24 14:36:15 +0100247 // Always let touch follow through to Workspace.
248 return false;
Sunny Goyald1580972019-04-09 11:52:49 -0700249 }
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -0700250
251 @Override
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800252 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
253 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
254
Alex Chau206ede92022-08-01 17:46:12 +0100255 DeviceProfile dp = mActivity.getDeviceProfile();
maxwenf069c2b2022-11-15 22:10:05 +0100256 int hotseatQsbWidth = dp.isQsbInline ? dp.hotseatQsbWidth : dp.getHostseatQsbWidth();
257 mQsb.measure(MeasureSpec.makeMeasureSpec(hotseatQsbWidth, MeasureSpec.EXACTLY),
Alex Chau206ede92022-08-01 17:46:12 +0100258 MeasureSpec.makeMeasureSpec(dp.hotseatQsbHeight, MeasureSpec.EXACTLY));
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800259 }
260
261 @Override
262 protected void onLayout(boolean changed, int l, int t, int r, int b) {
263 super.onLayout(changed, l, t, r, b);
264
Alex Chau206ede92022-08-01 17:46:12 +0100265 int qsbMeasuredWidth = mQsb.getMeasuredWidth();
Thales Limaa1012902022-01-13 17:58:42 +0000266 int left;
Alex Chau206ede92022-08-01 17:46:12 +0100267 DeviceProfile dp = mActivity.getDeviceProfile();
268 if (dp.isQsbInline) {
269 int qsbSpace = dp.hotseatBorderSpace;
Thales Lima612230d2022-03-31 18:04:37 +0100270 left = Utilities.isRtl(getResources()) ? r - getPaddingRight() + qsbSpace
Alex Chau206ede92022-08-01 17:46:12 +0100271 : l + getPaddingLeft() - qsbMeasuredWidth - qsbSpace;
Thales Limaa1012902022-01-13 17:58:42 +0000272 } else {
Alex Chau206ede92022-08-01 17:46:12 +0100273 left = (r - l - qsbMeasuredWidth) / 2;
Thales Limaa1012902022-01-13 17:58:42 +0000274 }
Alex Chau206ede92022-08-01 17:46:12 +0100275 int right = left + qsbMeasuredWidth;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800276
Alex Chau206ede92022-08-01 17:46:12 +0100277 int bottom = b - t - dp.getQsbOffsetY();
278 int top = bottom - dp.hotseatQsbHeight;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800279 mQsb.layout(left, top, right, bottom);
280 }
281
Tony Wickham39938cb2021-02-26 09:15:31 -0800282 /**
Tony Wickham9ce3b252021-03-22 14:43:58 -0700283 * Sets the alpha value of just our ShortcutAndWidgetContainer.
284 */
285 public void setIconsAlpha(float alpha) {
286 getShortcutsAndWidgets().setAlpha(alpha);
Tony Wickhamae72b462021-03-10 16:18:40 -0800287 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800288
Alex Chauc1d26442022-06-09 15:09:26 +0100289 /**
290 * Sets the alpha value of just our QSB.
291 */
292 public void setQsbAlpha(float alpha) {
293 mQsb.setAlpha(alpha);
294 }
295
Tracy Zhouae881972021-09-30 10:14:34 -0700296 public float getIconsAlpha() {
297 return getShortcutsAndWidgets().getAlpha();
298 }
299
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800300 /**
301 * Returns the QSB inside hotseat
302 */
303 public View getQsb() {
304 return mQsb;
305 }
Tony Wickham9ce3b252021-03-22 14:43:58 -0700306
Winson Chung3d503fb2011-07-13 17:25:49 -0700307}