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