blob: 0e9de45e2f4aec87861a7fb240473d9b087c677b [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
Samuel Fufa2ea01e42020-12-02 13:48:18 -060032import com.android.launcher3.config.FeatureFlags;
Tony Wickhamae72b462021-03-10 16:18:40 -080033import com.android.launcher3.util.MultiValueAlpha;
Samuel Fufa2ea01e42020-12-02 13:48:18 -060034
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -070035import java.util.function.Consumer;
36
Hyunyoung Song95786e02020-09-15 00:34:10 -070037/**
38 * View class that represents the bottom row of the home screen.
39 */
40public class Hotseat extends CellLayout implements Insettable {
Winson Chung3d503fb2011-07-13 17:25:49 -070041
Tony Wickhamae72b462021-03-10 16:18:40 -080042 private static final int ALPHA_INDEX_STATE = 0;
43 private static final int ALPHA_INDEX_REPLACE_TASKBAR = 1;
44 private static final int NUM_ALPHA_CHANNELS = 2;
45
Sunny Goyalc373e1c2021-03-09 17:27:53 -080046 // Ratio of empty space, qsb should take up to appear visually centered.
47 public static final float QSB_CENTER_FACTOR = .325f;
48
Sunny Goyal4ffec482016-02-09 11:28:52 -080049 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal12069e62018-01-16 14:17:20 -080050 private boolean mHasVerticalHotseat;
Adam Cohenb66675a2020-05-15 16:16:17 -070051 private Workspace mWorkspace;
52 private boolean mSendTouchToWorkspace;
Samuel Fufa2ea01e42020-12-02 13:48:18 -060053 @Nullable
54 private Consumer<Boolean> mOnVisibilityAggregatedCallback;
Winson Chung3d503fb2011-07-13 17:25:49 -070055
Tony Wickhamae72b462021-03-10 16:18:40 -080056 private final MultiValueAlpha mMultiValueAlpha;
Sunny Goyalc373e1c2021-03-09 17:27:53 -080057 private final View mQsb;
58 private final int mQsbHeight;
Tony Wickhamae72b462021-03-10 16:18:40 -080059
Winson Chung3d503fb2011-07-13 17:25:49 -070060 public Hotseat(Context context) {
61 this(context, null);
62 }
63
64 public Hotseat(Context context, AttributeSet attrs) {
65 this(context, attrs, 0);
66 }
67
68 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
69 super(context, attrs, defStyle);
Tony Wickhamae72b462021-03-10 16:18:40 -080070 mMultiValueAlpha = new MultiValueAlpha(this, NUM_ALPHA_CHANNELS, MultiValueAlpha.Mode.MAX);
71 mMultiValueAlpha.setUpdateVisibility(true);
Sunny Goyalc373e1c2021-03-09 17:27:53 -080072
73 mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat, this, false);
74 mQsbHeight = mQsb.getLayoutParams().height;
75 addView(mQsb);
Winson Chung3d503fb2011-07-13 17:25:49 -070076 }
77
Samuel Fufaaed008d2019-12-19 10:57:40 -080078 /**
79 * Returns orientation specific cell X given invariant order in the hotseat
80 */
81 public int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070082 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070083 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080084
Samuel Fufaaed008d2019-12-19 10:57:40 -080085 /**
86 * Returns orientation specific cell Y given invariant order in the hotseat
87 */
88 public int getCellYFromOrder(int rank) {
Sunny Goyal876e4622018-11-02 13:50:40 -070089 return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070090 }
91
Sunny Goyalab770a12018-11-14 15:17:26 -080092 public void resetLayout(boolean hasVerticalHotseat) {
Sunny Goyal876e4622018-11-02 13:50:40 -070093 removeAllViewsInLayout();
Sunny Goyal11e96492018-03-23 17:06:00 -070094 mHasVerticalHotseat = hasVerticalHotseat;
Sunny Goyalab770a12018-11-14 15:17:26 -080095 InvariantDeviceProfile idp = mActivity.getDeviceProfile().inv;
Sunny Goyal11e96492018-03-23 17:06:00 -070096 if (hasVerticalHotseat) {
Sunny Goyal876e4622018-11-02 13:50:40 -070097 setGridSize(1, idp.numHotseatIcons);
Sunny Goyal11e96492018-03-23 17:06:00 -070098 } else {
Samuel Fufa2ea01e42020-12-02 13:48:18 -060099 setGridSize(idp.numHotseatIcons, FeatureFlags.ENABLE_DEVICE_SEARCH.get() ? 2 : 1);
Sunny Goyal11e96492018-03-23 17:06:00 -0700100 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600101 showInlineQsb();
Winson Chung3d503fb2011-07-13 17:25:49 -0700102 }
Adam Cohene25af792013-06-06 23:08:25 -0700103
Adam Cohena5f4e482013-10-11 12:10:28 -0700104 @Override
Sunny Goyal07b69292018-01-08 14:19:34 -0800105 public void setInsets(Rect insets) {
106 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
Sunny Goyalc4d32012020-04-03 17:10:11 -0700107 DeviceProfile grid = mActivity.getDeviceProfile();
Sunny Goyal12069e62018-01-16 14:17:20 -0800108
Sunny Goyal11e96492018-03-23 17:06:00 -0700109 if (grid.isVerticalBarLayout()) {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800110 mQsb.setVisibility(View.GONE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800111 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800112 if (grid.isSeascape()) {
Sunny Goyal07b69292018-01-08 14:19:34 -0800113 lp.gravity = Gravity.LEFT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700114 lp.width = grid.hotseatBarSizePx + insets.left;
Sunny Goyal07b69292018-01-08 14:19:34 -0800115 } else {
116 lp.gravity = Gravity.RIGHT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700117 lp.width = grid.hotseatBarSizePx + insets.right;
Sunny Goyal07b69292018-01-08 14:19:34 -0800118 }
119 } else {
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800120 mQsb.setVisibility(View.VISIBLE);
Sunny Goyal07b69292018-01-08 14:19:34 -0800121 lp.gravity = Gravity.BOTTOM;
122 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800123 lp.height = (grid.isTaskbarPresent
124 ? grid.workspacePadding.bottom
125 : grid.hotseatBarSizePx)
126 + insets.bottom;
Sunny Goyal07b69292018-01-08 14:19:34 -0800127 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800128
Tony Wickhama29a0462021-03-02 12:28:25 -0800129 if (!grid.isTaskbarPresent) {
130 // When taskbar is present, we set the padding separately to ensure a seamless visual
131 // handoff between taskbar and hotseat during drag and drop.
132 Rect padding = grid.getHotseatLayoutPadding();
133 int paddingBottom = padding.bottom;
134 if (FeatureFlags.ENABLE_DEVICE_SEARCH.get() && !grid.isVerticalBarLayout()) {
135 paddingBottom -= grid.hotseatBarBottomPaddingPx;
136 }
137 setPadding(padding.left, padding.top, padding.right, paddingBottom);
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600138 }
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700139
Sunny Goyal07b69292018-01-08 14:19:34 -0800140 setLayoutParams(lp);
141 InsettableFrameLayout.dispatchInsets(this, insets);
142 }
Sunny Goyald1580972019-04-09 11:52:49 -0700143
Adam Cohenb66675a2020-05-15 16:16:17 -0700144 public void setWorkspace(Workspace w) {
145 mWorkspace = w;
146 }
147
148 @Override
149 public boolean onInterceptTouchEvent(MotionEvent ev) {
150 // We allow horizontal workspace scrolling from within the Hotseat. We do this by delegating
151 // touch intercept the Workspace, and if it intercepts, delegating touch to the Workspace
152 // for the remainder of the this input stream.
153 int yThreshold = getMeasuredHeight() - getPaddingBottom();
154 if (mWorkspace != null && ev.getY() <= yThreshold) {
155 mSendTouchToWorkspace = mWorkspace.onInterceptTouchEvent(ev);
156 return mSendTouchToWorkspace;
157 }
158 return false;
159 }
160
Sunny Goyald1580972019-04-09 11:52:49 -0700161 @Override
162 public boolean onTouchEvent(MotionEvent event) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700163 // See comment in #onInterceptTouchEvent
164 if (mSendTouchToWorkspace) {
165 final int action = event.getAction();
166 switch (action & MotionEvent.ACTION_MASK) {
167 case MotionEvent.ACTION_UP:
168 case MotionEvent.ACTION_CANCEL:
169 mSendTouchToWorkspace = false;
170 }
171 return mWorkspace.onTouchEvent(event);
172 }
Samuel Fufaeacaf8a2019-09-17 11:31:11 -0700173 return event.getY() > getCellHeight();
Sunny Goyald1580972019-04-09 11:52:49 -0700174 }
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -0700175
176 @Override
177 public void onVisibilityAggregated(boolean isVisible) {
178 super.onVisibilityAggregated(isVisible);
179
180 if (mOnVisibilityAggregatedCallback != null) {
181 mOnVisibilityAggregatedCallback.accept(isVisible);
182 }
183 }
184
185 /** Sets a callback to be called onVisibilityAggregated */
186 public void setOnVisibilityAggregatedCallback(@Nullable Consumer<Boolean> callback) {
187 mOnVisibilityAggregatedCallback = callback;
188 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600189
190 protected void showInlineQsb() {
191 //Does nothing
192 }
Tony Wickham39938cb2021-02-26 09:15:31 -0800193
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800194 @Override
195 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
196 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
197
198 int width = getShortcutsAndWidgets().getMeasuredWidth();
199 mQsb.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
200 MeasureSpec.makeMeasureSpec(mQsbHeight, MeasureSpec.EXACTLY));
201 }
202
203 @Override
204 protected void onLayout(boolean changed, int l, int t, int r, int b) {
205 super.onLayout(changed, l, t, r, b);
206
207 int qsbWidth = mQsb.getMeasuredWidth();
208 int left = (r - l - qsbWidth) / 2;
209 int right = left + qsbWidth;
210
211 DeviceProfile dp = mActivity.getDeviceProfile();
212 int freeSpace = dp.isTaskbarPresent
213 ? dp.workspacePadding.bottom
214 : dp.hotseatBarSizePx - dp.hotseatCellHeightPx - mQsbHeight;
215 int bottom = b - t
216 - (int) (freeSpace * QSB_CENTER_FACTOR)
217 - dp.getInsets().bottom;
218 int top = bottom - mQsbHeight;
219 mQsb.layout(left, top, right, bottom);
220 }
221
Tony Wickham39938cb2021-02-26 09:15:31 -0800222 /**
223 * Returns the first View for which the given itemOperator returns true, or null.
224 */
225 public View getFirstItemMatch(Workspace.ItemOperator itemOperator) {
226 return mWorkspace.getFirstMatch(new CellLayout[] { this }, itemOperator);
227 }
Tony Wickhamae72b462021-03-10 16:18:40 -0800228
229 public MultiValueAlpha.AlphaProperty getStateAlpha() {
230 return mMultiValueAlpha.getProperty(ALPHA_INDEX_STATE);
231 }
232
233 public MultiValueAlpha.AlphaProperty getReplaceTaskbarAlpha() {
234 return mMultiValueAlpha.getProperty(ALPHA_INDEX_REPLACE_TASKBAR);
235 }
Sunny Goyalc373e1c2021-03-09 17:27:53 -0800236
237 /**
238 * Returns the QSB inside hotseat
239 */
240 public View getQsb() {
241 return mQsb;
242 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700243}