Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 18 | |
| 19 | import android.app.WallpaperManager; |
| 20 | import android.content.Context; |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Paint; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 23 | import android.graphics.Rect; |
| 24 | import android.view.View; |
Michael Jurka | cf6125c | 2011-01-28 15:20:01 -0800 | [diff] [blame] | 25 | import android.view.ViewGroup; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 26 | import android.view.ViewGroup.LayoutParams; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 27 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 28 | public class ShortcutAndWidgetContainer extends ViewGroup { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 29 | static final String TAG = "CellLayoutChildren"; |
| 30 | |
| 31 | // These are temporary variables to prevent having to allocate a new object just to |
| 32 | // return an (x, y) value from helper functions. Do NOT use them to maintain other state. |
| 33 | private final int[] mTmpCellXY = new int[2]; |
| 34 | |
| 35 | private final WallpaperManager mWallpaperManager; |
| 36 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 37 | private boolean mIsHotseatLayout; |
| 38 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 39 | private int mCellWidth; |
| 40 | private int mCellHeight; |
| 41 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 42 | private int mWidthGap; |
| 43 | private int mHeightGap; |
| 44 | |
Adam Cohen | 2374abf | 2013-04-16 14:56:57 -0700 | [diff] [blame] | 45 | private int mCountX; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 46 | private int mCountY; |
Adam Cohen | 2374abf | 2013-04-16 14:56:57 -0700 | [diff] [blame] | 47 | |
| 48 | private boolean mInvertIfRtl = false; |
| 49 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 50 | public ShortcutAndWidgetContainer(Context context) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 51 | super(context); |
| 52 | mWallpaperManager = WallpaperManager.getInstance(context); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Adam Cohen | 2374abf | 2013-04-16 14:56:57 -0700 | [diff] [blame] | 55 | public void setCellDimensions(int cellWidth, int cellHeight, int widthGap, int heightGap, |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 56 | int countX, int countY) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 57 | mCellWidth = cellWidth; |
| 58 | mCellHeight = cellHeight; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 59 | mWidthGap = widthGap; |
| 60 | mHeightGap = heightGap; |
Adam Cohen | 2374abf | 2013-04-16 14:56:57 -0700 | [diff] [blame] | 61 | mCountX = countX; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 62 | mCountY = countY; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | public View getChildAt(int x, int y) { |
| 66 | final int count = getChildCount(); |
| 67 | for (int i = 0; i < count; i++) { |
| 68 | View child = getChildAt(i); |
| 69 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 70 | |
| 71 | if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) && |
Adam Cohen | f579b50 | 2011-04-19 17:03:08 -0700 | [diff] [blame] | 72 | (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 73 | return child; |
| 74 | } |
| 75 | } |
| 76 | return null; |
| 77 | } |
| 78 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 79 | public void addView(View child, int index, LayoutParams params, boolean inLayout) { |
| 80 | if (!inLayout) { |
| 81 | addView(child, index, params); |
| 82 | } else { |
| 83 | addViewInLayout(child, index, params, false); |
| 84 | } |
| 85 | } |
| 86 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 87 | @Override |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 88 | protected void dispatchDraw(Canvas canvas) { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 89 | @SuppressWarnings("all") // suppress dead code warning |
| 90 | final boolean debug = false; |
| 91 | if (debug) { |
| 92 | // Debug drawing for hit space |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 93 | Paint p = new Paint(); |
| 94 | p.setColor(0x6600FF00); |
| 95 | for (int i = getChildCount() - 1; i >= 0; i--) { |
| 96 | final View child = getChildAt(i); |
| 97 | final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 98 | |
| 99 | canvas.drawRect(lp.x, lp.y, lp.x + lp.width, lp.y + lp.height, p); |
| 100 | } |
| 101 | } |
| 102 | super.dispatchDraw(canvas); |
| 103 | } |
| 104 | |
| 105 | @Override |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 106 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 107 | int count = getChildCount(); |
Adam Cohen | ec40b2b | 2013-07-23 15:52:40 -0700 | [diff] [blame] | 108 | |
| 109 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 110 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 111 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 112 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 113 | for (int i = 0; i < count; i++) { |
| 114 | View child = getChildAt(i); |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 115 | if (child.getVisibility() != GONE) { |
| 116 | measureChild(child); |
| 117 | } |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 118 | } |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 121 | public void setupLp(CellLayout.LayoutParams lp) { |
Adam Cohen | 2374abf | 2013-04-16 14:56:57 -0700 | [diff] [blame] | 122 | lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap, invertLayoutHorizontally(), |
| 123 | mCountX); |
| 124 | } |
| 125 | |
| 126 | // Set whether or not to invert the layout horizontally if the layout is in RTL mode. |
| 127 | public void setInvertIfRtl(boolean invert) { |
| 128 | mInvertIfRtl = invert; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 131 | public void setIsHotseat(boolean isHotseat) { |
| 132 | mIsHotseatLayout = isHotseat; |
| 133 | } |
| 134 | |
Winson Chung | 3a6e7f3 | 2013-10-09 15:50:52 -0700 | [diff] [blame] | 135 | int getCellContentWidth() { |
| 136 | final LauncherAppState app = LauncherAppState.getInstance(); |
| 137 | final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 138 | return Math.min(getMeasuredHeight(), mIsHotseatLayout ? |
| 139 | grid.hotseatCellWidthPx: grid.cellWidthPx); |
| 140 | } |
| 141 | |
Winson Chung | 69737c3 | 2013-10-08 17:00:19 -0700 | [diff] [blame] | 142 | int getCellContentHeight() { |
| 143 | final LauncherAppState app = LauncherAppState.getInstance(); |
| 144 | final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 145 | return Math.min(getMeasuredHeight(), mIsHotseatLayout ? |
| 146 | grid.hotseatCellHeightPx : grid.cellHeightPx); |
| 147 | } |
| 148 | |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 149 | public void measureChild(View child) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 150 | final LauncherAppState app = LauncherAppState.getInstance(); |
| 151 | final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 152 | final int cellWidth = mCellWidth; |
| 153 | final int cellHeight = mCellHeight; |
| 154 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
Adam Cohen | ec40b2b | 2013-07-23 15:52:40 -0700 | [diff] [blame] | 155 | if (!lp.isFullscreen) { |
| 156 | lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, invertLayoutHorizontally(), |
| 157 | mCountX); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 158 | |
| 159 | if (child instanceof LauncherAppWidgetHostView) { |
| 160 | // Widgets have their own padding, so skip |
| 161 | } else { |
| 162 | // Otherwise, center the icon |
Winson Chung | 69737c3 | 2013-10-08 17:00:19 -0700 | [diff] [blame] | 163 | int cHeight = getCellContentHeight(); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 164 | int cellPaddingY = (int) Math.max(0, ((lp.height - cHeight) / 2f)); |
Winson Chung | cdef044 | 2013-09-19 17:32:58 -0700 | [diff] [blame] | 165 | int cellPaddingX = (int) (grid.edgeMarginPx / 2f); |
| 166 | child.setPadding(cellPaddingX, cellPaddingY, cellPaddingX, 0); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 167 | } |
Adam Cohen | ec40b2b | 2013-07-23 15:52:40 -0700 | [diff] [blame] | 168 | } else { |
| 169 | lp.x = 0; |
| 170 | lp.y = 0; |
| 171 | lp.width = getMeasuredWidth(); |
| 172 | lp.height = getMeasuredHeight(); |
| 173 | } |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 174 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); |
| 175 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, |
| 176 | MeasureSpec.EXACTLY); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 177 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 178 | } |
| 179 | |
Adam Cohen | 2374abf | 2013-04-16 14:56:57 -0700 | [diff] [blame] | 180 | private boolean invertLayoutHorizontally() { |
| 181 | return mInvertIfRtl && isLayoutRtl(); |
| 182 | } |
| 183 | |
| 184 | public boolean isLayoutRtl() { |
| 185 | return (getLayoutDirection() == LAYOUT_DIRECTION_RTL); |
| 186 | } |
| 187 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 188 | @Override |
| 189 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 190 | int count = getChildCount(); |
| 191 | for (int i = 0; i < count; i++) { |
| 192 | final View child = getChildAt(i); |
| 193 | if (child.getVisibility() != GONE) { |
| 194 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 195 | int childLeft = lp.x; |
| 196 | int childTop = lp.y; |
| 197 | child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); |
| 198 | |
| 199 | if (lp.dropped) { |
| 200 | lp.dropped = false; |
| 201 | |
| 202 | final int[] cellXY = mTmpCellXY; |
| 203 | getLocationOnScreen(cellXY); |
| 204 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), |
| 205 | WallpaperManager.COMMAND_DROP, |
| 206 | cellXY[0] + childLeft + lp.width / 2, |
| 207 | cellXY[1] + childTop + lp.height / 2, 0, null); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | @Override |
Michael Jurka | e6235dd | 2011-10-04 15:02:05 -0700 | [diff] [blame] | 214 | public boolean shouldDelayChildPressedState() { |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | @Override |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 219 | public void requestChildFocus(View child, View focused) { |
| 220 | super.requestChildFocus(child, focused); |
| 221 | if (child != null) { |
| 222 | Rect r = new Rect(); |
| 223 | child.getDrawingRect(r); |
| 224 | requestRectangleOnScreen(r); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void cancelLongPress() { |
| 230 | super.cancelLongPress(); |
| 231 | |
| 232 | // Cancel long press for all children |
| 233 | final int count = getChildCount(); |
| 234 | for (int i = 0; i < count; i++) { |
| 235 | final View child = getChildAt(i); |
| 236 | child.cancelLongPress(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 242 | final int count = getChildCount(); |
| 243 | for (int i = 0; i < count; i++) { |
| 244 | final View view = getChildAt(i); |
| 245 | view.setDrawingCacheEnabled(enabled); |
| 246 | // Update the drawing caches |
Adam Cohen | 8182e5b | 2011-08-01 15:43:31 -0700 | [diff] [blame] | 247 | if (!view.isHardwareAccelerated() && enabled) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 248 | view.buildDrawingCache(true); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 255 | super.setChildrenDrawnWithCacheEnabled(enabled); |
| 256 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 257 | } |