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