blob: e5b75c13823a456d9829ea3fa24215fcd5f50414 [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 Goyald1580972019-04-09 11:52:49 -070023import android.view.MotionEvent;
Tony Wickham39938cb2021-02-26 09:15:31 -080024import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080025import android.view.ViewDebug;
Sunny Goyal07b69292018-01-08 14:19:34 -080026import android.view.ViewGroup;
Winson Chung3d503fb2011-07-13 17:25:49 -070027import android.widget.FrameLayout;
28
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -070029import androidx.annotation.Nullable;
30
Samuel Fufa2ea01e42020-12-02 13:48:18 -060031import com.android.launcher3.config.FeatureFlags;
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 Goyal4ffec482016-02-09 11:28:52 -080045 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal12069e62018-01-16 14:17:20 -080046 private boolean mHasVerticalHotseat;
Adam Cohenb66675a2020-05-15 16:16:17 -070047 private Workspace mWorkspace;
48 private boolean mSendTouchToWorkspace;
Samuel Fufa2ea01e42020-12-02 13:48:18 -060049 @Nullable
50 private Consumer<Boolean> mOnVisibilityAggregatedCallback;
Winson Chung3d503fb2011-07-13 17:25:49 -070051
Tony Wickhamae72b462021-03-10 16:18:40 -080052 private final MultiValueAlpha mMultiValueAlpha;
53
Winson Chung3d503fb2011-07-13 17:25:49 -070054 public Hotseat(Context context) {
55 this(context, null);
56 }
57
58 public Hotseat(Context context, AttributeSet attrs) {
59 this(context, attrs, 0);
60 }
61
62 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
63 super(context, attrs, defStyle);
Tony Wickhamae72b462021-03-10 16:18:40 -080064 mMultiValueAlpha = new MultiValueAlpha(this, NUM_ALPHA_CHANNELS, MultiValueAlpha.Mode.MAX);
65 mMultiValueAlpha.setUpdateVisibility(true);
Winson Chung3d503fb2011-07-13 17:25:49 -070066 }
67
Samuel Fufaaed008d2019-12-19 10:57:40 -080068 /**
69 * Returns orientation specific cell X given invariant order in the hotseat
70 */
71 public int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070072 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070073 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080074
Samuel Fufaaed008d2019-12-19 10:57:40 -080075 /**
76 * Returns orientation specific cell Y given invariant order in the hotseat
77 */
78 public int getCellYFromOrder(int rank) {
Sunny Goyal876e4622018-11-02 13:50:40 -070079 return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0;
Winson Chung3d503fb2011-07-13 17:25:49 -070080 }
81
Sunny Goyalab770a12018-11-14 15:17:26 -080082 public void resetLayout(boolean hasVerticalHotseat) {
Sunny Goyal876e4622018-11-02 13:50:40 -070083 removeAllViewsInLayout();
Sunny Goyal11e96492018-03-23 17:06:00 -070084 mHasVerticalHotseat = hasVerticalHotseat;
Sunny Goyalab770a12018-11-14 15:17:26 -080085 InvariantDeviceProfile idp = mActivity.getDeviceProfile().inv;
Sunny Goyal11e96492018-03-23 17:06:00 -070086 if (hasVerticalHotseat) {
Sunny Goyal876e4622018-11-02 13:50:40 -070087 setGridSize(1, idp.numHotseatIcons);
Sunny Goyal11e96492018-03-23 17:06:00 -070088 } else {
Samuel Fufa2ea01e42020-12-02 13:48:18 -060089 setGridSize(idp.numHotseatIcons, FeatureFlags.ENABLE_DEVICE_SEARCH.get() ? 2 : 1);
Sunny Goyal11e96492018-03-23 17:06:00 -070090 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -060091 showInlineQsb();
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 Goyal07b69292018-01-08 14:19:34 -0800100 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
Sunny Goyal7e2e67f2018-01-26 13:40:08 -0800101 if (grid.isSeascape()) {
Sunny Goyal07b69292018-01-08 14:19:34 -0800102 lp.gravity = Gravity.LEFT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700103 lp.width = grid.hotseatBarSizePx + insets.left;
Sunny Goyal07b69292018-01-08 14:19:34 -0800104 } else {
105 lp.gravity = Gravity.RIGHT;
Tony Wickham1eeffbe2018-06-12 15:01:15 -0700106 lp.width = grid.hotseatBarSizePx + insets.right;
Sunny Goyal07b69292018-01-08 14:19:34 -0800107 }
108 } else {
109 lp.gravity = Gravity.BOTTOM;
110 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
Tony Wickhama29a0462021-03-02 12:28:25 -0800111 lp.height = grid.isTaskbarPresent
112 ? grid.taskbarSize
113 : grid.hotseatBarSizePx + insets.bottom;
Sunny Goyal07b69292018-01-08 14:19:34 -0800114 }
Tony Wickhama29a0462021-03-02 12:28:25 -0800115 if (!grid.isTaskbarPresent) {
116 // When taskbar is present, we set the padding separately to ensure a seamless visual
117 // handoff between taskbar and hotseat during drag and drop.
118 Rect padding = grid.getHotseatLayoutPadding();
119 int paddingBottom = padding.bottom;
120 if (FeatureFlags.ENABLE_DEVICE_SEARCH.get() && !grid.isVerticalBarLayout()) {
121 paddingBottom -= grid.hotseatBarBottomPaddingPx;
122 }
123 setPadding(padding.left, padding.top, padding.right, paddingBottom);
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600124 }
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700125
Sunny Goyal07b69292018-01-08 14:19:34 -0800126 setLayoutParams(lp);
127 InsettableFrameLayout.dispatchInsets(this, insets);
128 }
Sunny Goyald1580972019-04-09 11:52:49 -0700129
Adam Cohenb66675a2020-05-15 16:16:17 -0700130 public void setWorkspace(Workspace w) {
131 mWorkspace = w;
132 }
133
134 @Override
135 public boolean onInterceptTouchEvent(MotionEvent ev) {
136 // We allow horizontal workspace scrolling from within the Hotseat. We do this by delegating
137 // touch intercept the Workspace, and if it intercepts, delegating touch to the Workspace
138 // for the remainder of the this input stream.
139 int yThreshold = getMeasuredHeight() - getPaddingBottom();
140 if (mWorkspace != null && ev.getY() <= yThreshold) {
141 mSendTouchToWorkspace = mWorkspace.onInterceptTouchEvent(ev);
142 return mSendTouchToWorkspace;
143 }
144 return false;
145 }
146
Sunny Goyald1580972019-04-09 11:52:49 -0700147 @Override
148 public boolean onTouchEvent(MotionEvent event) {
Adam Cohenb66675a2020-05-15 16:16:17 -0700149 // See comment in #onInterceptTouchEvent
150 if (mSendTouchToWorkspace) {
151 final int action = event.getAction();
152 switch (action & MotionEvent.ACTION_MASK) {
153 case MotionEvent.ACTION_UP:
154 case MotionEvent.ACTION_CANCEL:
155 mSendTouchToWorkspace = false;
156 }
157 return mWorkspace.onTouchEvent(event);
158 }
Samuel Fufaeacaf8a2019-09-17 11:31:11 -0700159 return event.getY() > getCellHeight();
Sunny Goyald1580972019-04-09 11:52:49 -0700160 }
Schneider Victor-tulias3cf264f2020-09-22 12:58:38 -0700161
162 @Override
163 public void onVisibilityAggregated(boolean isVisible) {
164 super.onVisibilityAggregated(isVisible);
165
166 if (mOnVisibilityAggregatedCallback != null) {
167 mOnVisibilityAggregatedCallback.accept(isVisible);
168 }
169 }
170
171 /** Sets a callback to be called onVisibilityAggregated */
172 public void setOnVisibilityAggregatedCallback(@Nullable Consumer<Boolean> callback) {
173 mOnVisibilityAggregatedCallback = callback;
174 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600175
176 protected void showInlineQsb() {
177 //Does nothing
178 }
Tony Wickham39938cb2021-02-26 09:15:31 -0800179
180 /**
181 * Returns the first View for which the given itemOperator returns true, or null.
182 */
183 public View getFirstItemMatch(Workspace.ItemOperator itemOperator) {
184 return mWorkspace.getFirstMatch(new CellLayout[] { this }, itemOperator);
185 }
Tony Wickhamae72b462021-03-10 16:18:40 -0800186
187 public MultiValueAlpha.AlphaProperty getStateAlpha() {
188 return mMultiValueAlpha.getProperty(ALPHA_INDEX_STATE);
189 }
190
191 public MultiValueAlpha.AlphaProperty getReplaceTaskbarAlpha() {
192 return mMultiValueAlpha.getProperty(ALPHA_INDEX_REPLACE_TASKBAR);
193 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700194}