18c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka/* 28c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * Copyright (C) 2008 The Android Open Source Project 38c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * 48c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * Licensed under the Apache License, Version 2.0 (the "License"); 58c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * you may not use this file except in compliance with the License. 68c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * You may obtain a copy of the License at 78c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * 88c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * http://www.apache.org/licenses/LICENSE-2.0 98c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * 108c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * Unless required by applicable law or agreed to in writing, software 118c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * distributed under the License is distributed on an "AS IS" BASIS, 128c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * See the License for the specific language governing permissions and 148c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka * limitations under the License. 158c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka */ 168c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 178c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkapackage com.android.launcher2; 188c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 198c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.app.WallpaperManager; 208c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.content.Context; 21eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chungimport android.graphics.Canvas; 22eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chungimport android.graphics.Paint; 238c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.graphics.Rect; 248c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.view.View; 25cf6125c2d30ce02d8ab6cbe8e37a20f6a831e216Michael Jurkaimport android.view.ViewGroup; 268c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 27a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurkapublic class ShortcutAndWidgetContainer extends ViewGroup { 288c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka static final String TAG = "CellLayoutChildren"; 298c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 308c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // These are temporary variables to prevent having to allocate a new object just to 318c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // return an (x, y) value from helper functions. Do NOT use them to maintain other state. 328c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka private final int[] mTmpCellXY = new int[2]; 338c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 348c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka private final WallpaperManager mWallpaperManager; 358c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 36d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen private int mCellWidth; 37d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen private int mCellHeight; 38d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen 398c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka private int mWidthGap; 408c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka private int mHeightGap; 418c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 42a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurka public ShortcutAndWidgetContainer(Context context) { 438c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka super(context); 448c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mWallpaperManager = WallpaperManager.getInstance(context); 452801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen } 462801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen 472801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen public void enableHardwareLayers() { 48bdb5c5342adc550559fd723af461e53248f2fba8Michael Jurka setLayerType(LAYER_TYPE_HARDWARE, null); 498c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 508c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 517f4eabe3709a72b416569136e4a095431c493c8bAdam Cohen public void setCellDimensions(int cellWidth, int cellHeight, int widthGap, int heightGap ) { 528c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mCellWidth = cellWidth; 538c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mCellHeight = cellHeight; 548c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mWidthGap = widthGap; 558c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mHeightGap = heightGap; 568c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 578c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 588c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka public View getChildAt(int x, int y) { 598c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final int count = getChildCount(); 608c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka for (int i = 0; i < count; i++) { 618c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka View child = getChildAt(i); 628c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); 638c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 648c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) && 65f579b5041afe8272c79f9f13001120d37eeeee7bAdam Cohen (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) { 668c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka return child; 678c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 688c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 698c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka return null; 708c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 718c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 728c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 73eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung protected void dispatchDraw(Canvas canvas) { 743a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka @SuppressWarnings("all") // suppress dead code warning 753a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka final boolean debug = false; 763a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka if (debug) { 773a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka // Debug drawing for hit space 78eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung Paint p = new Paint(); 79eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung p.setColor(0x6600FF00); 80eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung for (int i = getChildCount() - 1; i >= 0; i--) { 81eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung final View child = getChildAt(i); 82eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); 83eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung 84eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung canvas.drawRect(lp.x, lp.y, lp.x + lp.width, lp.y + lp.height, p); 85eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung } 86eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung } 87eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung super.dispatchDraw(canvas); 88eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung } 89eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung 90eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung @Override 918c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 928c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int count = getChildCount(); 938c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka for (int i = 0; i < count; i++) { 948c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka View child = getChildAt(i); 95d5e42733799817d11d739b0a882da8dda721126dAdam Cohen measureChild(child); 968c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 978c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); 988c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); 998c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka setMeasuredDimension(widthSpecSize, heightSpecSize); 1008c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1018c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 102bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen public void setupLp(CellLayout.LayoutParams lp) { 103bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap); 104bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen } 105bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen 106d5e42733799817d11d739b0a882da8dda721126dAdam Cohen public void measureChild(View child) { 107d5e42733799817d11d739b0a882da8dda721126dAdam Cohen final int cellWidth = mCellWidth; 108d5e42733799817d11d739b0a882da8dda721126dAdam Cohen final int cellHeight = mCellHeight; 109d5e42733799817d11d739b0a882da8dda721126dAdam Cohen CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); 110d5e42733799817d11d739b0a882da8dda721126dAdam Cohen 1117f4eabe3709a72b416569136e4a095431c493c8bAdam Cohen lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap); 112d5e42733799817d11d739b0a882da8dda721126dAdam Cohen int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); 113d5e42733799817d11d739b0a882da8dda721126dAdam Cohen int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, 114d5e42733799817d11d739b0a882da8dda721126dAdam Cohen MeasureSpec.EXACTLY); 115d5e42733799817d11d739b0a882da8dda721126dAdam Cohen child.measure(childWidthMeasureSpec, childheightMeasureSpec); 116d5e42733799817d11d739b0a882da8dda721126dAdam Cohen } 117d5e42733799817d11d739b0a882da8dda721126dAdam Cohen 1188c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 1198c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka protected void onLayout(boolean changed, int l, int t, int r, int b) { 1208c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int count = getChildCount(); 1218c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka for (int i = 0; i < count; i++) { 1228c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final View child = getChildAt(i); 1238c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka if (child.getVisibility() != GONE) { 1248c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); 1258c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1268c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int childLeft = lp.x; 1278c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int childTop = lp.y; 1288c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); 1298c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1308c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka if (lp.dropped) { 1318c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka lp.dropped = false; 1328c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1338c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final int[] cellXY = mTmpCellXY; 1348c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka getLocationOnScreen(cellXY); 1358c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mWallpaperManager.sendWallpaperCommand(getWindowToken(), 1368c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka WallpaperManager.COMMAND_DROP, 1378c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka cellXY[0] + childLeft + lp.width / 2, 1388c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka cellXY[1] + childTop + lp.height / 2, 0, null); 1398c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1408c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1418c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1428c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1438c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1448c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 145e6235dd225404239b55c459245543f3302326112Michael Jurka public boolean shouldDelayChildPressedState() { 146e6235dd225404239b55c459245543f3302326112Michael Jurka return false; 147e6235dd225404239b55c459245543f3302326112Michael Jurka } 148e6235dd225404239b55c459245543f3302326112Michael Jurka 149e6235dd225404239b55c459245543f3302326112Michael Jurka @Override 1508c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka public void requestChildFocus(View child, View focused) { 1518c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka super.requestChildFocus(child, focused); 1528c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka if (child != null) { 1538c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka Rect r = new Rect(); 1548c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka child.getDrawingRect(r); 1558c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka requestRectangleOnScreen(r); 1568c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1578c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1588c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1598c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 1608c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka public void cancelLongPress() { 1618c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka super.cancelLongPress(); 1628c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1638c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // Cancel long press for all children 1648c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final int count = getChildCount(); 1658c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka for (int i = 0; i < count; i++) { 1668c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final View child = getChildAt(i); 1678c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka child.cancelLongPress(); 1688c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1698c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1708c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1718c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 1728c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka protected void setChildrenDrawingCacheEnabled(boolean enabled) { 1738c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final int count = getChildCount(); 1748c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka for (int i = 0; i < count; i++) { 1758c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final View view = getChildAt(i); 1768c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka view.setDrawingCacheEnabled(enabled); 1778c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // Update the drawing caches 1788182e5bc3c2d1a0140df345599b89369d457bb4aAdam Cohen if (!view.isHardwareAccelerated() && enabled) { 1798c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka view.buildDrawingCache(true); 1808c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1818c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1828c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1838c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1848c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 1858c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { 1868c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka super.setChildrenDrawnWithCacheEnabled(enabled); 1878c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 188d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen} 189