ShortcutAndWidgetContainer.java revision aa8a871e337fe42e9339b96833eaf37bf2b64b2f
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
17325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerpackage com.android.launcher3;
188c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
198c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.app.WallpaperManager;
208c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.content.Context;
218c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.graphics.Rect;
2293e1f04fa915fc22ecccc7e40794472914f3d446Jon Mirandaimport android.support.annotation.IntDef;
238c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurkaimport android.view.View;
24cf6125c2d30ce02d8ab6cbe8e37a20f6a831e216Michael Jurkaimport android.view.ViewGroup;
258c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
2693e1f04fa915fc22ecccc7e40794472914f3d446Jon Mirandaimport java.lang.annotation.Retention;
2793e1f04fa915fc22ecccc7e40794472914f3d446Jon Mirandaimport java.lang.annotation.RetentionPolicy;
2893e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda
29a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurkapublic class ShortcutAndWidgetContainer extends ViewGroup {
3093e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda    static final String TAG = "ShortcutAndWidgetContainer";
3193e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda
3293e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda    @Retention(RetentionPolicy.SOURCE)
3393e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda    @IntDef({DEFAULT, HOTSEAT, FOLDER})
3493e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda    public @interface ContainerType{}
3593e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda    public static final int DEFAULT = 0;
3693e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda    public static final int HOTSEAT = 1;
3793e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda    public static final int FOLDER = 2;
3893e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda
3993e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda    private int mContainerType = DEFAULT;
408c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
418c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    // These are temporary variables to prevent having to allocate a new object just to
428c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
438c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    private final int[] mTmpCellXY = new int[2];
448c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
458c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    private final WallpaperManager mWallpaperManager;
468c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
47d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    private int mCellWidth;
48d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen    private int mCellHeight;
49d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
502374abfda3e53f84e005df8923170308e4df8c03Adam Cohen    private int mCountX;
512374abfda3e53f84e005df8923170308e4df8c03Adam Cohen
522e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen    private Launcher mLauncher;
532e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen
542374abfda3e53f84e005df8923170308e4df8c03Adam Cohen    private boolean mInvertIfRtl = false;
552374abfda3e53f84e005df8923170308e4df8c03Adam Cohen
56a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurka    public ShortcutAndWidgetContainer(Context context) {
578c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        super(context);
582fd020860533e18c64a93d14d11cb2d34bc9cbafTony        mLauncher = Launcher.getLauncher(context);
598c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        mWallpaperManager = WallpaperManager.getInstance(context);
602801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
612801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
62aa8a871e337fe42e9339b96833eaf37bf2b64b2fSunny Goyal    public void setCellDimensions(int cellWidth, int cellHeight, int countX, int countY) {
638c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        mCellWidth = cellWidth;
648c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        mCellHeight = cellHeight;
652374abfda3e53f84e005df8923170308e4df8c03Adam Cohen        mCountX = countX;
668c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    }
678c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
688c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    public View getChildAt(int x, int y) {
698c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        final int count = getChildCount();
708c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        for (int i = 0; i < count; i++) {
718c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            View child = getChildAt(i);
728c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
738c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
748c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
75f579b5041afe8272c79f9f13001120d37eeeee7bAdam Cohen                    (lp.cellY <= y) && (y < lp.cellY + lp.cellVSpan)) {
768c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                return child;
778c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            }
788c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        }
798c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        return null;
808c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    }
818c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
828c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    @Override
838c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
848c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        int count = getChildCount();
85ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen
86ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
87ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen        int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
88ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen        setMeasuredDimension(widthSpecSize, heightSpecSize);
89ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen
908c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        for (int i = 0; i < count; i++) {
918c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            View child = getChildAt(i);
922824b0750e978e5a63f804a42e9eedba53e8d21dVladimir Marko            if (child.getVisibility() != GONE) {
932824b0750e978e5a63f804a42e9eedba53e8d21dVladimir Marko                measureChild(child);
942824b0750e978e5a63f804a42e9eedba53e8d21dVladimir Marko            }
958c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        }
968c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    }
978c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
98bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    public void setupLp(CellLayout.LayoutParams lp) {
99aa8a871e337fe42e9339b96833eaf37bf2b64b2fSunny Goyal        lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX);
1002374abfda3e53f84e005df8923170308e4df8c03Adam Cohen    }
1012374abfda3e53f84e005df8923170308e4df8c03Adam Cohen
1022374abfda3e53f84e005df8923170308e4df8c03Adam Cohen    // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
1032374abfda3e53f84e005df8923170308e4df8c03Adam Cohen    public void setInvertIfRtl(boolean invert) {
1042374abfda3e53f84e005df8923170308e4df8c03Adam Cohen        mInvertIfRtl = invert;
105bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    }
106bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen
10793e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda    public void setContainerType(@ContainerType int containerType) {
10893e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda        mContainerType = containerType;
1093a6e7f330e680ef718ca7c0921d842efb4d8bbaeWinson Chung    }
1103a6e7f330e680ef718ca7c0921d842efb4d8bbaeWinson Chung
11169737c3e49ff3edc42899609fdbc96e356f8638eWinson Chung    int getCellContentHeight() {
1122e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        final DeviceProfile grid = mLauncher.getDeviceProfile();
11393e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda        int cellContentHeight = grid.cellHeightPx;
11493e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda        if (mContainerType == HOTSEAT) {
11593e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda            cellContentHeight = grid.hotseatCellHeightPx;
11693e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda        } else if (mContainerType == FOLDER) {
11793e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda            cellContentHeight = grid.folderCellHeightPx;
11893e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda        }
11993e1f04fa915fc22ecccc7e40794472914f3d446Jon Miranda        return Math.min(getMeasuredHeight(), cellContentHeight);
12069737c3e49ff3edc42899609fdbc96e356f8638eWinson Chung    }
12169737c3e49ff3edc42899609fdbc96e356f8638eWinson Chung
122d5e42733799817d11d739b0a882da8dda721126dAdam Cohen    public void measureChild(View child) {
1232e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        final DeviceProfile grid = mLauncher.getDeviceProfile();
124d5e42733799817d11d739b0a882da8dda721126dAdam Cohen        final int cellWidth = mCellWidth;
125d5e42733799817d11d739b0a882da8dda721126dAdam Cohen        final int cellHeight = mCellHeight;
126d5e42733799817d11d739b0a882da8dda721126dAdam Cohen        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
127ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen        if (!lp.isFullscreen) {
128aa8a871e337fe42e9339b96833eaf37bf2b64b2fSunny Goyal            lp.setup(cellWidth, cellHeight, invertLayoutHorizontally(), mCountX);
1295f8afe6280eae34620067696173e71943e1a30a3Winson Chung
1305f8afe6280eae34620067696173e71943e1a30a3Winson Chung            if (child instanceof LauncherAppWidgetHostView) {
1315f8afe6280eae34620067696173e71943e1a30a3Winson Chung                // Widgets have their own padding, so skip
1325f8afe6280eae34620067696173e71943e1a30a3Winson Chung            } else {
1331f06427266c0cb5de4561fc7c620ff542f625300Winson                // Otherwise, center the icon/folder
13469737c3e49ff3edc42899609fdbc96e356f8638eWinson Chung                int cHeight = getCellContentHeight();
1355f8afe6280eae34620067696173e71943e1a30a3Winson Chung                int cellPaddingY = (int) Math.max(0, ((lp.height - cHeight) / 2f));
136cdef04403695872db8be6342b8dacd437ee02d52Winson Chung                int cellPaddingX = (int) (grid.edgeMarginPx / 2f);
137cdef04403695872db8be6342b8dacd437ee02d52Winson Chung                child.setPadding(cellPaddingX, cellPaddingY, cellPaddingX, 0);
1385f8afe6280eae34620067696173e71943e1a30a3Winson Chung            }
139ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen        } else {
140ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen            lp.x = 0;
141ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen            lp.y = 0;
142ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen            lp.width = getMeasuredWidth();
143ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen            lp.height = getMeasuredHeight();
144ec40b2b90c649be3b513af2ba174db56b54b64f6Adam Cohen        }
145d5e42733799817d11d739b0a882da8dda721126dAdam Cohen        int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
146a501d49a6a8b684bc83aa6b536f43247d75bacdfTony Wickham        int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
147d5e42733799817d11d739b0a882da8dda721126dAdam Cohen        child.measure(childWidthMeasureSpec, childheightMeasureSpec);
148d5e42733799817d11d739b0a882da8dda721126dAdam Cohen    }
149d5e42733799817d11d739b0a882da8dda721126dAdam Cohen
150fc3c1edf7bbc3f7cb23e79520731d13ccc2da046Sunny Goyal    public boolean invertLayoutHorizontally() {
1517066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal        return mInvertIfRtl && Utilities.isRtl(getResources());
1522374abfda3e53f84e005df8923170308e4df8c03Adam Cohen    }
1532374abfda3e53f84e005df8923170308e4df8c03Adam Cohen
1548c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    @Override
1558c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    protected void onLayout(boolean changed, int l, int t, int r, int b) {
1568c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        int count = getChildCount();
1578c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        for (int i = 0; i < count; i++) {
1588c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            final View child = getChildAt(i);
1598c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            if (child.getVisibility() != GONE) {
1608c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
1618c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                int childLeft = lp.x;
1628c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                int childTop = lp.y;
1638c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
1648c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
1658c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                if (lp.dropped) {
1668c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                    lp.dropped = false;
1678c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
1688c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                    final int[] cellXY = mTmpCellXY;
1698c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                    getLocationOnScreen(cellXY);
1708c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                    mWallpaperManager.sendWallpaperCommand(getWindowToken(),
1718c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                            WallpaperManager.COMMAND_DROP,
1728c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                            cellXY[0] + childLeft + lp.width / 2,
1738c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                            cellXY[1] + childTop + lp.height / 2, 0, null);
1748c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka                }
1758c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            }
1768c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        }
1778c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    }
1788c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
1798c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    @Override
180e6235dd225404239b55c459245543f3302326112Michael Jurka    public boolean shouldDelayChildPressedState() {
181e6235dd225404239b55c459245543f3302326112Michael Jurka        return false;
182e6235dd225404239b55c459245543f3302326112Michael Jurka    }
183e6235dd225404239b55c459245543f3302326112Michael Jurka
184e6235dd225404239b55c459245543f3302326112Michael Jurka    @Override
1858c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    public void requestChildFocus(View child, View focused) {
1868c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        super.requestChildFocus(child, focused);
1878c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        if (child != null) {
1888c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            Rect r = new Rect();
1898c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            child.getDrawingRect(r);
1908c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            requestRectangleOnScreen(r);
1918c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        }
1928c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    }
1938c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
1948c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    @Override
1958c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    public void cancelLongPress() {
1968c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        super.cancelLongPress();
1978c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka
1988c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        // Cancel long press for all children
1998c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        final int count = getChildCount();
2008c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        for (int i = 0; i < count; i++) {
2018c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            final View child = getChildAt(i);
2028c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            child.cancelLongPress();
2038c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka        }
2048c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka    }
205d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen}
206