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