blob: ed814199cbd2cc4183802fc19f9162212eb5fa75 [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
Adam Cohend4844c32011-02-18 19:25:06 -080019import java.util.ArrayList;
20
Michael Jurka8c920dd2011-01-20 14:16:56 -080021import android.app.WallpaperManager;
22import android.content.Context;
Michael Jurka8c920dd2011-01-20 14:16:56 -080023import android.graphics.Rect;
Adam Cohend4844c32011-02-18 19:25:06 -080024import android.view.MotionEvent;
Michael Jurka8c920dd2011-01-20 14:16:56 -080025import android.view.View;
Michael Jurkacf6125c2011-01-28 15:20:01 -080026import android.view.ViewGroup;
Michael Jurka8c920dd2011-01-20 14:16:56 -080027
Michael Jurkacf6125c2011-01-28 15:20:01 -080028public class CellLayoutChildren extends ViewGroup {
Michael Jurka8c920dd2011-01-20 14:16:56 -080029 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 Jurka8c920dd2011-01-20 14:16:56 -080037 private int mLeftPadding;
38 private int mTopPadding;
39
Adam Cohend4844c32011-02-18 19:25:06 -080040 private int mCellWidth;
41 private int mCellHeight;
42
Michael Jurka8c920dd2011-01-20 14:16:56 -080043 private int mWidthGap;
44 private int mHeightGap;
45
46 public CellLayoutChildren(Context context) {
47 super(context);
48 mWallpaperManager = WallpaperManager.getInstance(context);
Michael Jurkabdb5c532011-02-01 15:05:06 -080049 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurka8c920dd2011-01-20 14:16:56 -080050 }
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 Cohenf579b502011-04-19 17:03:08 -070069 (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080070 return child;
71 }
72 }
73 return null;
74 }
75
76 @Override
77 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080078 int count = getChildCount();
79 for (int i = 0; i < count; i++) {
80 View child = getChildAt(i);
Adam Cohend5e42732011-03-28 17:33:39 -070081 measureChild(child);
Michael Jurka8c920dd2011-01-20 14:16:56 -080082 }
83 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
84 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
85 setMeasuredDimension(widthSpecSize, heightSpecSize);
86 }
87
Adam Cohend5e42732011-03-28 17:33:39 -070088 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 Jurka8c920dd2011-01-20 14:16:56 -0800103 @Override
104 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800105 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 Cohend4844c32011-02-18 19:25:06 -0800177}