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 | |
Samuel Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 32 | import com.android.launcher3.config.FeatureFlags; |
Tony Wickham | ae72b46 | 2021-03-10 16:18:40 -0800 | [diff] [blame] | 33 | import com.android.launcher3.util.MultiValueAlpha; |
Samuel Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 34 | |
Schneider Victor-tulias | 3cf264f | 2020-09-22 12:58:38 -0700 | [diff] [blame] | 35 | import java.util.function.Consumer; |
| 36 | |
Hyunyoung Song | 95786e0 | 2020-09-15 00:34:10 -0700 | [diff] [blame] | 37 | /** |
| 38 | * View class that represents the bottom row of the home screen. |
| 39 | */ |
| 40 | public class Hotseat extends CellLayout implements Insettable { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 41 | |
Tony Wickham | ae72b46 | 2021-03-10 16:18:40 -0800 | [diff] [blame] | 42 | 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 Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 46 | // Ratio of empty space, qsb should take up to appear visually centered. |
| 47 | public static final float QSB_CENTER_FACTOR = .325f; |
| 48 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 49 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 12069e6 | 2018-01-16 14:17:20 -0800 | [diff] [blame] | 50 | private boolean mHasVerticalHotseat; |
Adam Cohen | b66675a | 2020-05-15 16:16:17 -0700 | [diff] [blame] | 51 | private Workspace mWorkspace; |
| 52 | private boolean mSendTouchToWorkspace; |
Samuel Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 53 | @Nullable |
| 54 | private Consumer<Boolean> mOnVisibilityAggregatedCallback; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 55 | |
Tony Wickham | ae72b46 | 2021-03-10 16:18:40 -0800 | [diff] [blame] | 56 | private final MultiValueAlpha mMultiValueAlpha; |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 57 | private final View mQsb; |
| 58 | private final int mQsbHeight; |
Tony Wickham | ae72b46 | 2021-03-10 16:18:40 -0800 | [diff] [blame] | 59 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 60 | 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 Wickham | ae72b46 | 2021-03-10 16:18:40 -0800 | [diff] [blame] | 70 | mMultiValueAlpha = new MultiValueAlpha(this, NUM_ALPHA_CHANNELS, MultiValueAlpha.Mode.MAX); |
| 71 | mMultiValueAlpha.setUpdateVisibility(true); |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 72 | |
| 73 | mQsb = LayoutInflater.from(context).inflate(R.layout.search_container_hotseat, this, false); |
| 74 | mQsbHeight = mQsb.getLayoutParams().height; |
| 75 | addView(mQsb); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 78 | /** |
| 79 | * Returns orientation specific cell X given invariant order in the hotseat |
| 80 | */ |
| 81 | public int getCellXFromOrder(int rank) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 82 | return mHasVerticalHotseat ? 0 : rank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 83 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 84 | |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Returns orientation specific cell Y given invariant order in the hotseat |
| 87 | */ |
| 88 | public int getCellYFromOrder(int rank) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 89 | return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 92 | public void resetLayout(boolean hasVerticalHotseat) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 93 | removeAllViewsInLayout(); |
Sunny Goyal | 11e9649 | 2018-03-23 17:06:00 -0700 | [diff] [blame] | 94 | mHasVerticalHotseat = hasVerticalHotseat; |
Sunny Goyal | ab770a1 | 2018-11-14 15:17:26 -0800 | [diff] [blame] | 95 | InvariantDeviceProfile idp = mActivity.getDeviceProfile().inv; |
Sunny Goyal | 11e9649 | 2018-03-23 17:06:00 -0700 | [diff] [blame] | 96 | if (hasVerticalHotseat) { |
Sunny Goyal | 876e462 | 2018-11-02 13:50:40 -0700 | [diff] [blame] | 97 | setGridSize(1, idp.numHotseatIcons); |
Sunny Goyal | 11e9649 | 2018-03-23 17:06:00 -0700 | [diff] [blame] | 98 | } else { |
Samuel Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 99 | setGridSize(idp.numHotseatIcons, FeatureFlags.ENABLE_DEVICE_SEARCH.get() ? 2 : 1); |
Sunny Goyal | 11e9649 | 2018-03-23 17:06:00 -0700 | [diff] [blame] | 100 | } |
Samuel Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 101 | showInlineQsb(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 102 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 103 | |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 104 | @Override |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 105 | public void setInsets(Rect insets) { |
| 106 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
Sunny Goyal | c4d3201 | 2020-04-03 17:10:11 -0700 | [diff] [blame] | 107 | DeviceProfile grid = mActivity.getDeviceProfile(); |
Sunny Goyal | 12069e6 | 2018-01-16 14:17:20 -0800 | [diff] [blame] | 108 | |
Sunny Goyal | 11e9649 | 2018-03-23 17:06:00 -0700 | [diff] [blame] | 109 | if (grid.isVerticalBarLayout()) { |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 110 | mQsb.setVisibility(View.GONE); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 111 | lp.height = ViewGroup.LayoutParams.MATCH_PARENT; |
Sunny Goyal | 7e2e67f | 2018-01-26 13:40:08 -0800 | [diff] [blame] | 112 | if (grid.isSeascape()) { |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 113 | lp.gravity = Gravity.LEFT; |
Tony Wickham | 1eeffbe | 2018-06-12 15:01:15 -0700 | [diff] [blame] | 114 | lp.width = grid.hotseatBarSizePx + insets.left; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 115 | } else { |
| 116 | lp.gravity = Gravity.RIGHT; |
Tony Wickham | 1eeffbe | 2018-06-12 15:01:15 -0700 | [diff] [blame] | 117 | lp.width = grid.hotseatBarSizePx + insets.right; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 118 | } |
| 119 | } else { |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 120 | mQsb.setVisibility(View.VISIBLE); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 121 | lp.gravity = Gravity.BOTTOM; |
| 122 | lp.width = ViewGroup.LayoutParams.MATCH_PARENT; |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 123 | lp.height = (grid.isTaskbarPresent |
| 124 | ? grid.workspacePadding.bottom |
| 125 | : grid.hotseatBarSizePx) |
Tony Wickham | b31d8fc | 2021-03-18 13:57:20 -0700 | [diff] [blame^] | 126 | + (grid.isTaskbarPresent ? grid.taskbarSize : insets.bottom); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 127 | } |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 128 | |
Tony Wickham | a29a046 | 2021-03-02 12:28:25 -0800 | [diff] [blame] | 129 | 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 Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 138 | } |
Sunny Goyal | 81b4c7b | 2018-03-26 12:10:31 -0700 | [diff] [blame] | 139 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 140 | setLayoutParams(lp); |
| 141 | InsettableFrameLayout.dispatchInsets(this, insets); |
| 142 | } |
Sunny Goyal | d158097 | 2019-04-09 11:52:49 -0700 | [diff] [blame] | 143 | |
Adam Cohen | b66675a | 2020-05-15 16:16:17 -0700 | [diff] [blame] | 144 | 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 Goyal | d158097 | 2019-04-09 11:52:49 -0700 | [diff] [blame] | 161 | @Override |
| 162 | public boolean onTouchEvent(MotionEvent event) { |
Adam Cohen | b66675a | 2020-05-15 16:16:17 -0700 | [diff] [blame] | 163 | // 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 Fufa | eacaf8a | 2019-09-17 11:31:11 -0700 | [diff] [blame] | 173 | return event.getY() > getCellHeight(); |
Sunny Goyal | d158097 | 2019-04-09 11:52:49 -0700 | [diff] [blame] | 174 | } |
Schneider Victor-tulias | 3cf264f | 2020-09-22 12:58:38 -0700 | [diff] [blame] | 175 | |
| 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 Fufa | 2ea01e4 | 2020-12-02 13:48:18 -0600 | [diff] [blame] | 189 | |
| 190 | protected void showInlineQsb() { |
| 191 | //Does nothing |
| 192 | } |
Tony Wickham | 39938cb | 2021-02-26 09:15:31 -0800 | [diff] [blame] | 193 | |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 194 | @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) |
Tony Wickham | b31d8fc | 2021-03-18 13:57:20 -0700 | [diff] [blame^] | 217 | - (dp.isTaskbarPresent ? dp.taskbarSize : dp.getInsets().bottom); |
Sunny Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 218 | int top = bottom - mQsbHeight; |
| 219 | mQsb.layout(left, top, right, bottom); |
| 220 | } |
| 221 | |
Tony Wickham | 39938cb | 2021-02-26 09:15:31 -0800 | [diff] [blame] | 222 | /** |
| 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 Wickham | ae72b46 | 2021-03-10 16:18:40 -0800 | [diff] [blame] | 228 | |
| 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 Goyal | c373e1c | 2021-03-09 17:27:53 -0800 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * Returns the QSB inside hotseat |
| 239 | */ |
| 240 | public View getQsb() { |
| 241 | return mQsb; |
| 242 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 243 | } |