blob: 4a8a27278a614c0c7c00dadbac8f52e6f3e1ba58 [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 Wickhamef0c5372016-08-22 15:23:01 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Tony Wickham462b5cc2016-04-01 16:00:49 -070021import android.animation.ArgbEvaluator;
22import android.animation.ValueAnimator;
Winson Chung3d503fb2011-07-13 17:25:49 -070023import android.content.Context;
Tony Wickham462b5cc2016-04-01 16:00:49 -070024import android.graphics.Color;
Tony Wickhamd6b40372015-09-23 18:37:57 -070025import android.graphics.Rect;
Tony Wickham462b5cc2016-04-01 16:00:49 -070026import android.graphics.drawable.ColorDrawable;
Winson Chungc58497e2013-09-03 17:48:37 -070027import android.graphics.drawable.Drawable;
Jon Miranda488d8ad2016-09-15 15:05:12 -070028import android.support.v4.content.ContextCompat;
Hyunyoung Songe4be3b32016-07-18 16:35:10 -070029import android.support.v4.graphics.ColorUtils;
Winson Chung3d503fb2011-07-13 17:25:49 -070030import android.util.AttributeSet;
Winson Chungc58497e2013-09-03 17:48:37 -070031import android.view.LayoutInflater;
Adam Cohena5f4e482013-10-11 12:10:28 -070032import android.view.MotionEvent;
Winsona49b1f72015-10-16 14:57:24 -070033import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080034import android.view.ViewDebug;
Winson Chung3d503fb2011-07-13 17:25:49 -070035import android.widget.FrameLayout;
Adam Cohen61f560d2013-09-30 15:58:20 -070036import android.widget.TextView;
Winson Chung3d503fb2011-07-13 17:25:49 -070037
Sunny Goyalbb011da2016-06-15 15:42:29 -070038import com.android.launcher3.config.FeatureFlags;
Tony Wickham462b5cc2016-04-01 16:00:49 -070039import com.android.launcher3.dynamicui.ExtractedColors;
Hyunyoung Songaa953652016-04-19 18:30:24 -070040import com.android.launcher3.logging.UserEventDispatcher;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053041import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
Hyunyoung Songddec1c72016-04-12 18:32:04 -070042import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
43
Winson Chung8f1eff72015-05-28 17:33:40 -070044public class Hotseat extends FrameLayout
Hyunyoung Songdf7ef682016-10-06 17:52:22 -070045 implements UserEventDispatcher.LogContainerProvider {
Winson Chung3d503fb2011-07-13 17:25:49 -070046
Winson Chung3d503fb2011-07-13 17:25:49 -070047 private CellLayout mContent;
48
Winson Chungc58497e2013-09-03 17:48:37 -070049 private Launcher mLauncher;
50
Sunny Goyal4ffec482016-02-09 11:28:52 -080051 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal4f3e9382015-06-05 00:13:25 -070052 private final boolean mHasVerticalHotseat;
Winson Chung3d503fb2011-07-13 17:25:49 -070053
Tony Wickham462b5cc2016-04-01 16:00:49 -070054 @ViewDebug.ExportedProperty(category = "launcher")
Tony Wickham462b5cc2016-04-01 16:00:49 -070055 private int mBackgroundColor;
56 @ViewDebug.ExportedProperty(category = "launcher")
57 private ColorDrawable mBackground;
Tony Wickhamef0c5372016-08-22 15:23:01 -070058 private ValueAnimator mBackgroundColorAnimator;
Tony Wickham462b5cc2016-04-01 16:00:49 -070059
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);
Tony2fd02082016-10-07 12:50:01 -070070 mLauncher = Launcher.getLauncher(context);
Sunny Goyal4f3e9382015-06-05 00:13:25 -070071 mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout();
Hyunyoung Songe4be3b32016-07-18 16:35:10 -070072 mBackgroundColor = ColorUtils.setAlphaComponent(
Jon Miranda488d8ad2016-09-15 15:05:12 -070073 ContextCompat.getColor(context, R.color.all_apps_container_color), 0);
Hyunyoung Songe4be3b32016-07-18 16:35:10 -070074 mBackground = new ColorDrawable(mBackgroundColor);
Tony Wickham462b5cc2016-04-01 16:00:49 -070075 setBackground(mBackground);
Winson Chung3d503fb2011-07-13 17:25:49 -070076 }
77
Tony Wickham95cdb3a2016-02-18 14:37:07 -080078 public CellLayout getLayout() {
Winson Chung3d503fb2011-07-13 17:25:49 -070079 return mContent;
80 }
Winson Chung11a1a532013-09-13 11:14:45 -070081
82 /**
Winson Chungc393b072015-05-20 15:03:13 -070083 * Returns whether there are other icons than the all apps button in the hotseat.
84 */
85 public boolean hasIcons() {
86 return mContent.getShortcutsAndWidgets().getChildCount() > 1;
87 }
88
89 /**
Winson Chung11a1a532013-09-13 11:14:45 -070090 * Registers the specified listener on the cell layout of the hotseat.
91 */
92 @Override
93 public void setOnLongClickListener(OnLongClickListener l) {
94 mContent.setOnLongClickListener(l);
95 }
Tony Wickham6cbd2222015-11-09 17:51:08 -080096
Winson Chung3d503fb2011-07-13 17:25:49 -070097 /* Get the orientation invariant order of the item in the hotseat for persistence. */
98 int getOrderInHotseat(int x, int y) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -070099 return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x;
Winson Chung3d503fb2011-07-13 17:25:49 -0700100 }
Hyunyoung Song31178b82015-02-24 14:12:51 -0800101
Winson Chung3d503fb2011-07-13 17:25:49 -0700102 /* Get the orientation specific coordinates given an invariant order in the hotseat. */
103 int getCellXFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700104 return mHasVerticalHotseat ? 0 : rank;
Winson Chung3d503fb2011-07-13 17:25:49 -0700105 }
Hyunyoung Song31178b82015-02-24 14:12:51 -0800106
Winson Chung3d503fb2011-07-13 17:25:49 -0700107 int getCellYFromOrder(int rank) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700108 return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800109 }
110
Winson Chung3d503fb2011-07-13 17:25:49 -0700111 @Override
112 protected void onFinishInflate() {
113 super.onFinishInflate();
Adam Cohen2e6da152015-05-06 11:42:25 -0700114 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chung3d503fb2011-07-13 17:25:49 -0700115 mContent = (CellLayout) findViewById(R.id.layout);
Sunny Goyalc13403c2016-11-18 23:44:48 -0800116 if (grid.isVerticalBarLayout()) {
117 mContent.setGridSize(1, grid.inv.numHotseatIcons);
Winson Chung5f8afe62013-08-12 16:19:28 -0700118 } else {
Sunny Goyalc13403c2016-11-18 23:44:48 -0800119 mContent.setGridSize(grid.inv.numHotseatIcons, 1);
Winson Chung5f8afe62013-08-12 16:19:28 -0700120 }
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();
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800131 DeviceProfile grid = mLauncher.getDeviceProfile();
132 int allAppsButtonRank = grid.inv.getAllAppsButtonRank();
Winson Chungc58497e2013-09-03 17:48:37 -0700133
Sunny Goyalbb011da2016-06-15 15:42:29 -0700134 LayoutInflater inflater = LayoutInflater.from(context);
135 TextView allAppsButton = (TextView)
136 inflater.inflate(R.layout.all_apps_button, mContent, false);
137 Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
Sunny Goyal55cb70b2016-11-12 09:58:29 -0800138 d.setBounds(0, 0, grid.iconSizePx, grid.iconSizePx);
Adam Cohen71b04732014-06-11 10:36:14 -0700139
Sunny Goyalbb011da2016-06-15 15:42:29 -0700140 int scaleDownPx = getResources().getDimensionPixelSize(R.dimen.all_apps_button_scale_down);
141 Rect bounds = d.getBounds();
142 d.setBounds(bounds.left, bounds.top + scaleDownPx / 2, bounds.right - scaleDownPx,
143 bounds.bottom - scaleDownPx / 2);
144 allAppsButton.setCompoundDrawables(null, d, null, null);
Winson Chungc58497e2013-09-03 17:48:37 -0700145
Sunny Goyalbb011da2016-06-15 15:42:29 -0700146 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
147 allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener());
148 if (mLauncher != null) {
149 mLauncher.setAllAppsButton(allAppsButton);
150 allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
151 allAppsButton.setOnClickListener(mLauncher);
152 allAppsButton.setOnLongClickListener(mLauncher);
153 allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
154 }
155
156 // Note: We do this to ensure that the hotseat is always laid out in the orientation of
157 // the hotseat in order regardless of which orientation they were added
158 int x = getCellXFromOrder(allAppsButtonRank);
159 int y = getCellYFromOrder(allAppsButtonRank);
160 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x, y, 1, 1);
161 lp.canReorder = false;
162 mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true);
Winson Chungc58497e2013-09-03 17:48:37 -0700163 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700164 }
Adam Cohene25af792013-06-06 23:08:25 -0700165
Adam Cohena5f4e482013-10-11 12:10:28 -0700166 @Override
167 public boolean onInterceptTouchEvent(MotionEvent ev) {
168 // We don't want any clicks to go through to the hotseat unless the workspace is in
Sunny Goyal4583d092016-08-17 11:11:48 -0700169 // the normal state or an accessible drag is in progress.
Tony Wickham0c7852f2016-12-02 14:47:04 -0800170 return !mLauncher.getWorkspace().workspaceIconsCanBeDragged() &&
Sunny Goyal4583d092016-08-17 11:11:48 -0700171 !mLauncher.getAccessibilityDelegate().isInAccessibleDrag();
Adam Cohena5f4e482013-10-11 12:10:28 -0700172 }
Winson Chung8f1eff72015-05-28 17:33:40 -0700173
174 @Override
Hyunyoung Songdf7ef682016-10-06 17:52:22 -0700175 public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) {
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700176 target.gridX = info.cellX;
177 target.gridY = info.cellY;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530178 targetParent.containerType = ContainerType.HOTSEAT;
Winson Chung8f1eff72015-05-28 17:33:40 -0700179 }
Tony Wickham462b5cc2016-04-01 16:00:49 -0700180
Tony Wickham462b5cc2016-04-01 16:00:49 -0700181 public void updateColor(ExtractedColors extractedColors, boolean animate) {
182 if (!mHasVerticalHotseat) {
183 int color = extractedColors.getColor(ExtractedColors.HOTSEAT_INDEX, Color.TRANSPARENT);
Tony Wickhamef0c5372016-08-22 15:23:01 -0700184 if (mBackgroundColorAnimator != null) {
185 mBackgroundColorAnimator.cancel();
186 }
Tony Wickham462b5cc2016-04-01 16:00:49 -0700187 if (!animate) {
188 setBackgroundColor(color);
189 } else {
Tony Wickhamef0c5372016-08-22 15:23:01 -0700190 mBackgroundColorAnimator = ValueAnimator.ofInt(mBackgroundColor, color);
191 mBackgroundColorAnimator.setEvaluator(new ArgbEvaluator());
192 mBackgroundColorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
Tony Wickham462b5cc2016-04-01 16:00:49 -0700193 @Override
194 public void onAnimationUpdate(ValueAnimator animation) {
195 mBackground.setColor((Integer) animation.getAnimatedValue());
196 }
197 });
Tony Wickhamef0c5372016-08-22 15:23:01 -0700198 mBackgroundColorAnimator.addListener(new AnimatorListenerAdapter() {
199 @Override
200 public void onAnimationEnd(Animator animation) {
201 mBackgroundColorAnimator = null;
202 }
203 });
204 mBackgroundColorAnimator.start();
Tony Wickham462b5cc2016-04-01 16:00:49 -0700205 }
206 mBackgroundColor = color;
207 }
208 }
Hyunyoung Songa0c56472016-06-20 13:54:42 -0700209
210 public void setBackgroundTransparent(boolean enable) {
Hyunyoung Songa0c56472016-06-20 13:54:42 -0700211 if (enable) {
Hyunyoung Song45eb7572016-07-01 18:04:07 -0700212 mBackground.setAlpha(0);
Hyunyoung Songa0c56472016-06-20 13:54:42 -0700213 } else {
Hyunyoung Song45eb7572016-07-01 18:04:07 -0700214 mBackground.setAlpha(255);
Hyunyoung Songa0c56472016-06-20 13:54:42 -0700215 }
216 }
Hyunyoung Songf7e5e372016-06-28 12:16:47 -0700217
Hyunyoung Songe4be3b32016-07-18 16:35:10 -0700218 public int getBackgroundDrawableColor() {
219 return mBackgroundColor;
Hyunyoung Songf7e5e372016-06-28 12:16:47 -0700220 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700221}