Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 22 | import android.view.MotionEvent; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewDebug; |
| 25 | import android.view.ViewGroup; |
| 26 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 27 | import com.android.launcher3.R; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 28 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 29 | /** |
| 30 | * An abstraction of the original CellLayout which supports laying out items |
| 31 | * which span multiple cells into a grid-like layout. Also supports dimming |
| 32 | * to give a preview of its contents. |
| 33 | */ |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 34 | public class PagedViewCellLayout extends ViewGroup implements Page { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 35 | static final String TAG = "PagedViewCellLayout"; |
| 36 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 37 | private int mCellCountX; |
| 38 | private int mCellCountY; |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 39 | private int mOriginalCellWidth; |
| 40 | private int mOriginalCellHeight; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 41 | private int mCellWidth; |
| 42 | private int mCellHeight; |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 43 | private int mOriginalWidthGap; |
| 44 | private int mOriginalHeightGap; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 45 | private int mWidthGap; |
| 46 | private int mHeightGap; |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 47 | private int mMaxGap; |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 48 | protected PagedViewCellLayoutChildren mChildren; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 49 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 50 | public PagedViewCellLayout(Context context) { |
| 51 | this(context, null); |
| 52 | } |
| 53 | |
| 54 | public PagedViewCellLayout(Context context, AttributeSet attrs) { |
| 55 | this(context, attrs, 0); |
| 56 | } |
| 57 | |
| 58 | public PagedViewCellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 59 | super(context, attrs, defStyle); |
| 60 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 61 | setAlwaysDrawnWithCacheEnabled(false); |
| 62 | |
| 63 | // setup default cell parameters |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 64 | Resources resources = context.getResources(); |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 65 | mOriginalCellWidth = mCellWidth = |
| 66 | resources.getDimensionPixelSize(R.dimen.apps_customize_cell_width); |
| 67 | mOriginalCellHeight = mCellHeight = |
| 68 | resources.getDimensionPixelSize(R.dimen.apps_customize_cell_height); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 69 | mCellCountX = LauncherModel.getCellCountX(); |
| 70 | mCellCountY = LauncherModel.getCellCountY(); |
Winson Chung | 6032e7e | 2011-11-08 15:47:17 -0800 | [diff] [blame] | 71 | mOriginalWidthGap = mOriginalHeightGap = mWidthGap = mHeightGap = -1; |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 72 | mMaxGap = resources.getDimensionPixelSize(R.dimen.apps_customize_max_gap); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 73 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 74 | mChildren = new PagedViewCellLayoutChildren(context); |
| 75 | mChildren.setCellDimensions(mCellWidth, mCellHeight); |
| 76 | mChildren.setGap(mWidthGap, mHeightGap); |
| 77 | |
| 78 | addView(mChildren); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 79 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 80 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 81 | public int getCellWidth() { |
| 82 | return mCellWidth; |
| 83 | } |
| 84 | |
| 85 | public int getCellHeight() { |
| 86 | return mCellHeight; |
| 87 | } |
| 88 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public void cancelLongPress() { |
| 91 | super.cancelLongPress(); |
| 92 | |
| 93 | // Cancel long press for all children |
| 94 | final int count = getChildCount(); |
| 95 | for (int i = 0; i < count; i++) { |
| 96 | final View child = getChildAt(i); |
| 97 | child.cancelLongPress(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public boolean addViewToCellLayout(View child, int index, int childId, |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 102 | PagedViewCellLayout.LayoutParams params) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 103 | final PagedViewCellLayout.LayoutParams lp = params; |
| 104 | |
| 105 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 106 | // per workspace screen |
| 107 | if (lp.cellX >= 0 && lp.cellX <= (mCellCountX - 1) && |
| 108 | lp.cellY >= 0 && (lp.cellY <= mCellCountY - 1)) { |
| 109 | // If the horizontal or vertical span is set to -1, it is taken to |
| 110 | // mean that it spans the extent of the CellLayout |
| 111 | if (lp.cellHSpan < 0) lp.cellHSpan = mCellCountX; |
| 112 | if (lp.cellVSpan < 0) lp.cellVSpan = mCellCountY; |
| 113 | |
| 114 | child.setId(childId); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 115 | mChildren.addView(child, index, lp); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 116 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | @Override |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 123 | public void removeAllViewsOnPage() { |
| 124 | mChildren.removeAllViews(); |
Michael Jurka | 47639b9 | 2013-01-14 12:42:27 +0100 | [diff] [blame] | 125 | setLayerType(LAYER_TYPE_NONE, null); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 129 | public void removeViewOnPageAt(int index) { |
| 130 | mChildren.removeViewAt(index); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 133 | /** |
| 134 | * Clears all the key listeners for the individual icons. |
| 135 | */ |
| 136 | public void resetChildrenOnKeyListeners() { |
| 137 | int childCount = mChildren.getChildCount(); |
| 138 | for (int j = 0; j < childCount; ++j) { |
| 139 | mChildren.getChildAt(j).setOnKeyListener(null); |
| 140 | } |
| 141 | } |
| 142 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 143 | @Override |
| 144 | public int getPageChildCount() { |
| 145 | return mChildren.getChildCount(); |
| 146 | } |
| 147 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 148 | public PagedViewCellLayoutChildren getChildrenLayout() { |
| 149 | return mChildren; |
| 150 | } |
| 151 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 152 | @Override |
| 153 | public View getChildOnPageAt(int i) { |
| 154 | return mChildren.getChildAt(i); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public int indexOfChildOnPage(View v) { |
| 159 | return mChildren.indexOfChild(v); |
| 160 | } |
| 161 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 162 | public int getCellCountX() { |
| 163 | return mCellCountX; |
| 164 | } |
| 165 | |
| 166 | public int getCellCountY() { |
| 167 | return mCellCountY; |
| 168 | } |
| 169 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 170 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 171 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
| 172 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 173 | |
| 174 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 175 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 176 | |
| 177 | if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 178 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 179 | } |
| 180 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 181 | int numWidthGaps = mCellCountX - 1; |
| 182 | int numHeightGaps = mCellCountY - 1; |
| 183 | |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 184 | if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 185 | int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight(); |
| 186 | int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom(); |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 187 | int hFreeSpace = hSpace - (mCellCountX * mOriginalCellWidth); |
| 188 | int vFreeSpace = vSpace - (mCellCountY * mOriginalCellHeight); |
| 189 | mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0); |
| 190 | mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 191 | |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 192 | mChildren.setGap(mWidthGap, mHeightGap); |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 193 | } else { |
| 194 | mWidthGap = mOriginalWidthGap; |
| 195 | mHeightGap = mOriginalHeightGap; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 196 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 197 | |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 198 | // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY |
| 199 | int newWidth = widthSpecSize; |
| 200 | int newHeight = heightSpecSize; |
| 201 | if (widthSpecMode == MeasureSpec.AT_MOST) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 202 | newWidth = getPaddingLeft() + getPaddingRight() + (mCellCountX * mCellWidth) + |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 203 | ((mCellCountX - 1) * mWidthGap); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 204 | newHeight = getPaddingTop() + getPaddingBottom() + (mCellCountY * mCellHeight) + |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 205 | ((mCellCountY - 1) * mHeightGap); |
| 206 | setMeasuredDimension(newWidth, newHeight); |
| 207 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 208 | |
| 209 | final int count = getChildCount(); |
| 210 | for (int i = 0; i < count; i++) { |
| 211 | View child = getChildAt(i); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 212 | int childWidthMeasureSpec = |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 213 | MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() - |
| 214 | getPaddingRight(), MeasureSpec.EXACTLY); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 215 | int childheightMeasureSpec = |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 216 | MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() - |
| 217 | getPaddingBottom(), MeasureSpec.EXACTLY); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 218 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 219 | } |
| 220 | |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 221 | setMeasuredDimension(newWidth, newHeight); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 224 | int getContentWidth() { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 225 | return getWidthBeforeFirstLayout() + getPaddingLeft() + getPaddingRight(); |
Michael Jurka | 0413dfa | 2011-04-05 16:52:32 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 228 | int getContentHeight() { |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 229 | if (mCellCountY > 0) { |
| 230 | return mCellCountY * mCellHeight + (mCellCountY - 1) * Math.max(0, mHeightGap); |
| 231 | } |
| 232 | return 0; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Michael Jurka | d92e741 | 2011-04-05 17:07:27 -0700 | [diff] [blame] | 235 | int getWidthBeforeFirstLayout() { |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 236 | if (mCellCountX > 0) { |
| 237 | return mCellCountX * mCellWidth + (mCellCountX - 1) * Math.max(0, mWidthGap); |
| 238 | } |
| 239 | return 0; |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 242 | @Override |
| 243 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 244 | int count = getChildCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 245 | for (int i = 0; i < count; i++) { |
| 246 | View child = getChildAt(i); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 247 | child.layout(getPaddingLeft(), getPaddingTop(), |
| 248 | r - l - getPaddingRight(), b - t - getPaddingBottom()); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 253 | public boolean onTouchEvent(MotionEvent event) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 254 | boolean result = super.onTouchEvent(event); |
| 255 | int count = getPageChildCount(); |
| 256 | if (count > 0) { |
| 257 | // We only intercept the touch if we are tapping in empty space after the final row |
| 258 | View child = getChildOnPageAt(count - 1); |
| 259 | int bottom = child.getBottom(); |
| 260 | int numRows = (int) Math.ceil((float) getPageChildCount() / getCellCountX()); |
| 261 | if (numRows < getCellCountY()) { |
| 262 | // Add a little bit of buffer if there is room for another row |
| 263 | bottom += mCellHeight / 2; |
| 264 | } |
| 265 | result = result || (event.getY() < bottom); |
| 266 | } |
| 267 | return result; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 270 | public void enableCenteredContent(boolean enabled) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 271 | mChildren.enableCenteredContent(enabled); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 274 | @Override |
| 275 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 276 | mChildren.setChildrenDrawingCacheEnabled(enabled); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | public void setCellCount(int xCount, int yCount) { |
| 280 | mCellCountX = xCount; |
| 281 | mCellCountY = yCount; |
| 282 | requestLayout(); |
| 283 | } |
| 284 | |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 285 | public void setGap(int widthGap, int heightGap) { |
Winson Chung | 6032e7e | 2011-11-08 15:47:17 -0800 | [diff] [blame] | 286 | mOriginalWidthGap = mWidthGap = widthGap; |
| 287 | mOriginalHeightGap = mHeightGap = heightGap; |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 288 | mChildren.setGap(widthGap, heightGap); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 291 | public int[] getCellCountForDimensions(int width, int height) { |
| 292 | // Always assume we're working with the smallest span to make sure we |
| 293 | // reserve enough space in both orientations |
| 294 | int smallerSize = Math.min(mCellWidth, mCellHeight); |
| 295 | |
| 296 | // Always round up to next largest cell |
| 297 | int spanX = (width + smallerSize) / smallerSize; |
| 298 | int spanY = (height + smallerSize) / smallerSize; |
| 299 | |
| 300 | return new int[] { spanX, spanY }; |
| 301 | } |
| 302 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Start dragging the specified child |
| 305 | * |
| 306 | * @param child The child that is being dragged |
| 307 | */ |
| 308 | void onDragChild(View child) { |
| 309 | PagedViewCellLayout.LayoutParams lp = (PagedViewCellLayout.LayoutParams) child.getLayoutParams(); |
| 310 | lp.isDragging = true; |
| 311 | } |
| 312 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 313 | /** |
| 314 | * Estimates the number of cells that the specified width would take up. |
| 315 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 316 | public int estimateCellHSpan(int width) { |
Winson Chung | 6032e7e | 2011-11-08 15:47:17 -0800 | [diff] [blame] | 317 | // We don't show the next/previous pages any more, so we use the full width, minus the |
| 318 | // padding |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 319 | int availWidth = width - (getPaddingLeft() + getPaddingRight()); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 320 | |
| 321 | // We know that we have to fit N cells with N-1 width gaps, so we just juggle to solve for N |
| 322 | int n = Math.max(1, (availWidth + mWidthGap) / (mCellWidth + mWidthGap)); |
| 323 | |
| 324 | // We don't do anything fancy to determine if we squeeze another row in. |
| 325 | return n; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 326 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 327 | |
| 328 | /** |
| 329 | * Estimates the number of cells that the specified height would take up. |
| 330 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 331 | public int estimateCellVSpan(int height) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 332 | // The space for a page is the height - top padding (current page) - bottom padding (current |
| 333 | // page) |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 334 | int availHeight = height - (getPaddingTop() + getPaddingBottom()); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 335 | |
| 336 | // We know that we have to fit N cells with N-1 height gaps, so we juggle to solve for N |
| 337 | int n = Math.max(1, (availHeight + mHeightGap) / (mCellHeight + mHeightGap)); |
| 338 | |
| 339 | // We don't do anything fancy to determine if we squeeze another row in. |
| 340 | return n; |
| 341 | } |
| 342 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 343 | /** Returns an estimated center position of the cell at the specified index */ |
| 344 | public int[] estimateCellPosition(int x, int y) { |
| 345 | return new int[] { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 346 | getPaddingLeft() + (x * mCellWidth) + (x * mWidthGap) + (mCellWidth / 2), |
| 347 | getPaddingTop() + (y * mCellHeight) + (y * mHeightGap) + (mCellHeight / 2) |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 348 | }; |
| 349 | } |
| 350 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 351 | public void calculateCellCount(int width, int height, int maxCellCountX, int maxCellCountY) { |
| 352 | mCellCountX = Math.min(maxCellCountX, estimateCellHSpan(width)); |
| 353 | mCellCountY = Math.min(maxCellCountY, estimateCellVSpan(height)); |
| 354 | requestLayout(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 355 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 356 | |
| 357 | /** |
Winson Chung | 5fc72b3 | 2011-10-11 17:53:58 -0700 | [diff] [blame] | 358 | * Estimates the width that the number of hSpan cells will take up. |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 359 | */ |
| 360 | public int estimateCellWidth(int hSpan) { |
| 361 | // TODO: we need to take widthGap into effect |
| 362 | return hSpan * mCellWidth; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Estimates the height that the number of vSpan cells will take up. |
| 367 | */ |
| 368 | public int estimateCellHeight(int vSpan) { |
| 369 | // TODO: we need to take heightGap into effect |
| 370 | return vSpan * mCellHeight; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 373 | @Override |
| 374 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 375 | return new PagedViewCellLayout.LayoutParams(getContext(), attrs); |
| 376 | } |
| 377 | |
| 378 | @Override |
| 379 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 380 | return p instanceof PagedViewCellLayout.LayoutParams; |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 385 | return new PagedViewCellLayout.LayoutParams(p); |
| 386 | } |
| 387 | |
| 388 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 389 | /** |
| 390 | * Horizontal location of the item in the grid. |
| 391 | */ |
| 392 | @ViewDebug.ExportedProperty |
| 393 | public int cellX; |
| 394 | |
| 395 | /** |
| 396 | * Vertical location of the item in the grid. |
| 397 | */ |
| 398 | @ViewDebug.ExportedProperty |
| 399 | public int cellY; |
| 400 | |
| 401 | /** |
| 402 | * Number of cells spanned horizontally by the item. |
| 403 | */ |
| 404 | @ViewDebug.ExportedProperty |
| 405 | public int cellHSpan; |
| 406 | |
| 407 | /** |
| 408 | * Number of cells spanned vertically by the item. |
| 409 | */ |
| 410 | @ViewDebug.ExportedProperty |
| 411 | public int cellVSpan; |
| 412 | |
| 413 | /** |
| 414 | * Is this item currently being dragged |
| 415 | */ |
| 416 | public boolean isDragging; |
| 417 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 418 | // a data object that you can bind to this layout params |
| 419 | private Object mTag; |
| 420 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 421 | // X coordinate of the view in the layout. |
| 422 | @ViewDebug.ExportedProperty |
| 423 | int x; |
| 424 | // Y coordinate of the view in the layout. |
| 425 | @ViewDebug.ExportedProperty |
| 426 | int y; |
| 427 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 428 | public LayoutParams() { |
| 429 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
| 430 | cellHSpan = 1; |
| 431 | cellVSpan = 1; |
| 432 | } |
| 433 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 434 | public LayoutParams(Context c, AttributeSet attrs) { |
| 435 | super(c, attrs); |
| 436 | cellHSpan = 1; |
| 437 | cellVSpan = 1; |
| 438 | } |
| 439 | |
| 440 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 441 | super(source); |
| 442 | cellHSpan = 1; |
| 443 | cellVSpan = 1; |
| 444 | } |
| 445 | |
| 446 | public LayoutParams(LayoutParams source) { |
| 447 | super(source); |
| 448 | this.cellX = source.cellX; |
| 449 | this.cellY = source.cellY; |
| 450 | this.cellHSpan = source.cellHSpan; |
| 451 | this.cellVSpan = source.cellVSpan; |
| 452 | } |
| 453 | |
| 454 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
| 455 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
| 456 | this.cellX = cellX; |
| 457 | this.cellY = cellY; |
| 458 | this.cellHSpan = cellHSpan; |
| 459 | this.cellVSpan = cellVSpan; |
| 460 | } |
| 461 | |
| 462 | public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap, |
| 463 | int hStartPadding, int vStartPadding) { |
| 464 | |
| 465 | final int myCellHSpan = cellHSpan; |
| 466 | final int myCellVSpan = cellVSpan; |
| 467 | final int myCellX = cellX; |
| 468 | final int myCellY = cellY; |
| 469 | |
| 470 | width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - |
| 471 | leftMargin - rightMargin; |
| 472 | height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - |
| 473 | topMargin - bottomMargin; |
| 474 | |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 475 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 476 | x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin; |
| 477 | y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin; |
| 478 | } else { |
| 479 | x = myCellX * (cellWidth + widthGap) + leftMargin; |
| 480 | y = myCellY * (cellHeight + heightGap) + topMargin; |
| 481 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 484 | public Object getTag() { |
| 485 | return mTag; |
| 486 | } |
| 487 | |
| 488 | public void setTag(Object tag) { |
| 489 | mTag = tag; |
| 490 | } |
| 491 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 492 | public String toString() { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 493 | return "(" + this.cellX + ", " + this.cellY + ", " + |
| 494 | this.cellHSpan + ", " + this.cellVSpan + ")"; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 498 | |
| 499 | interface Page { |
| 500 | public int getPageChildCount(); |
| 501 | public View getChildOnPageAt(int i); |
| 502 | public void removeAllViewsOnPage(); |
| 503 | public void removeViewOnPageAt(int i); |
| 504 | public int indexOfChildOnPage(View v); |
| 505 | } |