blob: 7e2b42e49851fb9f6e3ea04cd6140beed52933fe [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;
Hyunyoung Songe4be3b32016-07-18 16:35:10 -070026import android.support.v4.graphics.ColorUtils;
Winson Chung3d503fb2011-07-13 17:25:49 -070027import android.util.AttributeSet;
Winson Chungc58497e2013-09-03 17:48:37 -070028import android.view.LayoutInflater;
Adam Cohena5f4e482013-10-11 12:10:28 -070029import android.view.MotionEvent;
Winsona49b1f72015-10-16 14:57:24 -070030import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080031import android.view.ViewDebug;
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;
Tony Wickham462b5cc2016-04-01 16:00:49 -070036import com.android.launcher3.dynamicui.ExtractedColors;
Hyunyoung Songaa953652016-04-19 18:30:24 -070037import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Songddec1c72016-04-12 18:32:04 -070038import com.android.launcher3.userevent.nano.LauncherLogProto;
39import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
40
Winson Chung8f1eff72015-05-28 17:33:40 -070041public class Hotseat extends FrameLayout
Tony Wickham462b5cc2016-04-01 16:00:49 -070042 implements UserEventDispatcher.LaunchSourceProvider, Insettable {
Winson Chung3d503fb2011-07-13 17:25:49 -070043
Winson Chung3d503fb2011-07-13 17:25:49 -070044 private CellLayout mContent;
45
Winson Chungc58497e2013-09-03 17:48:37 -070046 private Launcher mLauncher;
47
Sunny Goyal4ffec482016-02-09 11:28:52 -080048 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal4f3e9382015-06-05 00:13:25 -070049 private final boolean mHasVerticalHotseat;
Winson Chung3d503fb2011-07-13 17:25:49 -070050
Tony Wickham462b5cc2016-04-01 16:00:49 -070051 @ViewDebug.ExportedProperty(category = "launcher")
52 private Rect mInsets = new Rect();
53
54 @ViewDebug.ExportedProperty(category = "launcher")
55 private int mBackgroundColor;
56 @ViewDebug.ExportedProperty(category = "launcher")
57 private ColorDrawable mBackground;
58
Winson Chung3d503fb2011-07-13 17:25:49 -070059 public Hotseat(Context context) {
60 this(context, null);
61 }
62
63 public Hotseat(Context context, AttributeSet attrs) {
64 this(context, attrs, 0);
65 }
66
67 public Hotseat(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
Adam Cohen2e6da152015-05-06 11:42:25 -070069 mLauncher = (Launcher) context;
Sunny Goyal4f3e9382015-06-05 00:13:25 -070070 mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout();
Hyunyoung Songe4be3b32016-07-18 16:35:10 -070071 mBackgroundColor = ColorUtils.setAlphaComponent(
72 context.getColor(R.color.all_apps_container_color), 0);
73 mBackground = new ColorDrawable(mBackgroundColor);
Tony Wickham462b5cc2016-04-01 16:00:49 -070074 setBackground(mBackground);
Winson Chung3d503fb2011-07-13 17:25:49 -070075 }
76
Tony Wickham95cdb3a2016-02-18 14:37:07 -080077 public CellLayout getLayout() {
Winson Chung3d503fb2011-07-13 17:25:49 -070078 return mContent;
79 }
Winson Chung11a1a532013-09-13 11:14:45 -070080
81 /**
Winson Chungc393b072015-05-20 15:03:13 -070082 * Returns whether there are other icons than the all apps button in the hotseat.
83 */
84 public boolean hasIcons() {
85 return mContent.getShortcutsAndWidgets().getChildCount() > 1;
86 }
87
88 /**
Winson Chung11a1a532013-09-13 11:14:45 -070089 * Registers the specified listener on the cell layout of the hotseat.
90 */
91 @Override
92 public void setOnLongClickListener(OnLongClickListener l) {
93 mContent.setOnLongClickListener(l);
94 }
Tony Wickham6cbd2222015-11-09 17:51:08 -080095
Winson Chung3d503fb2011-07-13 17:25:49 -070096 /* Get the orientation invariant order of the item in the hotseat for persistence. */
97 int getOrderInHotseat(int x, int y) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070098 return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -070099 }
Hyunyoung Song31178b82015-02-24 14:12:51 -0800100
Winson Chung3d503fb2011-07-13 17:25:49 -0700101 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
102 int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700103 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700104 }
Hyunyoung Song31178b82015-02-24 14:12:51 -0800105
Winson Chung3d503fb2011-07-13 17:25:49 -0700106 int getCellYFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700107 return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800108 }
109
Winson Chung3d503fb2011-07-13 17:25:49 -0700110 @Override
111 protected void onFinishInflate() {
112 super.onFinishInflate();
Adam Cohen2e6da152015-05-06 11:42:25 -0700113 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chung3d503fb2011-07-13 17:25:49 -0700114 mContent = (CellLayout) findViewById(R.id.layout);
Sunny Goyalc6205602015-05-21 20:46:33 -0700115 if (grid.isLandscape && !grid.isLargeTablet) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700116 mContent.setGridSize(1, (int) grid.inv.numHotseatIcons);
Winson Chung5f8afe62013-08-12 16:19:28 -0700117 } else {
Adam Cohen2e6da152015-05-06 11:42:25 -0700118 mContent.setGridSize((int) grid.inv.numHotseatIcons, 1);
Winson Chung5f8afe62013-08-12 16:19:28 -0700119 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800120 mContent.setIsHotseat(true);
Winson Chung3d503fb2011-07-13 17:25:49 -0700121
122 resetLayout();
123 }
124
125 void resetLayout() {
126 mContent.removeAllViewsInLayout();
Winson Chungc58497e2013-09-03 17:48:37 -0700127
Sunny Goyalbb011da2016-06-15 15:42:29 -0700128 if (!FeatureFlags.NO_ALL_APPS_ICON) {
129 // Add the Apps button
130 Context context = getContext();
131 int allAppsButtonRank = mLauncher.getDeviceProfile().inv.getAllAppsButtonRank();
Winson Chungc58497e2013-09-03 17:48:37 -0700132
Sunny Goyalbb011da2016-06-15 15:42:29 -0700133 LayoutInflater inflater = LayoutInflater.from(context);
134 TextView allAppsButton = (TextView)
135 inflater.inflate(R.layout.all_apps_button, mContent, false);
136 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Adam Cohen71b04732014-06-11 10:36:14 -0700137
Sunny Goyalbb011da2016-06-15 15:42:29 -0700138 mLauncher.resizeIconDrawable(d);
139 int scaleDownPx = getResources().getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
140 Rect bounds = d.getBounds();
141 d.setBounds(bounds.left, bounds.top + scaleDownPx / 2, bounds.right - scaleDownPx,
142 bounds.bottom - scaleDownPx / 2);
143 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700144
Sunny Goyalbb011da2016-06-15 15:42:29 -0700145 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
146 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
147 if (mLauncher != null) {
148 mLauncher.setAllAppsButton(allAppsButton);
149 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
150 allAppsButton.setOnClickListener(mLauncher);
151 allAppsButton.setOnLongClickListener(mLauncher);
152 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
153 }
154
155 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
156 // the hotseat in order regardless of which orientation they were added
157 int x = getCellXFromOrder(allAppsButtonRank);
158 int y = getCellYFromOrder(allAppsButtonRank);
159 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x, y, 1, 1);
160 lp.canReorder = false;
161 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chungc58497e2013-09-03 17:48:37 -0700162 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700163 }
Adam Cohene25af792013-06-06 23:08:25 -0700164
Adam Cohena5f4e482013-10-11 12:10:28 -0700165 @Override
166 public boolean onInterceptTouchEvent(MotionEvent ev) {
167 // We don't want any clicks to go through to the hotseat unless the workspace is in
168 // the normal state.
Adam Cohen6c5891a2014-07-09 23:53:15 -0700169 if (mLauncher.getWorkspace().workspaceInModalState()) {
Adam Cohena5f4e482013-10-11 12:10:28 -0700170 return true;
171 }
172 return false;
173 }
Winson Chung8f1eff72015-05-28 17:33:40 -0700174
175 @Override
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700176 public void fillInLaunchSourceData(View v, ItemInfo info, Target target, Target targetParent) {
177 target.itemType = LauncherLogProto.APP_ICON;
178 target.gridX = info.cellX;
179 target.gridY = info.cellY;
180 targetParent.containerType = LauncherLogProto.HOTSEAT;
Winson Chung8f1eff72015-05-28 17:33:40 -0700181 }
Tony Wickham462b5cc2016-04-01 16:00:49 -0700182
183 //Overridden so that the background color extends behind the navigation buttons.
184 @Override
185 public void setInsets(Rect insets) {
186 int rightInset = insets.right - mInsets.right;
187 int bottomInset = insets.bottom - mInsets.bottom;
188 mInsets.set(insets);
189 LayoutParams lp = (LayoutParams) getLayoutParams();
190 if (mHasVerticalHotseat) {
191 setPadding(getPaddingLeft(), getPaddingTop(),
192 getPaddingRight() + rightInset, getPaddingBottom());
193 if (lp.width != LayoutParams.MATCH_PARENT && lp.width != LayoutParams.WRAP_CONTENT) {
194 lp.width += rightInset;
195 }
196 } else {
197 setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(),
198 getPaddingBottom() + bottomInset);
199 if (lp.height != LayoutParams.MATCH_PARENT && lp.height != LayoutParams.WRAP_CONTENT) {
200 lp.height += bottomInset;
201 }
202 }
203 }
204
205 public void updateColor(ExtractedColors extractedColors, boolean animate) {
206 if (!mHasVerticalHotseat) {
207 int color = extractedColors.getColor(ExtractedColors.HOTSEAT_INDEX, Color.TRANSPARENT);
208 if (!animate) {
209 setBackgroundColor(color);
210 } else {
211 ValueAnimator animator = ValueAnimator.ofInt(mBackgroundColor, color);
212 animator.setEvaluator(new ArgbEvaluator());
213 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
214 @Override
215 public void onAnimationUpdate(ValueAnimator animation) {
216 mBackground.setColor((Integer) animation.getAnimatedValue());
217 }
218 });
219 animator.start();
220 }
221 mBackgroundColor = color;
222 }
223 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -0700224
225 public void setBackgroundTransparent(boolean enable) {
Hyunyoung Songa0c56472016-06-20 13:54:42 -0700226 if (enable) {
Hyunyoung Song45eb7572016-07-01 18:04:07 -0700227 mBackground.setAlpha(0);
Hyunyoung Songa0c56472016-06-20 13:54:42 -0700228 } else {
Hyunyoung Song45eb7572016-07-01 18:04:07 -0700229 mBackground.setAlpha(255);
Hyunyoung Songa0c56472016-06-20 13:54:42 -0700230 }
231 }
Hyunyoung Songf7e5e372016-06-28 12:16:47 -0700232
Hyunyoung Songe4be3b32016-07-18 16:35:10 -0700233 public int getBackgroundDrawableColor() {
234 return mBackgroundColor;
Hyunyoung Songf7e5e372016-06-28 12:16:47 -0700235 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700236}