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 | |
| 17 | package com.android.launcher2; |
| 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 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 36 | private int mCellWidth; |
| 37 | private int mCellHeight; |
| 38 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 39 | private int mWidthGap; |
| 40 | private int mHeightGap; |
| 41 | |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 42 | public ShortcutAndWidgetContainer(Context context) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 43 | super(context); |
| 44 | mWallpaperManager = WallpaperManager.getInstance(context); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | public void enableHardwareLayers() { |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 48 | setLayerType(LAYER_TYPE_HARDWARE, null); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 51 | public void setCellDimensions(int cellWidth, int cellHeight, int widthGap, int heightGap ) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 52 | mCellWidth = cellWidth; |
| 53 | mCellHeight = cellHeight; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 54 | mWidthGap = widthGap; |
| 55 | mHeightGap = heightGap; |
| 56 | } |
| 57 | |
| 58 | public View getChildAt(int x, int y) { |
| 59 | final int count = getChildCount(); |
| 60 | for (int i = 0; i < count; i++) { |
| 61 | View child = getChildAt(i); |
| 62 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 63 | |
| 64 | if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) && |
Adam Cohen | f579b50 | 2011-04-19 17:03:08 -0700 | [diff] [blame] | 65 | (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 66 | return child; |
| 67 | } |
| 68 | } |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | @Override |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 73 | protected void dispatchDraw(Canvas canvas) { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame^] | 74 | @SuppressWarnings("all") // suppress dead code warning |
| 75 | final boolean debug = false; |
| 76 | if (debug) { |
| 77 | // Debug drawing for hit space |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 78 | Paint p = new Paint(); |
| 79 | p.setColor(0x6600FF00); |
| 80 | for (int i = getChildCount() - 1; i >= 0; i--) { |
| 81 | final View child = getChildAt(i); |
| 82 | final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 83 | |
| 84 | canvas.drawRect(lp.x, lp.y, lp.x + lp.width, lp.y + lp.height, p); |
| 85 | } |
| 86 | } |
| 87 | super.dispatchDraw(canvas); |
| 88 | } |
| 89 | |
| 90 | @Override |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 91 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 92 | int count = getChildCount(); |
| 93 | for (int i = 0; i < count; i++) { |
| 94 | View child = getChildAt(i); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 95 | measureChild(child); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 96 | } |
| 97 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 98 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 99 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 100 | } |
| 101 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 102 | public void setupLp(CellLayout.LayoutParams lp) { |
| 103 | lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap); |
| 104 | } |
| 105 | |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 106 | public void measureChild(View child) { |
| 107 | final int cellWidth = mCellWidth; |
| 108 | final int cellHeight = mCellHeight; |
| 109 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 110 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 111 | lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 112 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); |
| 113 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, |
| 114 | MeasureSpec.EXACTLY); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 115 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 116 | } |
| 117 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 118 | @Override |
| 119 | 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] | 120 | int count = getChildCount(); |
| 121 | for (int i = 0; i < count; i++) { |
| 122 | final View child = getChildAt(i); |
| 123 | if (child.getVisibility() != GONE) { |
| 124 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 125 | |
| 126 | int childLeft = lp.x; |
| 127 | int childTop = lp.y; |
| 128 | child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); |
| 129 | |
| 130 | if (lp.dropped) { |
| 131 | lp.dropped = false; |
| 132 | |
| 133 | final int[] cellXY = mTmpCellXY; |
| 134 | getLocationOnScreen(cellXY); |
| 135 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), |
| 136 | WallpaperManager.COMMAND_DROP, |
| 137 | cellXY[0] + childLeft + lp.width / 2, |
| 138 | cellXY[1] + childTop + lp.height / 2, 0, null); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | @Override |
Michael Jurka | e6235dd | 2011-10-04 15:02:05 -0700 | [diff] [blame] | 145 | public boolean shouldDelayChildPressedState() { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | @Override |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 150 | public void requestChildFocus(View child, View focused) { |
| 151 | super.requestChildFocus(child, focused); |
| 152 | if (child != null) { |
| 153 | Rect r = new Rect(); |
| 154 | child.getDrawingRect(r); |
| 155 | requestRectangleOnScreen(r); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void cancelLongPress() { |
| 161 | super.cancelLongPress(); |
| 162 | |
| 163 | // Cancel long press for all children |
| 164 | final int count = getChildCount(); |
| 165 | for (int i = 0; i < count; i++) { |
| 166 | final View child = getChildAt(i); |
| 167 | child.cancelLongPress(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 173 | final int count = getChildCount(); |
| 174 | for (int i = 0; i < count; i++) { |
| 175 | final View view = getChildAt(i); |
| 176 | view.setDrawingCacheEnabled(enabled); |
| 177 | // Update the drawing caches |
Adam Cohen | 8182e5b | 2011-08-01 15:43:31 -0700 | [diff] [blame] | 178 | if (!view.isHardwareAccelerated() && enabled) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 179 | view.buildDrawingCache(true); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 186 | super.setChildrenDrawnWithCacheEnabled(enabled); |
| 187 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 188 | } |