blob: 09f9e827cdaec1c0a5c1b5b4f1b1e99583852b45 [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
19import android.content.Context;
Tony Wickhamd6b40372015-09-23 18:37:57 -070020import android.graphics.Rect;
Winson Chungc58497e2013-09-03 17:48:37 -070021import android.graphics.drawable.Drawable;
Winson Chung3d503fb2011-07-13 17:25:49 -070022import android.util.AttributeSet;
Winson Chungc58497e2013-09-03 17:48:37 -070023import android.view.LayoutInflater;
Adam Cohena5f4e482013-10-11 12:10:28 -070024import android.view.MotionEvent;
Winsona49b1f72015-10-16 14:57:24 -070025import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080026import android.view.ViewDebug;
Winson Chung3d503fb2011-07-13 17:25:49 -070027import android.widget.FrameLayout;
Adam Cohen61f560d2013-09-30 15:58:20 -070028import android.widget.TextView;
Winson Chung3d503fb2011-07-13 17:25:49 -070029
Sunny Goyalbb011da2016-06-15 15:42:29 -070030import com.android.launcher3.config.FeatureFlags;
Hyunyoung Songaa953652016-04-19 18:30:24 -070031import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053032import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Hyunyoung Songddec1c72016-04-12 18:32:04 -070033import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
34
Winson Chung8f1eff72015-05-28 17:33:40 -070035public class Hotseat extends FrameLayout
Hyunyoung Songdf7ef682016-10-06 17:52:22 -070036 implements UserEventDispatcher.LogContainerProvider {
Winson Chung3d503fb2011-07-13 17:25:49 -070037
Winson Chung3d503fb2011-07-13 17:25:49 -070038 private CellLayout mContent;
39
Winson Chungc58497e2013-09-03 17:48:37 -070040 private Launcher mLauncher;
41
Sunny Goyal4ffec482016-02-09 11:28:52 -080042 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal4f3e9382015-06-05 00:13:25 -070043 private final boolean mHasVerticalHotseat;
Winson Chung3d503fb2011-07-13 17:25:49 -070044
45 public Hotseat(Context context) {
46 this(context, null);
47 }
48
49 public Hotseat(Context context, AttributeSet attrs) {
50 this(context, attrs, 0);
51 }
52
53 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
54 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -070055 mLauncher = Launcher.getLauncher(context);
Sunny Goyal4f3e9382015-06-05 00:13:25 -070056 mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout();
Winson Chung3d503fb2011-07-13 17:25:49 -070057 }
58
Tony Wickham95cdb3a2016-02-18 14:37:07 -080059 public CellLayout getLayout() {
Winson Chung3d503fb2011-07-13 17:25:49 -070060 return mContent;
61 }
Winson Chung11a1a532013-09-13 11:14:45 -070062
63 /**
Winson Chungc393b072015-05-20 15:03:13 -070064 * Returns whether there are other icons than the all apps button in the hotseat.
65 */
66 public boolean hasIcons() {
67 return mContent.getShortcutsAndWidgets().getChildCount() > 1;
68 }
69
70 /**
Winson Chung11a1a532013-09-13 11:14:45 -070071 * Registers the specified listener on the cell layout of the hotseat.
72 */
73 @Override
74 public void setOnLongClickListener(OnLongClickListener l) {
75 mContent.setOnLongClickListener(l);
76 }
Tony Wickham6cbd2222015-11-09 17:51:08 -080077
Winson Chung3d503fb2011-07-13 17:25:49 -070078 /* Get the orientation invariant order of the item in the hotseat for persistence. */
79 int getOrderInHotseat(int x, int y) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070080 return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070081 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080082
Winson Chung3d503fb2011-07-13 17:25:49 -070083 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
84 int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070085 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -070086 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080087
Winson Chung3d503fb2011-07-13 17:25:49 -070088 int getCellYFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070089 return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0;
Hyunyoung Song31178b82015-02-24 14:12:51 -080090 }
91
Winson Chung3d503fb2011-07-13 17:25:49 -070092 @Override
93 protected void onFinishInflate() {
94 super.onFinishInflate();
Adam Cohen2e6da152015-05-06 11:42:25 -070095 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chung3d503fb2011-07-13 17:25:49 -070096 mContent = (CellLayout) findViewById(R.id.layout);
Sunny Goyalc13403c2016-11-18 23:44:48 -080097 if (grid.isVerticalBarLayout()) {
98 mContent.setGridSize(1, grid.inv.numHotseatIcons);
Winson Chung5f8afe62013-08-12 16:19:28 -070099 } else {
Sunny Goyalc13403c2016-11-18 23:44:48 -0800100 mContent.setGridSize(grid.inv.numHotseatIcons, 1);
Winson Chung5f8afe62013-08-12 16:19:28 -0700101 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700102
103 resetLayout();
104 }
105
106 void resetLayout() {
107 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700108
Sunny Goyalbb011da2016-06-15 15:42:29 -0700109 if (!FeatureFlags.NO_ALL_APPS_ICON) {
110 // Add the Apps button
111 Context context = getContext();
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800112 DeviceProfile grid = mLauncher.getDeviceProfile();
113 int allAppsButtonRank = grid.inv.getAllAppsButtonRank();
Winson Chungc58497e2013-09-03 17:48:37 -0700114
Sunny Goyalbb011da2016-06-15 15:42:29 -0700115 LayoutInflater inflater = LayoutInflater.from(context);
116 TextView allAppsButton = (TextView)
117 inflater.inflate(R.layout.all_apps_button, mContent, false);
118 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800119 d.setBounds(0, 0, grid.iconSizePx, grid.iconSizePx);
Adam Cohen71b04732014-06-11 10:36:14 -0700120
Sunny Goyalbb011da2016-06-15 15:42:29 -0700121 int scaleDownPx = getResources().getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
122 Rect bounds = d.getBounds();
123 d.setBounds(bounds.left, bounds.top + scaleDownPx / 2, bounds.right - scaleDownPx,
124 bounds.bottom - scaleDownPx / 2);
125 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700126
Sunny Goyalbb011da2016-06-15 15:42:29 -0700127 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
128 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
129 if (mLauncher != null) {
130 mLauncher.setAllAppsButton(allAppsButton);
Sunny Goyalbb011da2016-06-15 15:42:29 -0700131 allAppsButton.setOnClickListener(mLauncher);
Sunny Goyalbb011da2016-06-15 15:42:29 -0700132 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
133 }
134
135 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
136 // the hotseat in order regardless of which orientation they were added
137 int x = getCellXFromOrder(allAppsButtonRank);
138 int y = getCellYFromOrder(allAppsButtonRank);
139 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x, y, 1, 1);
140 lp.canReorder = false;
141 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chungc58497e2013-09-03 17:48:37 -0700142 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700143 }
Adam Cohene25af792013-06-06 23:08:25 -0700144
Adam Cohena5f4e482013-10-11 12:10:28 -0700145 @Override
146 public boolean onInterceptTouchEvent(MotionEvent ev) {
147 // We don't want any clicks to go through to the hotseat unless the workspace is in
Sunny Goyal4583d092016-08-17 11:11:48 -0700148 // the normal state or an accessible drag is in progress.
Tony Wickham0c7852f2016-12-02 14:47:04 -0800149 return !mLauncher.getWorkspace().workspaceIconsCanBeDragged() &&
Sunny Goyal4583d092016-08-17 11:11:48 -0700150 !mLauncher.getAccessibilityDelegate().isInAccessibleDrag();
Adam Cohena5f4e482013-10-11 12:10:28 -0700151 }
Winson Chung8f1eff72015-05-28 17:33:40 -0700152
153 @Override
Hyunyoung Songdf7ef682016-10-06 17:52:22 -0700154 public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) {
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700155 target.gridX = info.cellX;
156 target.gridY = info.cellY;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530157 targetParent.containerType = ContainerType.HOTSEAT;
Winson Chung8f1eff72015-05-28 17:33:40 -0700158 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700159}