blob: 8496fd5d9c30329b505a4710b68f9034dbd0045d [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
19import android.content.Context;
Tony Wickhamd6b40372015-09-23 18:37:57 -070020import android.graphics.Rect;
Winson Chung3d503fb2011-07-13 17:25:49 -070021import android.util.AttributeSet;
Sunny Goyal07b69292018-01-08 14:19:34 -080022import android.view.Gravity;
Sunny Goyalc373e1c2021-03-09 17:27:53 -080023import android.view.LayoutInflater;
Sunny Goyald1580972019-04-09 11:52:49 -070024import android.view.MotionEvent;
Tony Wickham39938cb2021-02-26 09:15:31 -080025import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080026import android.view.ViewDebug;
Sunny Goyal07b69292018-01-08 14:19:34 -080027import android.view.ViewGroup;
Winson Chung3d503fb2011-07-13 17:25:49 -070028import android.widget.FrameLayout;
29
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -070030import androidx.annotation.Nullable;
31
32import java.util.function.Consumer;
33
Hyunyoung Song95786e02020-09-15 00:34:10 -070034/**
35 * View class that represents the bottom row of the home screen.
36 */
37public class Hotseat extends CellLayout implements Insettable {
Winson Chung3d503fb2011-07-13 17:25:49 -070038
Sunny Goyalc373e1c2021-03-09 17:27:53 -080039 // Ratio of empty space, qsb should take up to appear visually centered.
40 public static final float QSB_CENTER_FACTOR = .325f;
41
Sunny Goyal4ffec482016-02-09 11:28:52 -080042 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal12069e62018-01-16 14:17:20 -080043 private boolean mHasVerticalHotseat;
Adam Cohenb66675a2020-05-15 16:16:17 -070044 private Workspace mWorkspace;
45 private boolean mSendTouchToWorkspace;
Samuel Fufa2ea01e42020-12-02 13:48:18 -060046 @Nullable
47 private Consumer<Boolean> mOnVisibilityAggregatedCallback;
Winson Chung3d503fb2011-07-13 17:25:49 -070048
Sunny Goyalc373e1c2021-03-09 17:27:53 -080049 private final View mQsb;
50 private final int mQsbHeight;
Tony Wickhamae72b462021-03-10 16:18:40 -080051
Tony Wickham9ce3b252021-03-22 14:43:58 -070052 private final View mTaskbarView;
53 private final int mTaskbarViewHeight;
54
Winson Chung3d503fb2011-07-13 17:25:49 -070055 public Hotseat(Context context) {
56 this(context, null);
57 }
58
59 public Hotseat(Context context, AttributeSet attrs) {
60 this(context, attrs, 0);
61 }
62
63 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
64 super(context, attrs, defStyle);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080065
66 mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat, this, false);
67 mQsbHeight = mQsb.getLayoutParams().height;
68 addView(mQsb);
Tony Wickham9ce3b252021-03-22 14:43:58 -070069
70 mTaskbarView = LayoutInflater.from(context).inflate(R.layout.taskbar_view, this, false);
71 mTaskbarViewHeight = mTaskbarView.getLayoutParams().height;
72 // We want taskbar in the back so its background applies to Hotseat as well.
73 addView(mTaskbarView, 0);
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
Sunny Goyalab770a12018-11-14 15:17:26 -080090 public void resetLayout(boolean hasVerticalHotseat) {
Sunny Goyal876e4622018-11-02 13:50:40 -070091 removeAllViewsInLayout();
Sunny Goyal11e96492018-03-23 17:06:00 -070092 mHasVerticalHotseat = hasVerticalHotseat;
Sunny Goyalab770a12018-11-14 15:17:26 -080093 InvariantDeviceProfile idp = mActivity.getDeviceProfile().inv;
Sunny Goyal11e96492018-03-23 17:06:00 -070094 if (hasVerticalHotseat) {
Tony Wickhamb87f3cd2021-04-07 15:02:37 -070095 setGridSize(1, idp.numShownHotseatIcons);
Sunny Goyal11e96492018-03-23 17:06:00 -070096 } else {
Tony Wickhamb87f3cd2021-04-07 15:02:37 -070097 setGridSize(idp.numShownHotseatIcons, 1);
Sunny Goyal11e96492018-03-23 17:06:00 -070098 }
Winson Chung3d503fb2011-07-13 17:25:49 -070099 }
Adam Cohene25af792013-06-06 23:08:25 -0700100
Adam Cohena5f4e482013-10-11 12:10:28 -0700101 @Override
Sunny Goyal07b69292018-01-08 14:19:34 -0800102 public void setInsets(Rect insets) {
103 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
Sunny Goyalc4d32012020-04-03 17:10:11 -0700104 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal12069e62018-01-16 14:17:20 -0800105
Sunny Goyal11e96492018-03-23 17:06:00 -0700106 if (grid.isVerticalBarLayout()) {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800107 mQsb.setVisibility(View.GONE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800108 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800109 if (grid.isSeascape()) {
Sunny Goyal07b69292018-01-08 14:19:34 -0800110 lp.gravity = Gravity.LEFT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700111 lp.width = grid.hotseatBarSizePx + insets.left;
Sunny Goyal07b69292018-01-08 14:19:34 -0800112 } else {
113 lp.gravity = Gravity.RIGHT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700114 lp.width = grid.hotseatBarSizePx + insets.right;
Sunny Goyal07b69292018-01-08 14:19:34 -0800115 }
116 } else {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800117 mQsb.setVisibility(View.VISIBLE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800118 lp.gravity = Gravity.BOTTOM;
119 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800120 lp.height = (grid.isTaskbarPresent
121 ? grid.workspacePadding.bottom
122 : grid.hotseatBarSizePx)
Tony Wickhamb31d8fc2021-03-18 13:57:20 -0700123 + (grid.isTaskbarPresent ? grid.taskbarSize : insets.bottom);
Sunny Goyal07b69292018-01-08 14:19:34 -0800124 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800125
Tony Wickhama29a0462021-03-02 12:28:25 -0800126 if (!grid.isTaskbarPresent) {
127 // When taskbar is present, we set the padding separately to ensure a seamless visual
128 // handoff between taskbar and hotseat during drag and drop.
129 Rect padding = grid.getHotseatLayoutPadding();
Tony Wickham72c15ef2021-03-18 18:04:02 -0700130 setPadding(padding.left, padding.top, padding.right, padding.bottom);
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600131 }
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700132
Sunny Goyal07b69292018-01-08 14:19:34 -0800133 setLayoutParams(lp);
134 InsettableFrameLayout.dispatchInsets(this, insets);
135 }
Sunny Goyald1580972019-04-09 11:52:49 -0700136
Adam Cohenb66675a2020-05-15 16:16:17 -0700137 public void setWorkspace(Workspace w) {
138 mWorkspace = w;
139 }
140
141 @Override
142 public boolean onInterceptTouchEvent(MotionEvent ev) {
143 // We allow horizontal workspace scrolling from within the Hotseat. We do this by delegating
144 // touch intercept the Workspace, and if it intercepts, delegating touch to the Workspace
145 // for the remainder of the this input stream.
146 int yThreshold = getMeasuredHeight() - getPaddingBottom();
147 if (mWorkspace != null && ev.getY() <= yThreshold) {
148 mSendTouchToWorkspace = mWorkspace.onInterceptTouchEvent(ev);
149 return mSendTouchToWorkspace;
150 }
151 return false;
152 }
153
Sunny Goyald1580972019-04-09 11:52:49 -0700154 @Override
155 public boolean onTouchEvent(MotionEvent event) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700156 // See comment in #onInterceptTouchEvent
157 if (mSendTouchToWorkspace) {
158 final int action = event.getAction();
159 switch (action & MotionEvent.ACTION_MASK) {
160 case MotionEvent.ACTION_UP:
161 case MotionEvent.ACTION_CANCEL:
162 mSendTouchToWorkspace = false;
163 }
164 return mWorkspace.onTouchEvent(event);
165 }
Samuel Fufaeacaf8a2019-09-17 11:31:11 -0700166 return event.getY() > getCellHeight();
Sunny Goyald1580972019-04-09 11:52:49 -0700167 }
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -0700168
169 @Override
170 public void onVisibilityAggregated(boolean isVisible) {
171 super.onVisibilityAggregated(isVisible);
172
173 if (mOnVisibilityAggregatedCallback != null) {
174 mOnVisibilityAggregatedCallback.accept(isVisible);
175 }
176 }
177
178 /** Sets a callback to be called onVisibilityAggregated */
179 public void setOnVisibilityAggregatedCallback(@Nullable Consumer<Boolean> callback) {
180 mOnVisibilityAggregatedCallback = callback;
181 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600182
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800183 @Override
184 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
185 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
186
187 int width = getShortcutsAndWidgets().getMeasuredWidth();
188 mQsb.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
189 MeasureSpec.makeMeasureSpec(mQsbHeight, MeasureSpec.EXACTLY));
Tony Wickham9ce3b252021-03-22 14:43:58 -0700190 mTaskbarView.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
191 MeasureSpec.makeMeasureSpec(mTaskbarViewHeight, MeasureSpec.EXACTLY));
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800192 }
193
194 @Override
195 protected void onLayout(boolean changed, int l, int t, int r, int b) {
196 super.onLayout(changed, l, t, r, b);
197
198 int qsbWidth = mQsb.getMeasuredWidth();
199 int left = (r - l - qsbWidth) / 2;
200 int right = left + qsbWidth;
201
Tony Wickham81f81752021-04-07 11:06:40 -0700202 int bottom = b - t - getQsbOffsetY();
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800203 int top = bottom - mQsbHeight;
204 mQsb.layout(left, top, right, bottom);
Tony Wickham9ce3b252021-03-22 14:43:58 -0700205
206 int taskbarWidth = mTaskbarView.getMeasuredWidth();
207 left = (r - l - taskbarWidth) / 2;
208 right = left + taskbarWidth;
Tony Wickham81f81752021-04-07 11:06:40 -0700209 bottom = b - t - getTaskbarOffsetY();
Tony Wickham9ce3b252021-03-22 14:43:58 -0700210 top = bottom - mTaskbarViewHeight;
211 mTaskbarView.layout(left, top, right, bottom);
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800212 }
213
Tony Wickham39938cb2021-02-26 09:15:31 -0800214 /**
Tony Wickham81f81752021-04-07 11:06:40 -0700215 * Returns the number of pixels the QSB is translated from the bottom of the screen.
216 */
217 private int getQsbOffsetY() {
218 DeviceProfile dp = mActivity.getDeviceProfile();
219 int freeSpace = dp.isTaskbarPresent
220 ? dp.workspacePadding.bottom
221 : dp.hotseatBarSizePx - dp.hotseatCellHeightPx - mQsbHeight;
222 return (int) (freeSpace * QSB_CENTER_FACTOR)
223 + (dp.isTaskbarPresent ? dp.taskbarSize : dp.getInsets().bottom);
224 }
225
226 /**
227 * Returns the number of pixels the taskbar is translated from the bottom of the screen.
228 */
229 public int getTaskbarOffsetY() {
230 return (getQsbOffsetY() - mTaskbarViewHeight) / 2;
231 }
232
233 /**
Tony Wickham9ce3b252021-03-22 14:43:58 -0700234 * Sets the alpha value of just our ShortcutAndWidgetContainer.
235 */
236 public void setIconsAlpha(float alpha) {
237 getShortcutsAndWidgets().setAlpha(alpha);
Tony Wickhamae72b462021-03-10 16:18:40 -0800238 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800239
240 /**
241 * Returns the QSB inside hotseat
242 */
243 public View getQsb() {
244 return mQsb;
245 }
Tony Wickham9ce3b252021-03-22 14:43:58 -0700246
247 /**
248 * Returns the Taskbar inside hotseat
249 */
250 public View getTaskbarView() {
251 return mTaskbarView;
252 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700253}