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); |
Winson Chung | be62afa | 2011-02-03 23:14:57 -0800 | [diff] [blame] | 50 | |
| 51 | // Disable multitouch for the workspace |
| 52 | setMotionEventSplittingEnabled(false); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public void setCellDimensions(int cellWidth, int cellHeight, |
| 56 | int leftPadding, int topPadding, int widthGap, int heightGap ) { |
| 57 | mCellWidth = cellWidth; |
| 58 | mCellHeight = cellHeight; |
| 59 | mLeftPadding = leftPadding; |
| 60 | mTopPadding = topPadding; |
| 61 | mWidthGap = widthGap; |
| 62 | mHeightGap = heightGap; |
| 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) && |
| 72 | (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) { |
| 73 | return child; |
| 74 | } |
| 75 | } |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 81 | final int cellWidth = mCellWidth; |
| 82 | final int cellHeight = mCellHeight; |
| 83 | int count = getChildCount(); |
| 84 | for (int i = 0; i < count; i++) { |
| 85 | View child = getChildAt(i); |
| 86 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 87 | |
| 88 | lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, |
| 89 | mLeftPadding, mTopPadding); |
| 90 | |
| 91 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); |
| 92 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, |
| 93 | MeasureSpec.EXACTLY); |
| 94 | |
| 95 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 96 | } |
| 97 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 98 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 99 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | 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] | 104 | int count = getChildCount(); |
| 105 | for (int i = 0; i < count; i++) { |
| 106 | final View child = getChildAt(i); |
| 107 | if (child.getVisibility() != GONE) { |
| 108 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); |
| 109 | |
| 110 | int childLeft = lp.x; |
| 111 | int childTop = lp.y; |
| 112 | child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); |
| 113 | |
| 114 | if (lp.dropped) { |
| 115 | lp.dropped = false; |
| 116 | |
| 117 | final int[] cellXY = mTmpCellXY; |
| 118 | getLocationOnScreen(cellXY); |
| 119 | mWallpaperManager.sendWallpaperCommand(getWindowToken(), |
| 120 | WallpaperManager.COMMAND_DROP, |
| 121 | cellXY[0] + childLeft + lp.width / 2, |
| 122 | cellXY[1] + childTop + lp.height / 2, 0, null); |
| 123 | |
| 124 | if (lp.animateDrop) { |
| 125 | lp.animateDrop = false; |
| 126 | |
| 127 | // This call does not result in a requestLayout(), but at one point did. |
| 128 | // We need to be cautious about any method calls within the layout pass |
| 129 | // to insure we don't leave the view tree in a bad state. |
| 130 | ((Workspace) mParent.getParent()).animateViewIntoPosition(child); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void requestChildFocus(View child, View focused) { |
| 139 | super.requestChildFocus(child, focused); |
| 140 | if (child != null) { |
| 141 | Rect r = new Rect(); |
| 142 | child.getDrawingRect(r); |
| 143 | requestRectangleOnScreen(r); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void cancelLongPress() { |
| 149 | super.cancelLongPress(); |
| 150 | |
| 151 | // Cancel long press for all children |
| 152 | final int count = getChildCount(); |
| 153 | for (int i = 0; i < count; i++) { |
| 154 | final View child = getChildAt(i); |
| 155 | child.cancelLongPress(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 161 | final int count = getChildCount(); |
| 162 | for (int i = 0; i < count; i++) { |
| 163 | final View view = getChildAt(i); |
| 164 | view.setDrawingCacheEnabled(enabled); |
| 165 | // Update the drawing caches |
| 166 | if (!view.isHardwareAccelerated()) { |
| 167 | view.buildDrawingCache(true); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 174 | super.setChildrenDrawnWithCacheEnabled(enabled); |
| 175 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame^] | 176 | |
| 177 | private final ArrayList<AppWidgetResizeFrame> mResizeFrames = new ArrayList<AppWidgetResizeFrame>(); |
| 178 | private AppWidgetResizeFrame mCurrentResizeFrame; |
| 179 | private int mXDown, mYDown; |
| 180 | private boolean mIsWidgetBeingResized; |
| 181 | |
| 182 | public void clearAllResizeFrames() { |
| 183 | for (AppWidgetResizeFrame frame: mResizeFrames) { |
| 184 | removeView(frame); |
| 185 | } |
| 186 | mResizeFrames.clear(); |
| 187 | } |
| 188 | |
| 189 | public boolean isWidgetBeingResized() { |
| 190 | return mIsWidgetBeingResized; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 195 | Rect hitRect = new Rect(); |
| 196 | |
| 197 | int x = (int) ev.getX(); |
| 198 | int y = (int) ev.getY(); |
| 199 | |
| 200 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 201 | for (AppWidgetResizeFrame child: mResizeFrames) { |
| 202 | child.getHitRect(hitRect); |
| 203 | if (hitRect.contains(x, y)) { |
| 204 | if (child.beginResizeIfPointInRegion(x - child.getLeft(), y - child.getTop())) { |
| 205 | mCurrentResizeFrame = child; |
| 206 | mIsWidgetBeingResized = true; |
| 207 | mXDown = x; |
| 208 | mYDown = y; |
| 209 | requestDisallowInterceptTouchEvent(true); |
| 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | mCurrentResizeFrame = null; |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public boolean onTouchEvent(MotionEvent ev) { |
| 221 | boolean handled = false; |
| 222 | Rect hitRect = new Rect(); |
| 223 | int action = ev.getAction(); |
| 224 | |
| 225 | int x = (int) ev.getX(); |
| 226 | int y = (int) ev.getY(); |
| 227 | |
| 228 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 229 | for (AppWidgetResizeFrame child: mResizeFrames) { |
| 230 | child.getHitRect(hitRect); |
| 231 | if (hitRect.contains(x, y)) { |
| 232 | if (child.beginResizeIfPointInRegion(x - child.getLeft(), y - child.getTop())) { |
| 233 | mCurrentResizeFrame = child; |
| 234 | mIsWidgetBeingResized = true; |
| 235 | mXDown = x; |
| 236 | mYDown = y; |
| 237 | requestDisallowInterceptTouchEvent(true); |
| 238 | return true; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | mCurrentResizeFrame = null; |
| 243 | } |
| 244 | |
| 245 | // TODO: Need to handle ACTION_POINTER_UP / multi-touch |
| 246 | if (mCurrentResizeFrame != null) { |
| 247 | switch (action) { |
| 248 | case MotionEvent.ACTION_MOVE: |
| 249 | mCurrentResizeFrame.visualizeResizeForDelta(x - mXDown, y - mYDown); |
| 250 | break; |
| 251 | case MotionEvent.ACTION_CANCEL: |
| 252 | case MotionEvent.ACTION_UP: { |
| 253 | mCurrentResizeFrame.commitResizeForDelta(x - mXDown, y - mYDown); |
| 254 | mIsWidgetBeingResized = false; |
| 255 | handled = true; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | return handled; |
| 260 | } |
| 261 | |
| 262 | public void addResizeFrame(ItemInfo itemInfo, LauncherAppWidgetHostView widget, CellLayout cellLayout) { |
| 263 | AppWidgetResizeFrame resizeFrame = new AppWidgetResizeFrame(getContext(), |
| 264 | itemInfo, widget, cellLayout); |
| 265 | |
| 266 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(-1, -1, -1, -1); |
| 267 | lp.isLockedToGrid = false; |
| 268 | |
| 269 | addView(resizeFrame, lp); |
| 270 | mResizeFrames.add(resizeFrame); |
| 271 | |
| 272 | resizeFrame.snapToWidget(false); |
| 273 | } |
| 274 | } |