blob: 7e1ecf5af8c46147cdae02ad7a6a17e620dc5218 [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
Tony Wickham462b5cc2016-04-01 16:00:49 -070019import android.animation.ArgbEvaluator;
20import android.animation.ValueAnimator;
Winson Chung3d503fb2011-07-13 17:25:49 -070021import android.content.Context;
Tony Wickham462b5cc2016-04-01 16:00:49 -070022import android.graphics.Color;
Tony Wickhamd6b40372015-09-23 18:37:57 -070023import android.graphics.Rect;
Tony Wickham462b5cc2016-04-01 16:00:49 -070024import android.graphics.drawable.ColorDrawable;
Winson Chungc58497e2013-09-03 17:48:37 -070025import android.graphics.drawable.Drawable;
Winson Chung3d503fb2011-07-13 17:25:49 -070026import android.util.AttributeSet;
Winson Chungc58497e2013-09-03 17:48:37 -070027import android.view.LayoutInflater;
Adam Cohena5f4e482013-10-11 12:10:28 -070028import android.view.MotionEvent;
Winsona49b1f72015-10-16 14:57:24 -070029import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080030import android.view.ViewDebug;
Winson Chung3d503fb2011-07-13 17:25:49 -070031import android.widget.FrameLayout;
Adam Cohen61f560d2013-09-30 15:58:20 -070032import android.widget.TextView;
Winson Chung3d503fb2011-07-13 17:25:49 -070033
Tony Wickham462b5cc2016-04-01 16:00:49 -070034import com.android.launcher3.dynamicui.ExtractedColors;
Hyunyoung Songaa953652016-04-19 18:30:24 -070035import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Songddec1c72016-04-12 18:32:04 -070036import com.android.launcher3.userevent.nano.LauncherLogProto;
37import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
38
Winson Chung8f1eff72015-05-28 17:33:40 -070039public class Hotseat extends FrameLayout
Tony Wickham462b5cc2016-04-01 16:00:49 -070040 implements UserEventDispatcher.LaunchSourceProvider, Insettable {
Winson Chung3d503fb2011-07-13 17:25:49 -070041
Winson Chung3d503fb2011-07-13 17:25:49 -070042 private CellLayout mContent;
43
Winson Chungc58497e2013-09-03 17:48:37 -070044 private Launcher mLauncher;
45
Sunny Goyal4ffec482016-02-09 11:28:52 -080046 @ViewDebug.ExportedProperty(category = "launcher")
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080047 private int mAllAppsButtonRank;
Adam Cohen307fe232012-08-16 17:55:58 -070048
Sunny Goyal4ffec482016-02-09 11:28:52 -080049 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal4f3e9382015-06-05 00:13:25 -070050 private final boolean mHasVerticalHotseat;
Winson Chung3d503fb2011-07-13 17:25:49 -070051
Tony Wickham462b5cc2016-04-01 16:00:49 -070052 @ViewDebug.ExportedProperty(category = "launcher")
53 private Rect mInsets = new Rect();
54
55 @ViewDebug.ExportedProperty(category = "launcher")
56 private int mBackgroundColor;
57 @ViewDebug.ExportedProperty(category = "launcher")
58 private ColorDrawable mBackground;
59
Winson Chung3d503fb2011-07-13 17:25:49 -070060 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);
Adam Cohen2e6da152015-05-06 11:42:25 -070070 mLauncher = (Launcher) context;
Sunny Goyal4f3e9382015-06-05 00:13:25 -070071 mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout();
Tony Wickham462b5cc2016-04-01 16:00:49 -070072 mBackground = new ColorDrawable();
73 setBackground(mBackground);
Winson Chung3d503fb2011-07-13 17:25:49 -070074 }
75
Tony Wickham95cdb3a2016-02-18 14:37:07 -080076 public CellLayout getLayout() {
Winson Chung3d503fb2011-07-13 17:25:49 -070077 return mContent;
78 }
Winson Chung11a1a532013-09-13 11:14:45 -070079
80 /**
Winson Chungc393b072015-05-20 15:03:13 -070081 * Returns whether there are other icons than the all apps button in the hotseat.
82 */
83 public boolean hasIcons() {
84 return mContent.getShortcutsAndWidgets().getChildCount() > 1;
85 }
86
87 /**
Winson Chung11a1a532013-09-13 11:14:45 -070088 * Registers the specified listener on the cell layout of the hotseat.
89 */
90 @Override
91 public void setOnLongClickListener(OnLongClickListener l) {
92 mContent.setOnLongClickListener(l);
93 }
Tony Wickham6cbd2222015-11-09 17:51:08 -080094
Winson Chung3d503fb2011-07-13 17:25:49 -070095 /* Get the orientation invariant order of the item in the hotseat for persistence. */
96 int getOrderInHotseat(int x, int y) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070097 return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070098 }
Hyunyoung Song31178b82015-02-24 14:12:51 -080099
Winson Chung3d503fb2011-07-13 17:25:49 -0700100 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
101 int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700102 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700103 }
Hyunyoung Song31178b82015-02-24 14:12:51 -0800104
Winson Chung3d503fb2011-07-13 17:25:49 -0700105 int getCellYFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700106 return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800107 }
108
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800109 public boolean isAllAppsButtonRank(int rank) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800110 return rank == mAllAppsButtonRank;
Winson Chungf30ad5f2011-08-08 10:55:42 -0700111 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700112
113 @Override
114 protected void onFinishInflate() {
115 super.onFinishInflate();
Adam Cohen2e6da152015-05-06 11:42:25 -0700116 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chung5f8afe62013-08-12 16:19:28 -0700117
Adam Cohen2e6da152015-05-06 11:42:25 -0700118 mAllAppsButtonRank = grid.inv.hotseatAllAppsRank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700119 mContent = (CellLayout) findViewById(R.id.layout);
Sunny Goyalc6205602015-05-21 20:46:33 -0700120 if (grid.isLandscape && !grid.isLargeTablet) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700121 mContent.setGridSize(1, (int) grid.inv.numHotseatIcons);
Winson Chung5f8afe62013-08-12 16:19:28 -0700122 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -0700123 mContent.setGridSize((int) grid.inv.numHotseatIcons, 1);
Winson Chung5f8afe62013-08-12 16:19:28 -0700124 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800125 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700126
127 resetLayout();
128 }
129
130 void resetLayout() {
131 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700132
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800133 // Add the Apps button
134 Context context = getContext();
Winson Chungc58497e2013-09-03 17:48:37 -0700135
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800136 LayoutInflater inflater = LayoutInflater.from(context);
137 TextView allAppsButton = (TextView)
138 inflater.inflate(R.layout.all_apps_button, mContent, false);
139 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Adam Cohen71b04732014-06-11 10:36:14 -0700140
Sunny Goyal53d7ee42015-05-22 12:25:45 -0700141 mLauncher.resizeIconDrawable(d);
Tony Wickhamd6b40372015-09-23 18:37:57 -0700142 int scaleDownPx = getResources().getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
143 Rect bounds = d.getBounds();
144 d.setBounds(bounds.left, bounds.top + scaleDownPx / 2, bounds.right - scaleDownPx,
145 bounds.bottom - scaleDownPx / 2);
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800146 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700147
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800148 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
149 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
150 if (mLauncher != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800151 mLauncher.setAllAppsButton(allAppsButton);
Winson Chung76648c52015-07-10 14:33:23 -0700152 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800153 allAppsButton.setOnClickListener(mLauncher);
Winson Chung76648c52015-07-10 14:33:23 -0700154 allAppsButton.setOnLongClickListener(mLauncher);
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800155 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
Winson Chungc58497e2013-09-03 17:48:37 -0700156 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800157
158 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
159 // the hotseat in order regardless of which orientation they were added
160 int x = getCellXFromOrder(mAllAppsButtonRank);
161 int y = getCellYFromOrder(mAllAppsButtonRank);
162 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
163 lp.canReorder = false;
164 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700165 }
Adam Cohene25af792013-06-06 23:08:25 -0700166
Adam Cohena5f4e482013-10-11 12:10:28 -0700167 @Override
168 public boolean onInterceptTouchEvent(MotionEvent ev) {
169 // We don't want any clicks to go through to the hotseat unless the workspace is in
170 // the normal state.
Adam Cohen6c5891a2014-07-09 23:53:15 -0700171 if (mLauncher.getWorkspace().workspaceInModalState()) {
Adam Cohena5f4e482013-10-11 12:10:28 -0700172 return true;
173 }
174 return false;
175 }
Winson Chung8f1eff72015-05-28 17:33:40 -0700176
177 @Override
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700178 public void fillInLaunchSourceData(View v, ItemInfo info, Target target, Target targetParent) {
179 target.itemType = LauncherLogProto.APP_ICON;
180 target.gridX = info.cellX;
181 target.gridY = info.cellY;
182 targetParent.containerType = LauncherLogProto.HOTSEAT;
Winson Chung8f1eff72015-05-28 17:33:40 -0700183 }
Tony Wickham462b5cc2016-04-01 16:00:49 -0700184
185 //Overridden so that the background color extends behind the navigation buttons.
186 @Override
187 public void setInsets(Rect insets) {
188 int rightInset = insets.right - mInsets.right;
189 int bottomInset = insets.bottom - mInsets.bottom;
190 mInsets.set(insets);
191 LayoutParams lp = (LayoutParams) getLayoutParams();
192 if (mHasVerticalHotseat) {
193 setPadding(getPaddingLeft(), getPaddingTop(),
194 getPaddingRight() + rightInset, getPaddingBottom());
195 if (lp.width != LayoutParams.MATCH_PARENT && lp.width != LayoutParams.WRAP_CONTENT) {
196 lp.width += rightInset;
197 }
198 } else {
199 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(),
200 getPaddingBottom() + bottomInset);
201 if (lp.height != LayoutParams.MATCH_PARENT && lp.height != LayoutParams.WRAP_CONTENT) {
202 lp.height += bottomInset;
203 }
204 }
205 }
206
207 public void updateColor(ExtractedColors extractedColors, boolean animate) {
208 if (!mHasVerticalHotseat) {
209 int color = extractedColors.getColor(ExtractedColors.HOTSEAT_INDEX, Color.TRANSPARENT);
210 if (!animate) {
211 setBackgroundColor(color);
212 } else {
213 ValueAnimator animator = ValueAnimator.ofInt(mBackgroundColor, color);
214 animator.setEvaluator(new ArgbEvaluator());
215 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
216 @Override
217 public void onAnimationUpdate(ValueAnimator animation) {
218 mBackground.setColor((Integer) animation.getAnimatedValue());
219 }
220 });
221 animator.start();
222 }
223 mBackgroundColor = color;
224 }
225 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700226}