Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 20 | import android.graphics.Rect; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 22 | import android.view.Gravity; |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 23 | import android.view.LayoutInflater; |
Sunny Goyal | d158097 | 2019-04-09 11:52:49 -0700 | [diff] [blame] | 24 | import android.view.MotionEvent; |
Tony Wickham | 39938cb | 2021-02-26 09:15:31 -0800 | [diff] [blame] | 25 | import android.view.View; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 26 | import android.view.ViewDebug; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 27 | import android.view.ViewGroup; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 28 | import android.widget.FrameLayout; |
| 29 | |
Schneider Victor-tulias | 3cf264f | 2020-09-22 12:58:38 -0700 | [diff] [blame] | 30 | import androidx.annotation.Nullable; |
| 31 | |
| 32 | import java.util.function.Consumer; |
| 33 | |
Hyunyoung Song | 95786e0 | 2020-09-15 00:34:10 -0700 | [diff] [blame] | 34 | /** |
| 35 | * View class that represents the bottom row of the home screen. |
| 36 | */ |
| 37 | public class Hotseat extends CellLayout implements Insettable { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 38 | |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 39 | // Ratio of empty space, qsb should take up to appear visually centered. |
| 40 | public static final float QSB_CENTER_FACTOR = .325f; |
| 41 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 42 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 12069e6 | 2018-01-16 14:17:20 -0800 | [diff] [blame] | 43 | private boolean mHasVerticalHotseat; |
Adam Cohen | b66675a | 2020-05-15 16:16:17 -0700 | [diff] [blame] | 44 | private Workspace mWorkspace; |
| 45 | private boolean mSendTouchToWorkspace; |
Samuel Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 46 | @Nullable |
| 47 | private Consumer<Boolean> mOnVisibilityAggregatedCallback; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 48 | |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 49 | private final View mQsb; |
| 50 | private final int mQsbHeight; |
Tony Wickham | ae72b46 | 2021-03-10 16:18:40 -0800 | [diff] [blame] | 51 | |
Tony Wickham | 9ce3b25 | 2021-03-22 14:43:58 -0700 | [diff] [blame^] | 52 | private final View mTaskbarView; |
| 53 | private final int mTaskbarViewHeight; |
| 54 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 55 | 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 Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 65 | |
| 66 | mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat, this, false); |
| 67 | mQsbHeight = mQsb.getLayoutParams().height; |
| 68 | addView(mQsb); |
Tony Wickham | 9ce3b25 | 2021-03-22 14:43:58 -0700 | [diff] [blame^] | 69 | |
| 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 Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Returns orientation specific cell X given invariant order in the hotseat |
| 78 | */ |
| 79 | public int getCellXFromOrder(int rank) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 80 | return mHasVerticalHotseat ? 0 : rank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 81 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 82 | |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Returns orientation specific cell Y given invariant order in the hotseat |
| 85 | */ |
| 86 | public int getCellYFromOrder(int rank) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 87 | return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 90 | public void resetLayout(boolean hasVerticalHotseat) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 91 | removeAllViewsInLayout(); |
Sunny Goyal | 11e9649 | 2018-03-23 17:06:00 -0700 | [diff] [blame] | 92 | mHasVerticalHotseat = hasVerticalHotseat; |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 93 | InvariantDeviceProfile idp = mActivity.getDeviceProfile().inv; |
Sunny Goyal | 11e9649 | 2018-03-23 17:06:00 -0700 | [diff] [blame] | 94 | if (hasVerticalHotseat) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 95 | setGridSize(1, idp.numHotseatIcons); |
Sunny Goyal | 11e9649 | 2018-03-23 17:06:00 -0700 | [diff] [blame] | 96 | } else { |
Tony Wickham | 72c15ef | 2021-03-18 18:04:02 -0700 | [diff] [blame] | 97 | setGridSize(idp.numHotseatIcons, 1); |
Sunny Goyal | 11e9649 | 2018-03-23 17:06:00 -0700 | [diff] [blame] | 98 | } |
Samuel Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 99 | showInlineQsb(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 100 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 101 | |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 102 | @Override |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 103 | public void setInsets(Rect insets) { |
| 104 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
Sunny Goyal | c4d3201 | 2020-04-03 17:10:11 -0700 | [diff] [blame] | 105 | DeviceProfile grid = mActivity.getDeviceProfile(); |
Sunny Goyal | 12069e6 | 2018-01-16 14:17:20 -0800 | [diff] [blame] | 106 | |
Sunny Goyal | 11e9649 | 2018-03-23 17:06:00 -0700 | [diff] [blame] | 107 | if (grid.isVerticalBarLayout()) { |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 108 | mQsb.setVisibility(View.GONE); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 109 | lp.height = ViewGroup.LayoutParams.MATCH_PARENT; |
Sunny Goyal | 7e2e67f | 2018-01-26 13:40:08 -0800 | [diff] [blame] | 110 | if (grid.isSeascape()) { |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 111 | lp.gravity = Gravity.LEFT; |
Tony Wickham | 1eeffbe | 2018-06-12 15:01:15 -0700 | [diff] [blame] | 112 | lp.width = grid.hotseatBarSizePx + insets.left; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 113 | } else { |
| 114 | lp.gravity = Gravity.RIGHT; |
Tony Wickham | 1eeffbe | 2018-06-12 15:01:15 -0700 | [diff] [blame] | 115 | lp.width = grid.hotseatBarSizePx + insets.right; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 116 | } |
| 117 | } else { |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 118 | mQsb.setVisibility(View.VISIBLE); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 119 | lp.gravity = Gravity.BOTTOM; |
| 120 | lp.width = ViewGroup.LayoutParams.MATCH_PARENT; |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 121 | lp.height = (grid.isTaskbarPresent |
| 122 | ? grid.workspacePadding.bottom |
| 123 | : grid.hotseatBarSizePx) |
Tony Wickham | b31d8fc | 2021-03-18 13:57:20 -0700 | [diff] [blame] | 124 | + (grid.isTaskbarPresent ? grid.taskbarSize : insets.bottom); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 125 | } |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 126 | |
Tony Wickham | a29a046 | 2021-03-02 12:28:25 -0800 | [diff] [blame] | 127 | if (!grid.isTaskbarPresent) { |
| 128 | // When taskbar is present, we set the padding separately to ensure a seamless visual |
| 129 | // handoff between taskbar and hotseat during drag and drop. |
| 130 | Rect padding = grid.getHotseatLayoutPadding(); |
Tony Wickham | 72c15ef | 2021-03-18 18:04:02 -0700 | [diff] [blame] | 131 | setPadding(padding.left, padding.top, padding.right, padding.bottom); |
Samuel Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 132 | } |
Sunny Goyal | 81b4c7b | 2018-03-26 12:10:31 -0700 | [diff] [blame] | 133 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 134 | setLayoutParams(lp); |
| 135 | InsettableFrameLayout.dispatchInsets(this, insets); |
| 136 | } |
Sunny Goyal | d158097 | 2019-04-09 11:52:49 -0700 | [diff] [blame] | 137 | |
Adam Cohen | b66675a | 2020-05-15 16:16:17 -0700 | [diff] [blame] | 138 | public void setWorkspace(Workspace w) { |
| 139 | mWorkspace = w; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 144 | // We allow horizontal workspace scrolling from within the Hotseat. We do this by delegating |
| 145 | // touch intercept the Workspace, and if it intercepts, delegating touch to the Workspace |
| 146 | // for the remainder of the this input stream. |
| 147 | int yThreshold = getMeasuredHeight() - getPaddingBottom(); |
| 148 | if (mWorkspace != null && ev.getY() <= yThreshold) { |
| 149 | mSendTouchToWorkspace = mWorkspace.onInterceptTouchEvent(ev); |
| 150 | return mSendTouchToWorkspace; |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
Sunny Goyal | d158097 | 2019-04-09 11:52:49 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public boolean onTouchEvent(MotionEvent event) { |
Adam Cohen | b66675a | 2020-05-15 16:16:17 -0700 | [diff] [blame] | 157 | // See comment in #onInterceptTouchEvent |
| 158 | if (mSendTouchToWorkspace) { |
| 159 | final int action = event.getAction(); |
| 160 | switch (action & MotionEvent.ACTION_MASK) { |
| 161 | case MotionEvent.ACTION_UP: |
| 162 | case MotionEvent.ACTION_CANCEL: |
| 163 | mSendTouchToWorkspace = false; |
| 164 | } |
| 165 | return mWorkspace.onTouchEvent(event); |
| 166 | } |
Samuel Fufa | eacaf8a | 2019-09-17 11:31:11 -0700 | [diff] [blame] | 167 | return event.getY() > getCellHeight(); |
Sunny Goyal | d158097 | 2019-04-09 11:52:49 -0700 | [diff] [blame] | 168 | } |
Schneider Victor-tulias | 3cf264f | 2020-09-22 12:58:38 -0700 | [diff] [blame] | 169 | |
| 170 | @Override |
| 171 | public void onVisibilityAggregated(boolean isVisible) { |
| 172 | super.onVisibilityAggregated(isVisible); |
| 173 | |
| 174 | if (mOnVisibilityAggregatedCallback != null) { |
| 175 | mOnVisibilityAggregatedCallback.accept(isVisible); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** Sets a callback to be called onVisibilityAggregated */ |
| 180 | public void setOnVisibilityAggregatedCallback(@Nullable Consumer<Boolean> callback) { |
| 181 | mOnVisibilityAggregatedCallback = callback; |
| 182 | } |
Samuel Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 183 | |
| 184 | protected void showInlineQsb() { |
| 185 | //Does nothing |
| 186 | } |
Tony Wickham | 39938cb | 2021-02-26 09:15:31 -0800 | [diff] [blame] | 187 | |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 188 | @Override |
| 189 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 190 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 191 | |
| 192 | int width = getShortcutsAndWidgets().getMeasuredWidth(); |
| 193 | mQsb.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), |
| 194 | MeasureSpec.makeMeasureSpec(mQsbHeight, MeasureSpec.EXACTLY)); |
Tony Wickham | 9ce3b25 | 2021-03-22 14:43:58 -0700 | [diff] [blame^] | 195 | mTaskbarView.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), |
| 196 | MeasureSpec.makeMeasureSpec(mTaskbarViewHeight, MeasureSpec.EXACTLY)); |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 201 | super.onLayout(changed, l, t, r, b); |
| 202 | |
| 203 | int qsbWidth = mQsb.getMeasuredWidth(); |
| 204 | int left = (r - l - qsbWidth) / 2; |
| 205 | int right = left + qsbWidth; |
| 206 | |
| 207 | DeviceProfile dp = mActivity.getDeviceProfile(); |
| 208 | int freeSpace = dp.isTaskbarPresent |
| 209 | ? dp.workspacePadding.bottom |
| 210 | : dp.hotseatBarSizePx - dp.hotseatCellHeightPx - mQsbHeight; |
| 211 | int bottom = b - t |
| 212 | - (int) (freeSpace * QSB_CENTER_FACTOR) |
Tony Wickham | b31d8fc | 2021-03-18 13:57:20 -0700 | [diff] [blame] | 213 | - (dp.isTaskbarPresent ? dp.taskbarSize : dp.getInsets().bottom); |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 214 | int top = bottom - mQsbHeight; |
| 215 | mQsb.layout(left, top, right, bottom); |
Tony Wickham | 9ce3b25 | 2021-03-22 14:43:58 -0700 | [diff] [blame^] | 216 | |
| 217 | int taskbarWidth = mTaskbarView.getMeasuredWidth(); |
| 218 | left = (r - l - taskbarWidth) / 2; |
| 219 | right = left + taskbarWidth; |
| 220 | bottom = b - t; |
| 221 | top = bottom - mTaskbarViewHeight; |
| 222 | mTaskbarView.layout(left, top, right, bottom); |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Tony Wickham | 39938cb | 2021-02-26 09:15:31 -0800 | [diff] [blame] | 225 | /** |
| 226 | * Returns the first View for which the given itemOperator returns true, or null. |
| 227 | */ |
| 228 | public View getFirstItemMatch(Workspace.ItemOperator itemOperator) { |
| 229 | return mWorkspace.getFirstMatch(new CellLayout[] { this }, itemOperator); |
| 230 | } |
Tony Wickham | ae72b46 | 2021-03-10 16:18:40 -0800 | [diff] [blame] | 231 | |
Tony Wickham | 9ce3b25 | 2021-03-22 14:43:58 -0700 | [diff] [blame^] | 232 | /** |
| 233 | * Sets the alpha value of just our ShortcutAndWidgetContainer. |
| 234 | */ |
| 235 | public void setIconsAlpha(float alpha) { |
| 236 | getShortcutsAndWidgets().setAlpha(alpha); |
Tony Wickham | ae72b46 | 2021-03-10 16:18:40 -0800 | [diff] [blame] | 237 | } |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 238 | |
| 239 | /** |
| 240 | * Returns the QSB inside hotseat |
| 241 | */ |
| 242 | public View getQsb() { |
| 243 | return mQsb; |
| 244 | } |
Tony Wickham | 9ce3b25 | 2021-03-22 14:43:58 -0700 | [diff] [blame^] | 245 | |
| 246 | /** |
| 247 | * Returns the Taskbar inside hotseat |
| 248 | */ |
| 249 | public View getTaskbarView() { |
| 250 | return mTaskbarView; |
| 251 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 252 | } |