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