blob: 1cf4c11b62568d28b2d0440df5c54d6d6e6fa085 [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
Winson Chung69737c32013-10-08 17:00:19 -0700126 int getCellContentHeight() {
127 final LauncherAppState app = LauncherAppState.getInstance();
128 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
129 return Math.min(getMeasuredHeight(), mIsHotseatLayout ?
130 grid.hotseatCellHeightPx : grid.cellHeightPx);
131 }
132
Adam Cohend5e42732011-03-28 17:33:39 -0700133 public void measureChild(View child) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700134 final LauncherAppState app = LauncherAppState.getInstance();
135 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohend5e42732011-03-28 17:33:39 -0700136 final int cellWidth = mCellWidth;
137 final int cellHeight = mCellHeight;
138 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
Adam Cohenec40b2b2013-07-23 15:52:40 -0700139 if (!lp.isFullscreen) {
140 lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, invertLayoutHorizontally(),
141 mCountX);
Winson Chung5f8afe62013-08-12 16:19:28 -0700142
143 if (child instanceof LauncherAppWidgetHostView) {
144 // Widgets have their own padding, so skip
145 } else {
146 // Otherwise, center the icon
Winson Chung69737c32013-10-08 17:00:19 -0700147 int cHeight = getCellContentHeight();
Winson Chung5f8afe62013-08-12 16:19:28 -0700148 int cellPaddingY = (int) Math.max(0, ((lp.height - cHeight) / 2f));
Winson Chungcdef0442013-09-19 17:32:58 -0700149 int cellPaddingX = (int) (grid.edgeMarginPx / 2f);
150 child.setPadding(cellPaddingX, cellPaddingY, cellPaddingX, 0);
Winson Chung5f8afe62013-08-12 16:19:28 -0700151 }
Adam Cohenec40b2b2013-07-23 15:52:40 -0700152 } else {
153 lp.x = 0;
154 lp.y = 0;
155 lp.width = getMeasuredWidth();
156 lp.height = getMeasuredHeight();
157 }
Adam Cohend5e42732011-03-28 17:33:39 -0700158 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
159 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
160 MeasureSpec.EXACTLY);
Adam Cohend5e42732011-03-28 17:33:39 -0700161 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
162 }
163
Adam Cohen2374abf2013-04-16 14:56:57 -0700164 private boolean invertLayoutHorizontally() {
165 return mInvertIfRtl && isLayoutRtl();
166 }
167
168 public boolean isLayoutRtl() {
169 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
170 }
171
Michael Jurka8c920dd2011-01-20 14:16:56 -0800172 @Override
173 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800174 int count = getChildCount();
175 for (int i = 0; i < count; i++) {
176 final View child = getChildAt(i);
177 if (child.getVisibility() != GONE) {
178 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800179 int childLeft = lp.x;
180 int childTop = lp.y;
181 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
182
183 if (lp.dropped) {
184 lp.dropped = false;
185
186 final int[] cellXY = mTmpCellXY;
187 getLocationOnScreen(cellXY);
188 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
189 WallpaperManager.COMMAND_DROP,
190 cellXY[0] + childLeft + lp.width / 2,
191 cellXY[1] + childTop + lp.height / 2, 0, null);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800192 }
193 }
194 }
195 }
196
197 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700198 public boolean shouldDelayChildPressedState() {
199 return false;
200 }
201
202 @Override
Michael Jurka8c920dd2011-01-20 14:16:56 -0800203 public void requestChildFocus(View child, View focused) {
204 super.requestChildFocus(child, focused);
205 if (child != null) {
206 Rect r = new Rect();
207 child.getDrawingRect(r);
208 requestRectangleOnScreen(r);
209 }
210 }
211
212 @Override
213 public void cancelLongPress() {
214 super.cancelLongPress();
215
216 // Cancel long press for all children
217 final int count = getChildCount();
218 for (int i = 0; i < count; i++) {
219 final View child = getChildAt(i);
220 child.cancelLongPress();
221 }
222 }
223
224 @Override
225 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
226 final int count = getChildCount();
227 for (int i = 0; i < count; i++) {
228 final View view = getChildAt(i);
229 view.setDrawingCacheEnabled(enabled);
230 // Update the drawing caches
Adam Cohen8182e5b2011-08-01 15:43:31 -0700231 if (!view.isHardwareAccelerated() && enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800232 view.buildDrawingCache(true);
233 }
234 }
235 }
236
237 @Override
238 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
239 super.setChildrenDrawnWithCacheEnabled(enabled);
240 }
Adam Cohend4844c32011-02-18 19:25:06 -0800241}