blob: eea38f1424994af228370252c453b0937d9e6c85 [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
Adam Cohend4844c32011-02-18 19:25:06 -080037 private int mCellWidth;
38 private int mCellHeight;
39
Michael Jurka8c920dd2011-01-20 14:16:56 -080040 private int mWidthGap;
41 private int mHeightGap;
42
43 public CellLayoutChildren(Context context) {
44 super(context);
45 mWallpaperManager = WallpaperManager.getInstance(context);
Michael Jurkabdb5c532011-02-01 15:05:06 -080046 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurka8c920dd2011-01-20 14:16:56 -080047 }
48
Adam Cohen7f4eabe2011-04-21 16:19:16 -070049 public void setCellDimensions(int cellWidth, int cellHeight, int widthGap, int heightGap ) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080050 mCellWidth = cellWidth;
51 mCellHeight = cellHeight;
Michael Jurka8c920dd2011-01-20 14:16:56 -080052 mWidthGap = widthGap;
53 mHeightGap = heightGap;
54 }
55
56 public View getChildAt(int x, int y) {
57 final int count = getChildCount();
58 for (int i = 0; i < count; i++) {
59 View child = getChildAt(i);
60 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
61
62 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
Adam Cohenf579b502011-04-19 17:03:08 -070063 (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080064 return child;
65 }
66 }
67 return null;
68 }
69
70 @Override
71 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080072 int count = getChildCount();
73 for (int i = 0; i < count; i++) {
74 View child = getChildAt(i);
Adam Cohend5e42732011-03-28 17:33:39 -070075 measureChild(child);
Michael Jurka8c920dd2011-01-20 14:16:56 -080076 }
77 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
78 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
79 setMeasuredDimension(widthSpecSize, heightSpecSize);
80 }
81
Adam Cohend5e42732011-03-28 17:33:39 -070082 public void measureChild(View child) {
83 final int cellWidth = mCellWidth;
84 final int cellHeight = mCellHeight;
85 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
86
Adam Cohen7f4eabe2011-04-21 16:19:16 -070087 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap);
Adam Cohend5e42732011-03-28 17:33:39 -070088 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
89 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
90 MeasureSpec.EXACTLY);
91
92 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
93 }
94
Michael Jurka8c920dd2011-01-20 14:16:56 -080095 @Override
96 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080097 int count = getChildCount();
98 for (int i = 0; i < count; i++) {
99 final View child = getChildAt(i);
100 if (child.getVisibility() != GONE) {
101 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
102
103 int childLeft = lp.x;
104 int childTop = lp.y;
105 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
106
107 if (lp.dropped) {
108 lp.dropped = false;
109
110 final int[] cellXY = mTmpCellXY;
111 getLocationOnScreen(cellXY);
112 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
113 WallpaperManager.COMMAND_DROP,
114 cellXY[0] + childLeft + lp.width / 2,
115 cellXY[1] + childTop + lp.height / 2, 0, null);
116
117 if (lp.animateDrop) {
118 lp.animateDrop = false;
119
120 // This call does not result in a requestLayout(), but at one point did.
121 // We need to be cautious about any method calls within the layout pass
122 // to insure we don't leave the view tree in a bad state.
123 ((Workspace) mParent.getParent()).animateViewIntoPosition(child);
124 }
125 }
126 }
127 }
128 }
129
130 @Override
131 public void requestChildFocus(View child, View focused) {
132 super.requestChildFocus(child, focused);
133 if (child != null) {
134 Rect r = new Rect();
135 child.getDrawingRect(r);
136 requestRectangleOnScreen(r);
137 }
138 }
139
140 @Override
141 public void cancelLongPress() {
142 super.cancelLongPress();
143
144 // Cancel long press for all children
145 final int count = getChildCount();
146 for (int i = 0; i < count; i++) {
147 final View child = getChildAt(i);
148 child.cancelLongPress();
149 }
150 }
151
152 @Override
153 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
154 final int count = getChildCount();
155 for (int i = 0; i < count; i++) {
156 final View view = getChildAt(i);
157 view.setDrawingCacheEnabled(enabled);
158 // Update the drawing caches
159 if (!view.isHardwareAccelerated()) {
160 view.buildDrawingCache(true);
161 }
162 }
163 }
164
165 @Override
166 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
167 super.setChildrenDrawnWithCacheEnabled(enabled);
168 }
Adam Cohend4844c32011-02-18 19:25:06 -0800169}