blob: a3e01a98b9b5e927bbe5b46556016f8248743d01 [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
Tony Wickhamae72b462021-03-10 16:18:40 -080032import com.android.launcher3.util.MultiValueAlpha;
Samuel Fufa2ea01e42020-12-02 13:48:18 -060033
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -070034import java.util.function.Consumer;
35
Hyunyoung Song95786e02020-09-15 00:34:10 -070036/**
37 * View class that represents the bottom row of the home screen.
38 */
39public class Hotseat extends CellLayout implements Insettable {
Winson Chung3d503fb2011-07-13 17:25:49 -070040
Tony Wickhamae72b462021-03-10 16:18:40 -080041 private static final int ALPHA_INDEX_STATE = 0;
42 private static final int ALPHA_INDEX_REPLACE_TASKBAR = 1;
43 private static final int NUM_ALPHA_CHANNELS = 2;
44
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;
47
Sunny Goyal4ffec482016-02-09 11:28:52 -080048 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal12069e62018-01-16 14:17:20 -080049 private boolean mHasVerticalHotseat;
Adam Cohenb66675a2020-05-15 16:16:17 -070050 private Workspace mWorkspace;
51 private boolean mSendTouchToWorkspace;
Samuel Fufa2ea01e42020-12-02 13:48:18 -060052 @Nullable
53 private Consumer<Boolean> mOnVisibilityAggregatedCallback;
Winson Chung3d503fb2011-07-13 17:25:49 -070054
Tony Wickhamae72b462021-03-10 16:18:40 -080055 private final MultiValueAlpha mMultiValueAlpha;
Sunny Goyalc373e1c2021-03-09 17:27:53 -080056 private final View mQsb;
57 private final int mQsbHeight;
Tony Wickhamae72b462021-03-10 16:18:40 -080058
Winson Chung3d503fb2011-07-13 17:25:49 -070059 public Hotseat(Context context) {
60 this(context, null);
61 }
62
63 public Hotseat(Context context, AttributeSet attrs) {
64 this(context, attrs, 0);
65 }
66
67 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
Tony Wickhamae72b462021-03-10 16:18:40 -080069 mMultiValueAlpha = new MultiValueAlpha(this, NUM_ALPHA_CHANNELS, MultiValueAlpha.Mode.MAX);
70 mMultiValueAlpha.setUpdateVisibility(true);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080071
72 mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat, this, false);
73 mQsbHeight = mQsb.getLayoutParams().height;
74 addView(mQsb);
Winson Chung3d503fb2011-07-13 17:25:49 -070075 }
76
Samuel Fufaaed008d2019-12-19 10:57:40 -080077 /**
78 * Returns orientation specific cell X given invariant order in the hotseat
79 */
80 public int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070081 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070082 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080083
Samuel Fufaaed008d2019-12-19 10:57:40 -080084 /**
85 * Returns orientation specific cell Y given invariant order in the hotseat
86 */
87 public int getCellYFromOrder(int rank) {
Sunny Goyal876e4622018-11-02 13:50:40 -070088 return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070089 }
90
Sunny Goyalab770a12018-11-14 15:17:26 -080091 public void resetLayout(boolean hasVerticalHotseat) {
Sunny Goyal876e4622018-11-02 13:50:40 -070092 removeAllViewsInLayout();
Sunny Goyal11e96492018-03-23 17:06:00 -070093 mHasVerticalHotseat = hasVerticalHotseat;
Sunny Goyalab770a12018-11-14 15:17:26 -080094 InvariantDeviceProfile idp = mActivity.getDeviceProfile().inv;
Sunny Goyal11e96492018-03-23 17:06:00 -070095 if (hasVerticalHotseat) {
Sunny Goyal876e4622018-11-02 13:50:40 -070096 setGridSize(1, idp.numHotseatIcons);
Sunny Goyal11e96492018-03-23 17:06:00 -070097 } else {
Tony Wickham72c15ef2021-03-18 18:04:02 -070098 setGridSize(idp.numHotseatIcons, 1);
Sunny Goyal11e96492018-03-23 17:06:00 -070099 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600100 showInlineQsb();
Winson Chung3d503fb2011-07-13 17:25:49 -0700101 }
Adam Cohene25af792013-06-06 23:08:25 -0700102
Adam Cohena5f4e482013-10-11 12:10:28 -0700103 @Override
Sunny Goyal07b69292018-01-08 14:19:34 -0800104 public void setInsets(Rect insets) {
105 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
Sunny Goyalc4d32012020-04-03 17:10:11 -0700106 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal12069e62018-01-16 14:17:20 -0800107
Sunny Goyal11e96492018-03-23 17:06:00 -0700108 if (grid.isVerticalBarLayout()) {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800109 mQsb.setVisibility(View.GONE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800110 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800111 if (grid.isSeascape()) {
Sunny Goyal07b69292018-01-08 14:19:34 -0800112 lp.gravity = Gravity.LEFT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700113 lp.width = grid.hotseatBarSizePx + insets.left;
Sunny Goyal07b69292018-01-08 14:19:34 -0800114 } else {
115 lp.gravity = Gravity.RIGHT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700116 lp.width = grid.hotseatBarSizePx + insets.right;
Sunny Goyal07b69292018-01-08 14:19:34 -0800117 }
118 } else {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800119 mQsb.setVisibility(View.VISIBLE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800120 lp.gravity = Gravity.BOTTOM;
121 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800122 lp.height = (grid.isTaskbarPresent
123 ? grid.workspacePadding.bottom
124 : grid.hotseatBarSizePx)
125 + insets.bottom;
Sunny Goyal07b69292018-01-08 14:19:34 -0800126 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800127
Tony Wickhama29a0462021-03-02 12:28:25 -0800128 if (!grid.isTaskbarPresent) {
129 // When taskbar is present, we set the padding separately to ensure a seamless visual
130 // handoff between taskbar and hotseat during drag and drop.
131 Rect padding = grid.getHotseatLayoutPadding();
Tony Wickham72c15ef2021-03-18 18:04:02 -0700132 setPadding(padding.left, padding.top, padding.right, padding.bottom);
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600133 }
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700134
Sunny Goyal07b69292018-01-08 14:19:34 -0800135 setLayoutParams(lp);
136 InsettableFrameLayout.dispatchInsets(this, insets);
137 }
Sunny Goyald1580972019-04-09 11:52:49 -0700138
Adam Cohenb66675a2020-05-15 16:16:17 -0700139 public void setWorkspace(Workspace w) {
140 mWorkspace = w;
141 }
142
143 @Override
144 public boolean onInterceptTouchEvent(MotionEvent ev) {
145 // We allow horizontal workspace scrolling from within the Hotseat. We do this by delegating
146 // touch intercept the Workspace, and if it intercepts, delegating touch to the Workspace
147 // for the remainder of the this input stream.
148 int yThreshold = getMeasuredHeight() - getPaddingBottom();
149 if (mWorkspace != null && ev.getY() <= yThreshold) {
150 mSendTouchToWorkspace = mWorkspace.onInterceptTouchEvent(ev);
151 return mSendTouchToWorkspace;
152 }
153 return false;
154 }
155
Sunny Goyald1580972019-04-09 11:52:49 -0700156 @Override
157 public boolean onTouchEvent(MotionEvent event) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700158 // See comment in #onInterceptTouchEvent
159 if (mSendTouchToWorkspace) {
160 final int action = event.getAction();
161 switch (action & MotionEvent.ACTION_MASK) {
162 case MotionEvent.ACTION_UP:
163 case MotionEvent.ACTION_CANCEL:
164 mSendTouchToWorkspace = false;
165 }
166 return mWorkspace.onTouchEvent(event);
167 }
Samuel Fufaeacaf8a2019-09-17 11:31:11 -0700168 return event.getY() > getCellHeight();
Sunny Goyald1580972019-04-09 11:52:49 -0700169 }
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -0700170
171 @Override
172 public void onVisibilityAggregated(boolean isVisible) {
173 super.onVisibilityAggregated(isVisible);
174
175 if (mOnVisibilityAggregatedCallback != null) {
176 mOnVisibilityAggregatedCallback.accept(isVisible);
177 }
178 }
179
180 /** Sets a callback to be called onVisibilityAggregated */
181 public void setOnVisibilityAggregatedCallback(@Nullable Consumer<Boolean> callback) {
182 mOnVisibilityAggregatedCallback = callback;
183 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600184
185 protected void showInlineQsb() {
186 //Does nothing
187 }
Tony Wickham39938cb2021-02-26 09:15:31 -0800188
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800189 @Override
190 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
191 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
192
193 int width = getShortcutsAndWidgets().getMeasuredWidth();
194 mQsb.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
195 MeasureSpec.makeMeasureSpec(mQsbHeight, MeasureSpec.EXACTLY));
196 }
197
198 @Override
199 protected void onLayout(boolean changed, int l, int t, int r, int b) {
200 super.onLayout(changed, l, t, r, b);
201
202 int qsbWidth = mQsb.getMeasuredWidth();
203 int left = (r - l - qsbWidth) / 2;
204 int right = left + qsbWidth;
205
206 DeviceProfile dp = mActivity.getDeviceProfile();
207 int freeSpace = dp.isTaskbarPresent
208 ? dp.workspacePadding.bottom
209 : dp.hotseatBarSizePx - dp.hotseatCellHeightPx - mQsbHeight;
210 int bottom = b - t
211 - (int) (freeSpace * QSB_CENTER_FACTOR)
212 - dp.getInsets().bottom;
213 int top = bottom - mQsbHeight;
214 mQsb.layout(left, top, right, bottom);
215 }
216
Tony Wickham39938cb2021-02-26 09:15:31 -0800217 /**
218 * Returns the first View for which the given itemOperator returns true, or null.
219 */
220 public View getFirstItemMatch(Workspace.ItemOperator itemOperator) {
221 return mWorkspace.getFirstMatch(new CellLayout[] { this }, itemOperator);
222 }
Tony Wickhamae72b462021-03-10 16:18:40 -0800223
224 public MultiValueAlpha.AlphaProperty getStateAlpha() {
225 return mMultiValueAlpha.getProperty(ALPHA_INDEX_STATE);
226 }
227
228 public MultiValueAlpha.AlphaProperty getReplaceTaskbarAlpha() {
229 return mMultiValueAlpha.getProperty(ALPHA_INDEX_REPLACE_TASKBAR);
230 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800231
232 /**
233 * Returns the QSB inside hotseat
234 */
235 public View getQsb() {
236 return mQsb;
237 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700238}