• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.app.WallpaperManager;
20 import android.content.Context;
21 import android.graphics.Rect;
22 import android.view.View;
23 import android.view.ViewGroup;
24 
25 public class CellLayoutChildren extends ViewGroup {
26     static final String TAG = "CellLayoutChildren";
27 
28     // These are temporary variables to prevent having to allocate a new object just to
29     // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
30     private final int[] mTmpCellXY = new int[2];
31 
32     private final WallpaperManager mWallpaperManager;
33 
34     private int mCellWidth;
35     private int mCellHeight;
36 
37     private int mWidthGap;
38     private int mHeightGap;
39 
CellLayoutChildren(Context context)40     public CellLayoutChildren(Context context) {
41         super(context);
42         mWallpaperManager = WallpaperManager.getInstance(context);
43     }
44 
enableHardwareLayers()45     public void enableHardwareLayers() {
46         setLayerType(LAYER_TYPE_HARDWARE, null);
47     }
48 
setCellDimensions(int cellWidth, int cellHeight, int widthGap, int heightGap )49     public void setCellDimensions(int cellWidth, int cellHeight, int widthGap, int heightGap ) {
50         mCellWidth = cellWidth;
51         mCellHeight = cellHeight;
52         mWidthGap = widthGap;
53         mHeightGap = heightGap;
54     }
55 
getChildAt(int x, int y)56     public View getChildAt(int x, int y) {
57         final int count = getChildCount();
58         for (int i = 0; i < count; i++) {
59             View child = getChildAt(i);
60             CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
61 
62             if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
63                     (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) {
64                 return child;
65             }
66         }
67         return null;
68     }
69 
70     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)71     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
72         int count = getChildCount();
73         for (int i = 0; i < count; i++) {
74             View child = getChildAt(i);
75             measureChild(child);
76         }
77         int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
78         int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
79         setMeasuredDimension(widthSpecSize, heightSpecSize);
80     }
81 
setupLp(CellLayout.LayoutParams lp)82     public void setupLp(CellLayout.LayoutParams lp) {
83         lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
84     }
85 
measureChild(View child)86     public void measureChild(View child) {
87         final int cellWidth = mCellWidth;
88         final int cellHeight = mCellHeight;
89         CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
90 
91         lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap);
92         int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
93         int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
94                 MeasureSpec.EXACTLY);
95         child.measure(childWidthMeasureSpec, childheightMeasureSpec);
96     }
97 
98     @Override
onLayout(boolean changed, int l, int t, int r, int b)99     protected void onLayout(boolean changed, int l, int t, int r, int b) {
100         int count = getChildCount();
101         for (int i = 0; i < count; i++) {
102             final View child = getChildAt(i);
103             if (child.getVisibility() != GONE) {
104                 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
105 
106                 int childLeft = lp.x;
107                 int childTop = lp.y;
108                 child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
109 
110                 if (lp.dropped) {
111                     lp.dropped = false;
112 
113                     final int[] cellXY = mTmpCellXY;
114                     getLocationOnScreen(cellXY);
115                     mWallpaperManager.sendWallpaperCommand(getWindowToken(),
116                             WallpaperManager.COMMAND_DROP,
117                             cellXY[0] + childLeft + lp.width / 2,
118                             cellXY[1] + childTop + lp.height / 2, 0, null);
119                 }
120             }
121         }
122     }
123 
124     @Override
shouldDelayChildPressedState()125     public boolean shouldDelayChildPressedState() {
126         return false;
127     }
128 
129     @Override
requestChildFocus(View child, View focused)130     public void requestChildFocus(View child, View focused) {
131         super.requestChildFocus(child, focused);
132         if (child != null) {
133             Rect r = new Rect();
134             child.getDrawingRect(r);
135             requestRectangleOnScreen(r);
136         }
137     }
138 
139     @Override
cancelLongPress()140     public void cancelLongPress() {
141         super.cancelLongPress();
142 
143         // Cancel long press for all children
144         final int count = getChildCount();
145         for (int i = 0; i < count; i++) {
146             final View child = getChildAt(i);
147             child.cancelLongPress();
148         }
149     }
150 
151     @Override
setChildrenDrawingCacheEnabled(boolean enabled)152     protected void setChildrenDrawingCacheEnabled(boolean enabled) {
153         final int count = getChildCount();
154         for (int i = 0; i < count; i++) {
155             final View view = getChildAt(i);
156             view.setDrawingCacheEnabled(enabled);
157             // Update the drawing caches
158             if (!view.isHardwareAccelerated() && enabled) {
159                 view.buildDrawingCache(true);
160             }
161         }
162     }
163 
164     @Override
setChildrenDrawnWithCacheEnabled(boolean enabled)165     protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
166         super.setChildrenDrawnWithCacheEnabled(enabled);
167     }
168 }
169