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