blob: 8bebdcd457a0c9943bc6e92669102aef0dd050f9 [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;
Winson Chungeecf02d2012-03-02 17:14:58 -080021import android.graphics.Canvas;
22import android.graphics.Paint;
Michael Jurka8c920dd2011-01-20 14:16:56 -080023import android.graphics.Rect;
24import android.view.View;
Michael Jurkacf6125c2011-01-28 15:20:01 -080025import android.view.ViewGroup;
Michael Jurka8c920dd2011-01-20 14:16:56 -080026
Michael Jurkaa52570f2012-03-20 03:18:20 -070027public class ShortcutAndWidgetContainer extends ViewGroup {
Michael Jurka8c920dd2011-01-20 14:16:56 -080028 static final String TAG = "CellLayoutChildren";
29
30 // These are temporary variables to prevent having to allocate a new object just to
31 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
32 private final int[] mTmpCellXY = new int[2];
33
34 private final WallpaperManager mWallpaperManager;
35
Adam Cohend4844c32011-02-18 19:25:06 -080036 private int mCellWidth;
37 private int mCellHeight;
38
Michael Jurka8c920dd2011-01-20 14:16:56 -080039 private int mWidthGap;
40 private int mHeightGap;
41
Michael Jurkaa52570f2012-03-20 03:18:20 -070042 public ShortcutAndWidgetContainer(Context context) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080043 super(context);
44 mWallpaperManager = WallpaperManager.getInstance(context);
Adam Cohen2801caf2011-05-13 20:57:39 -070045 }
46
47 public void enableHardwareLayers() {
Michael Jurkabdb5c532011-02-01 15:05:06 -080048 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurka8c920dd2011-01-20 14:16:56 -080049 }
50
Adam Cohen7f4eabe2011-04-21 16:19:16 -070051 public void setCellDimensions(int cellWidth, int cellHeight, int widthGap, int heightGap ) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080052 mCellWidth = cellWidth;
53 mCellHeight = cellHeight;
Michael Jurka8c920dd2011-01-20 14:16:56 -080054 mWidthGap = widthGap;
55 mHeightGap = heightGap;
56 }
57
58 public View getChildAt(int x, int y) {
59 final int count = getChildCount();
60 for (int i = 0; i < count; i++) {
61 View child = getChildAt(i);
62 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
63
64 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
Adam Cohenf579b502011-04-19 17:03:08 -070065 (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080066 return child;
67 }
68 }
69 return null;
70 }
71
72 @Override
Winson Chungeecf02d2012-03-02 17:14:58 -080073 protected void dispatchDraw(Canvas canvas) {
Michael Jurka3a9fced2012-04-13 14:44:29 -070074 @SuppressWarnings("all") // suppress dead code warning
75 final boolean debug = false;
76 if (debug) {
77 // Debug drawing for hit space
Winson Chungeecf02d2012-03-02 17:14:58 -080078 Paint p = new Paint();
79 p.setColor(0x6600FF00);
80 for (int i = getChildCount() - 1; i >= 0; i--) {
81 final View child = getChildAt(i);
82 final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
83
84 canvas.drawRect(lp.x, lp.y, lp.x + lp.width, lp.y + lp.height, p);
85 }
86 }
87 super.dispatchDraw(canvas);
88 }
89
90 @Override
Michael Jurka8c920dd2011-01-20 14:16:56 -080091 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080092 int count = getChildCount();
93 for (int i = 0; i < count; i++) {
94 View child = getChildAt(i);
Adam Cohend5e42732011-03-28 17:33:39 -070095 measureChild(child);
Michael Jurka8c920dd2011-01-20 14:16:56 -080096 }
97 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
98 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
99 setMeasuredDimension(widthSpecSize, heightSpecSize);
100 }
101
Adam Cohenbfbfd262011-06-13 16:55:12 -0700102 public void setupLp(CellLayout.LayoutParams lp) {
103 lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
104 }
105
Adam Cohend5e42732011-03-28 17:33:39 -0700106 public void measureChild(View child) {
107 final int cellWidth = mCellWidth;
108 final int cellHeight = mCellHeight;
109 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
110
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700111 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap);
Adam Cohend5e42732011-03-28 17:33:39 -0700112 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
113 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
114 MeasureSpec.EXACTLY);
Adam Cohend5e42732011-03-28 17:33:39 -0700115 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
116 }
117
Michael Jurka8c920dd2011-01-20 14:16:56 -0800118 @Override
119 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800120 int count = getChildCount();
121 for (int i = 0; i < count; i++) {
122 final View child = getChildAt(i);
123 if (child.getVisibility() != GONE) {
124 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
125
126 int childLeft = lp.x;
127 int childTop = lp.y;
128 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
129
130 if (lp.dropped) {
131 lp.dropped = false;
132
133 final int[] cellXY = mTmpCellXY;
134 getLocationOnScreen(cellXY);
135 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
136 WallpaperManager.COMMAND_DROP,
137 cellXY[0] + childLeft + lp.width / 2,
138 cellXY[1] + childTop + lp.height / 2, 0, null);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800139 }
140 }
141 }
142 }
143
144 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700145 public boolean shouldDelayChildPressedState() {
146 return false;
147 }
148
149 @Override
Michael Jurka8c920dd2011-01-20 14:16:56 -0800150 public void requestChildFocus(View child, View focused) {
151 super.requestChildFocus(child, focused);
152 if (child != null) {
153 Rect r = new Rect();
154 child.getDrawingRect(r);
155 requestRectangleOnScreen(r);
156 }
157 }
158
159 @Override
160 public void cancelLongPress() {
161 super.cancelLongPress();
162
163 // Cancel long press for all children
164 final int count = getChildCount();
165 for (int i = 0; i < count; i++) {
166 final View child = getChildAt(i);
167 child.cancelLongPress();
168 }
169 }
170
171 @Override
172 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
173 final int count = getChildCount();
174 for (int i = 0; i < count; i++) {
175 final View view = getChildAt(i);
176 view.setDrawingCacheEnabled(enabled);
177 // Update the drawing caches
Adam Cohen8182e5b2011-08-01 15:43:31 -0700178 if (!view.isHardwareAccelerated() && enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800179 view.buildDrawingCache(true);
180 }
181 }
182 }
183
184 @Override
185 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
186 super.setChildrenDrawnWithCacheEnabled(enabled);
187 }
Adam Cohend4844c32011-02-18 19:25:06 -0800188}