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 | |
| 17 | package com.android.launcher2; |
| 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 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 27 | import com.android.launcher.R; |
| 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; |
| 39 | private int mCellWidth; |
| 40 | private int mCellHeight; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 41 | private int mWidthGap; |
| 42 | private int mHeightGap; |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 43 | protected PagedViewCellLayoutChildren mChildren; |
| 44 | private PagedViewCellLayoutChildren mHolographicChildren; |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 45 | private boolean mAllowHardwareLayerCreation = false; |
| 46 | private boolean mCreateHardwareLayersIfAllowed = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 47 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 48 | public PagedViewCellLayout(Context context) { |
| 49 | this(context, null); |
| 50 | } |
| 51 | |
| 52 | public PagedViewCellLayout(Context context, AttributeSet attrs) { |
| 53 | this(context, attrs, 0); |
| 54 | } |
| 55 | |
| 56 | public PagedViewCellLayout(Context context, AttributeSet attrs, int defStyle) { |
| 57 | super(context, attrs, defStyle); |
| 58 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 59 | setAlwaysDrawnWithCacheEnabled(false); |
| 60 | |
| 61 | // setup default cell parameters |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 62 | Resources resources = context.getResources(); |
| 63 | mCellWidth = resources.getDimensionPixelSize(R.dimen.apps_customize_cell_width); |
| 64 | mCellHeight = resources.getDimensionPixelSize(R.dimen.apps_customize_cell_height); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 65 | mCellCountX = LauncherModel.getCellCountX(); |
| 66 | mCellCountY = LauncherModel.getCellCountY(); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 67 | mWidthGap = mHeightGap = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 68 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 69 | mChildren = new PagedViewCellLayoutChildren(context); |
| 70 | mChildren.setCellDimensions(mCellWidth, mCellHeight); |
| 71 | mChildren.setGap(mWidthGap, mHeightGap); |
| 72 | |
| 73 | addView(mChildren); |
| 74 | mHolographicChildren = new PagedViewCellLayoutChildren(context); |
| 75 | mHolographicChildren.setAlpha(0f); |
| 76 | mHolographicChildren.setCellDimensions(mCellWidth, mCellHeight); |
| 77 | mHolographicChildren.setGap(mWidthGap, mHeightGap); |
| 78 | |
| 79 | addView(mHolographicChildren); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 80 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 81 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 82 | public int getCellWidth() { |
| 83 | return mCellWidth; |
| 84 | } |
| 85 | |
| 86 | public int getCellHeight() { |
| 87 | return mCellHeight; |
| 88 | } |
| 89 | |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 90 | public void allowHardwareLayerCreation() { |
| 91 | // This is called after the first time we launch into All Apps. Before that point, |
| 92 | // there's no need for hardware layers here since there's a hardware layer set on the |
| 93 | // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here |
| 94 | // before the animation is done slows down the animation |
| 95 | if (!mAllowHardwareLayerCreation) { |
| 96 | mAllowHardwareLayerCreation = true; |
| 97 | if (mCreateHardwareLayersIfAllowed) { |
| 98 | createHardwareLayers(); |
| 99 | } |
| 100 | } |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public void setAlpha(float alpha) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 105 | mChildren.setAlpha(alpha); |
| 106 | mHolographicChildren.setAlpha(1.0f - alpha); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 109 | void destroyHardwareLayers() { |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 110 | // called when a page is no longer visible (triggered by loadAssociatedPages -> |
| 111 | // removeAllViewsOnPage) |
| 112 | mCreateHardwareLayersIfAllowed = false; |
| 113 | if (mAllowHardwareLayerCreation) { |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 114 | mChildren.destroyHardwareLayer(); |
| 115 | mHolographicChildren.destroyHardwareLayer(); |
| 116 | } |
| 117 | } |
| 118 | void createHardwareLayers() { |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 119 | // called when a page is visible (triggered by loadAssociatedPages -> syncPageItems) |
| 120 | mCreateHardwareLayersIfAllowed = true; |
| 121 | if (mAllowHardwareLayerCreation) { |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 122 | mChildren.createHardwareLayer(); |
| 123 | mHolographicChildren.createHardwareLayer(); |
| 124 | } |
| 125 | } |
| 126 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public void cancelLongPress() { |
| 129 | super.cancelLongPress(); |
| 130 | |
| 131 | // Cancel long press for all children |
| 132 | final int count = getChildCount(); |
| 133 | for (int i = 0; i < count; i++) { |
| 134 | final View child = getChildAt(i); |
| 135 | child.cancelLongPress(); |
| 136 | } |
| 137 | } |
| 138 | |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 139 | /** Syncs the holographic icon views to the child icon views */ |
| 140 | public void reloadHolographicIcons(boolean createHolographicOutlines) { |
| 141 | if (createHolographicOutlines) { |
| 142 | mChildren.loadHolographicOutlines(); |
| 143 | } else { |
| 144 | mChildren.clearHolographicOutlines(); |
| 145 | } |
| 146 | } |
| 147 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 148 | public boolean addViewToCellLayout(View child, int index, int childId, |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 149 | PagedViewCellLayout.LayoutParams params) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 150 | final PagedViewCellLayout.LayoutParams lp = params; |
| 151 | |
| 152 | // Generate an id for each view, this assumes we have at most 256x256 cells |
| 153 | // per workspace screen |
| 154 | if (lp.cellX >= 0 && lp.cellX <= (mCellCountX - 1) && |
| 155 | lp.cellY >= 0 && (lp.cellY <= mCellCountY - 1)) { |
| 156 | // If the horizontal or vertical span is set to -1, it is taken to |
| 157 | // mean that it spans the extent of the CellLayout |
| 158 | if (lp.cellHSpan < 0) lp.cellHSpan = mCellCountX; |
| 159 | if (lp.cellVSpan < 0) lp.cellVSpan = mCellCountY; |
| 160 | |
| 161 | child.setId(childId); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 162 | mChildren.addView(child, index, lp); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 163 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 164 | if (child instanceof PagedViewIcon) { |
| 165 | PagedViewIcon pagedViewIcon = (PagedViewIcon) child; |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 166 | if (mAllowHardwareLayerCreation) { |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 167 | pagedViewIcon.disableCache(); |
| 168 | } |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 169 | mHolographicChildren.addView(pagedViewIcon.getHolographicOutlineView(), |
| 170 | index, lp); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 171 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 172 | return true; |
| 173 | } |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | @Override |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 178 | public void removeAllViewsOnPage() { |
| 179 | mChildren.removeAllViews(); |
| 180 | mHolographicChildren.removeAllViews(); |
Michael Jurka | c5e4902 | 2011-02-16 12:04:02 -0800 | [diff] [blame] | 181 | destroyHardwareLayers(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 185 | public void removeViewOnPageAt(int index) { |
| 186 | mChildren.removeViewAt(index); |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 187 | mHolographicChildren.removeViewAt(index); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public int getPageChildCount() { |
| 192 | return mChildren.getChildCount(); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public View getChildOnPageAt(int i) { |
| 197 | return mChildren.getChildAt(i); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public int indexOfChildOnPage(View v) { |
| 202 | return mChildren.indexOfChild(v); |
| 203 | } |
| 204 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 205 | public int getCellCountX() { |
| 206 | return mCellCountX; |
| 207 | } |
| 208 | |
| 209 | public int getCellCountY() { |
| 210 | return mCellCountY; |
| 211 | } |
| 212 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 213 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 214 | // TODO: currently ignoring padding |
| 215 | |
| 216 | 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 | |
| 226 | final int cellWidth = mCellWidth; |
| 227 | final int cellHeight = mCellHeight; |
| 228 | |
| 229 | int numWidthGaps = mCellCountX - 1; |
| 230 | int numHeightGaps = mCellCountY - 1; |
| 231 | |
| 232 | int vSpaceLeft = heightSpecSize - mPaddingTop |
| 233 | - mPaddingBottom - (cellHeight * mCellCountY); |
| 234 | int heightGap = vSpaceLeft / numHeightGaps; |
| 235 | |
| 236 | int hSpaceLeft = widthSpecSize - mPaddingLeft |
| 237 | - mPaddingRight - (cellWidth * mCellCountX); |
| 238 | int widthGap = hSpaceLeft / numWidthGaps; |
| 239 | |
| 240 | // center it around the min gaps |
| 241 | int minGap = Math.min(widthGap, heightGap); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 242 | /* |
| 243 | if (minGap < heightGap) { |
| 244 | // vertical space has shrunken, so change padding accordingly |
| 245 | paddingTop += ((heightGap - minGap) * (mCellCountY - 1)) / 2; |
| 246 | } else if (minGap < widthGap) { |
| 247 | // horizontal space has shrunken, so change padding accordingly |
| 248 | paddingLeft += ((widthGap - minGap) * (mCellCountX - 1)) / 2; |
| 249 | } |
| 250 | */ |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 251 | if (mWidthGap > -1 && mHeightGap > -1) { |
| 252 | widthGap = mWidthGap; |
| 253 | heightGap = mHeightGap; |
| 254 | } else { |
| 255 | widthGap = heightGap = minGap; |
| 256 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 257 | |
| 258 | int newWidth = mPaddingLeft + mPaddingRight + (mCellCountX * cellWidth) + |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 259 | ((mCellCountX - 1) * widthGap); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 260 | int newHeight = mPaddingTop + mPaddingBottom + (mCellCountY * cellHeight) + |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 261 | ((mCellCountY - 1) * heightGap); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 262 | |
| 263 | final int count = getChildCount(); |
| 264 | for (int i = 0; i < count; i++) { |
| 265 | View child = getChildAt(i); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 266 | int childWidthMeasureSpec = |
| 267 | MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY); |
| 268 | int childheightMeasureSpec = |
| 269 | MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 270 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 271 | } |
| 272 | |
| 273 | setMeasuredDimension(newWidth, newHeight); |
| 274 | } |
| 275 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 276 | int getContentWidth() { |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 277 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 278 | // Return the distance from the left edge of the content of the leftmost icon to |
| 279 | // the right edge of the content of the rightmost icon |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 280 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 281 | // icons are centered within cells, find out how much padding that accounts for |
| 282 | return getWidthBeforeFirstLayout() - (mCellWidth - Utilities.getIconContentSize()); |
| 283 | } else { |
| 284 | return getWidthBeforeFirstLayout() + mPaddingLeft + mPaddingRight; |
| 285 | } |
Michael Jurka | 0413dfa | 2011-04-05 16:52:32 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 288 | int getContentHeight() { |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame^] | 289 | if (mCellCountY > 0) { |
| 290 | return mCellCountY * mCellHeight + (mCellCountY - 1) * Math.max(0, mHeightGap); |
| 291 | } |
| 292 | return 0; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Michael Jurka | d92e741 | 2011-04-05 17:07:27 -0700 | [diff] [blame] | 295 | int getWidthBeforeFirstLayout() { |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame^] | 296 | if (mCellCountX > 0) { |
| 297 | return mCellCountX * mCellWidth + (mCellCountX - 1) * Math.max(0, mWidthGap); |
| 298 | } |
| 299 | return 0; |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 302 | @Override |
| 303 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 304 | int count = getChildCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 305 | for (int i = 0; i < count; i++) { |
| 306 | View child = getChildAt(i); |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 307 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 308 | child.layout(0, 0, r - l, b - t); |
| 309 | } else { |
| 310 | child.layout(mPaddingLeft, mPaddingTop, getMeasuredWidth() - mPaddingRight, |
| 311 | getMeasuredHeight() - mPaddingBottom); |
| 312 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 317 | public boolean onTouchEvent(MotionEvent event) { |
| 318 | return super.onTouchEvent(event) || true; |
| 319 | } |
| 320 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 321 | public void enableCenteredContent(boolean enabled) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 322 | mChildren.enableCenteredContent(enabled); |
| 323 | mHolographicChildren.enableCenteredContent(enabled); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 326 | @Override |
| 327 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 328 | mChildren.setChildrenDrawingCacheEnabled(enabled); |
| 329 | mHolographicChildren.setChildrenDrawingCacheEnabled(enabled); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | public void setCellCount(int xCount, int yCount) { |
| 333 | mCellCountX = xCount; |
| 334 | mCellCountY = yCount; |
| 335 | requestLayout(); |
| 336 | } |
| 337 | |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 338 | public void setGap(int widthGap, int heightGap) { |
| 339 | mWidthGap = widthGap; |
| 340 | mHeightGap = heightGap; |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 341 | mChildren.setGap(widthGap, heightGap); |
| 342 | mHolographicChildren.setGap(widthGap, heightGap); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 345 | public int[] getCellCountForDimensions(int width, int height) { |
| 346 | // Always assume we're working with the smallest span to make sure we |
| 347 | // reserve enough space in both orientations |
| 348 | int smallerSize = Math.min(mCellWidth, mCellHeight); |
| 349 | |
| 350 | // Always round up to next largest cell |
| 351 | int spanX = (width + smallerSize) / smallerSize; |
| 352 | int spanY = (height + smallerSize) / smallerSize; |
| 353 | |
| 354 | return new int[] { spanX, spanY }; |
| 355 | } |
| 356 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 357 | /** |
| 358 | * Start dragging the specified child |
| 359 | * |
| 360 | * @param child The child that is being dragged |
| 361 | */ |
| 362 | void onDragChild(View child) { |
| 363 | PagedViewCellLayout.LayoutParams lp = (PagedViewCellLayout.LayoutParams) child.getLayoutParams(); |
| 364 | lp.isDragging = true; |
| 365 | } |
| 366 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 367 | /** |
| 368 | * Estimates the number of cells that the specified width would take up. |
| 369 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 370 | public int estimateCellHSpan(int width) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 371 | // TODO: we need to take widthGap into effect |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 372 | return (width + mCellWidth) / mCellWidth; |
| 373 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 374 | |
| 375 | /** |
| 376 | * Estimates the number of cells that the specified height would take up. |
| 377 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 378 | public int estimateCellVSpan(int height) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 379 | // TODO: we need to take heightGap into effect |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 380 | return (height + mCellHeight) / mCellHeight; |
| 381 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 382 | |
| 383 | /** |
| 384 | * Estimates the width that the number of vSpan cells will take up. |
| 385 | */ |
| 386 | public int estimateCellWidth(int hSpan) { |
| 387 | // TODO: we need to take widthGap into effect |
| 388 | return hSpan * mCellWidth; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Estimates the height that the number of vSpan cells will take up. |
| 393 | */ |
| 394 | public int estimateCellHeight(int vSpan) { |
| 395 | // TODO: we need to take heightGap into effect |
| 396 | return vSpan * mCellHeight; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 399 | @Override |
| 400 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 401 | return new PagedViewCellLayout.LayoutParams(getContext(), attrs); |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 406 | return p instanceof PagedViewCellLayout.LayoutParams; |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 411 | return new PagedViewCellLayout.LayoutParams(p); |
| 412 | } |
| 413 | |
| 414 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 415 | /** |
| 416 | * Horizontal location of the item in the grid. |
| 417 | */ |
| 418 | @ViewDebug.ExportedProperty |
| 419 | public int cellX; |
| 420 | |
| 421 | /** |
| 422 | * Vertical location of the item in the grid. |
| 423 | */ |
| 424 | @ViewDebug.ExportedProperty |
| 425 | public int cellY; |
| 426 | |
| 427 | /** |
| 428 | * Number of cells spanned horizontally by the item. |
| 429 | */ |
| 430 | @ViewDebug.ExportedProperty |
| 431 | public int cellHSpan; |
| 432 | |
| 433 | /** |
| 434 | * Number of cells spanned vertically by the item. |
| 435 | */ |
| 436 | @ViewDebug.ExportedProperty |
| 437 | public int cellVSpan; |
| 438 | |
| 439 | /** |
| 440 | * Is this item currently being dragged |
| 441 | */ |
| 442 | public boolean isDragging; |
| 443 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 444 | // a data object that you can bind to this layout params |
| 445 | private Object mTag; |
| 446 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 447 | // X coordinate of the view in the layout. |
| 448 | @ViewDebug.ExportedProperty |
| 449 | int x; |
| 450 | // Y coordinate of the view in the layout. |
| 451 | @ViewDebug.ExportedProperty |
| 452 | int y; |
| 453 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 454 | public LayoutParams() { |
| 455 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
| 456 | cellHSpan = 1; |
| 457 | cellVSpan = 1; |
| 458 | } |
| 459 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 460 | public LayoutParams(Context c, AttributeSet attrs) { |
| 461 | super(c, attrs); |
| 462 | cellHSpan = 1; |
| 463 | cellVSpan = 1; |
| 464 | } |
| 465 | |
| 466 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 467 | super(source); |
| 468 | cellHSpan = 1; |
| 469 | cellVSpan = 1; |
| 470 | } |
| 471 | |
| 472 | public LayoutParams(LayoutParams source) { |
| 473 | super(source); |
| 474 | this.cellX = source.cellX; |
| 475 | this.cellY = source.cellY; |
| 476 | this.cellHSpan = source.cellHSpan; |
| 477 | this.cellVSpan = source.cellVSpan; |
| 478 | } |
| 479 | |
| 480 | public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { |
| 481 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
| 482 | this.cellX = cellX; |
| 483 | this.cellY = cellY; |
| 484 | this.cellHSpan = cellHSpan; |
| 485 | this.cellVSpan = cellVSpan; |
| 486 | } |
| 487 | |
| 488 | public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap, |
| 489 | int hStartPadding, int vStartPadding) { |
| 490 | |
| 491 | final int myCellHSpan = cellHSpan; |
| 492 | final int myCellVSpan = cellVSpan; |
| 493 | final int myCellX = cellX; |
| 494 | final int myCellY = cellY; |
| 495 | |
| 496 | width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - |
| 497 | leftMargin - rightMargin; |
| 498 | height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - |
| 499 | topMargin - bottomMargin; |
| 500 | |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 501 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 502 | x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin; |
| 503 | y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin; |
| 504 | } else { |
| 505 | x = myCellX * (cellWidth + widthGap) + leftMargin; |
| 506 | y = myCellY * (cellHeight + heightGap) + topMargin; |
| 507 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 510 | public Object getTag() { |
| 511 | return mTag; |
| 512 | } |
| 513 | |
| 514 | public void setTag(Object tag) { |
| 515 | mTag = tag; |
| 516 | } |
| 517 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 518 | public String toString() { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 519 | return "(" + this.cellX + ", " + this.cellY + ", " + |
| 520 | this.cellHSpan + ", " + this.cellVSpan + ")"; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | } |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 524 | |
| 525 | interface Page { |
| 526 | public int getPageChildCount(); |
| 527 | public View getChildOnPageAt(int i); |
| 528 | public void removeAllViewsOnPage(); |
| 529 | public void removeViewOnPageAt(int i); |
| 530 | public int indexOfChildOnPage(View v); |
| 531 | } |