blob: 11b2f9e70019fec0a33ebc298bb7dddb3901d0c3 [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) &&
69 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
70 return child;
71 }
72 }
73 return null;
74 }
75
76 @Override
77 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
78 final int cellWidth = mCellWidth;
79 final int cellHeight = mCellHeight;
80 int count = getChildCount();
81 for (int i = 0; i < count; i++) {
82 View child = getChildAt(i);
83 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
84
85 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
86 mLeftPadding, mTopPadding);
87
88 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 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
95 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
96 setMeasuredDimension(widthSpecSize, heightSpecSize);
97 }
98
99 @Override
100 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800101 int count = getChildCount();
102 for (int i = 0; i < count; i++) {
103 final View child = getChildAt(i);
104 if (child.getVisibility() != GONE) {
105 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
106
107 int childLeft = lp.x;
108 int childTop = lp.y;
109 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
110
111 if (lp.dropped) {
112 lp.dropped = false;
113
114 final int[] cellXY = mTmpCellXY;
115 getLocationOnScreen(cellXY);
116 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
117 WallpaperManager.COMMAND_DROP,
118 cellXY[0] + childLeft + lp.width / 2,
119 cellXY[1] + childTop + lp.height / 2, 0, null);
120
121 if (lp.animateDrop) {
122 lp.animateDrop = false;
123
124 // This call does not result in a requestLayout(), but at one point did.
125 // We need to be cautious about any method calls within the layout pass
126 // to insure we don't leave the view tree in a bad state.
127 ((Workspace) mParent.getParent()).animateViewIntoPosition(child);
128 }
129 }
130 }
131 }
132 }
133
134 @Override
135 public void requestChildFocus(View child, View focused) {
136 super.requestChildFocus(child, focused);
137 if (child != null) {
138 Rect r = new Rect();
139 child.getDrawingRect(r);
140 requestRectangleOnScreen(r);
141 }
142 }
143
144 @Override
145 public void cancelLongPress() {
146 super.cancelLongPress();
147
148 // Cancel long press for all children
149 final int count = getChildCount();
150 for (int i = 0; i < count; i++) {
151 final View child = getChildAt(i);
152 child.cancelLongPress();
153 }
154 }
155
156 @Override
157 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
158 final int count = getChildCount();
159 for (int i = 0; i < count; i++) {
160 final View view = getChildAt(i);
161 view.setDrawingCacheEnabled(enabled);
162 // Update the drawing caches
163 if (!view.isHardwareAccelerated()) {
164 view.buildDrawingCache(true);
165 }
166 }
167 }
168
169 @Override
170 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
171 super.setChildrenDrawnWithCacheEnabled(enabled);
172 }
Adam Cohend4844c32011-02-18 19:25:06 -0800173}