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