blob: 25eacb52f20c0240861fdf4e7164c672cf166533 [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
Sunny Goyale969c682017-12-05 16:56:18 -080019import static com.android.launcher3.LauncherState.ALL_APPS;
Sunny Goyale969c682017-12-05 16:56:18 -080020
Winson Chung3d503fb2011-07-13 17:25:49 -070021import android.content.Context;
Tony Wickhamd6b40372015-09-23 18:37:57 -070022import android.graphics.Rect;
Winson Chungc58497e2013-09-03 17:48:37 -070023import android.graphics.drawable.Drawable;
Winson Chung3d503fb2011-07-13 17:25:49 -070024import android.util.AttributeSet;
Sunny Goyal07b69292018-01-08 14:19:34 -080025import android.view.Gravity;
Winson Chungc58497e2013-09-03 17:48:37 -070026import android.view.LayoutInflater;
Adam Cohena5f4e482013-10-11 12:10:28 -070027import android.view.MotionEvent;
Winsona49b1f72015-10-16 14:57:24 -070028import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080029import android.view.ViewDebug;
Sunny Goyal07b69292018-01-08 14:19:34 -080030import android.view.ViewGroup;
31import android.view.ViewGroup.LayoutParams;
Winson Chung3d503fb2011-07-13 17:25:49 -070032import android.widget.FrameLayout;
Adam Cohen61f560d2013-09-30 15:58:20 -070033import android.widget.TextView;
Winson Chung3d503fb2011-07-13 17:25:49 -070034
Sunny Goyalbb011da2016-06-15 15:42:29 -070035import com.android.launcher3.config.FeatureFlags;
Hyunyoung Songaa953652016-04-19 18:30:24 -070036import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyal07b69292018-01-08 14:19:34 -080037import com.android.launcher3.logging.UserEventDispatcher.LogContainerProvider;
Sunny Goyale969c682017-12-05 16:56:18 -080038import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053039import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Sunny Goyale969c682017-12-05 16:56:18 -080040import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
Hyunyoung Songddec1c72016-04-12 18:32:04 -070041import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
42
Sunny Goyal07b69292018-01-08 14:19:34 -080043public class Hotseat extends FrameLayout implements LogContainerProvider, Insettable {
Winson Chung3d503fb2011-07-13 17:25:49 -070044
Sunny Goyal07b69292018-01-08 14:19:34 -080045 private final Launcher mLauncher;
Winson Chung3d503fb2011-07-13 17:25:49 -070046 private CellLayout mContent;
47
Sunny Goyal4ffec482016-02-09 11:28:52 -080048 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal12069e62018-01-16 14:17:20 -080049 private boolean mHasVerticalHotseat;
Winson Chung3d503fb2011-07-13 17:25:49 -070050
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);
Tony2fd02082016-10-07 12:50:01 -070061 mLauncher = Launcher.getLauncher(context);
Winson Chung3d503fb2011-07-13 17:25:49 -070062 }
63
Tony Wickham95cdb3a2016-02-18 14:37:07 -080064 public CellLayout getLayout() {
Winson Chung3d503fb2011-07-13 17:25:49 -070065 return mContent;
66 }
Winson Chung11a1a532013-09-13 11:14:45 -070067
68 /**
69 * Registers the specified listener on the cell layout of the hotseat.
70 */
71 @Override
72 public void setOnLongClickListener(OnLongClickListener l) {
73 mContent.setOnLongClickListener(l);
74 }
Tony Wickham6cbd2222015-11-09 17:51:08 -080075
Winson Chung3d503fb2011-07-13 17:25:49 -070076 /* Get the orientation invariant order of the item in the hotseat for persistence. */
77 int getOrderInHotseat(int x, int y) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070078 return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070079 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080080
Winson Chung3d503fb2011-07-13 17:25:49 -070081 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
82 int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070083 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070084 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080085
Winson Chung3d503fb2011-07-13 17:25:49 -070086 int getCellYFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070087 return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0;
Hyunyoung Song31178b82015-02-24 14:12:51 -080088 }
89
Winson Chung3d503fb2011-07-13 17:25:49 -070090 @Override
91 protected void onFinishInflate() {
92 super.onFinishInflate();
Sunny Goyal12069e62018-01-16 14:17:20 -080093 mContent = findViewById(R.id.layout);
Winson Chung3d503fb2011-07-13 17:25:49 -070094
95 resetLayout();
96 }
97
98 void resetLayout() {
99 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700100
Sunny Goyalbb011da2016-06-15 15:42:29 -0700101 if (!FeatureFlags.NO_ALL_APPS_ICON) {
102 // Add the Apps button
103 Context context = getContext();
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800104 DeviceProfile grid = mLauncher.getDeviceProfile();
105 int allAppsButtonRank = grid.inv.getAllAppsButtonRank();
Winson Chungc58497e2013-09-03 17:48:37 -0700106
Sunny Goyalbb011da2016-06-15 15:42:29 -0700107 LayoutInflater inflater = LayoutInflater.from(context);
108 TextView allAppsButton = (TextView)
109 inflater.inflate(R.layout.all_apps_button, mContent, false);
110 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800111 d.setBounds(0, 0, grid.iconSizePx, grid.iconSizePx);
Adam Cohen71b04732014-06-11 10:36:14 -0700112
Sunny Goyalbb011da2016-06-15 15:42:29 -0700113 int scaleDownPx = getResources().getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
114 Rect bounds = d.getBounds();
115 d.setBounds(bounds.left, bounds.top + scaleDownPx / 2, bounds.right - scaleDownPx,
116 bounds.bottom - scaleDownPx / 2);
117 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700118
Sunny Goyalbb011da2016-06-15 15:42:29 -0700119 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
120 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
121 if (mLauncher != null) {
Sunny Goyale969c682017-12-05 16:56:18 -0800122 allAppsButton.setOnClickListener((v) -> {
123 if (!mLauncher.isInState(ALL_APPS)) {
124 mLauncher.getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
125 ControlType.ALL_APPS_BUTTON);
126 mLauncher.getStateManager().goToState(ALL_APPS);
127 }
128 });
Sunny Goyalbb011da2016-06-15 15:42:29 -0700129 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
130 }
131
132 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
133 // the hotseat in order regardless of which orientation they were added
134 int x = getCellXFromOrder(allAppsButtonRank);
135 int y = getCellYFromOrder(allAppsButtonRank);
136 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x, y, 1, 1);
137 lp.canReorder = false;
138 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chungc58497e2013-09-03 17:48:37 -0700139 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700140 }
Adam Cohene25af792013-06-06 23:08:25 -0700141
Adam Cohena5f4e482013-10-11 12:10:28 -0700142 @Override
143 public boolean onInterceptTouchEvent(MotionEvent ev) {
144 // We don't want any clicks to go through to the hotseat unless the workspace is in
Sunny Goyal4583d092016-08-17 11:11:48 -0700145 // the normal state or an accessible drag is in progress.
Tony Wickham0c7852f2016-12-02 14:47:04 -0800146 return !mLauncher.getWorkspace().workspaceIconsCanBeDragged() &&
Sunny Goyal4583d092016-08-17 11:11:48 -0700147 !mLauncher.getAccessibilityDelegate().isInAccessibleDrag();
Adam Cohena5f4e482013-10-11 12:10:28 -0700148 }
Winson Chung8f1eff72015-05-28 17:33:40 -0700149
150 @Override
Hyunyoung Songdf7ef682016-10-06 17:52:22 -0700151 public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) {
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700152 target.gridX = info.cellX;
153 target.gridY = info.cellY;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530154 targetParent.containerType = ContainerType.HOTSEAT;
Winson Chung8f1eff72015-05-28 17:33:40 -0700155 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800156
157 @Override
158 public void setInsets(Rect insets) {
159 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
160 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyal12069e62018-01-16 14:17:20 -0800161 mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout();
162
Sunny Goyal07b69292018-01-08 14:19:34 -0800163 if (mHasVerticalHotseat) {
Sunny Goyal12069e62018-01-16 14:17:20 -0800164 mContent.setGridSize(1, grid.inv.numHotseatIcons);
165
Sunny Goyal07b69292018-01-08 14:19:34 -0800166 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
167 if (insets.left > insets.right) {
168 lp.gravity = Gravity.LEFT;
169 lp.width = grid.hotseatBarSizePx + insets.left + grid.hotseatBarSidePaddingPx;
170 getLayout().setPadding(
171 insets.left, insets.top, grid.hotseatBarSidePaddingPx, insets.bottom);
172
173 } else {
174 lp.gravity = Gravity.RIGHT;
175 lp.width = grid.hotseatBarSizePx + insets.right + grid.hotseatBarSidePaddingPx;
176 getLayout().setPadding(
177 grid.hotseatBarSidePaddingPx, insets.top, insets.right, insets.bottom);
178 }
179 } else {
Sunny Goyal12069e62018-01-16 14:17:20 -0800180 mContent.setGridSize(grid.inv.numHotseatIcons, 1);
181
Sunny Goyal07b69292018-01-08 14:19:34 -0800182 lp.gravity = Gravity.BOTTOM;
183 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
184 lp.height = grid.hotseatBarSizePx + insets.bottom;
185
186 // We want the edges of the hotseat to line up with the edges of the workspace, but the
187 // icons in the hotseat are a different size, and so don't line up perfectly. To account for
188 // this, we pad the left and right of the hotseat with half of the difference of a workspace
189 // cell vs a hotseat cell.
190 float workspaceCellWidth = (float) grid.widthPx / grid.inv.numColumns;
191 float hotseatCellWidth = (float) grid.widthPx / grid.inv.numHotseatIcons;
192 int hotseatAdjustment = Math.round((workspaceCellWidth - hotseatCellWidth) / 2);
193 Rect workspacePadding = grid.workspacePadding;
194
195 getLayout().setPadding(
196 hotseatAdjustment + workspacePadding.left + grid.cellLayoutPaddingLeftRightPx,
197 grid.hotseatBarTopPaddingPx,
198 hotseatAdjustment + workspacePadding.right + grid.cellLayoutPaddingLeftRightPx,
199 grid.hotseatBarBottomPaddingPx + insets.bottom + grid.cellLayoutBottomPaddingPx);
200 }
201 setLayoutParams(lp);
202 InsettableFrameLayout.dispatchInsets(this, insets);
203 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700204}