blob: a6f7f42143c5b48d09a5fb3ef7a5cee1e3846101 [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;
24import android.view.View.MeasureSpec;
Michael Jurka8c920dd2011-01-20 14:16:56 -080025
Michael Jurkacf6125c2011-01-28 15:20:01 -080026public class CellLayoutChildren extends ViewGroup {
Michael Jurka8c920dd2011-01-20 14:16:56 -080027 static final String TAG = "CellLayoutChildren";
28
29 // These are temporary variables to prevent having to allocate a new object just to
30 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
31 private final int[] mTmpCellXY = new int[2];
32
33 private final WallpaperManager mWallpaperManager;
34
35 private int mCellWidth;
36 private int mCellHeight;
37
38 private int mLeftPadding;
39 private int mTopPadding;
40
41 private int mWidthGap;
42 private int mHeightGap;
43
44 public CellLayoutChildren(Context context) {
45 super(context);
46 mWallpaperManager = WallpaperManager.getInstance(context);
47 }
48
49 public void setCellDimensions(int cellWidth, int cellHeight,
50 int leftPadding, int topPadding, int widthGap, int heightGap ) {
51 mCellWidth = cellWidth;
52 mCellHeight = cellHeight;
53 mLeftPadding = leftPadding;
54 mTopPadding = topPadding;
55 mWidthGap = widthGap;
56 mHeightGap = heightGap;
57 }
58
59 public View getChildAt(int x, int y) {
60 final int count = getChildCount();
61 for (int i = 0; i < count; i++) {
62 View child = getChildAt(i);
63 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
64
65 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
66 (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
67 return child;
68 }
69 }
70 return null;
71 }
72
73 @Override
74 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
75 final int cellWidth = mCellWidth;
76 final int cellHeight = mCellHeight;
77 int count = getChildCount();
78 for (int i = 0; i < count; i++) {
79 View child = getChildAt(i);
80 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
81
82 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
83 mLeftPadding, mTopPadding);
84
85 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 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
92 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
93 setMeasuredDimension(widthSpecSize, heightSpecSize);
94 }
95
96 @Override
97 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080098 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}