blob: fcd6f19ae83d2868cc0b4142ea936684a203e5e3 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Michael Jurka8c920dd2011-01-20 14:16:56 -080018
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
Winson Chung5f8afe62013-08-12 16:19:28 -070036 private boolean mIsHotseatLayout;
37
Adam Cohend4844c32011-02-18 19:25:06 -080038 private int mCellWidth;
39 private int mCellHeight;
40
Michael Jurka8c920dd2011-01-20 14:16:56 -080041 private int mWidthGap;
42 private int mHeightGap;
43
Adam Cohen2374abf2013-04-16 14:56:57 -070044 private int mCountX;
Winson Chung5f8afe62013-08-12 16:19:28 -070045 private int mCountY;
Adam Cohen2374abf2013-04-16 14:56:57 -070046
47 private boolean mInvertIfRtl = false;
48
Michael Jurkaa52570f2012-03-20 03:18:20 -070049 public ShortcutAndWidgetContainer(Context context) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080050 super(context);
51 mWallpaperManager = WallpaperManager.getInstance(context);
Adam Cohen2801caf2011-05-13 20:57:39 -070052 }
53
Adam Cohen2374abf2013-04-16 14:56:57 -070054 public void setCellDimensions(int cellWidth, int cellHeight, int widthGap, int heightGap,
Winson Chung5f8afe62013-08-12 16:19:28 -070055 int countX, int countY) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080056 mCellWidth = cellWidth;
57 mCellHeight = cellHeight;
Michael Jurka8c920dd2011-01-20 14:16:56 -080058 mWidthGap = widthGap;
59 mHeightGap = heightGap;
Adam Cohen2374abf2013-04-16 14:56:57 -070060 mCountX = countX;
Winson Chung5f8afe62013-08-12 16:19:28 -070061 mCountY = countY;
Michael Jurka8c920dd2011-01-20 14:16:56 -080062 }
63
64 public View getChildAt(int x, int y) {
65 final int count = getChildCount();
66 for (int i = 0; i < count; i++) {
67 View child = getChildAt(i);
68 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
69
70 if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
Adam Cohenf579b502011-04-19 17:03:08 -070071 (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080072 return child;
73 }
74 }
75 return null;
76 }
77
78 @Override
Winson Chungeecf02d2012-03-02 17:14:58 -080079 protected void dispatchDraw(Canvas canvas) {
Michael Jurka3a9fced2012-04-13 14:44:29 -070080 @SuppressWarnings("all") // suppress dead code warning
81 final boolean debug = false;
82 if (debug) {
83 // Debug drawing for hit space
Winson Chungeecf02d2012-03-02 17:14:58 -080084 Paint p = new Paint();
85 p.setColor(0x6600FF00);
86 for (int i = getChildCount() - 1; i >= 0; i--) {
87 final View child = getChildAt(i);
88 final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
89
90 canvas.drawRect(lp.x, lp.y, lp.x + lp.width, lp.y + lp.height, p);
91 }
92 }
93 super.dispatchDraw(canvas);
94 }
95
96 @Override
Michael Jurka8c920dd2011-01-20 14:16:56 -080097 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Michael Jurka8c920dd2011-01-20 14:16:56 -080098 int count = getChildCount();
Adam Cohenec40b2b2013-07-23 15:52:40 -070099
100 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
101 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
102 setMeasuredDimension(widthSpecSize, heightSpecSize);
103
Michael Jurka8c920dd2011-01-20 14:16:56 -0800104 for (int i = 0; i < count; i++) {
105 View child = getChildAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100106 if (child.getVisibility() != GONE) {
107 measureChild(child);
108 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800109 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800110 }
111
Adam Cohenbfbfd262011-06-13 16:55:12 -0700112 public void setupLp(CellLayout.LayoutParams lp) {
Adam Cohen2374abf2013-04-16 14:56:57 -0700113 lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap, invertLayoutHorizontally(),
114 mCountX);
115 }
116
117 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
118 public void setInvertIfRtl(boolean invert) {
119 mInvertIfRtl = invert;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700120 }
121
Winson Chung5f8afe62013-08-12 16:19:28 -0700122 public void setIsHotseat(boolean isHotseat) {
123 mIsHotseatLayout = isHotseat;
124 }
125
Adam Cohend5e42732011-03-28 17:33:39 -0700126 public void measureChild(View child) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700127 final LauncherAppState app = LauncherAppState.getInstance();
128 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohend5e42732011-03-28 17:33:39 -0700129 final int cellWidth = mCellWidth;
130 final int cellHeight = mCellHeight;
131 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
Adam Cohenec40b2b2013-07-23 15:52:40 -0700132 if (!lp.isFullscreen) {
133 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, invertLayoutHorizontally(),
134 mCountX);
Winson Chung5f8afe62013-08-12 16:19:28 -0700135
136 if (child instanceof LauncherAppWidgetHostView) {
137 // Widgets have their own padding, so skip
138 } else {
139 // Otherwise, center the icon
140 int cHeight = mIsHotseatLayout ? grid.hotseatCellHeightPx : Math.min(getMeasuredHeight(), grid.cellHeightPx);
141 int cellPaddingY = (int) Math.max(0, ((lp.height - cHeight) / 2f));
Winson Chungcdef0442013-09-19 17:32:58 -0700142 int cellPaddingX = (int) (grid.edgeMarginPx / 2f);
143 child.setPadding(cellPaddingX, cellPaddingY, cellPaddingX, 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700144 }
Adam Cohenec40b2b2013-07-23 15:52:40 -0700145 } else {
146 lp.x = 0;
147 lp.y = 0;
148 lp.width = getMeasuredWidth();
149 lp.height = getMeasuredHeight();
150 }
Adam Cohend5e42732011-03-28 17:33:39 -0700151 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
152 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
153 MeasureSpec.EXACTLY);
Adam Cohend5e42732011-03-28 17:33:39 -0700154 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
155 }
156
Adam Cohen2374abf2013-04-16 14:56:57 -0700157 private boolean invertLayoutHorizontally() {
158 return mInvertIfRtl && isLayoutRtl();
159 }
160
161 public boolean isLayoutRtl() {
162 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
163 }
164
Michael Jurka8c920dd2011-01-20 14:16:56 -0800165 @Override
166 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800167 int count = getChildCount();
168 for (int i = 0; i < count; i++) {
169 final View child = getChildAt(i);
170 if (child.getVisibility() != GONE) {
171 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800172 int childLeft = lp.x;
173 int childTop = lp.y;
174 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
175
176 if (lp.dropped) {
177 lp.dropped = false;
178
179 final int[] cellXY = mTmpCellXY;
180 getLocationOnScreen(cellXY);
181 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
182 WallpaperManager.COMMAND_DROP,
183 cellXY[0] + childLeft + lp.width / 2,
184 cellXY[1] + childTop + lp.height / 2, 0, null);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800185 }
186 }
187 }
188 }
189
190 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700191 public boolean shouldDelayChildPressedState() {
192 return false;
193 }
194
195 @Override
Michael Jurka8c920dd2011-01-20 14:16:56 -0800196 public void requestChildFocus(View child, View focused) {
197 super.requestChildFocus(child, focused);
198 if (child != null) {
199 Rect r = new Rect();
200 child.getDrawingRect(r);
201 requestRectangleOnScreen(r);
202 }
203 }
204
205 @Override
206 public void cancelLongPress() {
207 super.cancelLongPress();
208
209 // Cancel long press for all children
210 final int count = getChildCount();
211 for (int i = 0; i < count; i++) {
212 final View child = getChildAt(i);
213 child.cancelLongPress();
214 }
215 }
216
217 @Override
218 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
219 final int count = getChildCount();
220 for (int i = 0; i < count; i++) {
221 final View view = getChildAt(i);
222 view.setDrawingCacheEnabled(enabled);
223 // Update the drawing caches
Adam Cohen8182e5b2011-08-01 15:43:31 -0700224 if (!view.isHardwareAccelerated() && enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800225 view.buildDrawingCache(true);
226 }
227 }
228 }
229
230 @Override
231 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
232 super.setChildrenDrawnWithCacheEnabled(enabled);
233 }
Adam Cohend4844c32011-02-18 19:25:06 -0800234}