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 | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 19 | import android.animation.ArgbEvaluator; |
| 20 | import android.animation.ValueAnimator; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 22 | import android.graphics.Color; |
Tony Wickham | d6b4037 | 2015-09-23 18:37:57 -0700 | [diff] [blame] | 23 | import android.graphics.Rect; |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.ColorDrawable; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 28 | import android.view.MotionEvent; |
Winson | a49b1f7 | 2015-10-16 14:57:24 -0700 | [diff] [blame] | 29 | import android.view.View; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 30 | import android.view.ViewDebug; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 32 | import android.widget.TextView; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 33 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 34 | import com.android.launcher3.config.FeatureFlags; |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 35 | import com.android.launcher3.dynamicui.ExtractedColors; |
Hyunyoung Song | aa95365 | 2016-04-19 18:30:24 -0700 | [diff] [blame] | 36 | import com.android.launcher3.logging.UserEventDispatcher; |
Hyunyoung Song | ddec1c7 | 2016-04-12 18:32:04 -0700 | [diff] [blame] | 37 | import com.android.launcher3.userevent.nano.LauncherLogProto; |
| 38 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
| 39 | |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 40 | public class Hotseat extends FrameLayout |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 41 | implements UserEventDispatcher.LaunchSourceProvider, Insettable { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 42 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 43 | private CellLayout mContent; |
| 44 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 45 | private Launcher mLauncher; |
| 46 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 47 | @ViewDebug.ExportedProperty(category = "launcher") |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 48 | private final boolean mHasVerticalHotseat; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 49 | |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 50 | @ViewDebug.ExportedProperty(category = "launcher") |
| 51 | private Rect mInsets = new Rect(); |
| 52 | |
| 53 | @ViewDebug.ExportedProperty(category = "launcher") |
| 54 | private int mBackgroundColor; |
| 55 | @ViewDebug.ExportedProperty(category = "launcher") |
| 56 | private ColorDrawable mBackground; |
| 57 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 58 | public Hotseat(Context context) { |
| 59 | this(context, null); |
| 60 | } |
| 61 | |
| 62 | public Hotseat(Context context, AttributeSet attrs) { |
| 63 | this(context, attrs, 0); |
| 64 | } |
| 65 | |
| 66 | public Hotseat(Context context, AttributeSet attrs, int defStyle) { |
| 67 | super(context, attrs, defStyle); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 68 | mLauncher = (Launcher) context; |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 69 | mHasVerticalHotseat = mLauncher.getDeviceProfile().isVerticalBarLayout(); |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 70 | mBackground = new ColorDrawable(); |
| 71 | setBackground(mBackground); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Tony Wickham | 95cdb3a | 2016-02-18 14:37:07 -0800 | [diff] [blame] | 74 | public CellLayout getLayout() { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 75 | return mContent; |
| 76 | } |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
Winson Chung | c393b07 | 2015-05-20 15:03:13 -0700 | [diff] [blame] | 79 | * Returns whether there are other icons than the all apps button in the hotseat. |
| 80 | */ |
| 81 | public boolean hasIcons() { |
| 82 | return mContent.getShortcutsAndWidgets().getChildCount() > 1; |
| 83 | } |
| 84 | |
| 85 | /** |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 86 | * Registers the specified listener on the cell layout of the hotseat. |
| 87 | */ |
| 88 | @Override |
| 89 | public void setOnLongClickListener(OnLongClickListener l) { |
| 90 | mContent.setOnLongClickListener(l); |
| 91 | } |
Tony Wickham | 6cbd222 | 2015-11-09 17:51:08 -0800 | [diff] [blame] | 92 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 93 | /* Get the orientation invariant order of the item in the hotseat for persistence. */ |
| 94 | int getOrderInHotseat(int x, int y) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 95 | return mHasVerticalHotseat ? (mContent.getCountY() - y - 1) : x; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 96 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 97 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 98 | /* Get the orientation specific coordinates given an invariant order in the hotseat. */ |
| 99 | int getCellXFromOrder(int rank) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 100 | return mHasVerticalHotseat ? 0 : rank; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 101 | } |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 102 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 103 | int getCellYFromOrder(int rank) { |
Sunny Goyal | 4f3e938 | 2015-06-05 00:13:25 -0700 | [diff] [blame] | 104 | return mHasVerticalHotseat ? (mContent.getCountY() - (rank + 1)) : 0; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 107 | @Override |
| 108 | protected void onFinishInflate() { |
| 109 | super.onFinishInflate(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 110 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 111 | mContent = (CellLayout) findViewById(R.id.layout); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 112 | if (grid.isLandscape && !grid.isLargeTablet) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 113 | mContent.setGridSize(1, (int) grid.inv.numHotseatIcons); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 114 | } else { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 115 | mContent.setGridSize((int) grid.inv.numHotseatIcons, 1); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 116 | } |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 117 | mContent.setIsHotseat(true); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 118 | |
| 119 | resetLayout(); |
| 120 | } |
| 121 | |
| 122 | void resetLayout() { |
| 123 | mContent.removeAllViewsInLayout(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 124 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 125 | if (!FeatureFlags.NO_ALL_APPS_ICON) { |
| 126 | // Add the Apps button |
| 127 | Context context = getContext(); |
| 128 | int allAppsButtonRank = mLauncher.getDeviceProfile().inv.getAllAppsButtonRank(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 129 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 130 | LayoutInflater inflater = LayoutInflater.from(context); |
| 131 | TextView allAppsButton = (TextView) |
| 132 | inflater.inflate(R.layout.all_apps_button, mContent, false); |
| 133 | Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon); |
Adam Cohen | 71b0473 | 2014-06-11 10:36:14 -0700 | [diff] [blame] | 134 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 135 | mLauncher.resizeIconDrawable(d); |
| 136 | int scaleDownPx = getResources().getDimensionPixelSize(R.dimen.all_apps_button_scale_down); |
| 137 | Rect bounds = d.getBounds(); |
| 138 | d.setBounds(bounds.left, bounds.top + scaleDownPx / 2, bounds.right - scaleDownPx, |
| 139 | bounds.bottom - scaleDownPx / 2); |
| 140 | allAppsButton.setCompoundDrawables(null, d, null, null); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 141 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 142 | allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label)); |
| 143 | allAppsButton.setOnKeyListener(new HotseatIconKeyEventListener()); |
| 144 | if (mLauncher != null) { |
| 145 | mLauncher.setAllAppsButton(allAppsButton); |
| 146 | allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener()); |
| 147 | allAppsButton.setOnClickListener(mLauncher); |
| 148 | allAppsButton.setOnLongClickListener(mLauncher); |
| 149 | allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler); |
| 150 | } |
| 151 | |
| 152 | // Note: We do this to ensure that the hotseat is always laid out in the orientation of |
| 153 | // the hotseat in order regardless of which orientation they were added |
| 154 | int x = getCellXFromOrder(allAppsButtonRank); |
| 155 | int y = getCellYFromOrder(allAppsButtonRank); |
| 156 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x, y, 1, 1); |
| 157 | lp.canReorder = false; |
| 158 | mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 159 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 160 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 161 | |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 164 | // We don't want any clicks to go through to the hotseat unless the workspace is in |
| 165 | // the normal state. |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 166 | if (mLauncher.getWorkspace().workspaceInModalState()) { |
Adam Cohen | a5f4e48 | 2013-10-11 12:10:28 -0700 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | return false; |
| 170 | } |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 171 | |
| 172 | @Override |
Hyunyoung Song | ddec1c7 | 2016-04-12 18:32:04 -0700 | [diff] [blame] | 173 | public void fillInLaunchSourceData(View v, ItemInfo info, Target target, Target targetParent) { |
| 174 | target.itemType = LauncherLogProto.APP_ICON; |
| 175 | target.gridX = info.cellX; |
| 176 | target.gridY = info.cellY; |
| 177 | targetParent.containerType = LauncherLogProto.HOTSEAT; |
Winson Chung | 8f1eff7 | 2015-05-28 17:33:40 -0700 | [diff] [blame] | 178 | } |
Tony Wickham | 462b5cc | 2016-04-01 16:00:49 -0700 | [diff] [blame] | 179 | |
| 180 | //Overridden so that the background color extends behind the navigation buttons. |
| 181 | @Override |
| 182 | public void setInsets(Rect insets) { |
| 183 | int rightInset = insets.right - mInsets.right; |
| 184 | int bottomInset = insets.bottom - mInsets.bottom; |
| 185 | mInsets.set(insets); |
| 186 | LayoutParams lp = (LayoutParams) getLayoutParams(); |
| 187 | if (mHasVerticalHotseat) { |
| 188 | setPadding(getPaddingLeft(), getPaddingTop(), |
| 189 | getPaddingRight() + rightInset, getPaddingBottom()); |
| 190 | if (lp.width != LayoutParams.MATCH_PARENT && lp.width != LayoutParams.WRAP_CONTENT) { |
| 191 | lp.width += rightInset; |
| 192 | } |
| 193 | } else { |
| 194 | setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), |
| 195 | getPaddingBottom() + bottomInset); |
| 196 | if (lp.height != LayoutParams.MATCH_PARENT && lp.height != LayoutParams.WRAP_CONTENT) { |
| 197 | lp.height += bottomInset; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | public void updateColor(ExtractedColors extractedColors, boolean animate) { |
| 203 | if (!mHasVerticalHotseat) { |
| 204 | int color = extractedColors.getColor(ExtractedColors.HOTSEAT_INDEX, Color.TRANSPARENT); |
| 205 | if (!animate) { |
| 206 | setBackgroundColor(color); |
| 207 | } else { |
| 208 | ValueAnimator animator = ValueAnimator.ofInt(mBackgroundColor, color); |
| 209 | animator.setEvaluator(new ArgbEvaluator()); |
| 210 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 211 | @Override |
| 212 | public void onAnimationUpdate(ValueAnimator animation) { |
| 213 | mBackground.setColor((Integer) animation.getAnimatedValue()); |
| 214 | } |
| 215 | }); |
| 216 | animator.start(); |
| 217 | } |
| 218 | mBackgroundColor = color; |
| 219 | } |
| 220 | } |
Hyunyoung Song | a0c5647 | 2016-06-20 13:54:42 -0700 | [diff] [blame^] | 221 | |
| 222 | public void setBackgroundTransparent(boolean enable) { |
| 223 | // This causes re-layout. Should replace the logic with simply setting the background alpha |
| 224 | if (enable) { |
| 225 | setBackground(null); |
| 226 | } else { |
| 227 | setBackground(mBackground); |
| 228 | } |
| 229 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 230 | } |