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