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