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; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 21 | import android.graphics.Rect; |
| 22 | import android.view.View; |
Michael Jurka | cf6125c | 2011-01-28 15:20:01 -0800 | [diff] [blame] | 23 | import android.view.ViewGroup; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 24 | |
Michael Jurka | cf6125c | 2011-01-28 15:20:01 -0800 | [diff] [blame] | 25 | public class CellLayoutChildren extends ViewGroup { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 26 | static final String TAG = "CellLayoutChildren"; |
| 27 | |
| 28 | // These are temporary variables to prevent having to allocate a new object just to |
| 29 | // return an (x, y) value from helper functions. Do NOT use them to maintain other state. |
| 30 | private final int[] mTmpCellXY = new int[2]; |
| 31 | |
| 32 | private final WallpaperManager mWallpaperManager; |
| 33 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 34 | private int mCellWidth; |
| 35 | private int mCellHeight; |
| 36 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 37 | private int mWidthGap; |
| 38 | private int mHeightGap; |
| 39 | |
| 40 | public CellLayoutChildren(Context context) { |
| 41 | super(context); |
| 42 | mWallpaperManager = WallpaperManager.getInstance(context); |
Michael Jurka | bdb5c53 | 2011-02-01 15:05:06 -0800 | [diff] [blame] | 43 | setLayerType(LAYER_TYPE_HARDWARE, null); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 46 | public void setCellDimensions(int cellWidth, int cellHeight, int widthGap, int heightGap ) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 47 | mCellWidth = cellWidth; |
| 48 | mCellHeight = cellHeight; |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 49 | mWidthGap = widthGap; |
| 50 | mHeightGap = heightGap; |
| 51 | } |
| 52 | |
| 53 | public View getChildAt(int x, int y) { |
| 54 | final int count = getChildCount(); |
| 55 | for (int i = 0; i < count; i++) { |
| 56 | View child = getChildAt(i); |
| 57 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 58 | |
| 59 | if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) && |
Adam Cohen | f579b50 | 2011-04-19 17:03:08 -0700 | [diff] [blame] | 60 | (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 61 | return child; |
| 62 | } |
| 63 | } |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 69 | int count = getChildCount(); |
| 70 | for (int i = 0; i < count; i++) { |
| 71 | View child = getChildAt(i); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 72 | measureChild(child); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 73 | } |
| 74 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 75 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 76 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 77 | } |
| 78 | |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 79 | public void measureChild(View child) { |
| 80 | final int cellWidth = mCellWidth; |
| 81 | final int cellHeight = mCellHeight; |
| 82 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 83 | |
Adam Cohen | 7f4eabe | 2011-04-21 16:19:16 -0700 | [diff] [blame] | 84 | lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap); |
Adam Cohen | d5e4273 | 2011-03-28 17:33:39 -0700 | [diff] [blame] | 85 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); |
| 86 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, |
| 87 | MeasureSpec.EXACTLY); |
| 88 | |
| 89 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 90 | } |
| 91 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 92 | @Override |
| 93 | 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] | 94 | int count = getChildCount(); |
| 95 | for (int i = 0; i < count; i++) { |
| 96 | final View child = getChildAt(i); |
| 97 | if (child.getVisibility() != GONE) { |
| 98 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 99 | |
| 100 | int childLeft = lp.x; |
| 101 | int childTop = lp.y; |
| 102 | child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); |
| 103 | |
| 104 | if (lp.dropped) { |
| 105 | lp.dropped = false; |
| 106 | |
| 107 | final int[] cellXY = mTmpCellXY; |
| 108 | getLocationOnScreen(cellXY); |
| 109 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), |
| 110 | WallpaperManager.COMMAND_DROP, |
| 111 | cellXY[0] + childLeft + lp.width / 2, |
| 112 | cellXY[1] + childTop + lp.height / 2, 0, null); |
| 113 | |
| 114 | if (lp.animateDrop) { |
| 115 | lp.animateDrop = false; |
| 116 | |
| 117 | // This call does not result in a requestLayout(), but at one point did. |
| 118 | // We need to be cautious about any method calls within the layout pass |
| 119 | // to insure we don't leave the view tree in a bad state. |
| 120 | ((Workspace) mParent.getParent()).animateViewIntoPosition(child); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void requestChildFocus(View child, View focused) { |
| 129 | super.requestChildFocus(child, focused); |
| 130 | if (child != null) { |
| 131 | Rect r = new Rect(); |
| 132 | child.getDrawingRect(r); |
| 133 | requestRectangleOnScreen(r); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void cancelLongPress() { |
| 139 | super.cancelLongPress(); |
| 140 | |
| 141 | // Cancel long press for all children |
| 142 | final int count = getChildCount(); |
| 143 | for (int i = 0; i < count; i++) { |
| 144 | final View child = getChildAt(i); |
| 145 | child.cancelLongPress(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 151 | final int count = getChildCount(); |
| 152 | for (int i = 0; i < count; i++) { |
| 153 | final View view = getChildAt(i); |
| 154 | view.setDrawingCacheEnabled(enabled); |
| 155 | // Update the drawing caches |
| 156 | if (!view.isHardwareAccelerated()) { |
| 157 | view.buildDrawingCache(true); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 164 | super.setChildrenDrawnWithCacheEnabled(enabled); |
| 165 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 166 | } |