blob: 1359a43e7e5f27c6b9e29fba826f89fa9a12d690 [file] [log] [blame]
Michael Jurka8245a862011-02-01 17:53:59 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
19import android.content.Context;
20import android.graphics.Rect;
21import android.view.MotionEvent;
22import android.view.View;
23import android.view.ViewGroup;
24
25/**
26 * An abstraction of the original CellLayout which supports laying out items
27 * which span multiple cells into a grid-like layout. Also supports dimming
28 * to give a preview of its contents.
29 */
30public class PagedViewCellLayoutChildren extends ViewGroup {
31 static final String TAG = "PagedViewCellLayout";
32
33 private boolean mCenterContent;
34
35 private int mCellWidth;
36 private int mCellHeight;
37 private int mWidthGap;
38 private int mHeightGap;
39
40 public PagedViewCellLayoutChildren(Context context) {
41 super(context);
42 setLayerType(LAYER_TYPE_HARDWARE, null);
43 }
44
45 @Override
46 public void cancelLongPress() {
47 super.cancelLongPress();
48
49 // Cancel long press for all children
50 final int count = getChildCount();
51 for (int i = 0; i < count; i++) {
52 final View child = getChildAt(i);
53 child.cancelLongPress();
54 }
55 }
56
57 public void setGap(int widthGap, int heightGap) {
58 mWidthGap = widthGap;
59 mHeightGap = heightGap;
60 requestLayout();
61 }
62
63 public void setCellDimensions(int width, int height) {
64 mCellWidth = width;
65 mCellHeight = height;
66 requestLayout();
67 }
68
69 @Override
70 public void requestChildFocus(View child, View focused) {
71 super.requestChildFocus(child, focused);
72 if (child != null) {
73 Rect r = new Rect();
74 child.getDrawingRect(r);
75 requestRectangleOnScreen(r);
76 }
77 }
78
79 @Override
80 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
81 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
82 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
83
84 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
85 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
86
87 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
88 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
89 }
90
91 final int count = getChildCount();
92 for (int i = 0; i < count; i++) {
93 View child = getChildAt(i);
94 PagedViewCellLayout.LayoutParams lp =
95 (PagedViewCellLayout.LayoutParams) child.getLayoutParams();
96 lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
97 ((ViewGroup)getParent()).getPaddingLeft(),
98 ((ViewGroup)getParent()).getPaddingTop());
99
100 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width,
101 MeasureSpec.EXACTLY);
102 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
103 MeasureSpec.EXACTLY);
104
105 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
106 }
107
108 setMeasuredDimension(widthSpecSize, heightSpecSize);
109 }
110
111 @Override
112 protected void onLayout(boolean changed, int l, int t, int r, int b) {
113 int count = getChildCount();
114
115 int offsetX = 0;
116 if (mCenterContent) {
117 // determine the max width of all the rows and center accordingly
118 int maxRowWidth = 0;
119 for (int i = 0; i < count; i++) {
120 View child = getChildAt(i);
121 if (child.getVisibility() != GONE) {
122 PagedViewCellLayout.LayoutParams lp =
123 (PagedViewCellLayout.LayoutParams) child.getLayoutParams();
124 maxRowWidth = Math.max(maxRowWidth, lp.x + lp.width);
125 }
126 }
127 offsetX = (getMeasuredWidth() / 2) - (maxRowWidth / 2);
128 }
129
130 for (int i = 0; i < count; i++) {
131 View child = getChildAt(i);
132 if (child.getVisibility() != GONE) {
133 PagedViewCellLayout.LayoutParams lp =
134 (PagedViewCellLayout.LayoutParams) child.getLayoutParams();
135
136 int childLeft = offsetX + lp.x;
137 int childTop = lp.y;
138 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
139 }
140 }
141 }
142
Michael Jurka8245a862011-02-01 17:53:59 -0800143 public void enableCenteredContent(boolean enabled) {
144 mCenterContent = enabled;
145 }
146
147 @Override
148 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
149 final int count = getChildCount();
150 for (int i = 0; i < count; i++) {
151 final View view = getChildAt(i);
152 view.setDrawingCacheEnabled(enabled);
153 // Update the drawing caches
154 if (!view.isHardwareAccelerated()) {
155 view.buildDrawingCache(true);
156 }
157 }
158 }
159}