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