ShortcutAndWidgetContainer.java revision d5e42733799817d11d739b0a882da8dda721126d
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 19d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohenimport java.util.ArrayList; 20d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen 218c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.app.WallpaperManager; 228c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.content.Context; 238c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.graphics.Rect; 24d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohenimport android.view.MotionEvent; 258c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.view.View; 26cf6125c2d30ce02d8ab6cbe8e37a20f6a831e216Michael Jurkaimport android.view.ViewGroup; 278c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 28cf6125c2d30ce02d8ab6cbe8e37a20f6a831e216Michael Jurkapublic class CellLayoutChildren extends ViewGroup { 298c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka static final String TAG = "CellLayoutChildren"; 308c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 318c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // These are temporary variables to prevent having to allocate a new object just to 328c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // return an (x, y) value from helper functions. Do NOT use them to maintain other state. 338c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka private final int[] mTmpCellXY = new int[2]; 348c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 358c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka private final WallpaperManager mWallpaperManager; 368c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 378c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka private int mLeftPadding; 388c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka private int mTopPadding; 398c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 40d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen private int mCellWidth; 41d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen private int mCellHeight; 42d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen 438c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka private int mWidthGap; 448c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka private int mHeightGap; 458c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 468c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka public CellLayoutChildren(Context context) { 478c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka super(context); 488c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mWallpaperManager = WallpaperManager.getInstance(context); 49bdb5c5342adc550559fd723af461e53248f2fba8Michael Jurka setLayerType(LAYER_TYPE_HARDWARE, null); 508c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 518c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 528c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka public void setCellDimensions(int cellWidth, int cellHeight, 538c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int leftPadding, int topPadding, int widthGap, int heightGap ) { 548c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mCellWidth = cellWidth; 558c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mCellHeight = cellHeight; 568c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mLeftPadding = leftPadding; 578c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mTopPadding = topPadding; 588c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mWidthGap = widthGap; 598c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mHeightGap = heightGap; 608c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 618c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 628c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka public View getChildAt(int x, int y) { 638c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final int count = getChildCount(); 648c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka for (int i = 0; i < count; i++) { 658c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka View child = getChildAt(i); 668c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); 678c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 688c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) && 698c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka (lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) { 708c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka return child; 718c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 728c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 738c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka return null; 748c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 758c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 768c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 778c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 788c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int count = getChildCount(); 798c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka for (int i = 0; i < count; i++) { 808c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka View child = getChildAt(i); 81d5e42733799817d11d739b0a882da8dda721126dAdam Cohen measureChild(child); 828c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 838c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); 848c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); 858c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka setMeasuredDimension(widthSpecSize, heightSpecSize); 868c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 878c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 88d5e42733799817d11d739b0a882da8dda721126dAdam Cohen public void measureChild(View child) { 89d5e42733799817d11d739b0a882da8dda721126dAdam Cohen final int cellWidth = mCellWidth; 90d5e42733799817d11d739b0a882da8dda721126dAdam Cohen final int cellHeight = mCellHeight; 91d5e42733799817d11d739b0a882da8dda721126dAdam Cohen CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); 92d5e42733799817d11d739b0a882da8dda721126dAdam Cohen 93d5e42733799817d11d739b0a882da8dda721126dAdam Cohen lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, 94d5e42733799817d11d739b0a882da8dda721126dAdam Cohen mLeftPadding, mTopPadding); 95d5e42733799817d11d739b0a882da8dda721126dAdam Cohen 96d5e42733799817d11d739b0a882da8dda721126dAdam Cohen int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); 97d5e42733799817d11d739b0a882da8dda721126dAdam Cohen int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, 98d5e42733799817d11d739b0a882da8dda721126dAdam Cohen MeasureSpec.EXACTLY); 99d5e42733799817d11d739b0a882da8dda721126dAdam Cohen 100d5e42733799817d11d739b0a882da8dda721126dAdam Cohen child.measure(childWidthMeasureSpec, childheightMeasureSpec); 101d5e42733799817d11d739b0a882da8dda721126dAdam Cohen } 102d5e42733799817d11d739b0a882da8dda721126dAdam Cohen 1038c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 1048c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka protected void onLayout(boolean changed, int l, int t, int r, int b) { 1058c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int count = getChildCount(); 1068c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka for (int i = 0; i < count; i++) { 1078c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final View child = getChildAt(i); 1088c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka if (child.getVisibility() != GONE) { 1098c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); 1108c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1118c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int childLeft = lp.x; 1128c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka int childTop = lp.y; 1138c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); 1148c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1158c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka if (lp.dropped) { 1168c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka lp.dropped = false; 1178c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1188c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final int[] cellXY = mTmpCellXY; 1198c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka getLocationOnScreen(cellXY); 1208c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka mWallpaperManager.sendWallpaperCommand(getWindowToken(), 1218c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka WallpaperManager.COMMAND_DROP, 1228c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka cellXY[0] + childLeft + lp.width / 2, 1238c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka cellXY[1] + childTop + lp.height / 2, 0, null); 1248c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1258c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka if (lp.animateDrop) { 1268c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka lp.animateDrop = false; 1278c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1288c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // This call does not result in a requestLayout(), but at one point did. 1298c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // We need to be cautious about any method calls within the layout pass 1308c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // to insure we don't leave the view tree in a bad state. 1318c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka ((Workspace) mParent.getParent()).animateViewIntoPosition(child); 1328c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1338c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1348c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1358c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1368c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1378c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1388c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 1398c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka public void requestChildFocus(View child, View focused) { 1408c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka super.requestChildFocus(child, focused); 1418c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka if (child != null) { 1428c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka Rect r = new Rect(); 1438c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka child.getDrawingRect(r); 1448c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka requestRectangleOnScreen(r); 1458c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1468c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1478c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1488c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 1498c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka public void cancelLongPress() { 1508c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka super.cancelLongPress(); 1518c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1528c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // Cancel long press for all children 1538c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final int count = getChildCount(); 1548c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka for (int i = 0; i < count; i++) { 1558c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final View child = getChildAt(i); 1568c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka child.cancelLongPress(); 1578c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1588c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1598c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1608c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 1618c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka protected void setChildrenDrawingCacheEnabled(boolean enabled) { 1628c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final int count = getChildCount(); 1638c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka for (int i = 0; i < count; i++) { 1648c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka final View view = getChildAt(i); 1658c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka view.setDrawingCacheEnabled(enabled); 1668c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka // Update the drawing caches 1678c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka if (!view.isHardwareAccelerated()) { 1688c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka view.buildDrawingCache(true); 1698c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1708c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1718c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 1728c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka 1738c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka @Override 1748c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { 1758c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka super.setChildrenDrawnWithCacheEnabled(enabled); 1768c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka } 177d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen} 178