blob: dc074a368535bf4f999271cd20549f8a64594206 [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;
micky3877362a642024-12-09 10:26:15 -050037
Liran Binyamin69f49742024-03-04 09:39:53 -050038import androidx.annotation.IntDef;
39
Liran Binyamina833af32023-08-02 16:30:27 -040040import com.android.launcher3.util.HorizontalInsettableView;
Liran Binyamin69f49742024-03-04 09:39:53 -050041import com.android.launcher3.util.MultiPropertyFactory.MultiProperty;
Liran Binyamina833af32023-08-02 16:30:27 -040042import com.android.launcher3.util.MultiTranslateDelegate;
Liran Binyamin69f49742024-03-04 09:39:53 -050043import com.android.launcher3.util.MultiValueAlpha;
Liran Binyamina833af32023-08-02 16:30:27 -040044import com.android.launcher3.views.ActivityContext;
45
Liran Binyamin69f49742024-03-04 09:39:53 -050046import java.io.PrintWriter;
47import java.lang.annotation.Retention;
48import java.lang.annotation.RetentionPolicy;
49
Hyunyoung Song95786e02020-09-15 00:34:10 -070050/**
51 * View class that represents the bottom row of the home screen.
52 */
53public class Hotseat extends CellLayout implements Insettable {
Winson Chung3d503fb2011-07-13 17:25:49 -070054
Liran Binyamin69f49742024-03-04 09:39:53 -050055 public static final int ALPHA_CHANNEL_TASKBAR_ALIGNMENT = 0;
56 public static final int ALPHA_CHANNEL_PREVIEW_RENDERER = 1;
57 public static final int ALPHA_CHANNEL_TASKBAR_STASH = 2;
58 public static final int ALPHA_CHANNEL_CHANNELS_COUNT = 3;
59
60 @Retention(RetentionPolicy.RUNTIME)
61 @IntDef({ALPHA_CHANNEL_TASKBAR_ALIGNMENT, ALPHA_CHANNEL_PREVIEW_RENDERER,
62 ALPHA_CHANNEL_TASKBAR_STASH})
63 public @interface HotseatQsbAlphaId {
64 }
65
Sunny Goyalc373e1c2021-03-09 17:27:53 -080066 // Ratio of empty space, qsb should take up to appear visually centered.
67 public static final float QSB_CENTER_FACTOR = .325f;
Liran Binyamina833af32023-08-02 16:30:27 -040068 private static final int BUBBLE_BAR_ADJUSTMENT_ANIMATION_DURATION_MS = 250;
Sunny Goyalc373e1c2021-03-09 17:27:53 -080069
Sunny Goyal4ffec482016-02-09 11:28:52 -080070 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal12069e62018-01-16 14:17:20 -080071 private boolean mHasVerticalHotseat;
Shikha Malhotraf78da1b2022-04-11 10:23:18 +000072 private Workspace<?> mWorkspace;
Adam Cohenb66675a2020-05-15 16:16:17 -070073 private boolean mSendTouchToWorkspace;
Liran Binyamin69f49742024-03-04 09:39:53 -050074 private final MultiValueAlpha mIconsAlphaChannels;
75 private final MultiValueAlpha mQsbAlphaChannels;
Winson Chung3d503fb2011-07-13 17:25:49 -070076
Sunny Goyalc373e1c2021-03-09 17:27:53 -080077 private final View mQsb;
Tony Wickhamae72b462021-03-10 16:18:40 -080078
Winson Chung3d503fb2011-07-13 17:25:49 -070079 public Hotseat(Context context) {
80 this(context, null);
81 }
82
83 public Hotseat(Context context, AttributeSet attrs) {
84 this(context, attrs, 0);
85 }
86
87 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
88 super(context, attrs, defStyle);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080089
maxwenf069c2b2022-11-15 22:10:05 +010090 if (Utilities.showHotseatQsbWidget(context)) {
91 mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat, this, false);
92 } else {
93 mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat_empty, this, false);
94 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -080095 addView(mQsb);
Liran Binyamin69f49742024-03-04 09:39:53 -050096 mIconsAlphaChannels = new MultiValueAlpha(getShortcutsAndWidgets(),
97 ALPHA_CHANNEL_CHANNELS_COUNT);
98 mQsbAlphaChannels = new MultiValueAlpha(mQsb, ALPHA_CHANNEL_CHANNELS_COUNT);
Winson Chung3d503fb2011-07-13 17:25:49 -070099 }
100
Samuel Fufaaed008d2019-12-19 10:57:40 -0800101 /**
102 * Returns orientation specific cell X given invariant order in the hotseat
103 */
104 public int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700105 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700106 }
Hyunyoung Song31178b82015-02-24 14:12:51 -0800107
Samuel Fufaaed008d2019-12-19 10:57:40 -0800108 /**
109 * Returns orientation specific cell Y given invariant order in the hotseat
110 */
111 public int getCellYFromOrder(int rank) {
Sunny Goyal876e4622018-11-02 13:50:40 -0700112 return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -0700113 }
114
Sihua Maa44f4ac2024-06-03 18:35:39 +0000115 boolean isHasVerticalHotseat() {
116 return mHasVerticalHotseat;
117 }
118
Sunny Goyalab770a12018-11-14 15:17:26 -0800119 public void resetLayout(boolean hasVerticalHotseat) {
Liran Binyamina833af32023-08-02 16:30:27 -0400120 ActivityContext activityContext = ActivityContext.lookupContext(getContext());
121 boolean bubbleBarEnabled = activityContext.isBubbleBarEnabled();
122 boolean hasBubbles = activityContext.hasBubbles();
Sunny Goyal876e4622018-11-02 13:50:40 -0700123 removeAllViewsInLayout();
Sunny Goyal11e96492018-03-23 17:06:00 -0700124 mHasVerticalHotseat = hasVerticalHotseat;
Sunny Goyal19ff7282021-04-22 10:12:54 -0700125 DeviceProfile dp = mActivity.getDeviceProfile();
Liran Binyamina833af32023-08-02 16:30:27 -0400126
127 if (bubbleBarEnabled) {
128 float adjustedBorderSpace = dp.getHotseatAdjustedBorderSpaceForBubbleBar(getContext());
129 if (hasBubbles && Float.compare(adjustedBorderSpace, 0f) != 0) {
Liran Binyamin891e9e32024-06-21 16:33:45 -0400130 getShortcutsAndWidgets().setTranslationProvider(cellX -> {
Liran Binyamina833af32023-08-02 16:30:27 -0400131 float borderSpaceDelta = adjustedBorderSpace - dp.hotseatBorderSpace;
Liran Binyamin891e9e32024-06-21 16:33:45 -0400132 return dp.iconSizePx + cellX * borderSpaceDelta;
Liran Binyamina833af32023-08-02 16:30:27 -0400133 });
134 if (mQsb instanceof HorizontalInsettableView) {
135 HorizontalInsettableView insettableQsb = (HorizontalInsettableView) mQsb;
136 final float insetFraction = (float) dp.iconSizePx / dp.hotseatQsbWidth;
137 // post this to the looper so that QSB has a chance to redraw itself, e.g.
138 // after device rotation
139 mQsb.post(() -> insettableQsb.setHorizontalInsets(insetFraction));
140 }
141 } else {
142 getShortcutsAndWidgets().setTranslationProvider(null);
143 if (mQsb instanceof HorizontalInsettableView) {
144 ((HorizontalInsettableView) mQsb).setHorizontalInsets(0);
145 }
146 }
147 }
148
Thales Lima8cd020b2022-03-15 20:15:14 +0000149 resetCellSize(dp);
Sunny Goyal11e96492018-03-23 17:06:00 -0700150 if (hasVerticalHotseat) {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700151 setGridSize(1, dp.numShownHotseatIcons);
Sunny Goyal11e96492018-03-23 17:06:00 -0700152 } else {
Sunny Goyal19ff7282021-04-22 10:12:54 -0700153 setGridSize(dp.numShownHotseatIcons, 1);
Sunny Goyal11e96492018-03-23 17:06:00 -0700154 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700155 }
Adam Cohene25af792013-06-06 23:08:25 -0700156
Liran Binyamina833af32023-08-02 16:30:27 -0400157 /**
158 * Adjust the hotseat icons for the bubble bar.
159 *
160 * <p>When the bubble bar becomes visible, if needed, this method animates the hotseat icons
161 * to reduce the spacing between them and make room for the bubble bar. The QSB width is
162 * animated as well to align with the hotseat icons.
163 *
164 * <p>When the bubble bar goes away, any adjustments that were previously made are reversed.
165 */
166 public void adjustForBubbleBar(boolean isBubbleBarVisible) {
167 DeviceProfile dp = mActivity.getDeviceProfile();
168 float adjustedBorderSpace = dp.getHotseatAdjustedBorderSpaceForBubbleBar(getContext());
169 if (Float.compare(adjustedBorderSpace, 0f) == 0) {
170 return;
171 }
172
173 ShortcutAndWidgetContainer icons = getShortcutsAndWidgets();
174 AnimatorSet animatorSet = new AnimatorSet();
175 float borderSpaceDelta = adjustedBorderSpace - dp.hotseatBorderSpace;
176
177 // update the translation provider for future layout passes of hotseat icons.
178 if (isBubbleBarVisible) {
Liran Binyamin891e9e32024-06-21 16:33:45 -0400179 icons.setTranslationProvider(cellX -> dp.iconSizePx + cellX * borderSpaceDelta);
Liran Binyamina833af32023-08-02 16:30:27 -0400180 } else {
181 icons.setTranslationProvider(null);
182 }
183
184 for (int i = 0; i < icons.getChildCount(); i++) {
185 View child = icons.getChildAt(i);
186 float tx = isBubbleBarVisible ? dp.iconSizePx + i * borderSpaceDelta : 0;
187 if (child instanceof Reorderable) {
188 MultiTranslateDelegate mtd = ((Reorderable) child).getTranslateDelegate();
189 animatorSet.play(
190 mtd.getTranslationX(INDEX_BUBBLE_ADJUSTMENT_ANIM).animateToValue(tx));
191 } else {
192 animatorSet.play(ObjectAnimator.ofFloat(child, VIEW_TRANSLATE_X, tx));
193 }
194 }
Liran Binyamin2a47a4a2024-07-24 12:25:09 -0400195 if (mQsb instanceof HorizontalInsettableView horizontalInsettableQsb) {
196 final float currentInsetFraction = horizontalInsettableQsb.getHorizontalInsets();
197 final float targetInsetFraction =
198 isBubbleBarVisible ? (float) dp.iconSizePx / dp.hotseatQsbWidth : 0;
199 ValueAnimator qsbAnimator =
200 ValueAnimator.ofFloat(currentInsetFraction, targetInsetFraction);
Liran Binyamina833af32023-08-02 16:30:27 -0400201 qsbAnimator.addUpdateListener(animation -> {
Liran Binyamin2a47a4a2024-07-24 12:25:09 -0400202 float insetFraction = (float) animation.getAnimatedValue();
Liran Binyamina833af32023-08-02 16:30:27 -0400203 horizontalInsettableQsb.setHorizontalInsets(insetFraction);
204 });
205 animatorSet.play(qsbAnimator);
206 }
207 animatorSet.setDuration(BUBBLE_BAR_ADJUSTMENT_ANIMATION_DURATION_MS).start();
208 }
209
Adam Cohena5f4e482013-10-11 12:10:28 -0700210 @Override
Sunny Goyal07b69292018-01-08 14:19:34 -0800211 public void setInsets(Rect insets) {
212 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
Sunny Goyalc4d32012020-04-03 17:10:11 -0700213 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal12069e62018-01-16 14:17:20 -0800214
Sunny Goyal11e96492018-03-23 17:06:00 -0700215 if (grid.isVerticalBarLayout()) {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800216 mQsb.setVisibility(View.GONE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800217 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800218 if (grid.isSeascape()) {
Sunny Goyal07b69292018-01-08 14:19:34 -0800219 lp.gravity = Gravity.LEFT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700220 lp.width = grid.hotseatBarSizePx + insets.left;
Sunny Goyal07b69292018-01-08 14:19:34 -0800221 } else {
222 lp.gravity = Gravity.RIGHT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700223 lp.width = grid.hotseatBarSizePx + insets.right;
Sunny Goyal07b69292018-01-08 14:19:34 -0800224 }
225 } else {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800226 mQsb.setVisibility(View.VISIBLE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800227 lp.gravity = Gravity.BOTTOM;
228 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
Thales Limab8c05952022-05-23 16:58:38 +0100229 lp.height = grid.hotseatBarSizePx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800230 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800231
Sunny Goyal57b22792021-05-25 14:35:01 -0700232 Rect padding = grid.getHotseatLayoutPadding(getContext());
233 setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyal07b69292018-01-08 14:19:34 -0800234 setLayoutParams(lp);
235 InsettableFrameLayout.dispatchInsets(this, insets);
236 }
Sunny Goyald1580972019-04-09 11:52:49 -0700237
Shikha Malhotraf78da1b2022-04-11 10:23:18 +0000238 public void setWorkspace(Workspace<?> w) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700239 mWorkspace = w;
Sebastian Franco2986e0b2024-01-25 10:23:39 -0800240 setCellLayoutContainer(w);
Adam Cohenb66675a2020-05-15 16:16:17 -0700241 }
242
243 @Override
244 public boolean onInterceptTouchEvent(MotionEvent ev) {
245 // We allow horizontal workspace scrolling from within the Hotseat. We do this by delegating
246 // touch intercept the Workspace, and if it intercepts, delegating touch to the Workspace
247 // for the remainder of the this input stream.
248 int yThreshold = getMeasuredHeight() - getPaddingBottom();
249 if (mWorkspace != null && ev.getY() <= yThreshold) {
250 mSendTouchToWorkspace = mWorkspace.onInterceptTouchEvent(ev);
251 return mSendTouchToWorkspace;
252 }
253 return false;
254 }
255
Sunny Goyald1580972019-04-09 11:52:49 -0700256 @Override
257 public boolean onTouchEvent(MotionEvent event) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700258 // See comment in #onInterceptTouchEvent
259 if (mSendTouchToWorkspace) {
260 final int action = event.getAction();
261 switch (action & MotionEvent.ACTION_MASK) {
262 case MotionEvent.ACTION_UP:
263 case MotionEvent.ACTION_CANCEL:
264 mSendTouchToWorkspace = false;
265 }
266 return mWorkspace.onTouchEvent(event);
267 }
Alex Chau21698c52021-09-24 14:36:15 +0100268 // Always let touch follow through to Workspace.
269 return false;
Sunny Goyald1580972019-04-09 11:52:49 -0700270 }
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -0700271
272 @Override
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800273 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
274 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
275
Alex Chau206ede92022-08-01 17:46:12 +0100276 DeviceProfile dp = mActivity.getDeviceProfile();
maxwenf069c2b2022-11-15 22:10:05 +0100277 int hotseatQsbWidth = dp.isQsbInline ? dp.hotseatQsbWidth : dp.getHostseatQsbWidth();
278 mQsb.measure(MeasureSpec.makeMeasureSpec(hotseatQsbWidth, MeasureSpec.EXACTLY),
Alex Chau206ede92022-08-01 17:46:12 +0100279 MeasureSpec.makeMeasureSpec(dp.hotseatQsbHeight, MeasureSpec.EXACTLY));
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800280 }
281
282 @Override
283 protected void onLayout(boolean changed, int l, int t, int r, int b) {
284 super.onLayout(changed, l, t, r, b);
285
Alex Chau206ede92022-08-01 17:46:12 +0100286 int qsbMeasuredWidth = mQsb.getMeasuredWidth();
Thales Limaa1012902022-01-13 17:58:42 +0000287 int left;
Alex Chau206ede92022-08-01 17:46:12 +0100288 DeviceProfile dp = mActivity.getDeviceProfile();
289 if (dp.isQsbInline) {
290 int qsbSpace = dp.hotseatBorderSpace;
Thales Lima612230d2022-03-31 18:04:37 +0100291 left = Utilities.isRtl(getResources()) ? r - getPaddingRight() + qsbSpace
Alex Chau206ede92022-08-01 17:46:12 +0100292 : l + getPaddingLeft() - qsbMeasuredWidth - qsbSpace;
Thales Limaa1012902022-01-13 17:58:42 +0000293 } else {
Alex Chau206ede92022-08-01 17:46:12 +0100294 left = (r - l - qsbMeasuredWidth) / 2;
Thales Limaa1012902022-01-13 17:58:42 +0000295 }
Alex Chau206ede92022-08-01 17:46:12 +0100296 int right = left + qsbMeasuredWidth;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800297
Alex Chau206ede92022-08-01 17:46:12 +0100298 int bottom = b - t - dp.getQsbOffsetY();
299 int top = bottom - dp.hotseatQsbHeight;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800300 mQsb.layout(left, top, right, bottom);
301 }
302
Tony Wickham39938cb2021-02-26 09:15:31 -0800303 /**
Liran Binyamin69f49742024-03-04 09:39:53 -0500304 * Sets the alpha value of the specified alpha channel of just our ShortcutAndWidgetContainer.
Tony Wickham9ce3b252021-03-22 14:43:58 -0700305 */
Liran Binyamin69f49742024-03-04 09:39:53 -0500306 public void setIconsAlpha(float alpha, @HotseatQsbAlphaId int channelId) {
307 getIconsAlpha(channelId).setValue(alpha);
Tony Wickhamae72b462021-03-10 16:18:40 -0800308 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800309
Alex Chauc1d26442022-06-09 15:09:26 +0100310 /**
311 * Sets the alpha value of just our QSB.
312 */
Liran Binyamin69f49742024-03-04 09:39:53 -0500313 public void setQsbAlpha(float alpha, @HotseatQsbAlphaId int channelId) {
314 getQsbAlpha(channelId).setValue(alpha);
Alex Chauc1d26442022-06-09 15:09:26 +0100315 }
316
Liran Binyamin69f49742024-03-04 09:39:53 -0500317 /** Returns the alpha channel for ShortcutAndWidgetContainer */
318 public MultiProperty getIconsAlpha(@HotseatQsbAlphaId int channelId) {
319 return mIconsAlphaChannels.get(channelId);
320 }
321
322 /** Returns the alpha channel for Qsb */
323 public MultiProperty getQsbAlpha(@HotseatQsbAlphaId int channelId) {
324 return mQsbAlphaChannels.get(channelId);
Tracy Zhouae881972021-09-30 10:14:34 -0700325 }
326
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800327 /**
328 * Returns the QSB inside hotseat
329 */
330 public View getQsb() {
331 return mQsb;
332 }
Tony Wickham9ce3b252021-03-22 14:43:58 -0700333
Liran Binyamin69f49742024-03-04 09:39:53 -0500334 /** Dumps the Hotseat internal state */
335 public void dump(String prefix, PrintWriter writer) {
336 writer.println(prefix + "Hotseat:");
337 mIconsAlphaChannels.dump(
338 prefix + "\t",
339 writer,
340 "mIconsAlphaChannels",
341 "ALPHA_CHANNEL_TASKBAR_ALIGNMENT",
342 "ALPHA_CHANNEL_PREVIEW_RENDERER",
343 "ALPHA_CHANNEL_TASKBAR_STASH");
344 mQsbAlphaChannels.dump(
345 prefix + "\t",
346 writer,
347 "mQsbAlphaChannels",
348 "ALPHA_CHANNEL_TASKBAR_ALIGNMENT",
349 "ALPHA_CHANNEL_PREVIEW_RENDERER",
350 "ALPHA_CHANNEL_TASKBAR_STASH"
351 );
352 }
353
Winson Chung3d503fb2011-07-13 17:25:49 -0700354}