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 | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 22 | import android.util.Log; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 23 | import android.view.MotionEvent; |
| 24 | import android.view.View; |
| 25 | import android.view.ViewDebug; |
| 26 | import android.view.ViewGroup; |
| 27 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 28 | import com.android.launcher.R; |
| 29 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 30 | /** |
| 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 Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 35 | public class PagedViewCellLayout extends ViewGroup implements Page { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 36 | static final String TAG = "PagedViewCellLayout"; |
| 37 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 38 | private int mCellCountX; |
| 39 | private int mCellCountY; |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 40 | private int mOriginalCellWidth; |
| 41 | private int mOriginalCellHeight; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 42 | private int mCellWidth; |
| 43 | private int mCellHeight; |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame^] | 44 | private int mOriginalWidthGap; |
| 45 | private int mOriginalHeightGap; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 46 | private int mWidthGap; |
| 47 | private int mHeightGap; |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 48 | private int mMaxGap; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 49 | private float mPeekWidth; |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 50 | protected PagedViewCellLayoutChildren mChildren; |
| 51 | private PagedViewCellLayoutChildren mHolographicChildren; |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 52 | private boolean mAllowHardwareLayerCreation = false; |
| 53 | private boolean mCreateHardwareLayersIfAllowed = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 54 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 55 | 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 Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 66 | setAlwaysDrawnWithCacheEnabled(false); |
| 67 | |
| 68 | // setup default cell parameters |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 69 | Resources resources = context.getResources(); |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 70 | mOriginalCellWidth = mCellWidth = |
| 71 | resources.getDimensionPixelSize(R.dimen.apps_customize_cell_width); |
| 72 | mOriginalCellHeight = mCellHeight = |
| 73 | resources.getDimensionPixelSize(R.dimen.apps_customize_cell_height); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 74 | mPeekWidth = resources.getDimensionPixelSize(R.dimen.apps_customize_peek_width); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 75 | mCellCountX = LauncherModel.getCellCountX(); |
| 76 | mCellCountY = LauncherModel.getCellCountY(); |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame^] | 77 | mOriginalHeightGap = mOriginalHeightGap = mWidthGap = mHeightGap = -1; |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 78 | mMaxGap = resources.getDimensionPixelSize(R.dimen.apps_customize_max_gap); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 79 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 80 | 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 Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 91 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 92 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 93 | public int getCellWidth() { |
| 94 | return mCellWidth; |
| 95 | } |
| 96 | |
| 97 | public int getCellHeight() { |
| 98 | return mCellHeight; |
| 99 | } |
| 100 | |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 101 | 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 Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public void setAlpha(float alpha) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 116 | mChildren.setAlpha(HolographicOutlineHelper.viewAlphaInterpolator(alpha)); |
| 117 | mHolographicChildren.setAlpha(HolographicOutlineHelper.highlightAlphaInterpolator(alpha)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 120 | void destroyHardwareLayers() { |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 121 | // called when a page is no longer visible (triggered by loadAssociatedPages -> |
| 122 | // removeAllViewsOnPage) |
| 123 | mCreateHardwareLayersIfAllowed = false; |
| 124 | if (mAllowHardwareLayerCreation) { |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 125 | mChildren.destroyHardwareLayer(); |
| 126 | mHolographicChildren.destroyHardwareLayer(); |
| 127 | } |
| 128 | } |
| 129 | void createHardwareLayers() { |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 130 | // called when a page is visible (triggered by loadAssociatedPages -> syncPageItems) |
| 131 | mCreateHardwareLayersIfAllowed = true; |
| 132 | if (mAllowHardwareLayerCreation) { |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 133 | mChildren.createHardwareLayer(); |
| 134 | mHolographicChildren.createHardwareLayer(); |
| 135 | } |
| 136 | } |
| 137 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 138 | @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 Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 151 | PagedViewCellLayout.LayoutParams params) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 152 | 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 Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 164 | mChildren.addView(child, index, lp); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 165 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 166 | if (child instanceof PagedViewIcon) { |
| 167 | PagedViewIcon pagedViewIcon = (PagedViewIcon) child; |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 168 | if (mAllowHardwareLayerCreation) { |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 169 | pagedViewIcon.disableCache(); |
| 170 | } |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 171 | mHolographicChildren.addView(pagedViewIcon.getHolographicOutlineView(), |
| 172 | index, lp); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 173 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | @Override |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 180 | public void removeAllViewsOnPage() { |
| 181 | mChildren.removeAllViews(); |
| 182 | mHolographicChildren.removeAllViews(); |
Michael Jurka | c5e4902 | 2011-02-16 12:04:02 -0800 | [diff] [blame] | 183 | destroyHardwareLayers(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 187 | public void removeViewOnPageAt(int index) { |
| 188 | mChildren.removeViewAt(index); |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 189 | mHolographicChildren.removeViewAt(index); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 190 | } |
| 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 Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 207 | public int getCellCountX() { |
| 208 | return mCellCountX; |
| 209 | } |
| 210 | |
| 211 | public int getCellCountY() { |
| 212 | return mCellCountY; |
| 213 | } |
| 214 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 215 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 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 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 226 | int numWidthGaps = mCellCountX - 1; |
| 227 | int numHeightGaps = mCellCountY - 1; |
| 228 | |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame^] | 229 | if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) { |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 230 | 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 Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 236 | |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 237 | mChildren.setGap(mWidthGap, mHeightGap); |
| 238 | mHolographicChildren.setGap(mWidthGap, mHeightGap); |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame^] | 239 | } else { |
| 240 | mWidthGap = mOriginalWidthGap; |
| 241 | mHeightGap = mOriginalHeightGap; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 242 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 243 | |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 244 | // 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 Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 254 | |
| 255 | final int count = getChildCount(); |
| 256 | for (int i = 0; i < count; i++) { |
| 257 | View child = getChildAt(i); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 258 | int childWidthMeasureSpec = |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 259 | MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft - |
| 260 | mPaddingRight, MeasureSpec.EXACTLY); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 261 | int childheightMeasureSpec = |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 262 | MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop - |
| 263 | mPaddingBottom, MeasureSpec.EXACTLY); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 264 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 265 | } |
| 266 | |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 267 | setMeasuredDimension(newWidth, newHeight); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 270 | int getContentWidth() { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 271 | return getWidthBeforeFirstLayout() + mPaddingLeft + mPaddingRight; |
Michael Jurka | 0413dfa | 2011-04-05 16:52:32 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 274 | int getContentHeight() { |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 275 | if (mCellCountY > 0) { |
| 276 | return mCellCountY * mCellHeight + (mCellCountY - 1) * Math.max(0, mHeightGap); |
| 277 | } |
| 278 | return 0; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Michael Jurka | d92e741 | 2011-04-05 17:07:27 -0700 | [diff] [blame] | 281 | int getWidthBeforeFirstLayout() { |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 282 | if (mCellCountX > 0) { |
| 283 | return mCellCountX * mCellWidth + (mCellCountX - 1) * Math.max(0, mWidthGap); |
| 284 | } |
| 285 | return 0; |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 288 | @Override |
| 289 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 290 | int count = getChildCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 291 | for (int i = 0; i < count; i++) { |
| 292 | View child = getChildAt(i); |
Michael Jurka | 9bfe2b2 | 2011-06-02 16:41:34 -0700 | [diff] [blame] | 293 | child.layout(mPaddingLeft, mPaddingTop, |
| 294 | r - l - mPaddingRight, b - t - mPaddingBottom); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 299 | public boolean onTouchEvent(MotionEvent event) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 300 | 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 Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 316 | public void enableCenteredContent(boolean enabled) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 317 | mChildren.enableCenteredContent(enabled); |
| 318 | mHolographicChildren.enableCenteredContent(enabled); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 321 | @Override |
| 322 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 323 | mChildren.setChildrenDrawingCacheEnabled(enabled); |
| 324 | mHolographicChildren.setChildrenDrawingCacheEnabled(enabled); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | public void setCellCount(int xCount, int yCount) { |
| 328 | mCellCountX = xCount; |
| 329 | mCellCountY = yCount; |
| 330 | requestLayout(); |
| 331 | } |
| 332 | |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 333 | public void setGap(int widthGap, int heightGap) { |
| 334 | mWidthGap = widthGap; |
| 335 | mHeightGap = heightGap; |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 336 | mChildren.setGap(widthGap, heightGap); |
| 337 | mHolographicChildren.setGap(widthGap, heightGap); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 340 | 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 Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 352 | /** |
| 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 362 | /** |
| 363 | * Estimates the number of cells that the specified width would take up. |
| 364 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 365 | public int estimateCellHSpan(int width) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 366 | // 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 376 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 377 | |
| 378 | /** |
| 379 | * Estimates the number of cells that the specified height would take up. |
| 380 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 381 | public int estimateCellVSpan(int height) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 382 | // 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 397 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 398 | |
| 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 415 | @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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 460 | // a data object that you can bind to this layout params |
| 461 | private Object mTag; |
| 462 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 463 | // 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 470 | public LayoutParams() { |
| 471 | super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
| 472 | cellHSpan = 1; |
| 473 | cellVSpan = 1; |
| 474 | } |
| 475 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 476 | 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 Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 517 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 518 | 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 Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 526 | public Object getTag() { |
| 527 | return mTag; |
| 528 | } |
| 529 | |
| 530 | public void setTag(Object tag) { |
| 531 | mTag = tag; |
| 532 | } |
| 533 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 534 | public String toString() { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 535 | return "(" + this.cellX + ", " + this.cellY + ", " + |
| 536 | this.cellHSpan + ", " + this.cellVSpan + ")"; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 540 | |
| 541 | interface 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 | } |