blob: 854645418a5b330b76378a8ef344c25b62e811d4 [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 Binyamina833af32023-08-02 16:30:27 -040036import com.android.launcher3.util.HorizontalInsettableView;
37import com.android.launcher3.util.MultiTranslateDelegate;
38import com.android.launcher3.views.ActivityContext;
39
Hyunyoung Song95786e02020-09-15 00:34:10 -070040/**
41 * View class that represents the bottom row of the home screen.
42 */
43public class Hotseat extends CellLayout implements Insettable {
Winson Chung3d503fb2011-07-13 17:25:49 -070044
Sunny Goyalc373e1c2021-03-09 17:27:53 -080045 // Ratio of empty space, qsb should take up to appear visually centered.
46 public static final float QSB_CENTER_FACTOR = .325f;
Liran Binyamina833af32023-08-02 16:30:27 -040047 private static final int BUBBLE_BAR_ADJUSTMENT_ANIMATION_DURATION_MS = 250;
Sunny Goyalc373e1c2021-03-09 17:27:53 -080048
Sunny Goyal4ffec482016-02-09 11:28:52 -080049 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal12069e62018-01-16 14:17:20 -080050 private boolean mHasVerticalHotseat;
Shikha Malhotraf78da1b2022-04-11 10:23:18 +000051 private Workspace<?> mWorkspace;
Adam Cohenb66675a2020-05-15 16:16:17 -070052 private boolean mSendTouchToWorkspace;
Winson Chung3d503fb2011-07-13 17:25:49 -070053
Sunny Goyalc373e1c2021-03-09 17:27:53 -080054 private final View mQsb;
Tony Wickhamae72b462021-03-10 16:18:40 -080055
Winson Chung3d503fb2011-07-13 17:25:49 -070056 public Hotseat(Context context) {
57 this(context, null);
58 }
59
60 public Hotseat(Context context, AttributeSet attrs) {
61 this(context, attrs, 0);
62 }
63
64 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
65 super(context, attrs, defStyle);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080066
67 mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat, this, false);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080068 addView(mQsb);
Winson Chung3d503fb2011-07-13 17:25:49 -070069 }
70
Samuel Fufaaed008d2019-12-19 10:57:40 -080071 /**
72 * Returns orientation specific cell X given invariant order in the hotseat
73 */
74 public int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070075 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070076 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080077
Samuel Fufaaed008d2019-12-19 10:57:40 -080078 /**
79 * Returns orientation specific cell Y given invariant order in the hotseat
80 */
81 public int getCellYFromOrder(int rank) {
Sunny Goyal876e4622018-11-02 13:50:40 -070082 return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070083 }
84
Sihua Maa44f4ac2024-06-03 18:35:39 +000085 boolean isHasVerticalHotseat() {
86 return mHasVerticalHotseat;
87 }
88
Sunny Goyalab770a12018-11-14 15:17:26 -080089 public void resetLayout(boolean hasVerticalHotseat) {
Liran Binyamina833af32023-08-02 16:30:27 -040090 ActivityContext activityContext = ActivityContext.lookupContext(getContext());
91 boolean bubbleBarEnabled = activityContext.isBubbleBarEnabled();
92 boolean hasBubbles = activityContext.hasBubbles();
Sunny Goyal876e4622018-11-02 13:50:40 -070093 removeAllViewsInLayout();
Sunny Goyal11e96492018-03-23 17:06:00 -070094 mHasVerticalHotseat = hasVerticalHotseat;
Sunny Goyal19ff7282021-04-22 10:12:54 -070095 DeviceProfile dp = mActivity.getDeviceProfile();
Liran Binyamina833af32023-08-02 16:30:27 -040096
97 if (bubbleBarEnabled) {
98 float adjustedBorderSpace = dp.getHotseatAdjustedBorderSpaceForBubbleBar(getContext());
99 if (hasBubbles && Float.compare(adjustedBorderSpace, 0f) != 0) {
100 getShortcutsAndWidgets().setTranslationProvider(child -> {
101 int index = getShortcutsAndWidgets().indexOfChild(child);
102 float borderSpaceDelta = adjustedBorderSpace - dp.hotseatBorderSpace;
103 return dp.iconSizePx + index * borderSpaceDelta;
104 });
105 if (mQsb instanceof HorizontalInsettableView) {
106 HorizontalInsettableView insettableQsb = (HorizontalInsettableView) mQsb;
107 final float insetFraction = (float) dp.iconSizePx / dp.hotseatQsbWidth;
108 // post this to the looper so that QSB has a chance to redraw itself, e.g.
109 // after device rotation
110 mQsb.post(() -> insettableQsb.setHorizontalInsets(insetFraction));
111 }
112 } else {
113 getShortcutsAndWidgets().setTranslationProvider(null);
114 if (mQsb instanceof HorizontalInsettableView) {
115 ((HorizontalInsettableView) mQsb).setHorizontalInsets(0);
116 }
117 }
118 }
119
Thales Lima8cd020b2022-03-15 20:15:14 +0000120 resetCellSize(dp);
Sunny Goyal11e96492018-03-23 17:06:00 -0700121 if (hasVerticalHotseat) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700122 setGridSize(1, dp.numShownHotseatIcons);
Sunny Goyal11e96492018-03-23 17:06:00 -0700123 } else {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700124 setGridSize(dp.numShownHotseatIcons, 1);
Sunny Goyal11e96492018-03-23 17:06:00 -0700125 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700126 }
Adam Cohene25af792013-06-06 23:08:25 -0700127
Liran Binyamina833af32023-08-02 16:30:27 -0400128 /**
129 * Adjust the hotseat icons for the bubble bar.
130 *
131 * <p>When the bubble bar becomes visible, if needed, this method animates the hotseat icons
132 * to reduce the spacing between them and make room for the bubble bar. The QSB width is
133 * animated as well to align with the hotseat icons.
134 *
135 * <p>When the bubble bar goes away, any adjustments that were previously made are reversed.
136 */
137 public void adjustForBubbleBar(boolean isBubbleBarVisible) {
138 DeviceProfile dp = mActivity.getDeviceProfile();
139 float adjustedBorderSpace = dp.getHotseatAdjustedBorderSpaceForBubbleBar(getContext());
140 if (Float.compare(adjustedBorderSpace, 0f) == 0) {
141 return;
142 }
143
144 ShortcutAndWidgetContainer icons = getShortcutsAndWidgets();
145 AnimatorSet animatorSet = new AnimatorSet();
146 float borderSpaceDelta = adjustedBorderSpace - dp.hotseatBorderSpace;
147
148 // update the translation provider for future layout passes of hotseat icons.
149 if (isBubbleBarVisible) {
150 icons.setTranslationProvider(child -> {
151 int index = icons.indexOfChild(child);
152 return dp.iconSizePx + index * borderSpaceDelta;
153 });
154 } else {
155 icons.setTranslationProvider(null);
156 }
157
158 for (int i = 0; i < icons.getChildCount(); i++) {
159 View child = icons.getChildAt(i);
160 float tx = isBubbleBarVisible ? dp.iconSizePx + i * borderSpaceDelta : 0;
161 if (child instanceof Reorderable) {
162 MultiTranslateDelegate mtd = ((Reorderable) child).getTranslateDelegate();
163 animatorSet.play(
164 mtd.getTranslationX(INDEX_BUBBLE_ADJUSTMENT_ANIM).animateToValue(tx));
165 } else {
166 animatorSet.play(ObjectAnimator.ofFloat(child, VIEW_TRANSLATE_X, tx));
167 }
168 }
169 if (mQsb instanceof HorizontalInsettableView) {
170 HorizontalInsettableView horizontalInsettableQsb = (HorizontalInsettableView) mQsb;
171 ValueAnimator qsbAnimator = ValueAnimator.ofFloat(0f, 1f);
172 qsbAnimator.addUpdateListener(animation -> {
173 float fraction = qsbAnimator.getAnimatedFraction();
174 float insetFraction = isBubbleBarVisible
175 ? (float) dp.iconSizePx * fraction / dp.hotseatQsbWidth
176 : (float) dp.iconSizePx * (1 - fraction) / dp.hotseatQsbWidth;
177 horizontalInsettableQsb.setHorizontalInsets(insetFraction);
178 });
179 animatorSet.play(qsbAnimator);
180 }
181 animatorSet.setDuration(BUBBLE_BAR_ADJUSTMENT_ANIMATION_DURATION_MS).start();
182 }
183
Adam Cohena5f4e482013-10-11 12:10:28 -0700184 @Override
Sunny Goyal07b69292018-01-08 14:19:34 -0800185 public void setInsets(Rect insets) {
186 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
Sunny Goyalc4d32012020-04-03 17:10:11 -0700187 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal12069e62018-01-16 14:17:20 -0800188
Sunny Goyal11e96492018-03-23 17:06:00 -0700189 if (grid.isVerticalBarLayout()) {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800190 mQsb.setVisibility(View.GONE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800191 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800192 if (grid.isSeascape()) {
Sunny Goyal07b69292018-01-08 14:19:34 -0800193 lp.gravity = Gravity.LEFT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700194 lp.width = grid.hotseatBarSizePx + insets.left;
Sunny Goyal07b69292018-01-08 14:19:34 -0800195 } else {
196 lp.gravity = Gravity.RIGHT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700197 lp.width = grid.hotseatBarSizePx + insets.right;
Sunny Goyal07b69292018-01-08 14:19:34 -0800198 }
199 } else {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800200 mQsb.setVisibility(View.VISIBLE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800201 lp.gravity = Gravity.BOTTOM;
202 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
Thales Limab8c05952022-05-23 16:58:38 +0100203 lp.height = grid.hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800204 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800205
Sunny Goyal57b22792021-05-25 14:35:01 -0700206 Rect padding = grid.getHotseatLayoutPadding(getContext());
207 setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyal07b69292018-01-08 14:19:34 -0800208 setLayoutParams(lp);
209 InsettableFrameLayout.dispatchInsets(this, insets);
210 }
Sunny Goyald1580972019-04-09 11:52:49 -0700211
Shikha Malhotraf78da1b2022-04-11 10:23:18 +0000212 public void setWorkspace(Workspace<?> w) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700213 mWorkspace = w;
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800214 setCellLayoutContainer(w);
Adam Cohenb66675a2020-05-15 16:16:17 -0700215 }
216
217 @Override
218 public boolean onInterceptTouchEvent(MotionEvent ev) {
219 // We allow horizontal workspace scrolling from within the Hotseat. We do this by delegating
220 // touch intercept the Workspace, and if it intercepts, delegating touch to the Workspace
221 // for the remainder of the this input stream.
222 int yThreshold = getMeasuredHeight() - getPaddingBottom();
223 if (mWorkspace != null && ev.getY() <= yThreshold) {
224 mSendTouchToWorkspace = mWorkspace.onInterceptTouchEvent(ev);
225 return mSendTouchToWorkspace;
226 }
227 return false;
228 }
229
Sunny Goyald1580972019-04-09 11:52:49 -0700230 @Override
231 public boolean onTouchEvent(MotionEvent event) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700232 // See comment in #onInterceptTouchEvent
233 if (mSendTouchToWorkspace) {
234 final int action = event.getAction();
235 switch (action & MotionEvent.ACTION_MASK) {
236 case MotionEvent.ACTION_UP:
237 case MotionEvent.ACTION_CANCEL:
238 mSendTouchToWorkspace = false;
239 }
240 return mWorkspace.onTouchEvent(event);
241 }
Alex Chau21698c52021-09-24 14:36:15 +0100242 // Always let touch follow through to Workspace.
243 return false;
Sunny Goyald1580972019-04-09 11:52:49 -0700244 }
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -0700245
246 @Override
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800247 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
248 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
249
Alex Chau206ede92022-08-01 17:46:12 +0100250 DeviceProfile dp = mActivity.getDeviceProfile();
251 mQsb.measure(MeasureSpec.makeMeasureSpec(dp.hotseatQsbWidth, MeasureSpec.EXACTLY),
252 MeasureSpec.makeMeasureSpec(dp.hotseatQsbHeight, MeasureSpec.EXACTLY));
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800253 }
254
255 @Override
256 protected void onLayout(boolean changed, int l, int t, int r, int b) {
257 super.onLayout(changed, l, t, r, b);
258
Alex Chau206ede92022-08-01 17:46:12 +0100259 int qsbMeasuredWidth = mQsb.getMeasuredWidth();
Thales Limaa1012902022-01-13 17:58:42 +0000260 int left;
Alex Chau206ede92022-08-01 17:46:12 +0100261 DeviceProfile dp = mActivity.getDeviceProfile();
262 if (dp.isQsbInline) {
263 int qsbSpace = dp.hotseatBorderSpace;
Thales Lima612230d2022-03-31 18:04:37 +0100264 left = Utilities.isRtl(getResources()) ? r - getPaddingRight() + qsbSpace
Alex Chau206ede92022-08-01 17:46:12 +0100265 : l + getPaddingLeft() - qsbMeasuredWidth - qsbSpace;
Thales Limaa1012902022-01-13 17:58:42 +0000266 } else {
Alex Chau206ede92022-08-01 17:46:12 +0100267 left = (r - l - qsbMeasuredWidth) / 2;
Thales Limaa1012902022-01-13 17:58:42 +0000268 }
Alex Chau206ede92022-08-01 17:46:12 +0100269 int right = left + qsbMeasuredWidth;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800270
Alex Chau206ede92022-08-01 17:46:12 +0100271 int bottom = b - t - dp.getQsbOffsetY();
272 int top = bottom - dp.hotseatQsbHeight;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800273 mQsb.layout(left, top, right, bottom);
274 }
275
Tony Wickham39938cb2021-02-26 09:15:31 -0800276 /**
Tony Wickham9ce3b252021-03-22 14:43:58 -0700277 * Sets the alpha value of just our ShortcutAndWidgetContainer.
278 */
279 public void setIconsAlpha(float alpha) {
280 getShortcutsAndWidgets().setAlpha(alpha);
Tony Wickhamae72b462021-03-10 16:18:40 -0800281 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800282
Alex Chauc1d26442022-06-09 15:09:26 +0100283 /**
284 * Sets the alpha value of just our QSB.
285 */
286 public void setQsbAlpha(float alpha) {
287 mQsb.setAlpha(alpha);
288 }
289
Tracy Zhouae881972021-09-30 10:14:34 -0700290 public float getIconsAlpha() {
291 return getShortcutsAndWidgets().getAlpha();
292 }
293
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800294 /**
295 * Returns the QSB inside hotseat
296 */
297 public View getQsb() {
298 return mQsb;
299 }
Tony Wickham9ce3b252021-03-22 14:43:58 -0700300
Winson Chung3d503fb2011-07-13 17:25:49 -0700301}