blob: bec00ec0cc34355e120a87ce328ed13dfa6f775d [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
2 * Copyright (C) 2010 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.content.Context;
Winson Chung785d2eb2011-04-14 16:08:02 -070020import android.content.res.Resources;
Winson Chung321e9ee2010-08-09 13:37:56 -070021import android.util.AttributeSet;
Winson Chungdb1138b2011-06-30 14:39:35 -070022import android.util.Log;
Winson Chung321e9ee2010-08-09 13:37:56 -070023import android.view.MotionEvent;
24import android.view.View;
25import android.view.ViewDebug;
26import android.view.ViewGroup;
27
Winson Chung785d2eb2011-04-14 16:08:02 -070028import com.android.launcher.R;
29
Winson Chung321e9ee2010-08-09 13:37:56 -070030/**
31 * An abstraction of the original CellLayout which supports laying out items
32 * which span multiple cells into a grid-like layout. Also supports dimming
33 * to give a preview of its contents.
34 */
Michael Jurka8245a862011-02-01 17:53:59 -080035public class PagedViewCellLayout extends ViewGroup implements Page {
Winson Chung321e9ee2010-08-09 13:37:56 -070036 static final String TAG = "PagedViewCellLayout";
37
Winson Chung321e9ee2010-08-09 13:37:56 -070038 private int mCellCountX;
39 private int mCellCountY;
Winson Chungdb1138b2011-06-30 14:39:35 -070040 private int mOriginalCellWidth;
41 private int mOriginalCellHeight;
Winson Chung321e9ee2010-08-09 13:37:56 -070042 private int mCellWidth;
43 private int mCellHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -070044 private int mOriginalWidthGap;
45 private int mOriginalHeightGap;
Winson Chungdf4b83d2010-10-20 17:49:27 -070046 private int mWidthGap;
47 private int mHeightGap;
Winson Chungdb1138b2011-06-30 14:39:35 -070048 private int mMaxGap;
Winson Chungf0ea4d32011-06-06 14:27:16 -070049 private float mPeekWidth;
Michael Jurka8245a862011-02-01 17:53:59 -080050 protected PagedViewCellLayoutChildren mChildren;
51 private PagedViewCellLayoutChildren mHolographicChildren;
Michael Jurkaabded662011-03-04 12:06:57 -080052 private boolean mAllowHardwareLayerCreation = false;
53 private boolean mCreateHardwareLayersIfAllowed = false;
Winson Chung321e9ee2010-08-09 13:37:56 -070054
Winson Chung321e9ee2010-08-09 13:37:56 -070055 public PagedViewCellLayout(Context context) {
56 this(context, null);
57 }
58
59 public PagedViewCellLayout(Context context, AttributeSet attrs) {
60 this(context, attrs, 0);
61 }
62
63 public PagedViewCellLayout(Context context, AttributeSet attrs, int defStyle) {
64 super(context, attrs, defStyle);
65
Winson Chung321e9ee2010-08-09 13:37:56 -070066 setAlwaysDrawnWithCacheEnabled(false);
67
68 // setup default cell parameters
Winson Chung785d2eb2011-04-14 16:08:02 -070069 Resources resources = context.getResources();
Winson Chungdb1138b2011-06-30 14:39:35 -070070 mOriginalCellWidth = mCellWidth =
71 resources.getDimensionPixelSize(R.dimen.apps_customize_cell_width);
72 mOriginalCellHeight = mCellHeight =
73 resources.getDimensionPixelSize(R.dimen.apps_customize_cell_height);
Winson Chungf0ea4d32011-06-06 14:27:16 -070074 mPeekWidth = resources.getDimensionPixelSize(R.dimen.apps_customize_peek_width);
Winson Chung321e9ee2010-08-09 13:37:56 -070075 mCellCountX = LauncherModel.getCellCountX();
76 mCellCountY = LauncherModel.getCellCountY();
Adam Cohen234c4cd2011-07-17 21:03:04 -070077 mOriginalHeightGap = mOriginalHeightGap = mWidthGap = mHeightGap = -1;
Winson Chungdb1138b2011-06-30 14:39:35 -070078 mMaxGap = resources.getDimensionPixelSize(R.dimen.apps_customize_max_gap);
Winson Chung321e9ee2010-08-09 13:37:56 -070079
Michael Jurka8245a862011-02-01 17:53:59 -080080 mChildren = new PagedViewCellLayoutChildren(context);
81 mChildren.setCellDimensions(mCellWidth, mCellHeight);
82 mChildren.setGap(mWidthGap, mHeightGap);
83
84 addView(mChildren);
85 mHolographicChildren = new PagedViewCellLayoutChildren(context);
86 mHolographicChildren.setAlpha(0f);
87 mHolographicChildren.setCellDimensions(mCellWidth, mCellHeight);
88 mHolographicChildren.setGap(mWidthGap, mHeightGap);
89
90 addView(mHolographicChildren);
Winson Chungb3347bb2010-08-19 14:51:28 -070091 }
Winson Chung321e9ee2010-08-09 13:37:56 -070092
Patrick Dubroy244d74c2011-05-19 16:48:48 -070093 public int getCellWidth() {
94 return mCellWidth;
95 }
96
97 public int getCellHeight() {
98 return mCellHeight;
99 }
100
Michael Jurkaabded662011-03-04 12:06:57 -0800101 public void allowHardwareLayerCreation() {
102 // This is called after the first time we launch into All Apps. Before that point,
103 // there's no need for hardware layers here since there's a hardware layer set on the
104 // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here
105 // before the animation is done slows down the animation
106 if (!mAllowHardwareLayerCreation) {
107 mAllowHardwareLayerCreation = true;
108 if (mCreateHardwareLayersIfAllowed) {
109 createHardwareLayers();
110 }
111 }
Michael Jurkac0759f52011-02-03 16:47:14 -0800112 }
113
Winson Chungb3347bb2010-08-19 14:51:28 -0700114 @Override
115 public void setAlpha(float alpha) {
Winson Chungb44b5242011-06-13 11:32:14 -0700116 mChildren.setAlpha(HolographicOutlineHelper.viewAlphaInterpolator(alpha));
117 mHolographicChildren.setAlpha(HolographicOutlineHelper.highlightAlphaInterpolator(alpha));
Winson Chung321e9ee2010-08-09 13:37:56 -0700118 }
119
Michael Jurkac0759f52011-02-03 16:47:14 -0800120 void destroyHardwareLayers() {
Michael Jurkaabded662011-03-04 12:06:57 -0800121 // called when a page is no longer visible (triggered by loadAssociatedPages ->
122 // removeAllViewsOnPage)
123 mCreateHardwareLayersIfAllowed = false;
124 if (mAllowHardwareLayerCreation) {
Michael Jurkac0759f52011-02-03 16:47:14 -0800125 mChildren.destroyHardwareLayer();
126 mHolographicChildren.destroyHardwareLayer();
127 }
128 }
129 void createHardwareLayers() {
Michael Jurkaabded662011-03-04 12:06:57 -0800130 // called when a page is visible (triggered by loadAssociatedPages -> syncPageItems)
131 mCreateHardwareLayersIfAllowed = true;
132 if (mAllowHardwareLayerCreation) {
Michael Jurkac0759f52011-02-03 16:47:14 -0800133 mChildren.createHardwareLayer();
134 mHolographicChildren.createHardwareLayer();
135 }
136 }
137
Winson Chung321e9ee2010-08-09 13:37:56 -0700138 @Override
139 public void cancelLongPress() {
140 super.cancelLongPress();
141
142 // Cancel long press for all children
143 final int count = getChildCount();
144 for (int i = 0; i < count; i++) {
145 final View child = getChildAt(i);
146 child.cancelLongPress();
147 }
148 }
149
150 public boolean addViewToCellLayout(View child, int index, int childId,
Winson Chung6a70e9f2011-05-17 16:24:49 -0700151 PagedViewCellLayout.LayoutParams params) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700152 final PagedViewCellLayout.LayoutParams lp = params;
153
154 // Generate an id for each view, this assumes we have at most 256x256 cells
155 // per workspace screen
156 if (lp.cellX >= 0 && lp.cellX <= (mCellCountX - 1) &&
157 lp.cellY >= 0 && (lp.cellY <= mCellCountY - 1)) {
158 // If the horizontal or vertical span is set to -1, it is taken to
159 // mean that it spans the extent of the CellLayout
160 if (lp.cellHSpan < 0) lp.cellHSpan = mCellCountX;
161 if (lp.cellVSpan < 0) lp.cellVSpan = mCellCountY;
162
163 child.setId(childId);
Michael Jurka8245a862011-02-01 17:53:59 -0800164 mChildren.addView(child, index, lp);
Winson Chung321e9ee2010-08-09 13:37:56 -0700165
Michael Jurka8245a862011-02-01 17:53:59 -0800166 if (child instanceof PagedViewIcon) {
167 PagedViewIcon pagedViewIcon = (PagedViewIcon) child;
Michael Jurkaabded662011-03-04 12:06:57 -0800168 if (mAllowHardwareLayerCreation) {
Michael Jurkac0759f52011-02-03 16:47:14 -0800169 pagedViewIcon.disableCache();
170 }
Winson Chung6a70e9f2011-05-17 16:24:49 -0700171 mHolographicChildren.addView(pagedViewIcon.getHolographicOutlineView(),
172 index, lp);
Michael Jurka8245a862011-02-01 17:53:59 -0800173 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700174 return true;
175 }
176 return false;
177 }
178
179 @Override
Michael Jurka8245a862011-02-01 17:53:59 -0800180 public void removeAllViewsOnPage() {
181 mChildren.removeAllViews();
182 mHolographicChildren.removeAllViews();
Michael Jurkac5e49022011-02-16 12:04:02 -0800183 destroyHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700184 }
185
186 @Override
Michael Jurka8245a862011-02-01 17:53:59 -0800187 public void removeViewOnPageAt(int index) {
188 mChildren.removeViewAt(index);
Winson Chung6a70e9f2011-05-17 16:24:49 -0700189 mHolographicChildren.removeViewAt(index);
Michael Jurka8245a862011-02-01 17:53:59 -0800190 }
191
192 @Override
193 public int getPageChildCount() {
194 return mChildren.getChildCount();
195 }
196
197 @Override
198 public View getChildOnPageAt(int i) {
199 return mChildren.getChildAt(i);
200 }
201
202 @Override
203 public int indexOfChildOnPage(View v) {
204 return mChildren.indexOfChild(v);
205 }
206
Winson Chung97d85d22011-04-13 11:27:36 -0700207 public int getCellCountX() {
208 return mCellCountX;
209 }
210
211 public int getCellCountY() {
212 return mCellCountY;
213 }
214
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
217 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
218
219 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
220 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
221
222 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
223 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
224 }
225
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 int numWidthGaps = mCellCountX - 1;
227 int numHeightGaps = mCellCountY - 1;
228
Adam Cohen234c4cd2011-07-17 21:03:04 -0700229 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chungdb1138b2011-06-30 14:39:35 -0700230 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
231 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
232 int hFreeSpace = hSpace - (mCellCountX * mOriginalCellWidth);
233 int vFreeSpace = vSpace - (mCellCountY * mOriginalCellHeight);
234 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
235 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung321e9ee2010-08-09 13:37:56 -0700236
Winson Chungdb1138b2011-06-30 14:39:35 -0700237 mChildren.setGap(mWidthGap, mHeightGap);
238 mHolographicChildren.setGap(mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700239 } else {
240 mWidthGap = mOriginalWidthGap;
241 mHeightGap = mOriginalHeightGap;
Winson Chungdf4b83d2010-10-20 17:49:27 -0700242 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700243
Winson Chungdb1138b2011-06-30 14:39:35 -0700244 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
245 int newWidth = widthSpecSize;
246 int newHeight = heightSpecSize;
247 if (widthSpecMode == MeasureSpec.AT_MOST) {
248 newWidth = mPaddingLeft + mPaddingRight + (mCellCountX * mCellWidth) +
249 ((mCellCountX - 1) * mWidthGap);
250 newHeight = mPaddingTop + mPaddingBottom + (mCellCountY * mCellHeight) +
251 ((mCellCountY - 1) * mHeightGap);
252 setMeasuredDimension(newWidth, newHeight);
253 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700254
255 final int count = getChildCount();
256 for (int i = 0; i < count; i++) {
257 View child = getChildAt(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800258 int childWidthMeasureSpec =
Winson Chungdb1138b2011-06-30 14:39:35 -0700259 MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
260 mPaddingRight, MeasureSpec.EXACTLY);
Michael Jurka8245a862011-02-01 17:53:59 -0800261 int childheightMeasureSpec =
Winson Chungdb1138b2011-06-30 14:39:35 -0700262 MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
263 mPaddingBottom, MeasureSpec.EXACTLY);
Winson Chung321e9ee2010-08-09 13:37:56 -0700264 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
265 }
266
Winson Chungdb1138b2011-06-30 14:39:35 -0700267 setMeasuredDimension(newWidth, newHeight);
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 }
269
Michael Jurka7ef959b2011-02-23 11:48:32 -0800270 int getContentWidth() {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700271 return getWidthBeforeFirstLayout() + mPaddingLeft + mPaddingRight;
Michael Jurka0413dfa2011-04-05 16:52:32 -0700272 }
273
Winson Chung4b576be2011-04-27 17:40:20 -0700274 int getContentHeight() {
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700275 if (mCellCountY > 0) {
276 return mCellCountY * mCellHeight + (mCellCountY - 1) * Math.max(0, mHeightGap);
277 }
278 return 0;
Winson Chung4b576be2011-04-27 17:40:20 -0700279 }
280
Michael Jurkad92e7412011-04-05 17:07:27 -0700281 int getWidthBeforeFirstLayout() {
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700282 if (mCellCountX > 0) {
283 return mCellCountX * mCellWidth + (mCellCountX - 1) * Math.max(0, mWidthGap);
284 }
285 return 0;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800286 }
287
Winson Chung321e9ee2010-08-09 13:37:56 -0700288 @Override
289 protected void onLayout(boolean changed, int l, int t, int r, int b) {
290 int count = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700291 for (int i = 0; i < count; i++) {
292 View child = getChildAt(i);
Michael Jurka9bfe2b22011-06-02 16:41:34 -0700293 child.layout(mPaddingLeft, mPaddingTop,
294 r - l - mPaddingRight, b - t - mPaddingBottom);
Winson Chung321e9ee2010-08-09 13:37:56 -0700295 }
296 }
297
298 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700299 public boolean onTouchEvent(MotionEvent event) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700300 boolean result = super.onTouchEvent(event);
301 int count = getPageChildCount();
302 if (count > 0) {
303 // We only intercept the touch if we are tapping in empty space after the final row
304 View child = getChildOnPageAt(count - 1);
305 int bottom = child.getBottom();
306 int numRows = (int) Math.ceil((float) getPageChildCount() / getCellCountX());
307 if (numRows < getCellCountY()) {
308 // Add a little bit of buffer if there is room for another row
309 bottom += mCellHeight / 2;
310 }
311 result = result || (event.getY() < bottom);
312 }
313 return result;
Winson Chung321e9ee2010-08-09 13:37:56 -0700314 }
315
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 public void enableCenteredContent(boolean enabled) {
Michael Jurka8245a862011-02-01 17:53:59 -0800317 mChildren.enableCenteredContent(enabled);
318 mHolographicChildren.enableCenteredContent(enabled);
Winson Chung80baf5a2010-08-09 16:03:15 -0700319 }
320
Winson Chung321e9ee2010-08-09 13:37:56 -0700321 @Override
322 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8245a862011-02-01 17:53:59 -0800323 mChildren.setChildrenDrawingCacheEnabled(enabled);
324 mHolographicChildren.setChildrenDrawingCacheEnabled(enabled);
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 }
326
327 public void setCellCount(int xCount, int yCount) {
328 mCellCountX = xCount;
329 mCellCountY = yCount;
330 requestLayout();
331 }
332
Winson Chungdf4b83d2010-10-20 17:49:27 -0700333 public void setGap(int widthGap, int heightGap) {
334 mWidthGap = widthGap;
335 mHeightGap = heightGap;
Michael Jurka8245a862011-02-01 17:53:59 -0800336 mChildren.setGap(widthGap, heightGap);
337 mHolographicChildren.setGap(widthGap, heightGap);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700338 }
339
Winson Chung80baf5a2010-08-09 16:03:15 -0700340 public int[] getCellCountForDimensions(int width, int height) {
341 // Always assume we're working with the smallest span to make sure we
342 // reserve enough space in both orientations
343 int smallerSize = Math.min(mCellWidth, mCellHeight);
344
345 // Always round up to next largest cell
346 int spanX = (width + smallerSize) / smallerSize;
347 int spanY = (height + smallerSize) / smallerSize;
348
349 return new int[] { spanX, spanY };
350 }
351
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 /**
353 * Start dragging the specified child
354 *
355 * @param child The child that is being dragged
356 */
357 void onDragChild(View child) {
358 PagedViewCellLayout.LayoutParams lp = (PagedViewCellLayout.LayoutParams) child.getLayoutParams();
359 lp.isDragging = true;
360 }
361
Winson Chunge3193b92010-09-10 11:44:42 -0700362 /**
363 * Estimates the number of cells that the specified width would take up.
364 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700365 public int estimateCellHSpan(int width) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700366 // The space for a page assuming that we want to show half of a column of the previous and
367 // next pages is the width - left padding (current & next page) - right padding (previous &
368 // current page) - half cell width (for previous and next pages)
369 int availWidth = (int) (width - (2 * mPaddingLeft + 2 * mPaddingRight) - (2 * mPeekWidth));
370
371 // We know that we have to fit N cells with N-1 width gaps, so we just juggle to solve for N
372 int n = Math.max(1, (availWidth + mWidthGap) / (mCellWidth + mWidthGap));
373
374 // We don't do anything fancy to determine if we squeeze another row in.
375 return n;
Winson Chung80baf5a2010-08-09 16:03:15 -0700376 }
Winson Chunge3193b92010-09-10 11:44:42 -0700377
378 /**
379 * Estimates the number of cells that the specified height would take up.
380 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700381 public int estimateCellVSpan(int height) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700382 // The space for a page is the height - top padding (current page) - bottom padding (current
383 // page)
384 int availHeight = height - (mPaddingTop + mPaddingBottom);
385
386 // We know that we have to fit N cells with N-1 height gaps, so we juggle to solve for N
387 int n = Math.max(1, (availHeight + mHeightGap) / (mCellHeight + mHeightGap));
388
389 // We don't do anything fancy to determine if we squeeze another row in.
390 return n;
391 }
392
393 public void calculateCellCount(int width, int height, int maxCellCountX, int maxCellCountY) {
394 mCellCountX = Math.min(maxCellCountX, estimateCellHSpan(width));
395 mCellCountY = Math.min(maxCellCountY, estimateCellVSpan(height));
396 requestLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700397 }
Winson Chunge3193b92010-09-10 11:44:42 -0700398
399 /**
400 * Estimates the width that the number of vSpan cells will take up.
401 */
402 public int estimateCellWidth(int hSpan) {
403 // TODO: we need to take widthGap into effect
404 return hSpan * mCellWidth;
405 }
406
407 /**
408 * Estimates the height that the number of vSpan cells will take up.
409 */
410 public int estimateCellHeight(int vSpan) {
411 // TODO: we need to take heightGap into effect
412 return vSpan * mCellHeight;
Winson Chung80baf5a2010-08-09 16:03:15 -0700413 }
414
Winson Chung321e9ee2010-08-09 13:37:56 -0700415 @Override
416 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
417 return new PagedViewCellLayout.LayoutParams(getContext(), attrs);
418 }
419
420 @Override
421 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
422 return p instanceof PagedViewCellLayout.LayoutParams;
423 }
424
425 @Override
426 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
427 return new PagedViewCellLayout.LayoutParams(p);
428 }
429
430 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
431 /**
432 * Horizontal location of the item in the grid.
433 */
434 @ViewDebug.ExportedProperty
435 public int cellX;
436
437 /**
438 * Vertical location of the item in the grid.
439 */
440 @ViewDebug.ExportedProperty
441 public int cellY;
442
443 /**
444 * Number of cells spanned horizontally by the item.
445 */
446 @ViewDebug.ExportedProperty
447 public int cellHSpan;
448
449 /**
450 * Number of cells spanned vertically by the item.
451 */
452 @ViewDebug.ExportedProperty
453 public int cellVSpan;
454
455 /**
456 * Is this item currently being dragged
457 */
458 public boolean isDragging;
459
Winson Chung80baf5a2010-08-09 16:03:15 -0700460 // a data object that you can bind to this layout params
461 private Object mTag;
462
Winson Chung321e9ee2010-08-09 13:37:56 -0700463 // X coordinate of the view in the layout.
464 @ViewDebug.ExportedProperty
465 int x;
466 // Y coordinate of the view in the layout.
467 @ViewDebug.ExportedProperty
468 int y;
469
Winson Chung80baf5a2010-08-09 16:03:15 -0700470 public LayoutParams() {
471 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
472 cellHSpan = 1;
473 cellVSpan = 1;
474 }
475
Winson Chung321e9ee2010-08-09 13:37:56 -0700476 public LayoutParams(Context c, AttributeSet attrs) {
477 super(c, attrs);
478 cellHSpan = 1;
479 cellVSpan = 1;
480 }
481
482 public LayoutParams(ViewGroup.LayoutParams source) {
483 super(source);
484 cellHSpan = 1;
485 cellVSpan = 1;
486 }
487
488 public LayoutParams(LayoutParams source) {
489 super(source);
490 this.cellX = source.cellX;
491 this.cellY = source.cellY;
492 this.cellHSpan = source.cellHSpan;
493 this.cellVSpan = source.cellVSpan;
494 }
495
496 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
497 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
498 this.cellX = cellX;
499 this.cellY = cellY;
500 this.cellHSpan = cellHSpan;
501 this.cellVSpan = cellVSpan;
502 }
503
504 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
505 int hStartPadding, int vStartPadding) {
506
507 final int myCellHSpan = cellHSpan;
508 final int myCellVSpan = cellVSpan;
509 final int myCellX = cellX;
510 final int myCellY = cellY;
511
512 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
513 leftMargin - rightMargin;
514 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
515 topMargin - bottomMargin;
516
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700517 if (LauncherApplication.isScreenLarge()) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700518 x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
519 y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
520 } else {
521 x = myCellX * (cellWidth + widthGap) + leftMargin;
522 y = myCellY * (cellHeight + heightGap) + topMargin;
523 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700524 }
525
Winson Chung80baf5a2010-08-09 16:03:15 -0700526 public Object getTag() {
527 return mTag;
528 }
529
530 public void setTag(Object tag) {
531 mTag = tag;
532 }
533
Winson Chung321e9ee2010-08-09 13:37:56 -0700534 public String toString() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700535 return "(" + this.cellX + ", " + this.cellY + ", " +
536 this.cellHSpan + ", " + this.cellVSpan + ")";
Winson Chung321e9ee2010-08-09 13:37:56 -0700537 }
538 }
539}
Michael Jurka8245a862011-02-01 17:53:59 -0800540
541interface Page {
542 public int getPageChildCount();
543 public View getChildOnPageAt(int i);
544 public void removeAllViewsOnPage();
545 public void removeViewOnPageAt(int i);
546 public int indexOfChildOnPage(View v);
547}