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 | |
Sunny Goyal | e969c68 | 2017-12-05 16:56:18 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.LauncherState.ALL_APPS; |
Sunny Goyal | e969c68 | 2017-12-05 16:56:18 -0800 | [diff] [blame] | 20 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 22 | import android.graphics.Rect; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 25 | import android.view.Gravity; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 26 | import android.view.LayoutInflater; |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 27 | import android.view.MotionEvent; |
Winson | a49b1f7 | 2015-10-16 14:57:24 -0700 | [diff] [blame] | 28 | import android.view.View; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 29 | import android.view.ViewDebug; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 30 | import android.view.ViewGroup; |
| 31 | import android.view.ViewGroup.LayoutParams; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 33 | import android.widget.TextView; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 35 | import com.android.launcher3.config.FeatureFlags; |
Hyunyoung Song | aa95365 | 2016-04-19 18:30:24 -0700 | [diff] [blame] | 36 | import com.android.launcher3.logging.UserEventDispatcher; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 37 | import com.android.launcher3.logging.UserEventDispatcher.LogContainerProvider; |
Sunny Goyal | e969c68 | 2017-12-05 16:56:18 -0800 | [diff] [blame] | 38 | import com.android.launcher3.userevent.nano.LauncherLogProto.Action; |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 39 | import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType; |
Sunny Goyal | e969c68 | 2017-12-05 16:56:18 -0800 | [diff] [blame] | 40 | import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType; |
Hyunyoung Song | ddec1c7 | 2016-04-12 18:32:04 -0700 | [diff] [blame] | 41 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
| 42 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 43 | public class Hotseat extends FrameLayout implements LogContainerProvider, Insettable { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 44 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 45 | private final Launcher mLauncher; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 46 | private CellLayout mContent; |
| 47 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 48 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 49 | private final boolean mHasVerticalHotseat; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 50 | |
| 51 | public Hotseat(Context context) { |
| 52 | this(context, null); |
| 53 | } |
| 54 | |
| 55 | public Hotseat(Context context, AttributeSet attrs) { |
| 56 | this(context, attrs, 0); |
| 57 | } |
| 58 | |
| 59 | public Hotseat(Context context, AttributeSet attrs, int defStyle) { |
| 60 | super(context, attrs, defStyle); |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 61 | mLauncher = Launcher.getLauncher(context); |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 62 | mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 65 | public CellLayout getLayout() { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 66 | return mContent; |
| 67 | } |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Registers the specified listener on the cell layout of the hotseat. |
| 71 | */ |
| 72 | @Override |
| 73 | public void setOnLongClickListener(OnLongClickListener l) { |
| 74 | mContent.setOnLongClickListener(l); |
| 75 | } |
Tony Wickham | 6cbd222 | 2015-11-09 17:51:08 -0800 | [diff] [blame] | 76 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 77 | /* Get the orientation invariant order of the item in the hotseat for persistence. */ |
| 78 | int getOrderInHotseat(int x, int y) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 79 | return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 80 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 81 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 82 | /* Get the orientation specific coordinates given an invariant order in the hotseat. */ |
| 83 | int getCellXFromOrder(int rank) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 84 | return mHasVerticalHotseat ? 0 : rank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 85 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 86 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 87 | int getCellYFromOrder(int rank) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 88 | return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 91 | @Override |
| 92 | protected void onFinishInflate() { |
| 93 | super.onFinishInflate(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 94 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 95 | mContent = (CellLayout) findViewById(R.id.layout); |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 96 | if (grid.isVerticalBarLayout()) { |
| 97 | mContent.setGridSize(1, grid.inv.numHotseatIcons); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 98 | } else { |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 99 | mContent.setGridSize(grid.inv.numHotseatIcons, 1); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 100 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 101 | |
| 102 | resetLayout(); |
| 103 | } |
| 104 | |
| 105 | void resetLayout() { |
| 106 | mContent.removeAllViewsInLayout(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 107 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 108 | if (!FeatureFlags.NO_ALL_APPS_ICON) { |
| 109 | // Add the Apps button |
| 110 | Context context = getContext(); |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 111 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
| 112 | int allAppsButtonRank = grid.inv.getAllAppsButtonRank(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 113 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 114 | LayoutInflater inflater = LayoutInflater.from(context); |
| 115 | TextView allAppsButton = (TextView) |
| 116 | inflater.inflate(R.layout.all_apps_button, mContent, false); |
| 117 | Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon); |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 118 | d.setBounds(0, 0, grid.iconSizePx, grid.iconSizePx); |
Adam Cohen | 71b0473 | 2014-06-11 10:36:14 -0700 | [diff] [blame] | 119 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 120 | int scaleDownPx = getResources().getDimensionPixelSize(R.dimen.all_apps_button_scale_down); |
| 121 | Rect bounds = d.getBounds(); |
| 122 | d.setBounds(bounds.left, bounds.top + scaleDownPx / 2, bounds.right - scaleDownPx, |
| 123 | bounds.bottom - scaleDownPx / 2); |
| 124 | allAppsButton.setCompoundDrawables(null, d, null, null); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 125 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 126 | allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label)); |
| 127 | allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener()); |
| 128 | if (mLauncher != null) { |
Sunny Goyal | e969c68 | 2017-12-05 16:56:18 -0800 | [diff] [blame] | 129 | allAppsButton.setOnClickListener((v) -> { |
| 130 | if (!mLauncher.isInState(ALL_APPS)) { |
| 131 | mLauncher.getUserEventDispatcher().logActionOnControl(Action.Touch.TAP, |
| 132 | ControlType.ALL_APPS_BUTTON); |
| 133 | mLauncher.getStateManager().goToState(ALL_APPS); |
| 134 | } |
| 135 | }); |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 136 | allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler); |
| 137 | } |
| 138 | |
| 139 | // Note: We do this to ensure that the hotseat is always laid out in the orientation of |
| 140 | // the hotseat in order regardless of which orientation they were added |
| 141 | int x = getCellXFromOrder(allAppsButtonRank); |
| 142 | int y = getCellYFromOrder(allAppsButtonRank); |
| 143 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x, y, 1, 1); |
| 144 | lp.canReorder = false; |
| 145 | mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 146 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 147 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 148 | |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 149 | @Override |
| 150 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 151 | // We don't want any clicks to go through to the hotseat unless the workspace is in |
Sunny Goyal | 4583d09 | 2016-08-17 11:11:48 -0700 | [diff] [blame] | 152 | // the normal state or an accessible drag is in progress. |
Tony Wickham | 0c7852f | 2016-12-02 14:47:04 -0800 | [diff] [blame] | 153 | return !mLauncher.getWorkspace().workspaceIconsCanBeDragged() && |
Sunny Goyal | 4583d09 | 2016-08-17 11:11:48 -0700 | [diff] [blame] | 154 | !mLauncher.getAccessibilityDelegate().isInAccessibleDrag(); |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 155 | } |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 156 | |
| 157 | @Override |
Hyunyoung Song | df7ef68 | 2016-10-06 17:52:22 -0700 | [diff] [blame] | 158 | public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) { |
Hyunyoung Song | ddec1c7 | 2016-04-12 18:32:04 -0700 | [diff] [blame] | 159 | target.gridX = info.cellX; |
| 160 | target.gridY = info.cellY; |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 161 | targetParent.containerType = ContainerType.HOTSEAT; |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 162 | } |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 163 | |
| 164 | @Override |
| 165 | public void setInsets(Rect insets) { |
| 166 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
| 167 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
| 168 | if (mHasVerticalHotseat) { |
| 169 | lp.height = ViewGroup.LayoutParams.MATCH_PARENT; |
| 170 | if (insets.left > insets.right) { |
| 171 | lp.gravity = Gravity.LEFT; |
| 172 | lp.width = grid.hotseatBarSizePx + insets.left + grid.hotseatBarSidePaddingPx; |
| 173 | getLayout().setPadding( |
| 174 | insets.left, insets.top, grid.hotseatBarSidePaddingPx, insets.bottom); |
| 175 | |
| 176 | } else { |
| 177 | lp.gravity = Gravity.RIGHT; |
| 178 | lp.width = grid.hotseatBarSizePx + insets.right + grid.hotseatBarSidePaddingPx; |
| 179 | getLayout().setPadding( |
| 180 | grid.hotseatBarSidePaddingPx, insets.top, insets.right, insets.bottom); |
| 181 | } |
| 182 | } else { |
| 183 | lp.gravity = Gravity.BOTTOM; |
| 184 | lp.width = ViewGroup.LayoutParams.MATCH_PARENT; |
| 185 | lp.height = grid.hotseatBarSizePx + insets.bottom; |
| 186 | |
| 187 | // We want the edges of the hotseat to line up with the edges of the workspace, but the |
| 188 | // icons in the hotseat are a different size, and so don't line up perfectly. To account for |
| 189 | // this, we pad the left and right of the hotseat with half of the difference of a workspace |
| 190 | // cell vs a hotseat cell. |
| 191 | float workspaceCellWidth = (float) grid.widthPx / grid.inv.numColumns; |
| 192 | float hotseatCellWidth = (float) grid.widthPx / grid.inv.numHotseatIcons; |
| 193 | int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2); |
| 194 | Rect workspacePadding = grid.workspacePadding; |
| 195 | |
| 196 | getLayout().setPadding( |
| 197 | hotseatAdjustment + workspacePadding.left + grid.cellLayoutPaddingLeftRightPx, |
| 198 | grid.hotseatBarTopPaddingPx, |
| 199 | hotseatAdjustment + workspacePadding.right + grid.cellLayoutPaddingLeftRightPx, |
| 200 | grid.hotseatBarBottomPaddingPx + insets.bottom + grid.cellLayoutBottomPaddingPx); |
| 201 | } |
| 202 | setLayoutParams(lp); |
| 203 | InsettableFrameLayout.dispatchInsets(this, insets); |
| 204 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 205 | } |