blob: b0e938a6ed8b0b3457f14ed4249a84a1201cd27d [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
Winson Chung3d503fb2011-07-13 17:25:49 -070052 public Hotseat(Context context) {
53 this(context, null);
54 }
55
56 public Hotseat(Context context, AttributeSet attrs) {
57 this(context, attrs, 0);
58 }
59
60 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
61 super(context, attrs, defStyle);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080062
63 mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat, this, false);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080064 addView(mQsb);
Tony Wickham9ce3b252021-03-22 14:43:58 -070065
Sunny Goyal57b22792021-05-25 14:35:01 -070066 mQsbHeight = getResources().getDimensionPixelSize(R.dimen.qsb_widget_height);
Winson Chung3d503fb2011-07-13 17:25:49 -070067 }
68
Samuel Fufaaed008d2019-12-19 10:57:40 -080069 /**
70 * Returns orientation specific cell X given invariant order in the hotseat
71 */
72 public int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070073 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070074 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080075
Samuel Fufaaed008d2019-12-19 10:57:40 -080076 /**
77 * Returns orientation specific cell Y given invariant order in the hotseat
78 */
79 public int getCellYFromOrder(int rank) {
Sunny Goyal876e4622018-11-02 13:50:40 -070080 return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070081 }
82
Sunny Goyalab770a12018-11-14 15:17:26 -080083 public void resetLayout(boolean hasVerticalHotseat) {
Sunny Goyal876e4622018-11-02 13:50:40 -070084 removeAllViewsInLayout();
Sunny Goyal11e96492018-03-23 17:06:00 -070085 mHasVerticalHotseat = hasVerticalHotseat;
Sunny Goyal19ff7282021-04-22 10:12:54 -070086 DeviceProfile dp = mActivity.getDeviceProfile();
Sunny Goyal11e96492018-03-23 17:06:00 -070087 if (hasVerticalHotseat) {
Sunny Goyal19ff7282021-04-22 10:12:54 -070088 setGridSize(1, dp.numShownHotseatIcons);
Sunny Goyal11e96492018-03-23 17:06:00 -070089 } else {
Sunny Goyal19ff7282021-04-22 10:12:54 -070090 setGridSize(dp.numShownHotseatIcons, 1);
Sunny Goyal11e96492018-03-23 17:06:00 -070091 }
Winson Chung3d503fb2011-07-13 17:25:49 -070092 }
Adam Cohene25af792013-06-06 23:08:25 -070093
Adam Cohena5f4e482013-10-11 12:10:28 -070094 @Override
Sunny Goyal07b69292018-01-08 14:19:34 -080095 public void setInsets(Rect insets) {
96 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
Sunny Goyalc4d32012020-04-03 17:10:11 -070097 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal12069e62018-01-16 14:17:20 -080098
Sunny Goyal11e96492018-03-23 17:06:00 -070099 if (grid.isVerticalBarLayout()) {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800100 mQsb.setVisibility(View.GONE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800101 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800102 if (grid.isSeascape()) {
Sunny Goyal07b69292018-01-08 14:19:34 -0800103 lp.gravity = Gravity.LEFT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700104 lp.width = grid.hotseatBarSizePx + insets.left;
Sunny Goyal07b69292018-01-08 14:19:34 -0800105 } else {
106 lp.gravity = Gravity.RIGHT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700107 lp.width = grid.hotseatBarSizePx + insets.right;
Sunny Goyal07b69292018-01-08 14:19:34 -0800108 }
109 } else {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800110 mQsb.setVisibility(View.VISIBLE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800111 lp.gravity = Gravity.BOTTOM;
112 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
Thales Lima7d149232022-03-21 18:27:45 +0000113 lp.height = grid.isTaskbarPresent
Sunny Goyal57b22792021-05-25 14:35:01 -0700114 ? grid.workspacePadding.bottom
Thales Lima7d149232022-03-21 18:27:45 +0000115 : grid.hotseatBarSizePx + insets.bottom;
Sunny Goyal07b69292018-01-08 14:19:34 -0800116 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800117
Sunny Goyal57b22792021-05-25 14:35:01 -0700118 Rect padding = grid.getHotseatLayoutPadding(getContext());
119 setPadding(padding.left, padding.top, padding.right, padding.bottom);
Sunny Goyal07b69292018-01-08 14:19:34 -0800120 setLayoutParams(lp);
121 InsettableFrameLayout.dispatchInsets(this, insets);
122 }
Sunny Goyald1580972019-04-09 11:52:49 -0700123
Adam Cohenb66675a2020-05-15 16:16:17 -0700124 public void setWorkspace(Workspace w) {
125 mWorkspace = w;
126 }
127
128 @Override
129 public boolean onInterceptTouchEvent(MotionEvent ev) {
130 // We allow horizontal workspace scrolling from within the Hotseat. We do this by delegating
131 // touch intercept the Workspace, and if it intercepts, delegating touch to the Workspace
132 // for the remainder of the this input stream.
133 int yThreshold = getMeasuredHeight() - getPaddingBottom();
134 if (mWorkspace != null && ev.getY() <= yThreshold) {
135 mSendTouchToWorkspace = mWorkspace.onInterceptTouchEvent(ev);
136 return mSendTouchToWorkspace;
137 }
138 return false;
139 }
140
Sunny Goyald1580972019-04-09 11:52:49 -0700141 @Override
142 public boolean onTouchEvent(MotionEvent event) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700143 // See comment in #onInterceptTouchEvent
144 if (mSendTouchToWorkspace) {
145 final int action = event.getAction();
146 switch (action & MotionEvent.ACTION_MASK) {
147 case MotionEvent.ACTION_UP:
148 case MotionEvent.ACTION_CANCEL:
149 mSendTouchToWorkspace = false;
150 }
151 return mWorkspace.onTouchEvent(event);
152 }
Alex Chau21698c52021-09-24 14:36:15 +0100153 // Always let touch follow through to Workspace.
154 return false;
Sunny Goyald1580972019-04-09 11:52:49 -0700155 }
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -0700156
157 @Override
158 public void onVisibilityAggregated(boolean isVisible) {
159 super.onVisibilityAggregated(isVisible);
160
161 if (mOnVisibilityAggregatedCallback != null) {
162 mOnVisibilityAggregatedCallback.accept(isVisible);
163 }
164 }
165
166 /** Sets a callback to be called onVisibilityAggregated */
167 public void setOnVisibilityAggregatedCallback(@Nullable Consumer<Boolean> callback) {
168 mOnVisibilityAggregatedCallback = callback;
169 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600170
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800171 @Override
172 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
173 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
174
Luca Zuccarini5c65feb2022-02-14 13:12:05 +0000175 int width;
176 if (mActivity.getDeviceProfile().isQsbInline) {
177 width = mActivity.getDeviceProfile().qsbWidth;
178 } else {
179 MarginLayoutParams qsbParams = (MarginLayoutParams) mQsb.getLayoutParams();
180 width = getShortcutsAndWidgets().getMeasuredWidth()
181 - qsbParams.getMarginStart()
182 - qsbParams.getMarginEnd();
183 }
Thales Limaa1012902022-01-13 17:58:42 +0000184
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800185 mQsb.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
186 MeasureSpec.makeMeasureSpec(mQsbHeight, MeasureSpec.EXACTLY));
187 }
188
189 @Override
190 protected void onLayout(boolean changed, int l, int t, int r, int b) {
191 super.onLayout(changed, l, t, r, b);
192
193 int qsbWidth = mQsb.getMeasuredWidth();
Thales Limaa1012902022-01-13 17:58:42 +0000194 int left;
195 if (mActivity.getDeviceProfile().isQsbInline) {
196 int qsbSpace = mActivity.getDeviceProfile().hotseatBorderSpace;
197 left = l + getPaddingLeft() - qsbWidth - qsbSpace;
198 } else {
199 left = (r - l - qsbWidth) / 2;
200 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800201 int right = left + qsbWidth;
202
Sunny Goyal57b22792021-05-25 14:35:01 -0700203 int bottom = b - t - mActivity.getDeviceProfile().getQsbOffsetY();
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800204 int top = bottom - mQsbHeight;
205 mQsb.layout(left, top, right, bottom);
206 }
207
Tony Wickham39938cb2021-02-26 09:15:31 -0800208 /**
Tony Wickham9ce3b252021-03-22 14:43:58 -0700209 * Sets the alpha value of just our ShortcutAndWidgetContainer.
210 */
211 public void setIconsAlpha(float alpha) {
212 getShortcutsAndWidgets().setAlpha(alpha);
Tony Wickhamae72b462021-03-10 16:18:40 -0800213 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800214
Tracy Zhouae881972021-09-30 10:14:34 -0700215 public float getIconsAlpha() {
216 return getShortcutsAndWidgets().getAlpha();
217 }
218
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800219 /**
220 * Returns the QSB inside hotseat
221 */
222 public View getQsb() {
223 return mQsb;
224 }
Tony Wickham9ce3b252021-03-22 14:43:58 -0700225
Winson Chung3d503fb2011-07-13 17:25:49 -0700226}