Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 18 | |
Tony Wickham | ef0c537 | 2016-08-22 15:23:01 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 21 | import android.animation.ArgbEvaluator; |
| 22 | import android.animation.ValueAnimator; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 24 | import android.graphics.Color; |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 25 | import android.graphics.Rect; |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.ColorDrawable; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.Drawable; |
Jon Miranda | 488d8ad | 2016-09-15 15:05:12 -0700 | [diff] [blame] | 28 | import android.support.v4.content.ContextCompat; |
Hyunyoung Song | e4be3b3 | 2016-07-18 16:35:10 -0700 | [diff] [blame] | 29 | import android.support.v4.graphics.ColorUtils; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 31 | import android.view.LayoutInflater; |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 32 | import android.view.MotionEvent; |
Winson | a49b1f7 | 2015-10-16 14:57:24 -0700 | [diff] [blame] | 33 | import android.view.View; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 34 | import android.view.ViewDebug; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 35 | import android.widget.FrameLayout; |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 36 | import android.widget.TextView; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 37 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 38 | import com.android.launcher3.config.FeatureFlags; |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 39 | import com.android.launcher3.dynamicui.ExtractedColors; |
Hyunyoung Song | aa95365 | 2016-04-19 18:30:24 -0700 | [diff] [blame] | 40 | import com.android.launcher3.logging.UserEventDispatcher; |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame^] | 41 | import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType; |
Hyunyoung Song | ddec1c7 | 2016-04-12 18:32:04 -0700 | [diff] [blame] | 42 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
| 43 | |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 44 | public class Hotseat extends FrameLayout |
Hyunyoung Song | df7ef68 | 2016-10-06 17:52:22 -0700 | [diff] [blame] | 45 | implements UserEventDispatcher.LogContainerProvider { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 46 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 47 | private CellLayout mContent; |
| 48 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 49 | private Launcher mLauncher; |
| 50 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 51 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 52 | private final boolean mHasVerticalHotseat; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 53 | |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 54 | @ViewDebug.ExportedProperty(category = "launcher") |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 55 | private int mBackgroundColor; |
| 56 | @ViewDebug.ExportedProperty(category = "launcher") |
| 57 | private ColorDrawable mBackground; |
Tony Wickham | ef0c537 | 2016-08-22 15:23:01 -0700 | [diff] [blame] | 58 | private ValueAnimator mBackgroundColorAnimator; |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 59 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 60 | 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); |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 70 | mLauncher = Launcher.getLauncher(context); |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 71 | mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout(); |
Hyunyoung Song | e4be3b3 | 2016-07-18 16:35:10 -0700 | [diff] [blame] | 72 | mBackgroundColor = ColorUtils.setAlphaComponent( |
Jon Miranda | 488d8ad | 2016-09-15 15:05:12 -0700 | [diff] [blame] | 73 | ContextCompat.getColor(context, R.color.all_apps_container_color), 0); |
Hyunyoung Song | e4be3b3 | 2016-07-18 16:35:10 -0700 | [diff] [blame] | 74 | mBackground = new ColorDrawable(mBackgroundColor); |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 75 | setBackground(mBackground); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 78 | public CellLayout getLayout() { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 79 | return mContent; |
| 80 | } |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
Winson Chung | c393b07 | 2015-05-20 15:03:13 -0700 | [diff] [blame] | 83 | * 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 Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 90 | * 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 Wickham | 6cbd222 | 2015-11-09 17:51:08 -0800 | [diff] [blame] | 96 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 97 | /* Get the orientation invariant order of the item in the hotseat for persistence. */ |
| 98 | int getOrderInHotseat(int x, int y) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 99 | return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 100 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 101 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 102 | /* Get the orientation specific coordinates given an invariant order in the hotseat. */ |
| 103 | int getCellXFromOrder(int rank) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 104 | return mHasVerticalHotseat ? 0 : rank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 105 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 106 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 107 | int getCellYFromOrder(int rank) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 108 | return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 111 | @Override |
| 112 | protected void onFinishInflate() { |
| 113 | super.onFinishInflate(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 114 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 115 | mContent = (CellLayout) findViewById(R.id.layout); |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 116 | if (grid.isVerticalBarLayout()) { |
| 117 | mContent.setGridSize(1, grid.inv.numHotseatIcons); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 118 | } else { |
Sunny Goyal | c13403c | 2016-11-18 23:44:48 -0800 | [diff] [blame] | 119 | mContent.setGridSize(grid.inv.numHotseatIcons, 1); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 120 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 121 | |
| 122 | resetLayout(); |
| 123 | } |
| 124 | |
| 125 | void resetLayout() { |
| 126 | mContent.removeAllViewsInLayout(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 127 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 128 | if (!FeatureFlags.NO_ALL_APPS_ICON) { |
| 129 | // Add the Apps button |
| 130 | Context context = getContext(); |
Sunny Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 131 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
| 132 | int allAppsButtonRank = grid.inv.getAllAppsButtonRank(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 133 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 134 | 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 Goyal | 55cb70b | 2016-11-12 09:58:29 -0800 | [diff] [blame] | 138 | d.setBounds(0, 0, grid.iconSizePx, grid.iconSizePx); |
Adam Cohen | 71b0473 | 2014-06-11 10:36:14 -0700 | [diff] [blame] | 139 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 140 | 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 Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 145 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 146 | 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 Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 163 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 164 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 165 | |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 166 | @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 Goyal | 4583d09 | 2016-08-17 11:11:48 -0700 | [diff] [blame] | 169 | // the normal state or an accessible drag is in progress. |
Tony Wickham | 0c7852f | 2016-12-02 14:47:04 -0800 | [diff] [blame] | 170 | return !mLauncher.getWorkspace().workspaceIconsCanBeDragged() && |
Sunny Goyal | 4583d09 | 2016-08-17 11:11:48 -0700 | [diff] [blame] | 171 | !mLauncher.getAccessibilityDelegate().isInAccessibleDrag(); |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 172 | } |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 173 | |
| 174 | @Override |
Hyunyoung Song | df7ef68 | 2016-10-06 17:52:22 -0700 | [diff] [blame] | 175 | public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) { |
Hyunyoung Song | ddec1c7 | 2016-04-12 18:32:04 -0700 | [diff] [blame] | 176 | target.gridX = info.cellX; |
| 177 | target.gridY = info.cellY; |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame^] | 178 | targetParent.containerType = ContainerType.HOTSEAT; |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 179 | } |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 180 | |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 181 | public void updateColor(ExtractedColors extractedColors, boolean animate) { |
| 182 | if (!mHasVerticalHotseat) { |
| 183 | int color = extractedColors.getColor(ExtractedColors.HOTSEAT_INDEX, Color.TRANSPARENT); |
Tony Wickham | ef0c537 | 2016-08-22 15:23:01 -0700 | [diff] [blame] | 184 | if (mBackgroundColorAnimator != null) { |
| 185 | mBackgroundColorAnimator.cancel(); |
| 186 | } |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 187 | if (!animate) { |
| 188 | setBackgroundColor(color); |
| 189 | } else { |
Tony Wickham | ef0c537 | 2016-08-22 15:23:01 -0700 | [diff] [blame] | 190 | mBackgroundColorAnimator = ValueAnimator.ofInt(mBackgroundColor, color); |
| 191 | mBackgroundColorAnimator.setEvaluator(new ArgbEvaluator()); |
| 192 | mBackgroundColorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 193 | @Override |
| 194 | public void onAnimationUpdate(ValueAnimator animation) { |
| 195 | mBackground.setColor((Integer) animation.getAnimatedValue()); |
| 196 | } |
| 197 | }); |
Tony Wickham | ef0c537 | 2016-08-22 15:23:01 -0700 | [diff] [blame] | 198 | mBackgroundColorAnimator.addListener(new AnimatorListenerAdapter() { |
| 199 | @Override |
| 200 | public void onAnimationEnd(Animator animation) { |
| 201 | mBackgroundColorAnimator = null; |
| 202 | } |
| 203 | }); |
| 204 | mBackgroundColorAnimator.start(); |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 205 | } |
| 206 | mBackgroundColor = color; |
| 207 | } |
| 208 | } |
Hyunyoung Song | a0c5647 | 2016-06-20 13:54:42 -0700 | [diff] [blame] | 209 | |
| 210 | public void setBackgroundTransparent(boolean enable) { |
Hyunyoung Song | a0c5647 | 2016-06-20 13:54:42 -0700 | [diff] [blame] | 211 | if (enable) { |
Hyunyoung Song | 45eb757 | 2016-07-01 18:04:07 -0700 | [diff] [blame] | 212 | mBackground.setAlpha(0); |
Hyunyoung Song | a0c5647 | 2016-06-20 13:54:42 -0700 | [diff] [blame] | 213 | } else { |
Hyunyoung Song | 45eb757 | 2016-07-01 18:04:07 -0700 | [diff] [blame] | 214 | mBackground.setAlpha(255); |
Hyunyoung Song | a0c5647 | 2016-06-20 13:54:42 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
Hyunyoung Song | f7e5e37 | 2016-06-28 12:16:47 -0700 | [diff] [blame] | 217 | |
Hyunyoung Song | e4be3b3 | 2016-07-18 16:35:10 -0700 | [diff] [blame] | 218 | public int getBackgroundDrawableColor() { |
| 219 | return mBackgroundColor; |
Hyunyoung Song | f7e5e37 | 2016-06-28 12:16:47 -0700 | [diff] [blame] | 220 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 221 | } |