Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [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; |
| 20 | import android.graphics.Rect; |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 21 | import android.view.View; |
| 22 | import android.view.ViewGroup; |
| 23 | |
| 24 | /** |
| 25 | * An abstraction of the original CellLayout which supports laying out items |
| 26 | * which span multiple cells into a grid-like layout. Also supports dimming |
| 27 | * to give a preview of its contents. |
| 28 | */ |
| 29 | public class PagedViewCellLayoutChildren extends ViewGroup { |
| 30 | static final String TAG = "PagedViewCellLayout"; |
| 31 | |
| 32 | private boolean mCenterContent; |
| 33 | |
| 34 | private int mCellWidth; |
| 35 | private int mCellHeight; |
| 36 | private int mWidthGap; |
| 37 | private int mHeightGap; |
| 38 | |
| 39 | public PagedViewCellLayoutChildren(Context context) { |
| 40 | super(context); |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public void cancelLongPress() { |
| 45 | super.cancelLongPress(); |
| 46 | |
| 47 | // Cancel long press for all children |
| 48 | final int count = getChildCount(); |
| 49 | for (int i = 0; i < count; i++) { |
| 50 | final View child = getChildAt(i); |
| 51 | child.cancelLongPress(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | public void setGap(int widthGap, int heightGap) { |
| 56 | mWidthGap = widthGap; |
| 57 | mHeightGap = heightGap; |
| 58 | requestLayout(); |
| 59 | } |
| 60 | |
| 61 | public void setCellDimensions(int width, int height) { |
| 62 | mCellWidth = width; |
| 63 | mCellHeight = height; |
| 64 | requestLayout(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void requestChildFocus(View child, View focused) { |
| 69 | super.requestChildFocus(child, focused); |
| 70 | if (child != null) { |
| 71 | Rect r = new Rect(); |
| 72 | child.getDrawingRect(r); |
| 73 | requestRectangleOnScreen(r); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 79 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
| 80 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 81 | |
| 82 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 83 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 84 | |
| 85 | if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 86 | throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); |
| 87 | } |
| 88 | |
| 89 | final int count = getChildCount(); |
| 90 | for (int i = 0; i < count; i++) { |
| 91 | View child = getChildAt(i); |
| 92 | PagedViewCellLayout.LayoutParams lp = |
| 93 | (PagedViewCellLayout.LayoutParams) child.getLayoutParams(); |
| 94 | lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap, |
| 95 | ((ViewGroup)getParent()).getPaddingLeft(), |
| 96 | ((ViewGroup)getParent()).getPaddingTop()); |
| 97 | |
| 98 | int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, |
| 99 | MeasureSpec.EXACTLY); |
| 100 | int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, |
| 101 | MeasureSpec.EXACTLY); |
| 102 | |
| 103 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 104 | } |
| 105 | |
| 106 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 111 | int count = getChildCount(); |
| 112 | |
| 113 | int offsetX = 0; |
| 114 | if (mCenterContent) { |
| 115 | // determine the max width of all the rows and center accordingly |
| 116 | int maxRowWidth = 0; |
| 117 | for (int i = 0; i < count; i++) { |
| 118 | View child = getChildAt(i); |
| 119 | if (child.getVisibility() != GONE) { |
| 120 | PagedViewCellLayout.LayoutParams lp = |
| 121 | (PagedViewCellLayout.LayoutParams) child.getLayoutParams(); |
| 122 | maxRowWidth = Math.max(maxRowWidth, lp.x + lp.width); |
| 123 | } |
| 124 | } |
| 125 | offsetX = (getMeasuredWidth() / 2) - (maxRowWidth / 2); |
| 126 | } |
| 127 | |
| 128 | for (int i = 0; i < count; i++) { |
| 129 | View child = getChildAt(i); |
| 130 | if (child.getVisibility() != GONE) { |
| 131 | PagedViewCellLayout.LayoutParams lp = |
| 132 | (PagedViewCellLayout.LayoutParams) child.getLayoutParams(); |
| 133 | |
| 134 | int childLeft = offsetX + lp.x; |
| 135 | int childTop = lp.y; |
| 136 | child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 141 | void destroyHardwareLayer() { |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 142 | if (getLayerType() != LAYER_TYPE_NONE) { |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 143 | setLayerType(LAYER_TYPE_NONE, null); |
| 144 | } |
| 145 | } |
| 146 | void createHardwareLayer() { |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 147 | if (getLayerType() != LAYER_TYPE_HARDWARE) { |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 148 | setLayerType(LAYER_TYPE_HARDWARE, null); |
| 149 | } |
| 150 | } |
| 151 | |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 152 | public void enableCenteredContent(boolean enabled) { |
| 153 | mCenterContent = enabled; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 158 | final int count = getChildCount(); |
| 159 | for (int i = 0; i < count; i++) { |
| 160 | final View view = getChildAt(i); |
| 161 | view.setDrawingCacheEnabled(enabled); |
| 162 | // Update the drawing caches |
| 163 | if (!view.isHardwareAccelerated()) { |
| 164 | view.buildDrawingCache(true); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |