Java tutorial
/* * Copyright (C) 2015 Kumaresan Rajeswaran * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.leanlauncher; import android.animation.Animator; import android.animation.Animator.AnimatorListener; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.LayoutTransition; import android.animation.ObjectAnimator; import android.animation.TimeInterpolator; import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; import android.app.WallpaperManager; import android.appwidget.AppWidgetHostView; import android.appwidget.AppWidgetProviderInfo; import android.content.ComponentName; import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Point; import android.graphics.PointF; import android.graphics.Rect; import android.graphics.Region.Op; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Handler; import android.os.IBinder; import android.support.v4.util.ArrayMap; import android.support.v4.view.ViewCompat; import android.util.AttributeSet; import android.util.Log; import android.view.Display; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.accessibility.AccessibilityManager; import android.view.animation.DecelerateInterpolator; import android.widget.TextView; import com.android.leanlauncher.LauncherSettings.Favorites; import com.android.leanlauncher.compat.UserHandleCompat; import java.util.ArrayList; import java.util.HashSet; import java.util.concurrent.atomic.AtomicInteger; /** * The workspace is a wide area with a wallpaper and a single page. * The page contains a number of icons, folders or widgets the user can * interact with. A workspace is meant to be used with a fixed width only. */ public class Workspace extends ViewGroup implements DropTarget, DragSource, View.OnTouchListener, DragController.DragListener, LauncherTransitionable, ViewGroup.OnHierarchyChangeListener { private static final String TAG = "LeanLauncher.Workspace"; private static final int CHILDREN_OUTLINE_FADE_OUT_DELAY = 0; private static final int CHILDREN_OUTLINE_FADE_OUT_DURATION = 375; private static final int CHILDREN_OUTLINE_FADE_IN_DURATION = 100; private static final int BACKGROUND_FADE_OUT_DURATION = 350; private static final float ALPHA_CUTOFF_THRESHOLD = 0.01f; static final boolean MAP_NO_RECURSE = false; static final boolean MAP_RECURSE = true; // These animators are used to fade the children's outlines private ObjectAnimator mChildrenOutlineFadeInAnimation; private ObjectAnimator mChildrenOutlineFadeOutAnimation; private float mChildrenOutlineAlpha = 0; // These properties refer to the background protection gradient used for AllApps and Customize private ValueAnimator mBackgroundFadeInAnimation; private ValueAnimator mBackgroundFadeOutAnimation; private LayoutTransition mLayoutTransition; private final WallpaperManager mWallpaperManager; private IBinder mWindowToken; private static boolean sAccessibilityEnabled; private CellLayout mWorkspace; /** * CellInfo for the cell that is currently being dragged */ private CellLayout.CellInfo mDragInfo; /** * Target drop area calculated during last acceptDrop call. */ private int[] mTargetCell = new int[2]; private int mDragOverX = -1; private int mDragOverY = -1; static Rect mLandscapeCellLayoutMetrics = null; static Rect mPortraitCellLayoutMetrics = null; /** * The CellLayout that is currently being dragged over */ private CellLayout mDragTargetLayout = null; /** * The CellLayout that we will show as glowing */ private CellLayout mDragOverlappingLayout = null; /** * The CellLayout which will be dropped to */ private CellLayout mDropToLayout = null; private Launcher mLauncher; private IconCache mIconCache; private DragController mDragController; // These are temporary variables to prevent having to allocate a new object just to // return an (x, y) value from helper functions. Do NOT use them to maintain other state. private int[] mTempCell = new int[2]; private float[] mDragViewVisualCenter = new float[2]; private float[] mTempCellLayoutCenterCoordinates = new float[2]; private Matrix mTempInverseMatrix = new Matrix(); private SpringLoadedDragController mSpringLoadedDragController; private float mSpringLoadedShrinkFactor; private float mOverviewModeShrinkFactor; private Rect mViewPort = new Rect(); // State variable that indicates whether the pages are small (ie when you're // in all apps or customize mode) enum State { NORMAL, NORMAL_HIDDEN, SPRING_LOADED, OVERVIEW, OVERVIEW_HIDDEN } private State mState = State.NORMAL; private boolean mIsSwitchingState = false; boolean mAnimatingViewIntoPlace = false; boolean mIsDragOccuring = false; /** Is the user is dragging an item near the edge of a page? */ private boolean mInScrollArea = false; private HolographicOutlineHelper mOutlineHelper; private Bitmap mDragOutline = null; private static final Rect sTempRect = new Rect(); private final int[] mTempXY = new int[2]; public static final int DRAG_BITMAP_PADDING = 2; private Point mDisplaySize = new Point(); private final Canvas mCanvas = new Canvas(); // Relating to the animation of items being dropped externally public static final int ANIMATE_INTO_POSITION_AND_DISAPPEAR = 0; public static final int ANIMATE_INTO_POSITION_AND_REMAIN = 1; public static final int ANIMATE_INTO_POSITION_AND_RESIZE = 2; public static final int COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION = 3; public static final int CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION = 4; // Related to dragging, folder creation and reordering private static final int DRAG_MODE_NONE = 0; private static final int DRAG_MODE_REORDER = 1; private int mDragMode = DRAG_MODE_NONE; private int mLastReorderX = -1; private int mLastReorderY = -1; private float mCurrentScale; private float mNewScale; private float mOldBackgroundAlpha; private float mNewBackgroundAlpha; private float mTransitionProgress; private Animator mStateAnimator = null; private int mTouchState = PagedView.TOUCH_STATE_REST; private Runnable mDeferredAction; private boolean mDeferDropAfterUninstall; private boolean mUninstallSuccessful; private final Runnable mBindPages = new Runnable() { @Override public void run() { mLauncher.getModel().bindRemainingSynchronousPages(); } }; /** * Used to inflate the Workspace from XML. * * @param context The application's context. * @param attrs The attributes set containing the Workspace's customization values. */ public Workspace(Context context, AttributeSet attrs) { this(context, attrs, 0); } /** * Used to inflate the Workspace from XML. * * @param context The application's context. * @param attrs The attributes set containing the Workspace's customization values. * @param defStyle Unused. */ public Workspace(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mOutlineHelper = HolographicOutlineHelper.obtain(context); mLauncher = (Launcher) context; final Resources res = getResources(); mWallpaperManager = WallpaperManager.getInstance(context); LauncherAppState app = LauncherAppState.getInstance(); DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Workspace, defStyle, 0); mSpringLoadedShrinkFactor = res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f; mOverviewModeShrinkFactor = grid.getOverviewModeScale(); a.recycle(); setOnHierarchyChangeListener(this); setHapticFeedbackEnabled(false); initWorkspace(); // Disable multitouch across the workspace/all apps/customize tray setMotionEventSplittingEnabled(true); setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); } // estimate the size of a widget with spans hSpan, vSpan. return MAX_VALUE for each // dimension if unsuccessful public int[] estimateItemSize(int hSpan, int vSpan, boolean springLoaded) { int[] size = new int[2]; if (getChildCount() > 0) { // Use the first non-custom page to estimate the child position Rect r = estimateItemPosition(mWorkspace, 0, 0, hSpan, vSpan); size[0] = r.width(); size[1] = r.height(); if (springLoaded) { size[0] *= mSpringLoadedShrinkFactor; size[1] *= mSpringLoadedShrinkFactor; } return size; } else { size[0] = Integer.MAX_VALUE; size[1] = Integer.MAX_VALUE; return size; } } public Rect estimateItemPosition(CellLayout cl, int hCell, int vCell, int hSpan, int vSpan) { Rect r = new Rect(); cl.cellToRect(hCell, vCell, hSpan, vSpan, r); return r; } public void onDragStart(final DragSource source, Object info, int dragAction) { mIsDragOccuring = true; mLauncher.lockScreenOrientation(mLauncher.getRequestedOrientation()); setChildrenBackgroundAlphaMultipliers(1f); } public void onDragEnd() { mIsDragOccuring = false; mLauncher.unlockScreenOrientation(false); } /** * Initializes various states for this workspace. */ protected void initWorkspace() { LauncherAppState app = LauncherAppState.getInstance(); mIconCache = app.getIconCache(); setWillNotDraw(false); setClipChildren(true); setClipToPadding(false); setChildrenDrawnWithCacheEnabled(true); setupLayoutTransition(); Display display = mLauncher.getWindowManager().getDefaultDisplay(); display.getSize(mDisplaySize); } private void setupLayoutTransition() { // We want to show layout transitions when pages are deleted, to close the gap. mLayoutTransition = new LayoutTransition(); mLayoutTransition.enableTransitionType(LayoutTransition.DISAPPEARING); mLayoutTransition.enableTransitionType(LayoutTransition.CHANGE_DISAPPEARING); mLayoutTransition.disableTransitionType(LayoutTransition.APPEARING); mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING); setLayoutTransition(mLayoutTransition); } void enableLayoutTransitions() { setLayoutTransition(mLayoutTransition); } void disableLayoutTransitions() { setLayoutTransition(null); } @Override public void onChildViewAdded(View parent, View child) { if (!(child instanceof CellLayout)) { throw new IllegalArgumentException("A Workspace can only have CellLayout children."); } CellLayout cl = ((CellLayout) child); cl.setOnInterceptTouchListener(this); cl.setClickable(true); cl.setImportantForAccessibility(ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO); } @Override public void onChildViewRemoved(View parent, View child) { } protected boolean shouldDrawChild(View child) { final CellLayout cl = (CellLayout) child; return cl.getVisibility() == VISIBLE && (mIsSwitchingState || cl.getShortcutsAndWidgets().getAlpha() > 0 || cl.getBackgroundAlpha() > 0); } boolean isTouchActive() { return mTouchState != PagedView.TOUCH_STATE_REST; } public void removeAllWorkspace() { // Disable all layout transitions before removing all pages to ensure that we don't get the // transition animations competing with us changing the scroll when we add pages or the // custom content screen disableLayoutTransitions(); // Remove the pages and clear the screen models removeAllViews(); // Re-enable the layout transitions enableLayoutTransitions(); } public void addNewWorkspace() { // Log to disk Log.e(TAG, "11683562 - addNewWorkspace(): " + getChildCount()); if (mWorkspace != null) { Log.e(TAG, "Screen already exists!"); return; } mWorkspace = (CellLayout) mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, null); mWorkspace.setOnLongClickListener(mLauncher); mWorkspace.setOnClickListener(mLauncher); mWorkspace.setSoundEffectsEnabled(false); mWorkspace.setDescendantFocusability(FOCUS_AFTER_DESCENDANTS); mWorkspace.setClipChildren(false); mWorkspace.setClipToPadding(false); // Add an all apps Icon addAllAppsIcon(); super.addView(mWorkspace); } public CellLayout getScreen() { return mWorkspace; } // At bind time, we use the rank (screenId) to compute x and y for hotseat items. // See implementation for parameter definition. void addInScreenFromBind(View child, long container, int x, int y, int spanX, int spanY) { addInScreen(child, container, x, y, spanX, spanY, false); } /** * Adds the specified child in the specified screen. The position and dimension of * the child are defined by x, y, spanX and spanY. * * @param child The child to add in one of the workspace's screens. * @param x The X position of the child in the screen's grid. * @param y The Y position of the child in the screen's grid. * @param spanX The number of cells spanned horizontally by the child. * @param spanY The number of cells spanned vertically by the child. * @param insert When true, the child is inserted at the beginning of the children list. */ void addInScreen(View child, long container, int x, int y, int spanX, int spanY, boolean insert) { if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { if (getScreen() == null) { Log.e(TAG, "Skipping child, screen not found"); // DEBUGGING - Print out the stack trace to see where we are adding from new Throwable().printStackTrace(); return; } } final CellLayout layout; layout = getScreen(); child.setOnKeyListener(new IconKeyEventListener()); ViewGroup.LayoutParams genericLp = child.getLayoutParams(); CellLayout.LayoutParams lp; if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) { lp = new CellLayout.LayoutParams(x, y, spanX, spanY); } else { lp = (CellLayout.LayoutParams) genericLp; lp.cellX = x; lp.cellY = y; lp.cellHSpan = spanX; lp.cellVSpan = spanY; } if (spanX < 0 && spanY < 0) { lp.isLockedToGrid = false; } // Get the canonical child id to uniquely represent this view in this screen ItemInfo info = (ItemInfo) child.getTag(); int childId = LauncherAppState.getInstance().getViewIdForItem(info); if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, true)) { // TODO: This branch occurs when the workspace is adding views // outside of the defined grid // maybe we should be deleting these items from the LauncherModel? Log.d(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout"); } child.setHapticFeedbackEnabled(false); child.setOnLongClickListener(mLauncher); if (child instanceof DropTarget) { mDragController.addDropTarget((DropTarget) child); } } private void addAllAppsIcon() { // Add the Apps button LayoutInflater inflater = LayoutInflater.from(getContext()); TextView allAppsButton = (TextView) inflater.inflate(R.layout.all_apps_button, mWorkspace, false); Drawable d = getResources().getDrawable(R.drawable.all_apps_button_icon); Utilities.resizeIconDrawable(d); allAppsButton.setCompoundDrawables(null, d, null, null); allAppsButton.setContentDescription(getResources().getString(R.string.all_apps_button_label)); mLauncher.setAllAppsButton(allAppsButton); allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener()); allAppsButton.setOnClickListener(mLauncher); CellLayout.LayoutParams lp = new CellLayout.LayoutParams((mWorkspace.getCountX() - 1) / 2, mWorkspace.getCountY() - 1, 1, 1); lp.canReorder = false; mWorkspace.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true); } /** * Called directly from a CellLayout (not by the framework), after we've been added as a * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout * that it should intercept touch events, which is not something that is normally supported. */ @Override public boolean onTouch(View v, MotionEvent event) { return (workspaceInModalState() || !isFinishedSwitchingState()); } public boolean isSwitchingState() { return mIsSwitchingState; } /** This differs from isSwitchingState in that we take into account how far the transition * has completed. */ public boolean isFinishedSwitchingState() { return !mIsSwitchingState || (mTransitionProgress > 0.5f); } protected void onWindowVisibilityChanged(int visibility) { mLauncher.onWindowVisibilityChanged(visibility); } @Override public boolean dispatchUnhandledMove(View focused, int direction) { // when the home screens are shrunken, shouldn't allow side-scrolling return !(workspaceInModalState() || !isFinishedSwitchingState()) && super.dispatchUnhandledMove(focused, direction); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_UP: if (mTouchState == PagedView.TOUCH_STATE_REST) { final CellLayout currentPage = mWorkspace; if (currentPage != null) { onWallpaperTap(ev); } } } return super.onInterceptTouchEvent(ev); } @Override public boolean onGenericMotionEvent(MotionEvent event) { return super.onGenericMotionEvent(event); } protected void reinflateWidgetsIfNecessary() { final int clCount = getChildCount(); for (int i = 0; i < clCount; i++) { CellLayout cl = (CellLayout) getChildAt(i); ShortcutAndWidgetContainer swc = cl.getShortcutsAndWidgets(); final int itemCount = swc.getChildCount(); for (int j = 0; j < itemCount; j++) { View v = swc.getChildAt(j); if (v != null && v.getTag() instanceof LauncherAppWidgetInfo) { LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag(); LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) info.hostView; if (lahv != null && lahv.isReinflateRequired()) { mLauncher.removeAppWidget(info); // Remove the current widget which is inflated with the wrong orientation cl.removeView(lahv); mLauncher.bindAppWidget(info); } } } } } @Override public void announceForAccessibility(CharSequence text) { // Don't announce if apps is on top of us. if (!mLauncher.isAllAppsVisible()) { super.announceForAccessibility(text); } } void showOutlines() { if (!workspaceInModalState() && !mIsSwitchingState) { if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel(); if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel(); mChildrenOutlineFadeInAnimation = LauncherAnimUtils.ofFloat(this, "childrenOutlineAlpha", 1.0f); mChildrenOutlineFadeInAnimation.setDuration(CHILDREN_OUTLINE_FADE_IN_DURATION); mChildrenOutlineFadeInAnimation.start(); } } void hideOutlines() { if (!workspaceInModalState() && !mIsSwitchingState) { if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel(); if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel(); mChildrenOutlineFadeOutAnimation = LauncherAnimUtils.ofFloat(this, "childrenOutlineAlpha", 0.0f); mChildrenOutlineFadeOutAnimation.setDuration(CHILDREN_OUTLINE_FADE_OUT_DURATION); mChildrenOutlineFadeOutAnimation.setStartDelay(CHILDREN_OUTLINE_FADE_OUT_DELAY); mChildrenOutlineFadeOutAnimation.start(); } } public void setChildrenOutlineAlpha(float alpha) { mChildrenOutlineAlpha = alpha; for (int i = 0; i < getChildCount(); i++) { CellLayout cl = (CellLayout) getChildAt(i); cl.setBackgroundAlpha(alpha); } } public float getChildrenOutlineAlpha() { return mChildrenOutlineAlpha; } private void animateBackgroundGradient(float finalAlpha, boolean animated) { final DragLayer dragLayer = mLauncher.getDragLayer(); if (mBackgroundFadeInAnimation != null) { mBackgroundFadeInAnimation.cancel(); mBackgroundFadeInAnimation = null; } if (mBackgroundFadeOutAnimation != null) { mBackgroundFadeOutAnimation.cancel(); mBackgroundFadeOutAnimation = null; } float startAlpha = dragLayer.getBackgroundAlpha(); if (finalAlpha != startAlpha) { if (animated) { mBackgroundFadeOutAnimation = LauncherAnimUtils.ofFloat(this, startAlpha, finalAlpha); mBackgroundFadeOutAnimation.addUpdateListener(new AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { dragLayer.setBackgroundAlpha((Float) animation.getAnimatedValue()); } }); mBackgroundFadeOutAnimation.setInterpolator(new DecelerateInterpolator(1.5f)); mBackgroundFadeOutAnimation.setDuration(BACKGROUND_FADE_OUT_DURATION); mBackgroundFadeOutAnimation.start(); } else { dragLayer.setBackgroundAlpha(finalAlpha); } } } private void setChildrenBackgroundAlphaMultipliers(float a) { for (int i = 0; i < getChildCount(); i++) { CellLayout child = (CellLayout) getChildAt(i); child.setBackgroundAlphaMultiplier(a); } } protected void onAttachedToWindow() { super.onAttachedToWindow(); mWindowToken = getWindowToken(); mDragController.setWindowToken(mWindowToken); } protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mWindowToken = null; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { Log.d(TAG, "OnLayout> " + getChildCount()); if (getChildCount() == 0) { return; } int offsetX = mWorkspace.getMeasuredWidth(); int offsetY = mWorkspace.getMeasuredHeight(); // Update the viewport offsets mViewPort.offset(offsetX, offsetY); final View child = getChildAt(0); if (child.getVisibility() != View.GONE) { child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight()); } } protected void onResume() { AccessibilityManager am = (AccessibilityManager) getContext() .getSystemService(Context.ACCESSIBILITY_SERVICE); sAccessibilityEnabled = am.isEnabled(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Call back to LauncherModel to finish binding after the first draw post(mBindPages); } @Override protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { return !mLauncher.isAllAppsVisible() && super.onRequestFocusInDescendants(direction, previouslyFocusedRect); } @Override public int getDescendantFocusability() { if (workspaceInModalState()) { return ViewGroup.FOCUS_BLOCK_DESCENDANTS; } return super.getDescendantFocusability(); } @Override public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { if (!mLauncher.isAllAppsVisible()) { super.addFocusables(views, direction, focusableMode); } } public boolean workspaceInModalState() { return mState != State.NORMAL; } private void updateChildrenLayersEnabled(boolean force) { boolean small = mState == State.OVERVIEW || mIsSwitchingState; boolean enableChildrenLayers = force || small || mAnimatingViewIntoPlace; mWorkspace.enableHardwareLayer(enableChildrenLayers); } public void buildPageHardwareLayers() { // force layers to be enabled just for the call to buildLayer updateChildrenLayersEnabled(true); if (getWindowToken() != null) { mWorkspace.buildHardwareLayer(); } updateChildrenLayersEnabled(false); } protected void onWallpaperTap(MotionEvent ev) { final int[] position = mTempCell; getLocationOnScreen(position); int pointerIndex = ev.getActionIndex(); position[0] += (int) ev.getX(pointerIndex); position[1] += (int) ev.getY(pointerIndex); mWallpaperManager.sendWallpaperCommand(getWindowToken(), ev.getAction() == MotionEvent.ACTION_UP ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP, position[0], position[1], 0, null); } /* * This interpolator emulates the rate at which the perceived scale of an object changes * as its distance from a camera increases. When this interpolator is applied to a scale * animation on a view, it evokes the sense that the object is shrinking due to moving away * from the camera. */ static class ZInterpolator implements TimeInterpolator { private float focalLength; public ZInterpolator(float foc) { focalLength = foc; } public float getInterpolation(float input) { return (1.0f - focalLength / (focalLength + input)) / (1.0f - focalLength / (focalLength + 1.0f)); } } /* * The exact reverse of ZInterpolator. */ static class InverseZInterpolator implements TimeInterpolator { private ZInterpolator zInterpolator; public InverseZInterpolator(float foc) { zInterpolator = new ZInterpolator(foc); } public float getInterpolation(float input) { return 1 - zInterpolator.getInterpolation(1 - input); } } /* * ZInterpolator compounded with an ease-out. */ static class ZoomOutInterpolator implements TimeInterpolator { private final DecelerateInterpolator decelerate = new DecelerateInterpolator(0.75f); private final ZInterpolator zInterpolator = new ZInterpolator(0.13f); public float getInterpolation(float input) { return decelerate.getInterpolation(zInterpolator.getInterpolation(input)); } } /* * InvereZInterpolator compounded with an ease-out. */ static class ZoomInInterpolator implements TimeInterpolator { private final InverseZInterpolator inverseZInterpolator = new InverseZInterpolator(0.35f); private final DecelerateInterpolator decelerate = new DecelerateInterpolator(3.0f); public float getInterpolation(float input) { return decelerate.getInterpolation(inverseZInterpolator.getInterpolation(input)); } } private final ZoomInInterpolator mZoomInInterpolator = new ZoomInInterpolator(); /* * * We call these methods (onDragStartedWithItemSpans/onDragStartedWithSize) whenever we * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace * * These methods mark the appropriate pages as accepting drops (which alters their visual * appearance). * */ private static Rect getDrawableBounds(Drawable d) { Rect bounds = new Rect(); d.copyBounds(bounds); if (bounds.width() == 0 || bounds.height() == 0) { bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); } else { bounds.offsetTo(0, 0); } if (d instanceof PreloadIconDrawable) { int inset = -((PreloadIconDrawable) d).getOutset(); bounds.inset(inset, inset); } return bounds; } public void onDragStartedWithItem(PendingAddWidgetInfo info, Bitmap b, boolean clipAlpha) { int[] size = estimateItemSize(info.spanX, info.spanY, false); // The outline is used to visualize where the item will land if dropped mDragOutline = createDragOutline(b, DRAG_BITMAP_PADDING, size[0], size[1], clipAlpha); } public void exitWidgetResizeMode() { DragLayer dragLayer = mLauncher.getDragLayer(); dragLayer.clearAllResizeFrames(); } Animator getChangeStateAnimation(final State state, boolean animated, ArrayList<View> layerViews) { return getChangeStateAnimation(state, animated, 0, layerViews); } public boolean isInOverviewMode() { return mState == State.OVERVIEW; } public boolean enterOverviewMode() { if (mTouchState != PagedView.TOUCH_STATE_REST) { return false; } enableOverviewMode(true, true); return true; } public void exitOverviewMode(boolean animated) { enableOverviewMode(false, animated); } private void enableOverviewMode(boolean enable, boolean animated) { State finalState = Workspace.State.OVERVIEW; if (!enable) { finalState = Workspace.State.NORMAL; } Animator workspaceAnim = getChangeStateAnimation(finalState, animated, 0); if (workspaceAnim != null) { onTransitionPrepare(); workspaceAnim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator arg0) { onTransitionEnd(); } }); workspaceAnim.start(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (getChildCount() == 0) { return; } // We measure the dimensions of the PagedView to be larger than the pages so that when we // zoom out (and scale down), the view is still contained in the parent int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) { return; } // Return early if we aren't given a proper dimension if (widthSize <= 0 || heightSize <= 0) { return; } mViewPort.set(0, 0, widthSize, heightSize); Log.d(TAG, "Workspace.onMeasure(): " + widthSize + ", " + heightSize); // disallowing padding in paged view (just pass 0) final View child = getChildAt(0); if (child.getVisibility() != GONE) { final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY); final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY); child.measure(childWidthMeasureSpec, childHeightMeasureSpec); } setMeasuredDimension(widthSize, heightSize); } int getOverviewModeTranslationY() { LauncherAppState app = LauncherAppState.getInstance(); DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); Rect overviewBar = grid.getOverviewModeButtonBarRect(); int availableHeight = mViewPort.height(); int scaledHeight = (int) (mOverviewModeShrinkFactor * mViewPort.height()); int offsetFromTopEdge = (availableHeight - scaledHeight) / 2; int offsetToCenterInOverview = (availableHeight - overviewBar.height() - scaledHeight) / 2; return -offsetFromTopEdge + offsetToCenterInOverview; } private void setState(State state) { mState = state; updateAccessibilityFlags(); } State getState() { return mState; } private void updateAccessibilityFlags() { int accessible = mState == State.NORMAL ? ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES : ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS; setImportantForAccessibility(accessible); } private static final int HIDE_WORKSPACE_DURATION = 100; Animator getChangeStateAnimation(final State state, boolean animated, int delay) { return getChangeStateAnimation(state, animated, delay, null); } Animator getChangeStateAnimation(final State state, boolean animated, int delay, ArrayList<View> layerViews) { if (mState == state) { return null; } AnimatorSet anim = animated ? LauncherAnimUtils.createAnimatorSet() : null; // We only want a single instance of a workspace animation to be running at once, so // we cancel any incomplete transition. if (mStateAnimator != null) { mStateAnimator.cancel(); } mStateAnimator = anim; final State oldState = mState; final boolean oldStateIsNormal = (oldState == State.NORMAL); final boolean oldStateIsOverview = (oldState == State.OVERVIEW); setState(state); final boolean stateIsNormal = (state == State.NORMAL); final boolean stateIsSpringLoaded = (state == State.SPRING_LOADED); final boolean stateIsNormalHidden = (state == State.NORMAL_HIDDEN); final boolean stateIsOverviewHidden = (state == State.OVERVIEW_HIDDEN); final boolean stateIsOverview = (state == State.OVERVIEW); float finalBackgroundAlpha = (stateIsSpringLoaded || stateIsOverview) ? 1.0f : 0f; float finalOverviewPanelAlpha = stateIsOverview ? 1f : 0f; float finalWorkspaceTranslationY = stateIsOverview || stateIsOverviewHidden ? getOverviewModeTranslationY() : 0; boolean workspaceToAllApps = (oldStateIsNormal && stateIsNormalHidden); boolean overviewToAllApps = (oldStateIsOverview && stateIsOverviewHidden); boolean allAppsToWorkspace = (stateIsNormalHidden && stateIsNormal); boolean workspaceToOverview = (oldStateIsNormal && stateIsOverview); boolean overviewToWorkspace = (oldStateIsOverview && stateIsNormal); mNewScale = 1.0f; if (state != State.NORMAL) { if (stateIsSpringLoaded) { mNewScale = mSpringLoadedShrinkFactor; } else if (stateIsOverview || stateIsOverviewHidden) { mNewScale = mOverviewModeShrinkFactor; } } final int duration; if (workspaceToAllApps || overviewToAllApps) { duration = HIDE_WORKSPACE_DURATION; //getResources().getInteger(R.integer.config_workspaceUnshrinkTime); } else if (workspaceToOverview || overviewToWorkspace) { duration = getResources().getInteger(R.integer.config_overviewTransitionTime); } else { duration = getResources().getInteger(R.integer.config_appsCustomizeWorkspaceShrinkTime); } final CellLayout cl = mWorkspace; float initialAlpha = cl.getShortcutsAndWidgets().getAlpha(); float finalAlpha; if (stateIsNormalHidden || stateIsOverviewHidden) { finalAlpha = 0f; } else { finalAlpha = 1f; } // If we are animating to/from the small state, then hide the side pages and fade the // current page in if (!mIsSwitchingState) { if (workspaceToAllApps || allAppsToWorkspace) { if (allAppsToWorkspace) { initialAlpha = 0f; } cl.setShortcutAndWidgetAlpha(initialAlpha); } } float oldAlpha = initialAlpha; float newAlpha = finalAlpha; if (animated) { mOldBackgroundAlpha = cl.getBackgroundAlpha(); mNewBackgroundAlpha = finalBackgroundAlpha; } else { cl.setBackgroundAlpha(finalBackgroundAlpha); cl.setShortcutAndWidgetAlpha(finalAlpha); } final View overviewPanel = mLauncher.getOverviewPanel(); if (animated) { LauncherViewPropertyAnimator scale = new LauncherViewPropertyAnimator(this); scale.scaleX(mNewScale).scaleY(mNewScale).translationY(finalWorkspaceTranslationY).setDuration(duration) .setInterpolator(mZoomInInterpolator); anim.play(scale); float currentAlpha = cl.getShortcutsAndWidgets().getAlpha(); if (oldAlpha == 0 && newAlpha == 0) { cl.setBackgroundAlpha(mNewBackgroundAlpha); cl.setShortcutAndWidgetAlpha(newAlpha); } else { if (layerViews != null) { layerViews.add(cl); } if (oldAlpha != newAlpha || currentAlpha != newAlpha) { LauncherViewPropertyAnimator alphaAnim = new LauncherViewPropertyAnimator( cl.getShortcutsAndWidgets()); alphaAnim.alpha(newAlpha).setDuration(duration).setInterpolator(mZoomInInterpolator); anim.play(alphaAnim); } if (mOldBackgroundAlpha != 0 || mNewBackgroundAlpha != 0) { ValueAnimator bgAnim = LauncherAnimUtils.ofFloat(cl, 0f, 1f); bgAnim.setInterpolator(mZoomInInterpolator); bgAnim.setDuration(duration); bgAnim.addUpdateListener(new LauncherAnimatorUpdateListener() { public void onAnimationUpdate(float a, float b) { cl.setBackgroundAlpha(a * mOldBackgroundAlpha + b * mNewBackgroundAlpha); } }); anim.play(bgAnim); } } Animator overviewPanelAlpha = new LauncherViewPropertyAnimator(overviewPanel) .alpha(finalOverviewPanelAlpha).withLayer(); overviewPanelAlpha.addListener(new AlphaUpdateListener(overviewPanel)); // For animation optimations, we may need to provide the Launcher transition // with a set of views on which to force build layers in certain scenarios. overviewPanel.setLayerType(View.LAYER_TYPE_HARDWARE, null); if (layerViews != null) { layerViews.add(overviewPanel); } if (workspaceToOverview) { overviewPanelAlpha.setInterpolator(null); } else if (overviewToWorkspace) { overviewPanelAlpha.setInterpolator(new DecelerateInterpolator(2)); } overviewPanelAlpha.setDuration(duration); anim.play(overviewPanelAlpha); anim.setStartDelay(delay); anim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { mStateAnimator = null; } }); } else { overviewPanel.setAlpha(finalOverviewPanelAlpha); AlphaUpdateListener.updateVisibility(overviewPanel); setScaleX(mNewScale); setScaleY(mNewScale); setTranslationY(finalWorkspaceTranslationY); } if (stateIsNormal) { animateBackgroundGradient(0f, animated); } else { animateBackgroundGradient(getResources().getInteger(R.integer.config_workspaceScrimAlpha) / 100f, animated); } return anim; } static class AlphaUpdateListener implements AnimatorUpdateListener, AnimatorListener { View view; public AlphaUpdateListener(View v) { view = v; } @Override public void onAnimationUpdate(ValueAnimator arg0) { updateVisibility(view); } public static void updateVisibility(View view) { // We want to avoid the extra layout pass by setting the views to GONE unless // accessibility is on, in which case not setting them to GONE causes a glitch. int invisibleState = sAccessibilityEnabled ? GONE : INVISIBLE; if (view.getAlpha() < ALPHA_CUTOFF_THRESHOLD && view.getVisibility() != invisibleState) { view.setVisibility(invisibleState); } else if (view.getAlpha() > ALPHA_CUTOFF_THRESHOLD && view.getVisibility() != VISIBLE) { view.setVisibility(VISIBLE); } } @Override public void onAnimationCancel(Animator arg0) { } @Override public void onAnimationEnd(Animator arg0) { updateVisibility(view); } @Override public void onAnimationRepeat(Animator arg0) { } @Override public void onAnimationStart(Animator arg0) { // We want the views to be visible for animation, so fade-in/out is visible view.setVisibility(VISIBLE); } } @Override public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) { onTransitionPrepare(); } @Override public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) { } @Override public void onLauncherTransitionStep(Launcher l, float t) { mTransitionProgress = t; } @Override public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) { onTransitionEnd(); } private void onTransitionPrepare() { mIsSwitchingState = true; // Invalidate here to ensure that the pages are rendered during the state change transition. invalidate(); } private void onTransitionEnd() { mIsSwitchingState = false; } @Override public View getContent() { return this; } /** * Draw the View v into the given Canvas. * * @param v the view to draw * @param destCanvas the canvas to draw on * @param padding the horizontal and vertical padding to use when drawing */ private static void drawDragView(View v, Canvas destCanvas, int padding) { final Rect clipRect = sTempRect; v.getDrawingRect(clipRect); destCanvas.save(); if (v instanceof TextView) { Drawable d = ((TextView) v).getCompoundDrawables()[1]; Rect bounds = getDrawableBounds(d); clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding); destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top); d.draw(destCanvas); } else { destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2); destCanvas.clipRect(clipRect, Op.REPLACE); v.draw(destCanvas); } destCanvas.restore(); } /** * Returns a new bitmap to show when the given View is being dragged around. * Responsibility for the bitmap is transferred to the caller. * @param expectedPadding padding to add to the drag view. If a different padding was used * its value will be changed */ public Bitmap createDragBitmap(View v, AtomicInteger expectedPadding) { Bitmap b; int padding = expectedPadding.get(); if (v instanceof TextView) { Drawable d = ((TextView) v).getCompoundDrawables()[1]; Rect bounds = getDrawableBounds(d); b = Bitmap.createBitmap(bounds.width() + padding, bounds.height() + padding, Bitmap.Config.ARGB_8888); expectedPadding.set(padding - bounds.left - bounds.top); } else { b = Bitmap.createBitmap(v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); } mCanvas.setBitmap(b); drawDragView(v, mCanvas, padding); mCanvas.setBitmap(null); return b; } /** * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. * Responsibility for the bitmap is transferred to the caller. */ private Bitmap createDragOutline(View v, int padding) { final int outlineColor = getResources().getColor(R.color.outline_color); final Bitmap b = Bitmap.createBitmap(v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888); mCanvas.setBitmap(b); drawDragView(v, mCanvas, padding); mOutlineHelper.applyExpensiveOutlineWithBlur(b, mCanvas, outlineColor, outlineColor); mCanvas.setBitmap(null); return b; } /** * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location. * Responsibility for the bitmap is transferred to the caller. */ private Bitmap createDragOutline(Bitmap orig, int padding, int w, int h, boolean clipAlpha) { final int outlineColor = getResources().getColor(R.color.outline_color); final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas.setBitmap(b); Rect src = new Rect(0, 0, orig.getWidth(), orig.getHeight()); float scaleFactor = Math.min((w - padding) / (float) orig.getWidth(), (h - padding) / (float) orig.getHeight()); int scaledWidth = (int) (scaleFactor * orig.getWidth()); int scaledHeight = (int) (scaleFactor * orig.getHeight()); Rect dst = new Rect(0, 0, scaledWidth, scaledHeight); // center the image dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2); mCanvas.drawBitmap(orig, src, dst, null); mOutlineHelper.applyExpensiveOutlineWithBlur(b, mCanvas, outlineColor, outlineColor, clipAlpha); mCanvas.setBitmap(null); return b; } void startDrag(CellLayout.CellInfo cellInfo) { View child = cellInfo.cell; // Make sure the drag was started by a long press as opposed to a long click. if (!child.isInTouchMode()) { return; } mDragInfo = cellInfo; child.setVisibility(INVISIBLE); CellLayout layout = (CellLayout) child.getParent().getParent(); layout.prepareChildForDrag(child); beginDragShared(child, this); } public void beginDragShared(View child, DragSource source) { child.clearFocus(); child.setPressed(false); // The outline is used to visualize where the item will land if dropped mDragOutline = createDragOutline(child, DRAG_BITMAP_PADDING); mLauncher.onDragStarted(child); // The drag bitmap follows the touch point around on the screen AtomicInteger padding = new AtomicInteger(DRAG_BITMAP_PADDING); final Bitmap b = createDragBitmap(child, padding); final int bmpWidth = b.getWidth(); final int bmpHeight = b.getHeight(); float scale = mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY); int dragLayerX = Math.round(mTempXY[0] - (bmpWidth - scale * child.getWidth()) / 2); int dragLayerY = Math.round(mTempXY[1] - (bmpHeight - scale * bmpHeight) / 2 - padding.get() / 2); LauncherAppState app = LauncherAppState.getInstance(); DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); Point dragVisualizeOffset = null; Rect dragRect = null; if (child instanceof BubbleTextView) { int iconSize = grid.iconSizePx; int top = child.getPaddingTop(); int left = (bmpWidth - iconSize) / 2; int right = left + iconSize; int bottom = top + iconSize; dragLayerY += top; // Note: The drag region is used to calculate drag layer offsets, but the // dragVisualizeOffset in addition to the dragRect (the size) to position the outline. dragVisualizeOffset = new Point(-padding.get() / 2, padding.get() / 2); dragRect = new Rect(left, top, right, bottom); } // Clear the pressed state if necessary if (child instanceof BubbleTextView) { BubbleTextView icon = (BubbleTextView) child; icon.clearPressedBackground(); } if (child.getTag() == null || !(child.getTag() instanceof ItemInfo)) { String msg = "Drag started with a view that has no tag set. This " + "will cause a crash (issue 11627249) down the line. " + "View: " + child + " tag: " + child.getTag(); throw new IllegalStateException(msg); } DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(), DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect, scale); dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor()); b.recycle(); } public boolean transitionStateShouldAllowDrop() { return ((!isSwitchingState() || mTransitionProgress > 0.5f) && (mState == State.NORMAL || mState == State.SPRING_LOADED)); } /** * {@inheritDoc} */ public boolean acceptDrop(DragObject d) { // If it's an external drop (e.g. from All Apps), check if it should be accepted CellLayout dropTargetLayout = mDropToLayout; if (d.dragSource != this) { // Don't accept the drop if we're not over a screen at time of drop if (dropTargetLayout == null) { return false; } if (!transitionStateShouldAllowDrop()) return false; mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, mDragViewVisualCenter); // We want the point to be mapped to the dragTarget. mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter, null); int spanX = 1; int spanY = 1; if (mDragInfo != null) { final CellLayout.CellInfo dragCellInfo = mDragInfo; spanX = dragCellInfo.spanX; spanY = dragCellInfo.spanY; } else { final ItemInfo dragInfo = (ItemInfo) d.dragInfo; spanX = dragInfo.spanX; spanY = dragInfo.spanY; } int minSpanX = spanX; int minSpanY = spanY; if (d.dragInfo instanceof PendingAddWidgetInfo) { minSpanX = ((PendingAddWidgetInfo) d.dragInfo).minSpanX; minSpanY = ((PendingAddWidgetInfo) d.dragInfo).minSpanY; } mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], minSpanX, minSpanY, dropTargetLayout, mTargetCell); float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell); int[] resultSpan = new int[2]; mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, null, mTargetCell, resultSpan, CellLayout.MODE_ACCEPT_DROP); boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0; // Don't accept the drop if there's no room for the item if (!foundCell) { mLauncher.showOutOfSpaceMessage(); return false; } } return true; } public void onDrop(final DragObject d) { mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, mDragViewVisualCenter); CellLayout dropTargetLayout = mDropToLayout; // We want the point to be mapped to the dragTarget. if (dropTargetLayout != null) { mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter, null); } boolean resizeOnDrop = false; if (d.dragSource != this) { final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1] }; onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d); } else if (mDragInfo != null) { final View cell = mDragInfo.cell; Runnable resizeRunnable = null; if (dropTargetLayout != null && !d.cancelled) { long container = LauncherSettings.Favorites.CONTAINER_DESKTOP; int spanX = mDragInfo.spanX; int spanY = mDragInfo.spanY; // First we find the cell nearest to point at which the item is // dropped, without any consideration to whether there is an item there. mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell); // Aside from the special case where we're dropping a shortcut onto a shortcut, // we need to find the nearest cell location that is vacant ItemInfo item = (ItemInfo) d.dragInfo; int minSpanX = item.spanX; int minSpanY = item.spanY; if (item.minSpanX > 0 && item.minSpanY > 0) { minSpanX = item.minSpanX; minSpanY = item.minSpanY; } int[] resultSpan = new int[2]; mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, cell, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP); boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0; // if the widget resizes on drop if (foundCell && (cell instanceof AppWidgetHostView) && (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY)) { resizeOnDrop = true; item.spanX = resultSpan[0]; item.spanY = resultSpan[1]; AppWidgetHostView awhv = (AppWidgetHostView) cell; AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, resultSpan[0], resultSpan[1]); } if (foundCell) { final ItemInfo info = (ItemInfo) cell.getTag(); // update the item's position after drop CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); lp.cellX = lp.tmpCellX = mTargetCell[0]; lp.cellY = lp.tmpCellY = mTargetCell[1]; lp.cellHSpan = item.spanX; lp.cellVSpan = item.spanY; lp.isLockedToGrid = true; if (cell instanceof LauncherAppWidgetHostView) { final CellLayout cellLayout = dropTargetLayout; // We post this call so that the widget has a chance to be placed // in its final location final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell; AppWidgetProviderInfo pinfo = hostView.getAppWidgetInfo(); if (pinfo != null && pinfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE) { resizeRunnable = new Runnable() { public void run() { DragLayer dragLayer = mLauncher.getDragLayer(); dragLayer.addResizeFrame(info, hostView, cellLayout); } }; } } LauncherModel.modifyItemInDatabase(mLauncher, info, container, lp.cellX, lp.cellY, item.spanX, item.spanY); } else { // If we can't find a drop location, we return the item to its original position CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); mTargetCell[0] = lp.cellX; mTargetCell[1] = lp.cellY; CellLayout layout = (CellLayout) cell.getParent().getParent(); layout.markCellsAsOccupiedForView(cell); } } final CellLayout parent = (CellLayout) cell.getParent().getParent(); final Runnable finalResizeRunnable = resizeRunnable; // Prepare it to be animated into its new position // This must be called after the view has been re-parented final Runnable onCompleteRunnable = new Runnable() { @Override public void run() { mAnimatingViewIntoPlace = false; if (finalResizeRunnable != null) { finalResizeRunnable.run(); } } }; mAnimatingViewIntoPlace = true; if (d.dragView.hasDrawn()) { final ItemInfo info = (ItemInfo) cell.getTag(); if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET) { int animationType = resizeOnDrop ? ANIMATE_INTO_POSITION_AND_RESIZE : ANIMATE_INTO_POSITION_AND_DISAPPEAR; animateWidgetDrop(info, parent, d.dragView, onCompleteRunnable, animationType, cell, false); } else { int duration = -1; mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration, onCompleteRunnable, this); } } else { d.deferDragViewCleanupPostAnimation = false; cell.setVisibility(VISIBLE); } parent.onDropChild(cell); } } public void onDragEnter(DragObject d) { mDropToLayout = null; CellLayout layout = getCurrentDropLayout(); setCurrentDropLayout(layout); setCurrentDragOverlappingLayout(layout); if (!workspaceInModalState()) { mLauncher.getDragLayer().showPageHints(); } } /** Return a rect that has the cellWidth/cellHeight (left, top), and * widthGap/heightGap (right, bottom) */ static Rect getCellLayoutMetrics(Launcher launcher, int orientation) { LauncherAppState app = LauncherAppState.getInstance(); DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); Display display = launcher.getWindowManager().getDefaultDisplay(); Point smallestSize = new Point(); Point largestSize = new Point(); display.getCurrentSizeRange(smallestSize, largestSize); int countX = (int) grid.numColumns; int countY = (int) grid.numRows; if (orientation == CellLayout.LANDSCAPE) { if (mLandscapeCellLayoutMetrics == null) { Rect padding = grid.getWorkspacePadding(CellLayout.LANDSCAPE); int width = largestSize.x - padding.left - padding.right; int height = smallestSize.y - padding.top - padding.bottom; mLandscapeCellLayoutMetrics = new Rect(); mLandscapeCellLayoutMetrics.set(grid.calculateCellWidth(width, countX), grid.calculateCellHeight(height, countY), 0, 0); } return mLandscapeCellLayoutMetrics; } else if (orientation == CellLayout.PORTRAIT) { if (mPortraitCellLayoutMetrics == null) { Rect padding = grid.getWorkspacePadding(CellLayout.PORTRAIT); int width = smallestSize.x - padding.left - padding.right; int height = largestSize.y - padding.top - padding.bottom; mPortraitCellLayoutMetrics = new Rect(); mPortraitCellLayoutMetrics.set(grid.calculateCellWidth(width, countX), grid.calculateCellHeight(height, countY), 0, 0); } return mPortraitCellLayoutMetrics; } return null; } public void onDragExit(DragObject d) { // Here we store the final page that will be dropped to, if the workspace in fact // receives the drop if (mInScrollArea) { mDropToLayout = mDragOverlappingLayout; } else { mDropToLayout = mDragTargetLayout; } // Reset the scroll area and previous drag target onResetScrollArea(); setCurrentDropLayout(null); setCurrentDragOverlappingLayout(null); mSpringLoadedDragController.cancel(); hideOutlines(); mLauncher.getDragLayer().hidePageHints(); } void setCurrentDropLayout(CellLayout layout) { if (mDragTargetLayout != null) { mDragTargetLayout.revertTempState(); mDragTargetLayout.onDragExit(); } mDragTargetLayout = layout; if (mDragTargetLayout != null) { mDragTargetLayout.onDragEnter(); } cleanupReorder(); setCurrentDropOverCell(-1, -1); } void setCurrentDragOverlappingLayout(CellLayout layout) { if (mDragOverlappingLayout != null) { mDragOverlappingLayout.setIsDragOverlapping(false); } mDragOverlappingLayout = layout; if (mDragOverlappingLayout != null) { mDragOverlappingLayout.setIsDragOverlapping(true); } invalidate(); } void setCurrentDropOverCell(int x, int y) { if (x != mDragOverX || y != mDragOverY) { mDragOverX = x; mDragOverY = y; setDragMode(DRAG_MODE_NONE); } } void setDragMode(int dragMode) { if (dragMode != mDragMode) { if (dragMode == DRAG_MODE_NONE) { // We don't want to cancel the re-order alarm every time the target cell changes // as this feels to slow / unresponsive. cleanupReorder(); } mDragMode = dragMode; } } private void cleanupReorder() { mLastReorderX = -1; mLastReorderY = -1; } /* * * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's * coordinate space. The argument xy is modified with the return result. * * if cachedInverseMatrix is not null, this method will just use that matrix instead of * computing it itself; we use this to avoid redundant matrix inversions in * findMatchingPageForDragOver * */ void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) { xy[0] = xy[0] - v.getLeft(); xy[1] = xy[1] - v.getTop(); } /* * * Convert the 2D coordinate xy from this CellLayout's coordinate space to * the parent View's coordinate space. The argument xy is modified with the return result. * */ void mapPointFromChildToSelf(View v, float[] xy) { xy[0] += v.getLeft(); xy[1] += v.getTop(); } static private float squaredDistance(float[] point1, float[] point2) { float distanceX = point1[0] - point2[0]; float distanceY = point2[1] - point2[1]; return distanceX * distanceX + distanceY * distanceY; } /* * * This method returns the CellLayout that is currently being dragged to. In order to drag * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one * * Return null if no CellLayout is currently being dragged over * */ private CellLayout findMatchingPageForDragOver(DragView dragView, float originX, float originY, boolean exact) { // We loop through all the screens (ie CellLayouts) and see which ones overlap // with the item being dragged and then choose the one that's closest to the touch point final int screenCount = getChildCount(); CellLayout bestMatchingScreen = null; float smallestDistSoFar = Float.MAX_VALUE; for (int i = 0; i < screenCount; i++) { CellLayout cl = (CellLayout) getChildAt(i); final float[] touchXy = { originX, originY }; // Transform the touch coordinates to the CellLayout's local coordinates // If the touch point is within the bounds of the cell layout, we can return immediately cl.getMatrix().invert(mTempInverseMatrix); mapPointFromSelfToChild(cl, touchXy, mTempInverseMatrix); if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() && touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) { return cl; } if (!exact) { // Get the center of the cell layout in screen coordinates final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates; cellLayoutCenter[0] = cl.getWidth() / 2; cellLayoutCenter[1] = cl.getHeight() / 2; mapPointFromChildToSelf(cl, cellLayoutCenter); touchXy[0] = originX; touchXy[1] = originY; // Calculate the distance between the center of the CellLayout // and the touch point float dist = squaredDistance(touchXy, cellLayoutCenter); if (dist < smallestDistSoFar) { smallestDistSoFar = dist; bestMatchingScreen = cl; } } } return bestMatchingScreen; } // This is used to compute the visual center of the dragView. This point is then // used to visualize drop locations and determine where to drop an item. The idea is that // the visual center represents the user's interpretation of where the item is, and hence // is the appropriate point to use when determining drop location. private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset, DragView dragView, float[] recycle) { float res[]; if (recycle == null) { res = new float[2]; } else { res = recycle; } // First off, the drag view has been shifted in a way that is not represented in the // x and y values or the x/yOffsets. Here we account for that shift. x += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetX); y += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); // These represent the visual top and left of drag view if a dragRect was provided. // If a dragRect was not provided, then they correspond to the actual view left and // top, as the dragRect is in that case taken to be the entire dragView. // R.dimen.dragViewOffsetY. int left = x - xOffset; int top = y - yOffset; // In order to find the visual center, we shift by half the dragRect res[0] = left + dragView.getDragRegion().width() / 2; res[1] = top + dragView.getDragRegion().height() / 2; return res; } private boolean isDragWidget(DragObject d) { return (d.dragInfo instanceof LauncherAppWidgetInfo || d.dragInfo instanceof PendingAddWidgetInfo); } private boolean isExternalDragWidget(DragObject d) { return d.dragSource != this && isDragWidget(d); } public void onDragOver(DragObject d) { // Skip drag over events while we are dragging over side pages if (mInScrollArea || !transitionStateShouldAllowDrop()) return; Rect r = new Rect(); CellLayout layout = null; ItemInfo item = (ItemInfo) d.dragInfo; if (item == null) { return; } // Ensure that we have proper spans for the item that we are dropping if (item.spanX < 0 || item.spanY < 0) throw new RuntimeException("Improper spans found"); mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, mDragViewVisualCenter); final View child = (mDragInfo == null) ? null : mDragInfo.cell; // Identify whether we have dragged over a side page if (workspaceInModalState()) { layout = findMatchingPageForDragOver(d.dragView, d.x, d.y, false); if (layout != mDragTargetLayout) { setCurrentDropLayout(layout); setCurrentDragOverlappingLayout(layout); boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED); if (isInSpringLoadedMode) { mSpringLoadedDragController.setAlarm(mDragTargetLayout); } } } else { layout = getCurrentDropLayout(); if (layout != mDragTargetLayout) { setCurrentDropLayout(layout); setCurrentDragOverlappingLayout(layout); } } // Handle the drag over if (mDragTargetLayout != null) { // We want the point to be mapped to the dragTarget. mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null); ItemInfo info = (ItemInfo) d.dragInfo; int minSpanX = item.spanX; int minSpanY = item.spanY; if (item.minSpanX > 0 && item.minSpanY > 0) { minSpanX = item.minSpanX; minSpanY = item.minSpanY; } mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], minSpanX, minSpanY, mDragTargetLayout, mTargetCell); int reorderX = mTargetCell[0]; int reorderY = mTargetCell[1]; setCurrentDropOverCell(mTargetCell[0], mTargetCell[1]); float targetCellDistance = mDragTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell); final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0], mTargetCell[1]); boolean nearestDropOccupied = mDragTargetLayout.isNearestDropLocationOccupied( (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], item.spanX, item.spanY, child, mTargetCell); if (!nearestDropOccupied) { mDragTargetLayout.visualizeDropLocation(child, mDragOutline, (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], mTargetCell[0], mTargetCell[1], item.spanX, item.spanY, false, d.dragView.getDragVisualizeOffset(), d.dragView.getDragRegion()); } else if ((mDragMode == DRAG_MODE_NONE || mDragMode == DRAG_MODE_REORDER) && (mLastReorderX != reorderX || mLastReorderY != reorderY)) { int[] resultSpan = new int[2]; mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], minSpanX, minSpanY, item.spanX, item.spanY, child, mTargetCell, resultSpan, CellLayout.MODE_SHOW_REORDER_HINT); } if (!nearestDropOccupied) { if (mDragTargetLayout != null) { mDragTargetLayout.revertTempState(); } } } } @Override public void getHitRectRelativeToDragLayer(Rect outRect) { // We want the workspace to have the whole area of the display (it will find the correct // cell layout to drop to in the existing drag/drop logic. mLauncher.getDragLayer().getDescendantRectRelativeToSelf(this, outRect); } /** * Drop an item that didn't originate on one of the workspace screens. * It may have come from Launcher (e.g. from all apps or customize), or it may have * come from another app altogether. * * NOTE: This can also be called when we are outside of a drag event, when we want * to add an item to one of the workspace screens. */ private void onDropExternal(final int[] touchXY, final Object dragInfo, final CellLayout cellLayout, boolean insertAtFirst, DragObject d) { final Runnable exitSpringLoadedRunnable = new Runnable() { @Override public void run() { mLauncher.exitSpringLoadedDragModeDelayed(true, Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null); } }; ItemInfo info = (ItemInfo) dragInfo; int spanX = info.spanX; int spanY = info.spanY; if (mDragInfo != null) { spanX = mDragInfo.spanX; spanY = mDragInfo.spanY; } final long container = LauncherSettings.Favorites.CONTAINER_DESKTOP; if (info instanceof PendingAddWidgetInfo) { final PendingAddWidgetInfo pendingInfo = (PendingAddWidgetInfo) dragInfo; final ItemInfo item = (ItemInfo) d.dragInfo; boolean updateWidgetSize = false; int minSpanX = item.spanX; int minSpanY = item.spanY; if (item.minSpanX > 0 && item.minSpanY > 0) { minSpanX = item.minSpanX; minSpanY = item.minSpanY; } int[] resultSpan = new int[2]; mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], minSpanX, minSpanY, info.spanX, info.spanY, null, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP_EXTERNAL); if (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY) { updateWidgetSize = true; } item.spanX = resultSpan[0]; item.spanY = resultSpan[1]; Runnable onAnimationCompleteRunnable = new Runnable() { @Override public void run() { // When dragging and dropping from customization tray, we deal with creating // widgets/shortcuts/folders in a slightly different way switch (pendingInfo.itemType) { case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: int span[] = new int[2]; span[0] = item.spanX; span[1] = item.spanY; mLauncher.addAppWidgetFromDrop((PendingAddWidgetInfo) pendingInfo, container, mTargetCell, span, null); break; case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: mLauncher.processShortcutFromDrop(pendingInfo.componentName, container, mTargetCell, null); break; default: throw new IllegalStateException("Unknown item type: " + pendingInfo.itemType); } } }; View finalView = pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ? ((PendingAddWidgetInfo) pendingInfo).boundWidget : null; if (finalView != null && updateWidgetSize) { AppWidgetHostView awhv = (AppWidgetHostView) finalView; AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, item.spanX, item.spanY); } int animationStyle = ANIMATE_INTO_POSITION_AND_DISAPPEAR; animateWidgetDrop(info, cellLayout, d.dragView, onAnimationCompleteRunnable, animationStyle, finalView, true); } else { // This is for other drag/drop cases, like dragging from All Apps View view = null; switch (info.itemType) { case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: if (info.container == NO_ID && info instanceof AppInfo) { // Came from all apps -- make a copy info = new ShortcutInfo((AppInfo) info); } view = mLauncher.createShortcut(R.layout.application, cellLayout, (ShortcutInfo) info); break; default: throw new IllegalStateException("Unknown item type: " + info.itemType); } // First we find the cell nearest to point at which the item is // dropped, without any consideration to whether there is an item there. if (touchXY != null) { d.postAnimationRunnable = exitSpringLoadedRunnable; } if (touchXY != null) { // when dragging and dropping, just find the closest free spot mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], 1, 1, 1, 1, null, mTargetCell, null, CellLayout.MODE_ON_DROP_EXTERNAL); } else { cellLayout.findCellForSpan(mTargetCell, 1, 1); } // Add the item to DB before adding to screen ensures that the container and other // values of the info is properly updated. LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, mTargetCell[0], mTargetCell[1]); addInScreen(view, container, mTargetCell[0], mTargetCell[1], info.spanX, info.spanY, insertAtFirst); cellLayout.onDropChild(view); cellLayout.getShortcutsAndWidgets().measureChild(view); if (d.dragView != null) { // We wrap the animation call in the temporary set and reset of the current // cellLayout to its final transform -- this means we animate the drag view to // the correct final location. setFinalTransitionTransform(cellLayout); mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view, exitSpringLoadedRunnable, this); resetTransitionTransform(cellLayout); } } } public Bitmap createWidgetBitmap(ItemInfo widgetInfo, View layout) { int[] unScaledSize = estimateItemSize(widgetInfo.spanX, widgetInfo.spanY, false); int visibility = layout.getVisibility(); layout.setVisibility(VISIBLE); int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY); int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY); Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1], Bitmap.Config.ARGB_8888); mCanvas.setBitmap(b); layout.measure(width, height); layout.layout(0, 0, unScaledSize[0], unScaledSize[1]); layout.draw(mCanvas); mCanvas.setBitmap(null); layout.setVisibility(visibility); return b; } private void getFinalPositionForDropAnimation(int[] loc, float[] scaleXY, DragView dragView, CellLayout layout, ItemInfo info, int[] targetCell, boolean external, boolean scale) { // Now we animate the dragView, (ie. the widget or shortcut preview) into its final // location and size on the home screen. int spanX = info.spanX; int spanY = info.spanY; Rect r = estimateItemPosition(layout, targetCell[0], targetCell[1], spanX, spanY); loc[0] = r.left; loc[1] = r.top; setFinalTransitionTransform(layout); float cellLayoutScale = mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(layout, loc, true); resetTransitionTransform(layout); float dragViewScaleX; float dragViewScaleY; if (scale) { dragViewScaleX = (1.0f * r.width()) / dragView.getMeasuredWidth(); dragViewScaleY = (1.0f * r.height()) / dragView.getMeasuredHeight(); } else { dragViewScaleX = 1f; dragViewScaleY = 1f; } // The animation will scale the dragView about its center, so we need to center about // the final location. loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2; loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2; scaleXY[0] = dragViewScaleX * cellLayoutScale; scaleXY[1] = dragViewScaleY * cellLayoutScale; } public void animateWidgetDrop(ItemInfo info, CellLayout cellLayout, DragView dragView, final Runnable onCompleteRunnable, int animationType, final View finalView, boolean external) { Rect from = new Rect(); mLauncher.getDragLayer().getViewRectRelativeToSelf(dragView, from); int[] finalPos = new int[2]; float scaleXY[] = new float[2]; getFinalPositionForDropAnimation(finalPos, scaleXY, dragView, cellLayout, info, mTargetCell, external, true); Resources res = mLauncher.getResources(); final int duration = res.getInteger(R.integer.config_dropAnimMaxDuration) - 200; // In the case where we've prebound the widget, we remove it from the DragLayer if (finalView instanceof AppWidgetHostView && external) { Log.d(TAG, "6557954 Animate widget drop, final view is appWidgetHostView"); mLauncher.getDragLayer().removeView(finalView); } if ((animationType == ANIMATE_INTO_POSITION_AND_RESIZE || external) && finalView != null) { Bitmap crossFadeBitmap = createWidgetBitmap(info, finalView); dragView.setCrossFadeBitmap(crossFadeBitmap); dragView.crossFade((int) (duration * 0.8f)); } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET && external) { scaleXY[0] = scaleXY[1] = Math.min(scaleXY[0], scaleXY[1]); } DragLayer dragLayer = mLauncher.getDragLayer(); if (animationType == CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION) { mLauncher.getDragLayer().animateViewIntoPosition(dragView, finalPos, 0f, 0.1f, 0.1f, DragLayer.ANIMATION_END_DISAPPEAR, onCompleteRunnable, duration); } else { int endStyle; if (animationType == ANIMATE_INTO_POSITION_AND_REMAIN) { endStyle = DragLayer.ANIMATION_END_REMAIN_VISIBLE; } else { endStyle = DragLayer.ANIMATION_END_DISAPPEAR; } Runnable onComplete = new Runnable() { @Override public void run() { if (finalView != null) { finalView.setVisibility(VISIBLE); } if (onCompleteRunnable != null) { onCompleteRunnable.run(); } } }; dragLayer.animateViewIntoPosition(dragView, from.left, from.top, finalPos[0], finalPos[1], 1, 1, 1, scaleXY[0], scaleXY[1], onComplete, endStyle, duration, this); } } public void setFinalTransitionTransform(CellLayout layout) { if (isSwitchingState()) { mCurrentScale = getScaleX(); setScaleX(mNewScale); setScaleY(mNewScale); } } public void resetTransitionTransform(CellLayout layout) { if (isSwitchingState()) { setScaleX(mCurrentScale); setScaleY(mCurrentScale); } } /** * Return the current {@link CellLayout}, correctly picking the destination * screen while a scroll is in progress. */ public CellLayout getCurrentDropLayout() { return mWorkspace; } /** * Calculate the nearest cell where the given object would be dropped. * * pixelX and pixelY should be in the coordinate system of layout */ private int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, CellLayout layout, int[] recycle) { return layout.findNearestArea(pixelX, pixelY, spanX, spanY, recycle); } void setup(DragController dragController) { mSpringLoadedDragController = new SpringLoadedDragController(mLauncher); mDragController = dragController; } /** * Called at the end of a drag which originated on the workspace. */ public void onDropCompleted(final View target, final DragObject d, final boolean isFlingToDelete, final boolean success) { if (mDeferDropAfterUninstall) { mDeferredAction = new Runnable() { public void run() { onDropCompleted(target, d, isFlingToDelete, success); mDeferredAction = null; } }; return; } boolean beingCalledAfterUninstall = mDeferredAction != null; if (success && !(beingCalledAfterUninstall && !mUninstallSuccessful)) { if (target != this && mDragInfo != null) { CellLayout parentCell = mWorkspace; if (parentCell != null) { parentCell.removeView(mDragInfo.cell); } if (mDragInfo.cell instanceof DropTarget) { mDragController.removeDropTarget((DropTarget) mDragInfo.cell); } } } else if (mDragInfo != null) { CellLayout cellLayout = mWorkspace; if (cellLayout != null) { cellLayout.onDropChild(mDragInfo.cell); } } if ((d.cancelled || (beingCalledAfterUninstall && !mUninstallSuccessful)) && mDragInfo.cell != null) { mDragInfo.cell.setVisibility(VISIBLE); } mDragOutline = null; mDragInfo = null; } public void deferCompleteDropAfterUninstallActivity() { mDeferDropAfterUninstall = true; } /// maybe move this into a smaller part public void onUninstallActivityReturned(boolean success) { mDeferDropAfterUninstall = false; mUninstallSuccessful = success; if (mDeferredAction != null) { mDeferredAction.run(); } } void updateItemLocationsInDatabase(CellLayout cl) { int count = cl.getShortcutsAndWidgets().getChildCount(); int container = Favorites.CONTAINER_DESKTOP; for (int i = 0; i < count; i++) { View v = cl.getShortcutsAndWidgets().getChildAt(i); ItemInfo info = (ItemInfo) v.getTag(); // Null check required as the AllApps button doesn't have an item info if (info != null && info.requiresDbUpdate) { info.requiresDbUpdate = false; LauncherModel.modifyItemInDatabase(mLauncher, info, container, info.cellX, info.cellY, info.spanX, info.spanY); } } } @Override public float getIntrinsicIconScaleFactor() { return 1f; } @Override public boolean supportsFlingToDelete() { return true; } @Override public boolean supportsAppInfoDropTarget() { return false; } @Override public boolean supportsDeleteDropTarget() { return true; } @Override public void onFlingToDelete(DragObject d, int x, int y, PointF vec) { // Do nothing } @Override public void onFlingToDeleteCompleted() { // Do nothing } public boolean isDropEnabled() { return true; } private void onResetScrollArea() { setCurrentDragOverlappingLayout(null); mInScrollArea = false; } /** * We should only use this to search for specific children. Do not use this method to modify * ShortcutsAndWidgetsContainer directly. Includes ShortcutAndWidgetContainers from * the hotseat and workspace pages */ ArrayList<ShortcutAndWidgetContainer> getAllShortcutAndWidgetContainers() { ArrayList<ShortcutAndWidgetContainer> childrenLayouts = new ArrayList<ShortcutAndWidgetContainer>(); int screenCount = getChildCount(); for (int screen = 0; screen < screenCount; screen++) { childrenLayouts.add(((CellLayout) getChildAt(screen)).getShortcutsAndWidgets()); } return childrenLayouts; } public LauncherAppWidgetHostView getWidgetForAppWidgetId(final int appWidgetId) { return (LauncherAppWidgetHostView) getFirstMatch(new ItemOperator() { @Override public boolean evaluate(ItemInfo info, View v, View parent) { return (info instanceof LauncherAppWidgetInfo) && ((LauncherAppWidgetInfo) info).appWidgetId == appWidgetId; } }); } private View getFirstMatch(final ItemOperator operator) { final View[] value = new View[1]; mapOverItems(MAP_NO_RECURSE, new ItemOperator() { @Override public boolean evaluate(ItemInfo info, View v, View parent) { if (operator.evaluate(info, v, parent)) { value[0] = v; return true; } return false; } }); return value[0]; } void clearDropTargets() { mapOverItems(MAP_NO_RECURSE, new ItemOperator() { @Override public boolean evaluate(ItemInfo info, View v, View parent) { if (v instanceof DropTarget) { mDragController.removeDropTarget((DropTarget) v); } // not done, process all the shortcuts return false; } }); } public void disableShortcutsByPackageName(final ArrayList<String> packages, final UserHandleCompat user, final int reason) { final HashSet<String> packageNames = new HashSet<String>(); packageNames.addAll(packages); mapOverItems(MAP_RECURSE, new ItemOperator() { @Override public boolean evaluate(ItemInfo info, View v, View parent) { if (info instanceof ShortcutInfo && v instanceof BubbleTextView) { ShortcutInfo shortcutInfo = (ShortcutInfo) info; ComponentName cn = shortcutInfo.getTargetComponent(); if (user.equals(shortcutInfo.user) && cn != null && packageNames.contains(cn.getPackageName())) { shortcutInfo.isDisabled |= reason; BubbleTextView shortcut = (BubbleTextView) v; shortcut.applyFromShortcutInfo(shortcutInfo, mIconCache, true, true); if (parent != null) { parent.invalidate(); } } } // process all the shortcuts return false; } }); } // Removes ALL items that match a given package name, this is usually called when a package // has been removed and we want to remove all components (widgets, shortcuts, apps) that // belong to that package. void removeItemsByPackageName(final ArrayList<String> packages, final UserHandleCompat user) { final HashSet<String> packageNames = new HashSet<String>(); packageNames.addAll(packages); // Filter out all the ItemInfos that this is going to affect final HashSet<ItemInfo> infos = new HashSet<ItemInfo>(); final HashSet<ComponentName> cns = new HashSet<ComponentName>(); ViewGroup layout = mWorkspace.getShortcutsAndWidgets(); int childCount = layout.getChildCount(); for (int i = 0; i < childCount; ++i) { View view = layout.getChildAt(i); infos.add((ItemInfo) view.getTag()); } LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() { @Override public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) { if (packageNames.contains(cn.getPackageName()) && info.user.equals(user)) { cns.add(cn); return true; } return false; } }; LauncherModel.filterItemInfos(infos, filter); // Remove the affected components removeItemsByComponentName(cns, user); } /** * Removes items that match the item info specified. When applications are removed * as a part of an update, this is called to ensure that other widgets and application * shortcuts are not removed. */ void removeItemsByComponentName(final HashSet<ComponentName> componentNames, final UserHandleCompat user) { final ViewGroup layout = mWorkspace.getShortcutsAndWidgets(); final ArrayMap<ItemInfo, View> children = new ArrayMap<>(); for (int j = 0; j < layout.getChildCount(); j++) { final View view = layout.getChildAt(j); children.put((ItemInfo) view.getTag(), view); } final ArrayList<View> childrenToRemove = new ArrayList<View>(); LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() { @Override public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) { if (componentNames.contains(cn) && info.user.equals(user)) { childrenToRemove.add(children.get(info)); return true; } return false; } }; LauncherModel.filterItemInfos(children.keySet(), filter); // Remove all the other children for (View child : childrenToRemove) { // Note: We can not remove the view directly from CellLayoutChildren as this // does not re-mark the spaces as unoccupied. mWorkspace.removeViewInLayout(child); if (child instanceof DropTarget) { mDragController.removeDropTarget((DropTarget) child); } } if (childrenToRemove.size() > 0) { layout.requestLayout(); layout.invalidate(); } } interface ItemOperator { /** * Process the next itemInfo * * @param info info for the shortcut * @param view view for the shortcut * @param parent containing folder, or null * @return true if done, false to continue the map */ boolean evaluate(ItemInfo info, View view, View parent); } /** * Map the operator over the shortcuts and widgets, return the first-non-null value. * * @param recurse true: iterate over folder children. false: op get the folders themselves. * @param op the operator to map over the shortcuts */ void mapOverItems(boolean recurse, ItemOperator op) { ArrayList<ShortcutAndWidgetContainer> containers = getAllShortcutAndWidgetContainers(); final int containerCount = containers.size(); for (int containerIdx = 0; containerIdx < containerCount; containerIdx++) { ShortcutAndWidgetContainer container = containers.get(containerIdx); // map over all the shortcuts on the workspace final int itemCount = container.getChildCount(); for (int itemIdx = 0; itemIdx < itemCount; itemIdx++) { View item = container.getChildAt(itemIdx); ItemInfo info = (ItemInfo) item.getTag(); if (op.evaluate(info, item, null)) { return; } } } } void updateShortcuts(ArrayList<ShortcutInfo> shortcuts) { final HashSet<ShortcutInfo> updates = new HashSet<ShortcutInfo>(shortcuts); mapOverItems(MAP_RECURSE, new ItemOperator() { @Override public boolean evaluate(ItemInfo info, View v, View parent) { if (info instanceof ShortcutInfo && v instanceof BubbleTextView && updates.contains(info)) { ShortcutInfo si = (ShortcutInfo) info; BubbleTextView shortcut = (BubbleTextView) v; shortcut.applyFromShortcutInfo(si, mIconCache, true, true); if (parent != null) { parent.invalidate(); } } // process all the shortcuts return false; } }); } void widgetsRestored(ArrayList<LauncherAppWidgetInfo> changedInfo) { if (!changedInfo.isEmpty()) { DeferredWidgetRefresh widgetRefresh = new DeferredWidgetRefresh(changedInfo, mLauncher.getAppWidgetHost()); if (LauncherModel.findAppWidgetProviderInfoWithComponent(getContext(), changedInfo.get(0).providerName) != null) { // Re-inflate the widgets which have changed status widgetRefresh.run(); } else { // widgetRefresh will automatically run when the packages are updated. // For now just update the progress bars for (LauncherAppWidgetInfo info : changedInfo) { if (info.hostView instanceof PendingAppWidgetHostView) { info.installProgress = 100; ((PendingAppWidgetHostView) info.hostView).applyState(); } } } } } public void getLocationInDragLayer(int[] loc) { mLauncher.getDragLayer().getLocationInDragLayer(this, loc); } /** * Used as a workaround to ensure that the AppWidgetService receives the * PACKAGE_ADDED broadcast before updating widgets. */ private class DeferredWidgetRefresh implements Runnable { private final ArrayList<LauncherAppWidgetInfo> mInfos; private final LauncherAppWidgetHost mHost; private final Handler mHandler; private boolean mRefreshPending; public DeferredWidgetRefresh(ArrayList<LauncherAppWidgetInfo> infos, LauncherAppWidgetHost host) { mInfos = infos; mHost = host; mHandler = new Handler(); mRefreshPending = true; mHost.addProviderChangeListener(this); // Force refresh after 10 seconds, if we don't get the provider changed event. // This could happen when the provider is no longer available in the app. mHandler.postDelayed(this, 10000); } @Override public void run() { mHost.removeProviderChangeListener(this); mHandler.removeCallbacks(this); if (!mRefreshPending) { return; } mRefreshPending = false; for (LauncherAppWidgetInfo info : mInfos) { if (info.hostView instanceof PendingAppWidgetHostView) { PendingAppWidgetHostView view = (PendingAppWidgetHostView) info.hostView; mLauncher.removeAppWidget(info); CellLayout cl = (CellLayout) view.getParent().getParent(); // Remove the current widget cl.removeView(view); mLauncher.bindAppWidget(info); } } } } }