Workspace.java revision 16fed41e5e680c547b23e108788eb85f1b04d36d
131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17a5902524d4403885eb4c50360bf3465c6be796efJoe Onoratopackage com.android.launcher2;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurkaimport com.android.launcher.R;
20495f2892288504f4ab87e62957b3f71144dd73c2Adam Powell
210280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurkaimport android.animation.Animator;
22de7658b5e02ae10010e44fcf8d9c5814f54d9eb0Patrick Dubroyimport android.animation.Animator.AnimatorListener;
23b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haaseimport android.animation.AnimatorSet;
240280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurkaimport android.animation.ObjectAnimator;
250280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurkaimport android.animation.PropertyValuesHolder;
268f573952b8729b4319043ae0908997ecd2d68951Dianne Hackbornimport android.app.WallpaperManager;
27629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetManager;
28629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetProviderInfo;
29495f2892288504f4ab87e62957b3f71144dd73c2Adam Powellimport android.content.ComponentName;
3031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Context;
3131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Intent;
32495f2892288504f4ab87e62957b3f71144dd73c2Adam Powellimport android.content.pm.PackageManager;
335c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.content.pm.ProviderInfo;
347247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroyimport android.content.res.Resources;
3531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.res.TypedArray;
36f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohenimport android.graphics.Camera;
3731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Canvas;
38f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohenimport android.graphics.Color;
39a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurkaimport android.graphics.Matrix;
4031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Rect;
41f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Projectimport android.graphics.drawable.Drawable;
425c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guyimport android.net.Uri;
43956091ba7863bd72086275e61084864994dd6fa7Joe Onoratoimport android.os.IBinder;
44495f2892288504f4ab87e62957b3f71144dd73c2Adam Powellimport android.os.Parcelable;
4531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.util.AttributeSet;
46291ad12232c98e383d44d76ffe09e97e204c61bcDaniel Sandlerimport android.util.Log;
4731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.MotionEvent;
4831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.View;
49f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Projectimport android.widget.TextView;
5031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
51af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurkaimport java.util.ArrayList;
52af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurkaimport java.util.HashSet;
53edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
5431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
550142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka * The workspace is a wide area with a wallpaper and a finite number of pages.
560142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka * Each page contains a number of icons, folders or widgets the user can
57aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung * interact with. A workspace is meant to be used with a fixed width only.
5831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
590142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurkapublic class Workspace extends SmoothPagedView
60dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        implements DropTarget, DragSource, DragScroller, View.OnTouchListener {
61e47f55c30b9c24f01f2be861247c92f17fbe4a61Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
623a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato    private static final String TAG = "Launcher.Workspace";
630142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
64dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    // This is how much the workspace shrinks when we enter all apps or
65dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    // customization mode
66dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    private static final float SHRINK_FACTOR = 0.16f;
67a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen
68f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    // Y rotation to apply to the workspace screens
69f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    private static final float WORKSPACE_ROTATION = 12.5f;
70a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen
71a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen    // These are extra scale factors to apply to the mini home screens
72a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen    // so as to achieve the desired transform
73a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen    private static final float EXTRA_SCALE_FACTOR_0 = 0.97f;
74a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen    private static final float EXTRA_SCALE_FACTOR_1 = 1.0f;
75e54cec82a915c2bc8a7d719228c5d907f3fe14ecAdam Cohen    private static final float EXTRA_SCALE_FACTOR_2 = 1.08f;
76a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen
77f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    private static final int BACKGROUND_FADE_OUT_DELAY = 300;
78f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    private static final int BACKGROUND_FADE_OUT_DURATION = 300;
79f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    private static final int BACKGROUND_FADE_IN_DURATION = 100;
80f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
81f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    static final int SCROLL_RIGHT = 0;
82f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    static final int SCROLL_LEFT = 1;
83f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
84f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    // These animators are used to fade the
85f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    private ObjectAnimator<Float> mBackgroundFadeIn;
86f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    private ObjectAnimator<Float> mBackgroundFadeOut;
87f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    private float mBackgroundAlpha = 0;
88f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
890142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    private enum ShrinkPosition { SHRINK_TO_TOP, SHRINK_TO_MIDDLE, SHRINK_TO_BOTTOM };
9031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
918f573952b8729b4319043ae0908997ecd2d68951Dianne Hackborn    private final WallpaperManager mWallpaperManager;
92aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
930142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    private int mDefaultPage;
9431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9579212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka    private boolean mWaitingToShrinkToBottom = false;
9631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
9831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * CellInfo for the cell that is currently being dragged
9931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
10031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private CellLayout.CellInfo mDragInfo;
101aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
10270864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    /**
10370864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * Target drop area calculated during last acceptDrop call.
10470864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     */
10570864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    private int[] mTargetCell = null;
10631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1076569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    /**
1086569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy     * The CellLayout that is currently being dragged over
1096569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy     */
1106569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    private CellLayout mDragTargetLayout = null;
1116569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
11231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private Launcher mLauncher;
1130589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private IconCache mIconCache;
11400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    private DragController mDragController;
115aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
11631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private int[] mTempCell = new int[2];
11770864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    private int[] mTempEstimate = new int[2];
1180280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    private float[] mTempOriginXY = new float[2];
119a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    private float[] mTempDragCoordinates = new float[2];
120a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    private float[] mTempDragBottomRightCoordinates = new float[2];
1210280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    private Matrix mTempInverseMatrix = new Matrix();
12231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
123d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static final int DEFAULT_CELL_COUNT_X = 4;
124d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen    private static final int DEFAULT_CELL_COUNT_Y = 4;
12531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1268a73c51ee87b6d9b12daba188034889caf7a905bRomain Guy    private Drawable mPreviousIndicator;
1278a73c51ee87b6d9b12daba188034889caf7a905bRomain Guy    private Drawable mNextIndicator;
128aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1290142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    // State variable that indicated whether the pages are small (ie when you're
130dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    // in all apps or customize mode)
131dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    private boolean mIsSmall;
132b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase    private AnimatorListener mUnshrinkAnimationListener;
133dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka
13431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
13531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Used to inflate the Workspace from XML.
13631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
13731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param context The application's context.
1380142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka     * @param attrs The attributes set containing the Workspace's customization values.
13931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
14031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public Workspace(Context context, AttributeSet attrs) {
14131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        this(context, attrs, 0);
14231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
14331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
14531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Used to inflate the Workspace from XML.
14631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
14731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param context The application's context.
1480142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka     * @param attrs The attributes set containing the Workspace's customization values.
14931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param defStyle Unused.
15031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
15131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public Workspace(Context context, AttributeSet attrs, int defStyle) {
15231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super(context, attrs, defStyle);
1530142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        mContentIsRefreshable = false;
1545f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka
1555f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        if (!LauncherApplication.isScreenXLarge()) {
1565f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            mFadeInAdjacentScreens = false;
1575f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        }
15831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1598f573952b8729b4319043ae0908997ecd2d68951Dianne Hackborn        mWallpaperManager = WallpaperManager.getInstance(context);
160aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
161aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        TypedArray a = context.obtainStyledAttributes(attrs,
162aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                R.styleable.Workspace, defStyle, 0);
163d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        int cellCountX = a.getInt(R.styleable.Workspace_cellCountX, DEFAULT_CELL_COUNT_X);
164d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        int cellCountY = a.getInt(R.styleable.Workspace_cellCountY, DEFAULT_CELL_COUNT_Y);
1650142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1);
16631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        a.recycle();
16731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
168d22015cd37ea6ef53762eca5be57daca123ff607Adam Cohen        LauncherModel.updateWorkspaceLayoutCells(cellCountX, cellCountY);
1690d44e9482b95d8f163b28bf20131c4349185b589Joe Onorato        setHapticFeedbackEnabled(false);
1700142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
17131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        initWorkspace();
17231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
17331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
17531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Initializes various states for this workspace.
17631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
1770142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    protected void initWorkspace() {
1780589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        Context context = getContext();
1790142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        mCurrentPage = mDefaultPage;
1800142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        Launcher.setScreen(mCurrentPage);
1810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        LauncherApplication app = (LauncherApplication)context.getApplicationContext();
1820589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = app.getIconCache();
18331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
184b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase        mUnshrinkAnimationListener = new AnimatorListener() {
185b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase            public void onAnimationStart(Animator animation) {}
186b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase            public void onAnimationEnd(Animator animation) {
187dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka                mIsSmall = false;
188dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka            }
189b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase            public void onAnimationCancel(Animator animation) {}
190b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase            public void onAnimationRepeat(Animator animation) {}
191dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        };
1920142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
1930142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        mSnapVelocity = 600;
19431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
19531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
197f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    protected int getScrollMode() {
198f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        if (LauncherApplication.isScreenXLarge()) {
199f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            return SmoothPagedView.QUINTIC_MODE;
200f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        } else {
201f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            return SmoothPagedView.OVERSHOOT_MODE;
202f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        }
203f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
204f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
205f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    @Override
20631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void addView(View child, int index, LayoutParams params) {
20731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(child instanceof CellLayout)) {
20831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
20931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
21031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.addView(child, index, params);
21131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
21231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
21331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
21431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void addView(View child) {
21531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(child instanceof CellLayout)) {
21631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
21731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
21831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.addView(child);
21931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
22031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
22131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
22231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void addView(View child, int index) {
22331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(child instanceof CellLayout)) {
22431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
22531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
22631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.addView(child, index);
22731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
22831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
22931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
23031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void addView(View child, int width, int height) {
23131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(child instanceof CellLayout)) {
23231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
23331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
23431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.addView(child, width, height);
23531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
23631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
23731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
23831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void addView(View child, LayoutParams params) {
23931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(child instanceof CellLayout)) {
24031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
24131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
24231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.addView(child, params);
24331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
24431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
24531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
24631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @return The open folder on the current screen, or null if there is none
24731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
24831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    Folder getOpenFolder() {
2490142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage);
2500142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        int count = currentPage.getChildCount();
25131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        for (int i = 0; i < count; i++) {
2520142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            View child = currentPage.getChildAt(i);
253aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            if (child instanceof Folder) {
254aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                Folder folder = (Folder) child;
255aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                if (folder.getInfo().opened)
256aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    return folder;
25731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
25831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
25931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return null;
26031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
26131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
26231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    ArrayList<Folder> getOpenFolders() {
263dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
264dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        ArrayList<Folder> folders = new ArrayList<Folder>(screenCount);
26531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
266dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        for (int screen = 0; screen < screenCount; screen++) {
2670142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            CellLayout currentPage = (CellLayout) getChildAt(screen);
2680142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            int count = currentPage.getChildCount();
26931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            for (int i = 0; i < count; i++) {
2700142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                View child = currentPage.getChildAt(i);
271aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                if (child instanceof Folder) {
272aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    Folder folder = (Folder) child;
273aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    if (folder.getInfo().opened)
274aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                        folders.add(folder);
27531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    break;
27631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
27731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
27831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
27931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
28031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return folders;
28131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
28231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2830142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    boolean isDefaultPageShowing() {
2840142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        return mCurrentPage == mDefaultPage;
28531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
28631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
28731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
28831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Sets the current screen.
28931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
2900142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka     * @param currentPage
29131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
2920142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
2930142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    void setCurrentPage(int currentPage) {
2940142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        super.setCurrentPage(currentPage);
2950142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        updateWallpaperOffset(mScrollX);
29631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
29731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
29831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
29931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Adds the specified child in the specified screen. The position and dimension of
30031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * the child are defined by x, y, spanX and spanY.
30131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
30231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param child The child to add in one of the workspace's screens.
30331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param screen The screen in which to add the child.
30431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param x The X position of the child in the screen's grid.
30531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param y The Y position of the child in the screen's grid.
30631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param spanX The number of cells spanned horizontally by the child.
30731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param spanY The number of cells spanned vertically by the child.
30831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
30931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    void addInScreen(View child, int screen, int x, int y, int spanX, int spanY) {
31031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        addInScreen(child, screen, x, y, spanX, spanY, false);
31131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
31231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
313aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    void addInFullScreen(View child, int screen) {
314aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        addInScreen(child, screen, 0, 0, -1, -1);
315aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
316aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
31731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
31831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Adds the specified child in the specified screen. The position and dimension of
31931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * the child are defined by x, y, spanX and spanY.
32031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
32131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param child The child to add in one of the workspace's screens.
32231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param screen The screen in which to add the child.
32331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param x The X position of the child in the screen's grid.
32431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param y The Y position of the child in the screen's grid.
32531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param spanX The number of cells spanned horizontally by the child.
32631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param spanY The number of cells spanned vertically by the child.
32731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param insert When true, the child is inserted at the beginning of the children list.
32831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
32931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    void addInScreen(View child, int screen, int x, int y, int spanX, int spanY, boolean insert) {
33031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (screen < 0 || screen >= getChildCount()) {
331291ad12232c98e383d44d76ffe09e97e204c61bcDaniel Sandler            Log.e(TAG, "The screen must be >= 0 and < " + getChildCount()
332291ad12232c98e383d44d76ffe09e97e204c61bcDaniel Sandler                + " (was " + screen + "); skipping child");
333291ad12232c98e383d44d76ffe09e97e204c61bcDaniel Sandler            return;
33431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
33531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
33631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final CellLayout group = (CellLayout) getChildAt(screen);
33731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
33831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (lp == null) {
33931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp = new CellLayout.LayoutParams(x, y, spanX, spanY);
34031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } else {
34131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellX = x;
34231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellY = y;
34331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellHSpan = spanX;
34431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellVSpan = spanY;
34531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
346aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3476569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        // Get the canonical child id to uniquely represent this view in this screen
348845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka        int childId = LauncherModel.getCellLayoutChildId(-1, screen, x, y, spanX, spanY);
349aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        if (!group.addViewToCellLayout(child, insert ? 0 : -1, childId, lp)) {
350aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            // TODO: This branch occurs when the workspace is adding views
351aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            // outside of the defined grid
3526569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            // maybe we should be deleting these items from the LauncherModel?
353aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            Log.w(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout");
354aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
355aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
35631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(child instanceof Folder)) {
3570d44e9482b95d8f163b28bf20131c4349185b589Joe Onorato            child.setHapticFeedbackEnabled(false);
35831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            child.setOnLongClickListener(mLongClickListener);
35931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
36000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        if (child instanceof DropTarget) {
361aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            mDragController.addDropTarget((DropTarget) child);
36200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        }
36331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
36431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
365dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    public boolean onTouch(View v, MotionEvent event) {
366dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        // this is an intercepted event being forwarded from a cell layout
367dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        if (mIsSmall) {
3682b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy            mLauncher.onWorkspaceClick((CellLayout) v);
36996226223d9849842bb2a67af051acbae9e0677d5Michael Jurka            return true;
370dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        }
371dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        return false;
3720e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka    }
3730e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka
3745f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    @Override
3755f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    public boolean dispatchUnhandledMove(View focused, int direction) {
3765f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        if (mIsSmall) {
3775f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            // when the home screens are shrunken, shouldn't allow side-scrolling
3785f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            return false;
3795f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        }
3805f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        return super.dispatchUnhandledMove(focused, direction);
3815f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    }
3825f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka
3835f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    @Override
3845f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    public boolean onInterceptTouchEvent(MotionEvent ev) {
3855f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        if (mIsSmall) {
3865f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            // when the home screens are shrunken, shouldn't allow side-scrolling
3875f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            return false;
3885f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        }
3895f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        return super.onInterceptTouchEvent(ev);
3905f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    }
3915f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka
3920142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    protected void pageBeginMoving() {
3930142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        if (mNextPage != INVALID_PAGE) {
3940142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            // we're snapping to a particular screen
39516fed41e5e680c547b23e108788eb85f1b04d36dMichael Jurka            enableChildrenCache(mCurrentPage, mNextPage);
3960142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        } else {
3970142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            // this is when user is actively dragging a particular screen, they might
3980142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            // swipe it either left or right (but we won't advance by more than one screen)
39916fed41e5e680c547b23e108788eb85f1b04d36dMichael Jurka            enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1);
40031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
401f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        showOutlines();
40231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
40331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
4040142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    protected void pageEndMoving() {
40516fed41e5e680c547b23e108788eb85f1b04d36dMichael Jurka        clearChildrenCache();
406f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        hideOutlines();
4070142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    }
4080142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
4090142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
4100142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    protected void notifyPageSwitchListener() {
4110142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        super.notifyPageSwitchListener();
4120142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
4130142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        if (mPreviousIndicator != null) {
4140142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            // if we know the next page, we show the indication for it right away; it looks
4150142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            // weird if the indicators are lagging
4160142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            int page = mNextPage;
4170142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            if (page == INVALID_PAGE) {
4180142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                page = mCurrentPage;
4190142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            }
4200142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            mPreviousIndicator.setLevel(page);
4210142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            mNextIndicator.setLevel(page);
4220142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        }
4230142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        Launcher.setScreen(mCurrentPage);
4240142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    };
4250142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
4268f573952b8729b4319043ae0908997ecd2d68951Dianne Hackborn    private void updateWallpaperOffset() {
427798300c4c05b12228f2e4f31c49c3cb728a37889Romain Guy        updateWallpaperOffset(getChildAt(getChildCount() - 1).getRight() - (mRight - mLeft));
428798300c4c05b12228f2e4f31c49c3cb728a37889Romain Guy    }
429798300c4c05b12228f2e4f31c49c3cb728a37889Romain Guy
430798300c4c05b12228f2e4f31c49c3cb728a37889Romain Guy    private void updateWallpaperOffset(int scrollRange) {
43186f7753bbd6d87ce85195b0715e5548edbfdb7d1Winson Chung        final boolean isStaticWallpaper = (mWallpaperManager != null) &&
43286f7753bbd6d87ce85195b0715e5548edbfdb7d1Winson Chung                (mWallpaperManager.getWallpaperInfo() == null);
43386f7753bbd6d87ce85195b0715e5548edbfdb7d1Winson Chung        if (LauncherApplication.isScreenXLarge() && !isStaticWallpaper) {
43486f7753bbd6d87ce85195b0715e5548edbfdb7d1Winson Chung            IBinder token = getWindowToken();
43586f7753bbd6d87ce85195b0715e5548edbfdb7d1Winson Chung            if (token != null) {
43686f7753bbd6d87ce85195b0715e5548edbfdb7d1Winson Chung                mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 0 );
43786f7753bbd6d87ce85195b0715e5548edbfdb7d1Winson Chung                mWallpaperManager.setWallpaperOffsets(getWindowToken(),
43886f7753bbd6d87ce85195b0715e5548edbfdb7d1Winson Chung                        Math.max(0.f, Math.min(mScrollX/(float)scrollRange, 1.f)), 0);
43986f7753bbd6d87ce85195b0715e5548edbfdb7d1Winson Chung            }
440956091ba7863bd72086275e61084864994dd6fa7Joe Onorato        }
4418f573952b8729b4319043ae0908997ecd2d68951Dianne Hackborn    }
442aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
443f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    private float getScaleXForRotation(float degrees) {
444f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        return (float) (1.0f / Math.cos(Math.PI * degrees / 180.0f));
445f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
446f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
447f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    public void showOutlines() {
448f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        if (mBackgroundFadeOut != null) mBackgroundFadeOut.cancel();
449f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        if (mBackgroundFadeIn != null) mBackgroundFadeIn.cancel();
450f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        mBackgroundFadeIn = new ObjectAnimator<Float>(BACKGROUND_FADE_IN_DURATION, this,
451f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                        new PropertyValuesHolder<Float>("backgroundAlpha", 1.0f));
452f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        mBackgroundFadeIn.start();
453f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
454f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
455f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    public void hideOutlines() {
456f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        if (mBackgroundFadeIn != null) mBackgroundFadeIn.cancel();
457f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        if (mBackgroundFadeOut != null) mBackgroundFadeOut.cancel();
458f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        mBackgroundFadeOut = new ObjectAnimator<Float>(BACKGROUND_FADE_OUT_DURATION, this,
459f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                        new PropertyValuesHolder<Float>("backgroundAlpha", 0.0f));
460f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        mBackgroundFadeOut.setStartDelay(BACKGROUND_FADE_OUT_DELAY);
461f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        mBackgroundFadeOut.start();
462f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
463f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
464f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    public void setBackgroundAlpha(float alpha) {
465f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        mBackgroundAlpha = alpha;
466f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        for (int i = 0; i < getChildCount(); i++) {
467f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            CellLayout cl = (CellLayout) getChildAt(i);
468f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            cl.setBackgroundAlpha(alpha);
469f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        }
470f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
471f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
472f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    public float getBackgroundAlpha() {
473f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        return mBackgroundAlpha;
474f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
475f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
476f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    @Override
477f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    protected void screenScrolled(int screenCenter) {
478f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        View cur = getChildAt(mCurrentPage);
479f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        View toRight = getChildAt(mCurrentPage + 1);
480f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        View toLeft = getChildAt(mCurrentPage - 1);
481f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
482f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        for (int i = 0; i < mCurrentPage - 1; i++) {
483f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            View v = getChildAt(i);
484f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            if (v != null) {
485f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                v.setRotationY(WORKSPACE_ROTATION);
486f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                v.setScaleX(getScaleXForRotation(WORKSPACE_ROTATION));
487f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            }
488f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        }
489f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        for (int i = mCurrentPage + 1; i < getChildCount(); i++) {
490f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            View v = getChildAt(i);
491f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            if (v != null) {
492f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                v.setRotationY(-WORKSPACE_ROTATION);
493f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                v.setScaleX(getScaleXForRotation(-WORKSPACE_ROTATION));
494f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            }
495f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        }
496f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
497f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        int pageWidth = cur.getMeasuredWidth();
498f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        int delta = screenCenter - (mCurrentPage * pageWidth + pageWidth / 2 +
499f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                getRelativeChildOffset(0));
500f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
501f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        float scrollProgress = Math.abs(delta/(pageWidth*1.0f));
502f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        int scrollDirection = delta > 0 ? SCROLL_LEFT : SCROLL_RIGHT;
503f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
504f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        float rotation;
505f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
506f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        if (scrollDirection == SCROLL_RIGHT) {
507f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            rotation = -scrollProgress * WORKSPACE_ROTATION;
508f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            cur.setRotationY(rotation);
509f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            cur.setScaleX(getScaleXForRotation(rotation));
510f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            if (toLeft != null) {
511f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                rotation = WORKSPACE_ROTATION * (1 - scrollProgress);
512f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                toLeft.setRotationY(rotation);
513f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                toLeft.setScaleX(getScaleXForRotation(rotation));
514f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            }
515f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            if (toRight != null) {
516f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                toRight.setRotationY(-WORKSPACE_ROTATION);
517f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                toRight.setScaleX(getScaleXForRotation(WORKSPACE_ROTATION));
518f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            }
519f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        } else {
520f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            rotation = scrollProgress * WORKSPACE_ROTATION;
521f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            cur.setRotationY(rotation);
522f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            cur.setScaleX(getScaleXForRotation(rotation));
523f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
524f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            if (toRight != null) {
525f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                rotation = -WORKSPACE_ROTATION * (1 - scrollProgress);
526f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                toRight.setRotationY(rotation);
527f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                toRight.setScaleX(getScaleXForRotation(rotation));
528f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            }
529f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            if (toLeft != null) {
530f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                toLeft.setRotationY(WORKSPACE_ROTATION);
531f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                toLeft.setScaleX(getScaleXForRotation(WORKSPACE_ROTATION));
532f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            }
533f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        }
534f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen    }
535f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
53600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    protected void onAttachedToWindow() {
53700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        super.onAttachedToWindow();
538956091ba7863bd72086275e61084864994dd6fa7Joe Onorato        computeScroll();
53900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mDragController.setWindowToken(getWindowToken());
54000acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
54100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
54231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
54328750fba6a2d141eb9a1e566718c17236030b815Michael Jurka    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
5440142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        super.onLayout(changed, left, top, right, bottom);
545aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
54679212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka        // if shrinkToBottom() is called on initialization, it has to be deferred
54779212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka        // until after the first call to onLayout so that it has the correct width
54879212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka        if (mWaitingToShrinkToBottom) {
54979212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka            shrinkToBottom(false);
55079212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka            mWaitingToShrinkToBottom = false;
55179212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka        }
55279212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka
553aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        if (LauncherApplication.isInPlaceRotationEnabled()) {
554aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            // When the device is rotated, the scroll position of the current screen
555aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            // needs to be refreshed
5560142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            setCurrentPage(getCurrentPage());
557aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
55831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
55931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
56031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
5610142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    protected void dispatchDraw(Canvas canvas) {
5620142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        if (mIsSmall) {
5630142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            // Draw all the workspaces if we're small
5640142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            final int pageCount = getChildCount();
5650142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            final long drawingTime = getDrawingTime();
5660142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            for (int i = 0; i < pageCount; i++) {
5670142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                final View page = (View) getChildAt(i);
5680142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
5690142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                drawChild(canvas, page, drawingTime);
5700142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            }
5710142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        } else {
5720142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            super.dispatchDraw(canvas);
57331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
57431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
57531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
57631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
57731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
578678862186af4ea70d9b4012c9bc127e7fea5e607Joe Onorato        if (!mLauncher.isAllAppsVisible()) {
57931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final Folder openFolder = getOpenFolder();
58031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (openFolder != null) {
58131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                return openFolder.requestFocus(direction, previouslyFocusedRect);
58231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            } else {
5830142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
58431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
58531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
58631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return false;
58731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
58831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
58931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
590c2e24c0a37ddeab930f731c062705d435e477f75Romain Guy    public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
591678862186af4ea70d9b4012c9bc127e7fea5e607Joe Onorato        if (!mLauncher.isAllAppsVisible()) {
59231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final Folder openFolder = getOpenFolder();
5930142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            if (openFolder != null) {
59431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                openFolder.addFocusables(views, direction);
5950142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            } else {
5960142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                super.addFocusables(views, direction, focusableMode);
59731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
59831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
59931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
60031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
60131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
6027bb1749c69384faf00b238f0684d3b2e23406451Joe Onorato    public boolean dispatchTouchEvent(MotionEvent ev) {
6037bb1749c69384faf00b238f0684d3b2e23406451Joe Onorato        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
60454dd75463d0eb47c2f468e19063bdc4141dfdf74Michael Jurka            // (In XLarge mode, the workspace is shrunken below all apps, and responds to taps
60554dd75463d0eb47c2f468e19063bdc4141dfdf74Michael Jurka            // ie when you click on a mini-screen, it zooms back to that screen)
6066b4adbc0558fcebbd44998bd00dcd334ddbee32dJoe Onorato            if (!LauncherApplication.isScreenXLarge() && mLauncher.isAllAppsVisible()) {
6077bb1749c69384faf00b238f0684d3b2e23406451Joe Onorato                return false;
6087bb1749c69384faf00b238f0684d3b2e23406451Joe Onorato            }
6097bb1749c69384faf00b238f0684d3b2e23406451Joe Onorato        }
6107bb1749c69384faf00b238f0684d3b2e23406451Joe Onorato        return super.dispatchTouchEvent(ev);
6117bb1749c69384faf00b238f0684d3b2e23406451Joe Onorato    }
6127bb1749c69384faf00b238f0684d3b2e23406451Joe Onorato
6130142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    void enableChildrenCache(int fromPage, int toPage) {
6140142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        if (fromPage > toPage) {
6150142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            final int temp = fromPage;
6160142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            fromPage = toPage;
6170142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            toPage = temp;
6183a2b3f2be58843d26549fb0ec6c6533627c7cd19Mike Cleron        }
619aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
620dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
621fea5d0250767ab938f623a404e6292a32dd2fdf5Adam Powell
6220142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        fromPage = Math.max(fromPage, 0);
6230142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        toPage = Math.min(toPage, screenCount - 1);
624fea5d0250767ab938f623a404e6292a32dd2fdf5Adam Powell
6250142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        for (int i = fromPage; i <= toPage; i++) {
62631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final CellLayout layout = (CellLayout) getChildAt(i);
62731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            layout.setChildrenDrawnWithCacheEnabled(true);
62831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            layout.setChildrenDrawingCacheEnabled(true);
62931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
63031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
63131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
63231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    void clearChildrenCache() {
633dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
634dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        for (int i = 0; i < screenCount; i++) {
63531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final CellLayout layout = (CellLayout) getChildAt(i);
63631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            layout.setChildrenDrawnWithCacheEnabled(false);
63731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
63831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
63931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
64031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
64131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public boolean onTouchEvent(MotionEvent ev) {
6422bc6b7c2b8b651dde0a65c64c127f5b85c1290d2Joe Onorato        if (mLauncher.isAllAppsVisible()) {
6432bc6b7c2b8b651dde0a65c64c127f5b85c1290d2Joe Onorato            // Cancel any scrolling that is in progress.
6442bc6b7c2b8b651dde0a65c64c127f5b85c1290d2Joe Onorato            if (!mScroller.isFinished()) {
6452bc6b7c2b8b651dde0a65c64c127f5b85c1290d2Joe Onorato                mScroller.abortAnimation();
6462bc6b7c2b8b651dde0a65c64c127f5b85c1290d2Joe Onorato            }
6470142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            snapToPage(mCurrentPage);
6487c312c15e33b6cffbda707429e88ade498fc9f5eJoe Onorato            return false; // We don't want the events.  Let them fall through to the all apps view.
64931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
65031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
6510142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        return super.onTouchEvent(ev);
65231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
653aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
65401f0ed4126be412c8c4352026dad3b2a49832267Michael Jurka    public boolean isSmall() {
65501f0ed4126be412c8c4352026dad3b2a49832267Michael Jurka        return mIsSmall;
65601f0ed4126be412c8c4352026dad3b2a49832267Michael Jurka    }
65701f0ed4126be412c8c4352026dad3b2a49832267Michael Jurka
6586b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy    void shrinkToTop(boolean animated) {
6590142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        shrink(ShrinkPosition.SHRINK_TO_TOP, animated);
66001f0ed4126be412c8c4352026dad3b2a49832267Michael Jurka    }
66101f0ed4126be412c8c4352026dad3b2a49832267Michael Jurka
66201f0ed4126be412c8c4352026dad3b2a49832267Michael Jurka    void shrinkToMiddle() {
6630142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        shrink(ShrinkPosition.SHRINK_TO_MIDDLE, true);
664213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka    }
665213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka
666213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka    void shrinkToBottom() {
66779212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka        shrinkToBottom(true);
66879212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka    }
66979212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka
67079212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka    void shrinkToBottom(boolean animated) {
67179212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka        if (mFirstLayout) {
67279212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka            // (mFirstLayout == "first layout has not happened yet")
67379212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka            // if we get a call to shrink() as part of our initialization (for example, if
67479212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka            // Launcher is started in All Apps mode) then we need to wait for a layout call
67579212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka            // to get our width so we can layout the mini-screen views correctly
67679212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka            mWaitingToShrinkToBottom = true;
67779212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka        } else {
6780142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            shrink(ShrinkPosition.SHRINK_TO_BOTTOM, animated);
67979212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka        }
680213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka    }
681213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka
682a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen    private float getYScaleForScreen(int screen) {
683a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        int x = Math.abs(screen - 2);
684a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen
685a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        // TODO: This should be generalized for use with arbitrary rotation angles.
686a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        switch(x) {
687a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen            case 0: return EXTRA_SCALE_FACTOR_0;
688a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen            case 1: return EXTRA_SCALE_FACTOR_1;
689a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen            case 2: return EXTRA_SCALE_FACTOR_2;
690a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        }
691a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        return 1.0f;
692a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen    }
693a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen
694dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    // we use this to shrink the workspace for the all apps view and the customize view
6950142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    private void shrink(ShrinkPosition shrinkPosition, boolean animated) {
696dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        mIsSmall = true;
6975f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        // we intercept and reject all touch events when we're small, so be sure to reset the state
6985f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        mTouchState = TOUCH_STATE_REST;
6995f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        mActivePointerId = INVALID_POINTER;
7005f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka
7017247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy        final Resources res = getResources();
702dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenWidth = getWidth();
703213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka        final int screenHeight = getHeight();
7040142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
7050142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        // Making the assumption that all pages have the same width as the 0th
7060142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        final int pageWidth = getChildAt(0).getMeasuredWidth();
7070142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        final int pageHeight = getChildAt(0).getMeasuredHeight();
7080142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
7090142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        final int scaledPageWidth = (int) (SHRINK_FACTOR * pageWidth);
7100142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        final int scaledPageHeight = (int) (SHRINK_FACTOR * pageHeight);
711a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        final float extraScaledSpacing = res.getDimension(R.dimen.smallScreenExtraSpacing);
712dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka
713dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
714a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        float totalWidth = screenCount * scaledPageWidth + (screenCount - 1) * extraScaledSpacing;
715213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka
716213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka        float newY = getResources().getDimension(R.dimen.smallScreenVerticalMargin);
7170142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        if (shrinkPosition == ShrinkPosition.SHRINK_TO_BOTTOM) {
7180142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            newY = screenHeight - newY - scaledPageHeight;
7190142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        } else if (shrinkPosition == ShrinkPosition.SHRINK_TO_MIDDLE) {
7200142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            newY = screenHeight / 2 - scaledPageHeight / 2;
721213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka        }
722dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka
723dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        // We animate all the screens to the centered position in workspace
724dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        // At the same time, the screens become greyed/dimmed
725dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka
726dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        // newX is initialized to the left-most position of the centered screens
7270142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        float newX = mScroller.getFinalX() + screenWidth / 2 - totalWidth / 2;
728a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen
729a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        // We are going to scale about the center of the view, so we need to adjust the positions
730a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        // of the views accordingly
731a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        newX -= (pageWidth - scaledPageWidth) / 2.0f;
732a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen        newY -= (pageHeight - scaledPageHeight) / 2.0f;
733dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        for (int i = 0; i < screenCount; i++) {
734dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka            CellLayout cl = (CellLayout) getChildAt(i);
735a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen
736f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            float rotation = (-i + 2) * WORKSPACE_ROTATION;
737a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen            float rotationScaleX = (float) (1.0f / Math.cos(Math.PI * rotation / 180.0f));
738a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen            float rotationScaleY = getYScaleForScreen(i);
739a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen
74079212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka            if (animated) {
7417247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy                final int duration = res.getInteger(R.integer.config_workspaceShrinkTime);
7425f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                new ObjectAnimator<Float>(duration, cl,
7435f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                        new PropertyValuesHolder<Float>("x", newX),
7445f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                        new PropertyValuesHolder<Float>("y", newY),
745e54cec82a915c2bc8a7d719228c5d907f3fe14ecAdam Cohen                        new PropertyValuesHolder<Float>("scaleX", SHRINK_FACTOR * rotationScaleX),
746e54cec82a915c2bc8a7d719228c5d907f3fe14ecAdam Cohen                        new PropertyValuesHolder<Float>("scaleY", SHRINK_FACTOR * rotationScaleY),
7475f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                        new PropertyValuesHolder<Float>("backgroundAlpha", 1.0f),
74816fed41e5e680c547b23e108788eb85f1b04d36dMichael Jurka                        new PropertyValuesHolder<Float>("alpha", 1.0f),
7495f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                        new PropertyValuesHolder<Float>("rotationY", rotation)).start();
75079212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka            } else {
75179212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka                cl.setX((int)newX);
75279212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka                cl.setY((int)newY);
753f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                cl.setScaleX(SHRINK_FACTOR * rotationScaleX);
754f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                cl.setScaleY(SHRINK_FACTOR * rotationScaleY);
7555f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                cl.setBackgroundAlpha(1.0f);
7565f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                cl.setAlpha(0.0f);
757a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen                cl.setRotationY(rotation);
75879212d81361d1ad8c941c48f8323eb526643ca68Michael Jurka            }
759dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka            // increment newX for the next screen
760a985e598f6071f4caca15ba3cb6b2cd3e38b217dAdam Cohen            newX += scaledPageWidth + extraScaledSpacing;
761dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka            cl.setOnInterceptTouchListener(this);
762dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        }
763dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        setChildrenDrawnWithCacheEnabled(true);
764dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    }
765dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka
766dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    // We call this when we trigger an unshrink by clicking on the CellLayout cl
7672b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy    public void unshrink(CellLayout clThatWasClicked) {
7680142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        int newCurrentPage = mCurrentPage;
7694cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka        final int screenCount = getChildCount();
7704cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka        for (int i = 0; i < screenCount; i++) {
7714cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka            if (getChildAt(i) == clThatWasClicked) {
7720142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                newCurrentPage = i;
7734cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka            }
7744cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka        }
7750142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        unshrink(newCurrentPage);
7764cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka    }
7774cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka
7780142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    private void unshrink(int newCurrentPage) {
779b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka        if (mIsSmall) {
7805f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage);
7815f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            int delta = newX - mScrollX;
7824cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka
783b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka            final int screenCount = getChildCount();
784b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka            for (int i = 0; i < screenCount; i++) {
785b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka                CellLayout cl = (CellLayout) getChildAt(i);
786b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka                cl.setX(cl.getX() + delta);
787b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka            }
7880142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            snapToPage(newCurrentPage);
789b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka            unshrink();
7900142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
7910142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            setCurrentPage(newCurrentPage);
792b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka        }
793dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    }
794dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka
7954cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka    void unshrink() {
7964cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka        unshrink(true);
7974cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka    }
7984cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka
7994cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka    void unshrink(boolean animated) {
800b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka        if (mIsSmall) {
801b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase            AnimatorSet s = new AnimatorSet();
802b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka            final int screenCount = getChildCount();
8034cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka
8044cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka            final int duration = getResources().getInteger(R.integer.config_workspaceUnshrinkTime);
805b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka            for (int i = 0; i < screenCount; i++) {
8067247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy                final CellLayout cl = (CellLayout)getChildAt(i);
8075f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                float finalAlphaValue = (i == mCurrentPage) ? 1.0f : 0.0f;
808f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                float rotation = 0.0f;
809f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
810f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                if (i < mCurrentPage) {
811f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                    rotation = WORKSPACE_ROTATION;
812f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                } else if (i > mCurrentPage) {
813f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                    rotation = -WORKSPACE_ROTATION;
814f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                }
815f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen
8164cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka                if (animated) {
8174cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka                    s.playTogether(
818b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase                            new ObjectAnimator<Float>(duration, cl, "translationX", 0.0f),
819b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase                            new ObjectAnimator<Float>(duration, cl, "translationY", 0.0f),
820b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase                            new ObjectAnimator<Float>(duration, cl, "scaleX", 1.0f),
821b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase                            new ObjectAnimator<Float>(duration, cl, "scaleY", 1.0f),
8225f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                            new ObjectAnimator<Float>(duration, cl, "backgroundAlpha", 0.0f),
8235f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                            new ObjectAnimator<Float>(duration, cl, "alpha", finalAlphaValue),
824f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                            new ObjectAnimator<Float>(duration, cl, "rotationY", rotation));
8254cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka                } else {
8264cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka                    cl.setTranslationX(0.0f);
8274cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka                    cl.setTranslationY(0.0f);
8284cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka                    cl.setScaleX(1.0f);
8294cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka                    cl.setScaleY(1.0f);
8305f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka                    cl.setBackgroundAlpha(0.0f);
831f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                    cl.setAlpha(finalAlphaValue);
832f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen                    cl.setRotationY(rotation);
8334cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka                }
834b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka            }
835b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka            s.addListener(mUnshrinkAnimationListener);
836b0f28bd9a52fa4e343c8299d1c3e225d8e01c1e9Michael Jurka            s.start();
837dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        }
838dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    }
839dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka
84031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    void startDrag(CellLayout.CellInfo cellInfo) {
84131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        View child = cellInfo.cell;
842aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
84331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Make sure the drag was started by a long press as opposed to a long click.
8447984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert        if (!child.isInTouchMode()) {
84531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            return;
84631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
847aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
84831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mDragInfo = cellInfo;
8490142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        mDragInfo.screen = mCurrentPage;
850aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
8510142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        CellLayout current = ((CellLayout) getChildAt(mCurrentPage));
85231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
85331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        current.onDragChild(child);
85400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mDragController.startDrag(child, this, child.getTag(), DragController.DRAG_ACTION_MOVE);
855de7658b5e02ae10010e44fcf8d9c5814f54d9eb0Patrick Dubroy        current.onDragEnter(child);
85631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        invalidate();
85731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
85831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8590280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    void addApplicationShortcut(ShortcutInfo info, int screen, int cellX, int cellY,
8600280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            boolean insertAtFirst, int intersectX, int intersectY) {
8610280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        final CellLayout cellLayout = (CellLayout) getChildAt(screen);
8620280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        View view = mLauncher.createShortcut(R.layout.application, cellLayout, (ShortcutInfo) info);
86331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8640280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        final int[] cellXY = new int[2];
8650280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        cellLayout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY);
8660280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, insertAtFirst);
8670280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        LauncherModel.addOrMoveItemInDatabase(mLauncher, info,
8680280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
8690280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                cellXY[0], cellXY[1]);
87031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
87131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8720280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
87300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
87400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato            DragView dragView, Object dragInfo) {
8750280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        CellLayout cellLayout;
876a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        int originX = x - xOffset;
877a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        int originY = y - yOffset;
878a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        if (mIsSmall) {
8790280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            cellLayout = findMatchingPageForDragOver(dragView, originX, originY);
880a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            if (cellLayout == null) {
881a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                // cancel the drag if we're not over a mini-screen at time of drop
882a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                // TODO: maybe add a nice fade here?
883a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                return;
884a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            }
8850280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            // get originX and originY in the local coordinate system of the screen
8860280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            mTempOriginXY[0] = originX;
8870280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            mTempOriginXY[1] = originY;
8880280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            mapPointGlobalToLocal(cellLayout, mTempOriginXY);
8890280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            originX = (int)mTempOriginXY[0];
8900280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            originY = (int)mTempOriginXY[1];
8910280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        } else {
8920280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            cellLayout = getCurrentDropLayout();
893a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        }
894c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka
89531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (source != this) {
896a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            onDropExternal(originX, originY, dragInfo, cellLayout);
89731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } else {
89831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            // Move internally
89931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (mDragInfo != null) {
90031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                final View cell = mDragInfo.cell;
901a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
9026a1435d78d5133b1f37274c4d358bf6d22e10229Michael Jurka                mTargetCell = findNearestVacantArea(originX, originY,
903aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                        mDragInfo.spanX, mDragInfo.spanY, cell, cellLayout,
904aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                        mTargetCell);
905c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka
906c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka                int screen = indexOfChild(cellLayout);
907c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka                if (screen != mDragInfo.screen) {
908c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka                    final CellLayout originalCellLayout = (CellLayout) getChildAt(mDragInfo.screen);
909c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka                    originalCellLayout.removeView(cell);
910c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka                    addInScreen(cell, screen, mTargetCell[0], mTargetCell[1],
911c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka                            mDragInfo.spanX, mDragInfo.spanY);
912c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka                }
913aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                cellLayout.onDropChild(cell);
91431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
915aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                // update the item's position after drop
91684f296c106cb1c7b6d3ae6c6d5508a17f1324e29Romain Guy                final ItemInfo info = (ItemInfo) cell.getTag();
9170280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
9180280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                cellLayout.onMove(cell, mTargetCell[0], mTargetCell[1]);
919aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                lp.cellX = mTargetCell[0];
920aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                lp.cellY = mTargetCell[1];
92160587efe1a60ed698db9e2cafcd48f2c3fc8aa40Michael Jurka                cell.setId(LauncherModel.getCellLayoutChildId(-1, mDragInfo.screen,
922845ba3b17b83a2b11d79c6fb076cf96ab4a737dfMichael Jurka                        mTargetCell[0], mTargetCell[1], mDragInfo.spanX, mDragInfo.spanY));
923aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
92431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                LauncherModel.moveItemInDatabase(mLauncher, info,
925c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka                        LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
926aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                        lp.cellX, lp.cellY);
92731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
92831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
92931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
93031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
931aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    public void onDragEnter(DragSource source, int x, int y, int xOffset,
932aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            int yOffset, DragView dragView, Object dragInfo) {
933de7658b5e02ae10010e44fcf8d9c5814f54d9eb0Patrick Dubroy        getCurrentDropLayout().onDragEnter(dragView);
93431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
93531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
936440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy    public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
9376569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            DragView dragView, Object dragInfo) {
9386569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
9390280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        if (mIsSmall) {
9400280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            // If we're shrunken, don't let anyone drag on folders/etc  that are on the mini-screens
9410280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            return null;
9420280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        }
943440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        // We may need to delegate the drag to a child view. If a 1x1 item
944440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        // would land in a cell occupied by a DragTarget (e.g. a Folder),
945440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        // then drag events should be handled by that child.
946440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy
9476569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        ItemInfo item = (ItemInfo)dragInfo;
948440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        CellLayout currentLayout = getCurrentDropLayout();
949440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy
950440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        int dragPointX, dragPointY;
951440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        if (item.spanX == 1 && item.spanY == 1) {
952440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy            // For a 1x1, calculate the drop cell exactly as in onDragOver
953440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy            dragPointX = x - xOffset;
954440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy            dragPointY = y - yOffset;
955440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        } else {
956440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy            // Otherwise, use the exact drag coordinates
957440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy            dragPointX = x;
958440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy            dragPointY = y;
959440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        }
9600280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        dragPointX += mScrollX - currentLayout.getLeft();
9610280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        dragPointY += mScrollY - currentLayout.getTop();
962440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy
963440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        // If we are dragging over a cell that contains a DropTarget that will
964440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        // accept the drop, delegate to that DropTarget.
965440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        final int[] cellXY = mTempCell;
966440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        currentLayout.estimateDropCell(dragPointX, dragPointY, item.spanX, item.spanY, cellXY);
967440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        View child = currentLayout.getChildAt(cellXY[0], cellXY[1]);
968440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        if (child instanceof DropTarget) {
969440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy            DropTarget target = (DropTarget)child;
970440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy            if (target.acceptDrop(source, x, y, xOffset, yOffset, dragView, dragInfo)) {
971440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy                return target;
972440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy            }
973440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        }
974440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy        return null;
975440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy    }
9766569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
9770280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    private void mapPointGlobalToLocal(View v, float[] xy) {
9780280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        xy[0] = xy[0] + mScrollX - v.getLeft();
9790280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        xy[1] = xy[1] + mScrollY - v.getTop();
9800280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        v.getMatrix().invert(mTempInverseMatrix);
9810280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        mTempInverseMatrix.mapPoints(xy);
9820280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
9830280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
984a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    // xy = upper left corner of item being dragged
985a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    // bottomRightXy = lower right corner of item being dragged
986a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    // This method will see which mini-screen is most overlapped by the item being dragged, and
987a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    // return it. It will also transform the parameters xy and bottomRightXy into the local
988a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    // coordinate space of the returned screen
9890280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    private CellLayout findMatchingPageForDragOver(DragView dragView, int originX, int originY) {
9900280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        float x = originX + dragView.getScaledDragRegionXOffset();
9910280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        float y = originY + dragView.getScaledDragRegionYOffset();
9920280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        float right = x + dragView.getScaledDragRegionWidth();
9930280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        float bottom = y + dragView.getScaledDragRegionHeight();
994a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
995a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        // We loop through all the screens (ie CellLayouts) and see which one overlaps the most
996a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        // with the item being dragged.
997a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        final int screenCount = getChildCount();
998a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        CellLayout bestMatchingScreen = null;
9990280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        float smallestDistSoFar = Float.MAX_VALUE;
10000280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        final float[] xy = mTempDragCoordinates;
10010280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        final float[] bottomRightXy = mTempDragBottomRightCoordinates;
1002a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        for (int i = 0; i < screenCount; i++) {
1003a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            CellLayout cl = (CellLayout)getChildAt(i);
1004a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            // Transform the coordinates of the item being dragged to the CellLayout's coordinates
1005a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            float left = cl.getLeft();
1006a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            float top = cl.getTop();
1007a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            xy[0] = x + mScrollX - left;
1008a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            xy[1] = y + mScrollY - top;
1009a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
1010a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            bottomRightXy[0] = right + mScrollX - left;
1011a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            bottomRightXy[1] = bottom + mScrollY - top;
1012a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
10130280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            cl.getMatrix().invert(mTempInverseMatrix);
10140280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            mTempInverseMatrix.mapPoints(xy);
10150280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            mTempInverseMatrix.mapPoints(bottomRightXy);
1016a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
1017a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            float dragRegionX = xy[0];
1018a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            float dragRegionY = xy[1];
1019a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            float dragRegionRight = bottomRightXy[0];
1020a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            float dragRegionBottom = bottomRightXy[1];
10210280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            float dragRegionCenterX = (dragRegionX + dragRegionRight) / 2.0f;
10220280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            float dragRegionCenterY = (dragRegionY + dragRegionBottom) / 2.0f;
1023a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
1024a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            // Find the overlapping region
1025a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            float overlapLeft = Math.max(0f, dragRegionX);
1026a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            float overlapTop = Math.max(0f, dragRegionY);
1027a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            float overlapBottom = Math.min(cl.getHeight(), dragRegionBottom);
1028a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            float overlapRight = Math.min(cl.getWidth(), dragRegionRight);
1029a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            if (overlapRight >= 0 && overlapLeft <= cl.getWidth() &&
10300280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    (overlapTop >= 0 && overlapBottom <= cl.getHeight())) {
10310280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                // Calculate the distance between the two centers
10320280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                float distX = dragRegionCenterX - cl.getWidth()/2;
10330280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                float distY = dragRegionCenterY - cl.getHeight()/2;
10340280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                float dist = distX * distX + distY * distY;
10350280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
1036a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                float overlap = (overlapRight - overlapLeft) * (overlapBottom - overlapTop);
10370280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
10380280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                // Calculate the closest overlapping region
10390280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                if (overlap > 0 && dist < smallestDistSoFar) {
10400280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    smallestDistSoFar = dist;
1041a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                    bestMatchingScreen = cl;
1042a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                }
1043a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka             }
1044a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        }
10450280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
10460280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        if (bestMatchingScreen != mDragTargetLayout) {
1047a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            if (mDragTargetLayout != null) {
10480280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                mDragTargetLayout.onDragExit();
1049a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            }
1050a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            mDragTargetLayout = bestMatchingScreen;
1051a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        }
1052a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        return bestMatchingScreen;
1053a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    }
1054a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
1055440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy    public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
1056440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy            DragView dragView, Object dragInfo) {
10570280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        CellLayout currentLayout;
10580280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        int originX = x - xOffset;
10590280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        int originY = y - yOffset;
10600280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        if (mIsSmall) {
10610280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            currentLayout = findMatchingPageForDragOver(dragView, originX, originY);
10620280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
10630280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            if (currentLayout == null) {
10640280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                return;
10650280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            }
10660280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
10670280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            currentLayout.setHover(true);
10680280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            // get originX and originY in the local coordinate system of the screen
10690280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            mTempOriginXY[0] = originX;
10700280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            mTempOriginXY[1] = originY;
10710280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            mapPointGlobalToLocal(currentLayout, mTempOriginXY);
10720280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            originX = (int)mTempOriginXY[0];
10730280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            originY = (int)mTempOriginXY[1];
10740280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        } else {
10750280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            currentLayout = getCurrentDropLayout();
10760280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        }
1077440c360bc395c43683fa9ca226e59f9e35f9e926Patrick Dubroy
1078976ebec9e31643d3513f9f0de2b433d9aa186ce9Patrick Dubroy        final ItemInfo item = (ItemInfo)dragInfo;
10796569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
10806569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        if (dragInfo instanceof LauncherAppWidgetInfo) {
10816569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo)dragInfo;
10826569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
10836569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            if (widgetInfo.spanX == -1) {
10846569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy                // Calculate the grid spans needed to fit this widget
10858f86ddcb90063a56c25c9c782316574bc4e5dd93Patrick Dubroy                int[] spans = currentLayout.rectToCell(widgetInfo.minWidth, widgetInfo.minHeight, null);
10866569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy                item.spanX = spans[0];
10876569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy                item.spanY = spans[1];
10886569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            }
10896569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        }
1090a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
1091a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        if (source != this) {
1092a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            // This is a hack to fix the point used to determine which cell an icon from the all
1093a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            // apps screen is over
1094a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            if (item != null && item.spanX == 1 && currentLayout != null) {
1095a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                int dragRegionLeft = (dragView.getWidth() - currentLayout.getCellWidth()) / 2;
1096a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
1097a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                originX += dragRegionLeft - dragView.getDragRegionLeft();
1098a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                if (dragView.getDragRegionWidth() != currentLayout.getCellWidth()) {
1099a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                    dragView.setDragRegion(dragView.getDragRegionLeft(), dragView.getDragRegionTop(),
1100a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                            currentLayout.getCellWidth(), dragView.getDragRegionHeight());
1101a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                }
1102a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            }
1103a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        }
11046569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        if (currentLayout != mDragTargetLayout) {
11056569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            if (mDragTargetLayout != null) {
11060280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                mDragTargetLayout.onDragExit();
1107de7658b5e02ae10010e44fcf8d9c5814f54d9eb0Patrick Dubroy                currentLayout.onDragEnter(dragView);
11086569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            }
11096569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            mDragTargetLayout = currentLayout;
11106569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        }
11116569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
1112a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        // only visualize the drop locations for moving icons within the home screen on tablet
1113a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        // on phone, we also visualize icons dragged in from All Apps
1114a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        if ((!LauncherApplication.isScreenXLarge() || source == this)
1115a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                && mDragTargetLayout != null) {
1116a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            final View child = (mDragInfo == null) ? null : mDragInfo.cell;
11175f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            int localOriginX = originX - (mDragTargetLayout.getLeft() - mScrollX);
11185f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            int localOriginY = originY - (mDragTargetLayout.getTop() - mScrollY);
1119a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka            mDragTargetLayout.visualizeDropLocation(
1120de7658b5e02ae10010e44fcf8d9c5814f54d9eb0Patrick Dubroy                    child, localOriginX, localOriginY, item.spanX, item.spanY);
1121976ebec9e31643d3513f9f0de2b433d9aa186ce9Patrick Dubroy        }
112231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
112331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1124aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    public void onDragExit(DragSource source, int x, int y, int xOffset,
1125aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            int yOffset, DragView dragView, Object dragInfo) {
11266569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        if (mDragTargetLayout != null) {
11270280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            mDragTargetLayout.onDragExit();
11286569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            mDragTargetLayout = null;
11296569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        }
113031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
113131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1132aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    private void onDropExternal(int x, int y, Object dragInfo,
1133aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            CellLayout cellLayout) {
113431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        onDropExternal(x, y, dragInfo, cellLayout, false);
113531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1136aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
11372b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy    /**
11382b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy     * Add the item specified by dragInfo to the given layout.
11392b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy     * This is basically the equivalent of onDropExternal, except it's not initiated
11402b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy     * by drag and drop.
11412b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy     * @return true if successful
11422b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy     */
11432b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy    public boolean addExternalItemToScreen(Object dragInfo, View layout) {
11442b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy        CellLayout cl = (CellLayout) layout;
11452b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy        ItemInfo info = (ItemInfo) dragInfo;
11462b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy
11470280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        if (cl.findCellForSpan(mTempEstimate, info.spanX, info.spanY)) {
11482b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy            onDropExternal(0, 0, dragInfo, cl, false);
11492b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy            return true;
11502b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy        }
11510280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        mLauncher.showOutOfSpaceMessage();
11522b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy        return false;
11532b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy    }
11542b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy
11550280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    // Drag from somewhere else
1156aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    private void onDropExternal(int x, int y, Object dragInfo,
1157aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            CellLayout cellLayout, boolean insertAtFirst) {
11580280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        int screen = indexOfChild(cellLayout);
11590280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        if (dragInfo instanceof PendingAddItemInfo) {
11600280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            PendingAddItemInfo info = (PendingAddItemInfo) dragInfo;
11610280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            // When dragging and dropping from customization tray, we deal with creating
11620280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            // widgets/shortcuts/folders in a slightly different way
11630280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            int[] touchXY = new int[2];
11640280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            touchXY[0] = x;
11650280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            touchXY[1] = y;
11660280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            switch (info.itemType) {
11670280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
11680280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    mLauncher.addAppWidgetFromDrop(info.componentName, screen, touchXY);
11690280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    break;
11700280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
11710280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    mLauncher.addLiveFolderFromDrop(info.componentName, screen, touchXY);
11720280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    break;
11730280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
11740280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    mLauncher.processShortcutFromDrop(info.componentName, screen, touchXY);
11750280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    break;
11760280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                default:
11770280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    throw new IllegalStateException("Unknown item type: " + info.itemType);
11780280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            }
11790280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            cellLayout.onDragExit();
11800280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            return;
11810280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        }
11820280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
11830280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        // This is for other drag/drop cases, like dragging from All Apps
118431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        ItemInfo info = (ItemInfo) dragInfo;
118531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1186af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka        View view = null;
118731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
118831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        switch (info.itemType) {
118931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
119031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1191e48e7c1a62e6a367803dca62c8fce9de57121b0fJoe Onorato            if (info.container == NO_ID && info instanceof ApplicationInfo) {
119231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                // Came from all apps -- make a copy
1193aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                info = new ShortcutInfo((ApplicationInfo) info);
119431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
1195aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            view = mLauncher.createShortcut(R.layout.application, cellLayout,
1196aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    (ShortcutInfo) info);
119731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            break;
119831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
119931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher,
12000280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    cellLayout, ((UserFolderInfo) info));
1201af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka            break;
120231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        default:
12030280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            throw new IllegalStateException("Unknown item type: " + info.itemType);
120431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
120531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12066569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        // If the view is null, it has already been added.
12076569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        if (view == null) {
12080280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            cellLayout.onDragExit();
12096569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        } else {
12107ded67cd64153cd41bb133a6aab11b3254b4a5d9Michael Jurka            mTargetCell = findNearestVacantArea(x, y, 1, 1, null, cellLayout, mTargetCell);
1211aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            addInScreen(view, indexOfChild(cellLayout), mTargetCell[0],
1212aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    mTargetCell[1], info.spanX, info.spanY, insertAtFirst);
1213aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            cellLayout.onDropChild(view);
12146569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
121531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1216af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka            LauncherModel.addOrMoveItemInDatabase(mLauncher, info,
12170280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    LauncherSettings.Favorites.CONTAINER_DESKTOP, screen,
1218aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    lp.cellX, lp.cellY);
1219af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka        }
122031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1221aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
122270864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    /**
122370864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * Return the current {@link CellLayout}, correctly picking the destination
122470864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * screen while a scroll is in progress.
122570864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     */
122670864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    private CellLayout getCurrentDropLayout() {
12270142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        int index = mScroller.isFinished() ? mCurrentPage : mNextPage;
122870864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey        return (CellLayout) getChildAt(index);
122970864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    }
123031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
123170864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    /**
12320280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     * Return the current CellInfo describing our current drag; this method exists
12330280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     * so that Launcher can sync this object with the correct info when the activity is created/
12340280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     * destroyed
12350280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     *
12360280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     */
12370280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    public CellLayout.CellInfo getDragInfo() {
12380280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        return mDragInfo;
12390280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
12400280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
12410280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    /**
124270864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * {@inheritDoc}
124370864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     */
124470864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    public boolean acceptDrop(DragSource source, int x, int y,
124500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato            int xOffset, int yOffset, DragView dragView, Object dragInfo) {
12460280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        CellLayout layout;
12470280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        if (mIsSmall) {
12480280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            layout = findMatchingPageForDragOver(dragView, x - xOffset, y - yOffset);
12490280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            if (layout == null) {
12500280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                // cancel the drag if we're not over a mini-screen at time of drop
12510280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                return false;
12520280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            }
12530280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        } else {
12540280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            layout = getCurrentDropLayout();
12550280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        }
1256c28de51eedb26848abf9245ddd19e021d30be318Michael Jurka        final CellLayout.CellInfo dragCellInfo = mDragInfo;
1257c28de51eedb26848abf9245ddd19e021d30be318Michael Jurka        final int spanX = dragCellInfo == null ? 1 : dragCellInfo.spanX;
1258c28de51eedb26848abf9245ddd19e021d30be318Michael Jurka        final int spanY = dragCellInfo == null ? 1 : dragCellInfo.spanY;
12594c58c485d8c02f8ca7e8b4d93140440f6a3a5131Romain Guy
1260c28de51eedb26848abf9245ddd19e021d30be318Michael Jurka        final View ignoreView = dragCellInfo == null ? null : dragCellInfo.cell;
12614c58c485d8c02f8ca7e8b4d93140440f6a3a5131Romain Guy
12620280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        if (layout.findCellForSpanIgnoring(null, spanX, spanY, ignoreView)) {
12630e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka            return true;
12640e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka        } else {
12650280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            mLauncher.showOutOfSpaceMessage();
12660e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka            return false;
12670e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka        }
126870864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    }
1269aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
127070864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    /**
127170864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * Calculate the nearest cell where the given object would be dropped.
127270864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     */
12736a1435d78d5133b1f37274c4d358bf6d22e10229Michael Jurka    private int[] findNearestVacantArea(int pixelX, int pixelY,
127470864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey            int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) {
12756569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
12765f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        int localPixelX = pixelX - (layout.getLeft() - mScrollX);
12775f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        int localPixelY = pixelY - (layout.getTop() - mScrollY);
12786569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
127970864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey        // Find the best target drop location
12800280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        return layout.findNearestVacantArea(
1281fc9f07d42511231a26fa182c32e2efaa0f41d478Michael Jurka                localPixelX, localPixelY, spanX, spanY, ignoreView, recycle);
128270864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    }
1283aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
12848f86ddcb90063a56c25c9c782316574bc4e5dd93Patrick Dubroy    /**
12858f86ddcb90063a56c25c9c782316574bc4e5dd93Patrick Dubroy     * Estimate the size that a child with the given dimensions will take in the current screen.
12868f86ddcb90063a56c25c9c782316574bc4e5dd93Patrick Dubroy     */
12878f86ddcb90063a56c25c9c782316574bc4e5dd93Patrick Dubroy    void estimateChildSize(int minWidth, int minHeight, int[] result) {
12880142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        ((CellLayout)getChildAt(mCurrentPage)).estimateChildSize(minWidth, minHeight, result);
12898f86ddcb90063a56c25c9c782316574bc4e5dd93Patrick Dubroy    }
12908f86ddcb90063a56c25c9c782316574bc4e5dd93Patrick Dubroy
129131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    void setLauncher(Launcher launcher) {
129231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mLauncher = launcher;
129331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
129431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
129500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    public void setDragController(DragController dragController) {
129600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mDragController = dragController;
129731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
129831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
129931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void onDropCompleted(View target, boolean success) {
1300aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        if (success) {
130131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (target != this && mDragInfo != null) {
130231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
130331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                cellLayout.removeView(mDragInfo.cell);
130400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                if (mDragInfo.cell instanceof DropTarget) {
130500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                    mDragController.removeDropTarget((DropTarget)mDragInfo.cell);
130600acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato                }
1307aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                // final Object tag = mDragInfo.cell.getTag();
130831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
130931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } else {
131031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (mDragInfo != null) {
131131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
131231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                cellLayout.onDropAborted(mDragInfo.cell);
131331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
131431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
131531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
131631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mDragInfo = null;
131731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
131831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13190280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    public boolean isDropEnabled() {
13200280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        return true;
13210280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
13220280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
13230142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
13240142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    protected void onRestoreInstanceState(Parcelable state) {
13250142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        super.onRestoreInstanceState(state);
13260142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        Launcher.setScreen(mCurrentPage);
13270142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    }
13280142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
13290142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
133031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void scrollLeft() {
13317f0f4f3c35fe0b6a94504ec39ac127fa935282efMichael Jurka        if (!mIsSmall) {
13320142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            super.scrollLeft();
133331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
133431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
133531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13360142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
133731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void scrollRight() {
13387f0f4f3c35fe0b6a94504ec39ac127fa935282efMichael Jurka        if (!mIsSmall) {
13390142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            super.scrollRight();
134031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
134131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1342138a04170d964da9cdec228e95b976875ae52481Karl Rosaen
134331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public Folder getFolderForTag(Object tag) {
1344dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
134531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        for (int screen = 0; screen < screenCount; screen++) {
134631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            CellLayout currentScreen = ((CellLayout) getChildAt(screen));
134731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            int count = currentScreen.getChildCount();
134831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            for (int i = 0; i < count; i++) {
134931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                View child = currentScreen.getChildAt(i);
135031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
135131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
135231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    Folder f = (Folder) child;
1353aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    if (f.getInfo() == tag && f.getInfo().opened) {
135431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                        return f;
135531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    }
135631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
135731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
135831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
135931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return null;
136031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
136131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
136231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public View getViewForTag(Object tag) {
136331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        int screenCount = getChildCount();
136431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        for (int screen = 0; screen < screenCount; screen++) {
136531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            CellLayout currentScreen = ((CellLayout) getChildAt(screen));
136631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            int count = currentScreen.getChildCount();
136731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            for (int i = 0; i < count; i++) {
136831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                View child = currentScreen.getChildAt(i);
136931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                if (child.getTag() == tag) {
137031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    return child;
137131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
137231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
137331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
137431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return null;
137531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
137631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
137731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
137864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato    void removeItems(final ArrayList<ApplicationInfo> apps) {
1379dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
13805c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy        final PackageManager manager = getContext().getPackageManager();
1381629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy        final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext());
1382574d20ec84551370987dde530c27ec493bdef564Romain Guy
138364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        final HashSet<String> packageNames = new HashSet<String>();
138464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        final int appCount = apps.size();
138564e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        for (int i = 0; i < appCount; i++) {
138664e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato            packageNames.add(apps.get(i).componentName.getPackageName());
138764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato        }
138864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato
1389dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        for (int i = 0; i < screenCount; i++) {
139031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final CellLayout layout = (CellLayout) getChildAt(i);
1391629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy
1392629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy            // Avoid ANRs by treating each screen separately
1393629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy            post(new Runnable() {
1394629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                public void run() {
1395629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    final ArrayList<View> childrenToRemove = new ArrayList<View>();
1396629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    childrenToRemove.clear();
1397aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1398629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    int childCount = layout.getChildCount();
1399629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    for (int j = 0; j < childCount; j++) {
1400629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        final View view = layout.getChildAt(j);
1401629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        Object tag = view.getTag();
1402aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
14030589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                        if (tag instanceof ShortcutInfo) {
14040589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            final ShortcutInfo info = (ShortcutInfo) tag;
1405629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final Intent intent = info.intent;
1406629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final ComponentName name = intent.getComponent();
1407aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
140864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                            if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
140964e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                for (String packageName: packageNames) {
141064e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    if (packageName.equals(name.getPackageName())) {
141164e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
141264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        childrenToRemove.add(view);
141364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    }
141464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                }
1415629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            }
1416629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        } else if (tag instanceof UserFolderInfo) {
1417629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final UserFolderInfo info = (UserFolderInfo) tag;
14180589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            final ArrayList<ShortcutInfo> contents = info.contents;
14190589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                            final ArrayList<ShortcutInfo> toRemove = new ArrayList<ShortcutInfo>(1);
1420629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final int contentsCount = contents.size();
1421629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            boolean removedFromFolder = false;
1422aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1423629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            for (int k = 0; k < contentsCount; k++) {
14240589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                                final ShortcutInfo appInfo = contents.get(k);
1425629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                                final Intent intent = appInfo.intent;
1426629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                                final ComponentName name = intent.getComponent();
1427aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
142864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
142964e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    for (String packageName: packageNames) {
143064e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        if (packageName.equals(name.getPackageName())) {
143164e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                            toRemove.add(appInfo);
143273013bf94f49ffbacba2b8300f6a2dd4eeebbd13Brad Fitzpatrick                                            LauncherModel.deleteItemFromDatabase(mLauncher, appInfo);
143364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                            removedFromFolder = true;
143464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        }
143564e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    }
1436629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                                }
1437629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            }
1438aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1439629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            contents.removeAll(toRemove);
1440629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            if (removedFromFolder) {
1441629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                                final Folder folder = getOpenFolder();
1442aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                                if (folder != null)
1443aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                                    folder.notifyDataSetChanged();
1444629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            }
1445629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        } else if (tag instanceof LiveFolderInfo) {
1446629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final LiveFolderInfo info = (LiveFolderInfo) tag;
1447629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final Uri uri = info.uri;
1448629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final ProviderInfo providerInfo = manager.resolveContentProvider(
1449629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                                    uri.getAuthority(), 0);
1450629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy
1451f11079b11e98912ed56546eaa300efd06c6eb917Joe Onorato                            if (providerInfo != null) {
145264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                for (String packageName: packageNames) {
145364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    if (packageName.equals(providerInfo.packageName)) {
145464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
1455aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                                        childrenToRemove.add(view);
145664e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    }
145764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                }
1458629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            }
1459629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        } else if (tag instanceof LauncherAppWidgetInfo) {
1460629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag;
1461629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            final AppWidgetProviderInfo provider =
1462629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                                    widgets.getAppWidgetInfo(info.appWidgetId);
1463e1cc6c3745c9ba721b6ab888d74c9d9e1d13ea6aDaniel Sandler                            if (provider != null) {
146464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                for (String packageName: packageNames) {
146564e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    if (packageName.equals(provider.provider.getPackageName())) {
146664e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
1467aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                                        childrenToRemove.add(view);
146864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                    }
146964e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                }
1470629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            }
1471574d20ec84551370987dde530c27ec493bdef564Romain Guy                        }
1472574d20ec84551370987dde530c27ec493bdef564Romain Guy                    }
1473aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1474629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    childCount = childrenToRemove.size();
1475629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    for (int j = 0; j < childCount; j++) {
1476629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        View child = childrenToRemove.get(j);
1477629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        layout.removeViewInLayout(child);
1478629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        if (child instanceof DropTarget) {
1479629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            mDragController.removeDropTarget((DropTarget)child);
1480629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        }
1481574d20ec84551370987dde530c27ec493bdef564Romain Guy                    }
1482aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1483629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                    if (childCount > 0) {
1484629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        layout.requestLayout();
1485629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        layout.invalidate();
14865c16f3ecd6b47bff3abbe40deb3d39c66a3b0012Romain Guy                    }
148731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
1488629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy            });
148931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
149031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1491f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
149264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato    void updateShortcuts(ArrayList<ApplicationInfo> apps) {
1493dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
1494dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        for (int i = 0; i < screenCount; i++) {
1495f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            final CellLayout layout = (CellLayout) getChildAt(i);
1496f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            int childCount = layout.getChildCount();
1497f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            for (int j = 0; j < childCount; j++) {
1498f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                final View view = layout.getChildAt(j);
1499f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                Object tag = view.getTag();
15000589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                if (tag instanceof ShortcutInfo) {
15010589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                    ShortcutInfo info = (ShortcutInfo)tag;
1502f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    // We need to check for ACTION_MAIN otherwise getComponent() might
1503f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    // return null for some shortcuts (for instance, for shortcuts to
1504f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    // web pages.)
1505f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    final Intent intent = info.intent;
1506f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    final ComponentName name = intent.getComponent();
1507f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
150864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                            Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
150964e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                        final int appCount = apps.size();
1510aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                        for (int k = 0; k < appCount; k++) {
151164e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                            ApplicationInfo app = apps.get(k);
151264e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                            if (app.componentName.equals(name)) {
151364e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                info.setIcon(mIconCache.getIcon(info.intent));
151464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                ((TextView)view).setCompoundDrawablesWithIntrinsicBounds(null,
151564e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        new FastBitmapDrawable(info.getIcon(mIconCache)),
151664e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                        null, null);
151764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                                }
151864e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato                        }
1519f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                    }
1520f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project                }
1521f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project            }
1522f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project        }
1523f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
1524f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
152514f122bf847e50a3e7730ccbe57abc25d086a01bJoe Onorato    void moveToDefaultScreen(boolean animate) {
1526c45b16862918a0c6c253bba12032165b030565afJoe Onorato        if (animate) {
15274cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka            if (mIsSmall) {
15280142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                unshrink(mDefaultPage);
15294cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka            } else {
15300142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                snapToPage(mDefaultPage);
15314cb3724a2c1e5f278e1531d643accc40fcd8e219Michael Jurka            }
1532c45b16862918a0c6c253bba12032165b030565afJoe Onorato        } else {
15330142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            setCurrentPage(mDefaultPage);
1534c45b16862918a0c6c253bba12032165b030565afJoe Onorato        }
15350142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        getChildAt(mDefaultPage).requestFocus();
153631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
153731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
15388a73c51ee87b6d9b12daba188034889caf7a905bRomain Guy    void setIndicators(Drawable previous, Drawable next) {
15398a73c51ee87b6d9b12daba188034889caf7a905bRomain Guy        mPreviousIndicator = previous;
15408a73c51ee87b6d9b12daba188034889caf7a905bRomain Guy        mNextIndicator = next;
15410142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        previous.setLevel(mCurrentPage);
15420142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        next.setLevel(mCurrentPage);
15438a73c51ee87b6d9b12daba188034889caf7a905bRomain Guy    }
15448a73c51ee87b6d9b12daba188034889caf7a905bRomain Guy
15450142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
15460142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    public void syncPages() {
15470142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    }
154831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
15490142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
15500142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    public void syncPageItems(int page) {
155131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
15520142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
155331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
1554