WidgetsContainerView.java revision abef55a1c1f1fda4f42e4b4223534dd21f3dc19f
13f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song/*
23f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song * Copyright (C) 2015 The Android Open Source Project
33f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song *
43f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song * Licensed under the Apache License, Version 2.0 (the "License");
53f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song * you may not use this file except in compliance with the License.
63f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song * You may obtain a copy of the License at
73f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song *
83f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song *      http://www.apache.org/licenses/LICENSE-2.0
93f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song *
103f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song * Unless required by applicable law or agreed to in writing, software
113f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song * distributed under the License is distributed on an "AS IS" BASIS,
123f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song * See the License for the specific language governing permissions and
143f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song * limitations under the License.
153f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song */
163f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
173f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songpackage com.android.launcher3.widget;
183f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
193f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport android.content.Context;
203f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport android.graphics.Bitmap;
213f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport android.graphics.Rect;
223f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport android.graphics.drawable.Drawable;
23261194387beebaa7927ec4e310274218b651494dSunny Goyalimport android.graphics.drawable.InsetDrawable;
243f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport android.support.v7.widget.LinearLayoutManager;
25e809332032c8f0c2f361eb8fc753f3b57d9cfaaaHyunyoung Songimport android.support.v7.widget.RecyclerView.State;
263f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport android.util.AttributeSet;
273f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport android.util.Log;
283f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport android.view.View;
299600d20110861db08f0db21358161dc0ae6b2037Sunny Goyalimport android.view.ViewGroup;
30b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Songimport android.widget.Toast;
31f52abd7758b8f245522936e941336e574debabf5Sunny Goyal
329480415d9baf00ecfaac8ab3f608b16a1faa6518Winson Chungimport com.android.launcher3.BaseContainerView;
333f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.CellLayout;
343f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.DeleteDropTarget;
35fe7580aac4a75dfae5e59c52608182c5e9f76011Hyunyoung Songimport com.android.launcher3.DeviceProfile;
363f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.DragSource;
373f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.DropTarget.DragObject;
38261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.folder.Folder;
393f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.IconCache;
403f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.ItemInfo;
413f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.Launcher;
423f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.LauncherAppState;
433f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.PendingAddItemInfo;
443f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.R;
453f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.Utilities;
463f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.WidgetPreviewLoader;
473f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.Workspace;
48fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragController;
4958376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyalimport com.android.launcher3.model.WidgetsModel;
50316490e636aad788fcfbfc2e04dd4f0e145bdd00Sunny Goyalimport com.android.launcher3.util.Thunk;
513f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
523f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song/**
533f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song * The widgets list view container.
543f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song */
559480415d9baf00ecfaac8ab3f608b16a1faa6518Winson Chungpublic class WidgetsContainerView extends BaseContainerView
56a0628cc521614835933db74ba5a2536a936629c5Tony Wickham        implements View.OnLongClickListener, View.OnClickListener, DragSource {
57b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song    private static final String TAG = "WidgetsContainerView";
58a0628cc521614835933db74ba5a2536a936629c5Tony Wickham    private static final boolean LOGD = false;
593f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
603f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    /* Global instances that are used inside this container. */
61316490e636aad788fcfbfc2e04dd4f0e145bdd00Sunny Goyal    @Thunk Launcher mLauncher;
623f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    private DragController mDragController;
633f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    private IconCache mIconCache;
643f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
653f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    /* Recycler view related member variables */
66a6194d3d9205488d6f5400193f8d4d6aaf140218Sunny Goyal    private WidgetsRecyclerView mRecyclerView;
673f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    private WidgetsListAdapter mAdapter;
683f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
69b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song    /* Touch handling related member variables. */
70b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song    private Toast mWidgetInstructionToast;
713f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
723f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    /* Rendering related. */
733f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    private WidgetPreviewLoader mWidgetPreviewLoader;
74b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song
753f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public WidgetsContainerView(Context context) {
763f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        this(context, null);
773f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
783f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
793f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public WidgetsContainerView(Context context, AttributeSet attrs) {
803f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        this(context, attrs, 0);
813f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
823f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
833f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public WidgetsContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
843f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        super(context, attrs, defStyleAttr);
85abef55a1c1f1fda4f42e4b4223534dd21f3dc19fAndrew Sapperstein        mLauncher = Launcher.getLauncher(context);
863f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        mDragController = mLauncher.getDragController();
87abef55a1c1f1fda4f42e4b4223534dd21f3dc19fAndrew Sapperstein        mAdapter = new WidgetsListAdapter(this, this, context);
883f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        mIconCache = (LauncherAppState.getInstance()).getIconCache();
89a0628cc521614835933db74ba5a2536a936629c5Tony Wickham        if (LOGD) {
903f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            Log.d(TAG, "WidgetsContainerView constructor");
913f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        }
923f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
933f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
943f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    @Override
953f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    protected void onFinishInflate() {
96a0628cc521614835933db74ba5a2536a936629c5Tony Wickham        super.onFinishInflate();
97a6194d3d9205488d6f5400193f8d4d6aaf140218Sunny Goyal        mRecyclerView = (WidgetsRecyclerView) getContentView().findViewById(R.id.widgets_list_view);
98a6194d3d9205488d6f5400193f8d4d6aaf140218Sunny Goyal        mRecyclerView.setAdapter(mAdapter);
99b7271fd17ea78acbf8506b2c9a34f741680fc4a9Hyunyoung Song        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
10005c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal
10105c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal        Rect bgPadding = new Rect();
10205c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal        getRevealView().getBackground().getPadding(bgPadding);
10305c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal        if (Utilities.isRtl(getResources())) {
10405c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal            getContentView().setPadding(0, bgPadding.top,
10505c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal                    bgPadding.right, bgPadding.bottom);
10605c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal            mRecyclerView.updateBackgroundPadding(new Rect(bgPadding.left, 0, 0, 0));
10705c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal        } else {
10805c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal            getContentView().setPadding(bgPadding.left, bgPadding.top,
10905c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal                    0, bgPadding.bottom);
11005c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal            mRecyclerView.updateBackgroundPadding(new Rect(0, 0, bgPadding.right, 0));
11105c8c57fa72a81f34058036f6dc30c084ca6742bSunny Goyal        }
1123f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
1133f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
1143f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    //
1153f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    // Returns views used for launcher transitions.
1163f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    //
1173f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
1183f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public void scrollToTop() {
119a6194d3d9205488d6f5400193f8d4d6aaf140218Sunny Goyal        mRecyclerView.scrollToPosition(0);
1203f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
1213f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
1223f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    //
1233f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    // Touch related handling.
1243f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    //
1253f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
1263f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    @Override
127b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song    public void onClick(View v) {
128b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song        // When we have exited widget tray or are in transition, disregard clicks
129b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song        if (!mLauncher.isWidgetsViewVisible()
130b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song                || mLauncher.getWorkspace().isSwitchingState()
131b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song                || !(v instanceof WidgetCell)) return;
132b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song
133b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song        // Let the user know that they have to long press to add a widget
134b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song        if (mWidgetInstructionToast != null) {
135b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song            mWidgetInstructionToast.cancel();
136b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song        }
137e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal
138e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        CharSequence msg = Utilities.wrapForTts(
139e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                getContext().getText(R.string.long_press_widget_to_add),
140e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                getContext().getString(R.string.long_accessible_way_to_add));
141e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        mWidgetInstructionToast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
142b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song        mWidgetInstructionToast.show();
143b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song    }
144b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song
145b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song    @Override
1463f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public boolean onLongClick(View v) {
147a0628cc521614835933db74ba5a2536a936629c5Tony Wickham        if (LOGD) {
1483f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            Log.d(TAG, String.format("onLonglick [v=%s]", v));
1493f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        }
1503f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // Return early if this is not initiated from a touch
1513f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        if (!v.isInTouchMode()) return false;
1523f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // When we have exited all apps or are in transition, disregard long clicks
1533f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        if (!mLauncher.isWidgetsViewVisible() ||
1543f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                mLauncher.getWorkspace().isSwitchingState()) return false;
1553f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // Return if global dragging is not enabled
1563f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        if (!mLauncher.isDraggingEnabled()) return false;
1573f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
158b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song        boolean status = beginDragging(v);
1595b9ebcab3c6c55dcb7575fdbcb77020a5ad274d4Sunny Goyal        if (status && v.getTag() instanceof PendingAddWidgetInfo) {
1605b9ebcab3c6c55dcb7575fdbcb77020a5ad274d4Sunny Goyal            WidgetHostViewLoader hostLoader = new WidgetHostViewLoader(mLauncher, v);
1615b9ebcab3c6c55dcb7575fdbcb77020a5ad274d4Sunny Goyal            boolean preloadStatus = hostLoader.preloadWidget();
162a0628cc521614835933db74ba5a2536a936629c5Tony Wickham            if (LOGD) {
1635b9ebcab3c6c55dcb7575fdbcb77020a5ad274d4Sunny Goyal                Log.d(TAG, String.format("preloading widget [status=%s]", preloadStatus));
1645b9ebcab3c6c55dcb7575fdbcb77020a5ad274d4Sunny Goyal            }
1655b9ebcab3c6c55dcb7575fdbcb77020a5ad274d4Sunny Goyal            mLauncher.getDragController().addDragListener(hostLoader);
166b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song        }
167b99ff3e83270e113f6182e337c4f7b0223bad92bHyunyoung Song        return status;
1683f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
1693f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
1703f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    private boolean beginDragging(View v) {
1713f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        if (v instanceof WidgetCell) {
1723f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            if (!beginDraggingWidget((WidgetCell) v)) {
1733f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                return false;
1743f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            }
1753f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        } else {
1763f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            Log.e(TAG, "Unexpected dragging view: " + v);
1773f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        }
1783f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
17958376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        // We don't enter spring-loaded mode if the drag has been cancelled
18058376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        if (mLauncher.getDragController().isDragging()) {
18158376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal            // Go into spring loaded mode (must happen before we startDrag())
18258376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal            mLauncher.enterSpringLoadedDragMode();
18358376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        }
1843f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
1853f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        return true;
1863f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
1873f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
1883f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    private boolean beginDraggingWidget(WidgetCell v) {
1893f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // Get the widget preview as the drag representation
190f52abd7758b8f245522936e941336e574debabf5Sunny Goyal        WidgetImageView image = (WidgetImageView) v.findViewById(R.id.widget_preview);
1913f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
1923f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
1933f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
1943f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // we abort the drag.
195f52abd7758b8f245522936e941336e574debabf5Sunny Goyal        if (image.getBitmap() == null) {
1963f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            return false;
1973f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        }
1983f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
1993f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // Compose the drag image
2003f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        Bitmap preview;
2013f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        float scale = 1f;
202f52abd7758b8f245522936e941336e574debabf5Sunny Goyal        final Rect bounds = image.getBitmapBounds();
2033f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2043f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        if (createItemInfo instanceof PendingAddWidgetInfo) {
2053f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            // This can happen in some weird cases involving multi-touch. We can't start dragging
2063f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            // the widget if this is null, so we break out.
2073f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2083f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
2093f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            int[] size = mLauncher.getWorkspace().estimateItemSize(createWidgetInfo, true);
2103f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
211f52abd7758b8f245522936e941336e574debabf5Sunny Goyal            Bitmap icon = image.getBitmap();
2123f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            float minScale = 1.25f;
213f52abd7758b8f245522936e941336e574debabf5Sunny Goyal            int maxWidth = Math.min((int) (icon.getWidth() * minScale), size[0]);
2143f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2153f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            int[] previewSizeBeforeScale = new int[1];
2162e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen            preview = getWidgetPreviewLoader().generateWidgetPreview(mLauncher,
2172e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen                    createWidgetInfo.info, maxWidth, null, previewSizeBeforeScale);
218f52abd7758b8f245522936e941336e574debabf5Sunny Goyal
219f52abd7758b8f245522936e941336e574debabf5Sunny Goyal            if (previewSizeBeforeScale[0] < icon.getWidth()) {
220f52abd7758b8f245522936e941336e574debabf5Sunny Goyal                // The icon has extra padding around it.
221f52abd7758b8f245522936e941336e574debabf5Sunny Goyal                int padding = (icon.getWidth() - previewSizeBeforeScale[0]) / 2;
222f52abd7758b8f245522936e941336e574debabf5Sunny Goyal                if (icon.getWidth() > image.getWidth()) {
223f52abd7758b8f245522936e941336e574debabf5Sunny Goyal                    padding = padding * image.getWidth() / icon.getWidth();
224f52abd7758b8f245522936e941336e574debabf5Sunny Goyal                }
225f52abd7758b8f245522936e941336e574debabf5Sunny Goyal
226f52abd7758b8f245522936e941336e574debabf5Sunny Goyal                bounds.left += padding;
227f52abd7758b8f245522936e941336e574debabf5Sunny Goyal                bounds.right -= padding;
2283f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            }
229f52abd7758b8f245522936e941336e574debabf5Sunny Goyal            scale = bounds.width() / (float) preview.getWidth();
2303f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        } else {
2313f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag();
2323f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.activityInfo);
2333f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            preview = Utilities.createIconBitmap(icon, mLauncher);
2343f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            createItemInfo.spanX = createItemInfo.spanY = 1;
23553d7ee4285842732517edcccbdcaa92dccb1e107Sunny Goyal            scale = ((float) mLauncher.getDeviceProfile().iconSizePx) / preview.getWidth();
2363f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        }
2373f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2383f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // Don't clip alpha values for the drag outline if we're using the default widget preview
2393f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        boolean clipAlpha = !(createItemInfo instanceof PendingAddWidgetInfo &&
2403f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                (((PendingAddWidgetInfo) createItemInfo).previewImage == 0));
2413f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2423f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // Start the drag
2433f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        mLauncher.lockScreenOrientation();
2443f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        mDragController.startDrag(image, preview, this, createItemInfo,
245f52abd7758b8f245522936e941336e574debabf5Sunny Goyal                bounds, DragController.DRAG_ACTION_COPY, scale);
246869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham        // This call expects the extra empty screen to already be created, which is why we call it
247869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham        // after mDragController.startDrag().
248869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham        mLauncher.getWorkspace().onDragStartedWithItem(createItemInfo, preview, clipAlpha);
249f52abd7758b8f245522936e941336e574debabf5Sunny Goyal
2503f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        preview.recycle();
2513f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        return true;
2523f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
2533f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2543f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    //
2553f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    // Drag related handling methods that implement {@link DragSource} interface.
2563f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    //
2573f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2583f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    @Override
2593f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public boolean supportsFlingToDelete() {
2609aae47f8a6e23f1805ee3d44512fc1f80286e6ddTony Wickham        return true;
2613f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
2623f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2633f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    @Override
2643f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public boolean supportsAppInfoDropTarget() {
2653f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        return true;
2663f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
2673f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2683f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    /*
2693f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song     * Both this method and {@link #supportsFlingToDelete} has to return {@code false} for the
2703f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song     * {@link DeleteDropTarget} to be invisible.)
2713f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song     */
2723f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    @Override
2733f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public boolean supportsDeleteDropTarget() {
2743f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        return false;
2753f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
2763f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2773f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    @Override
2783f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public float getIntrinsicIconScaleFactor() {
2793f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        return 0;
2803f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
2813f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2823f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    @Override
2833f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public void onFlingToDeleteCompleted() {
2843f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // We just dismiss the drag when we fling, so cleanup here
2853f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        mLauncher.exitSpringLoadedDragModeDelayed(true,
2863f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
2873f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        mLauncher.unlockScreenOrientation(false);
2883f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
2893f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
2903f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    @Override
2913f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
2923f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            boolean success) {
293a0628cc521614835933db74ba5a2536a936629c5Tony Wickham        if (LOGD) {
294a0628cc521614835933db74ba5a2536a936629c5Tony Wickham            Log.d(TAG, "onDropCompleted");
295a0628cc521614835933db74ba5a2536a936629c5Tony Wickham        }
2963f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        if (isFlingToDelete || !success || (target != mLauncher.getWorkspace() &&
2973f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                !(target instanceof DeleteDropTarget) && !(target instanceof Folder))) {
2983f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            // Exit spring loaded mode if we have not successfully dropped or have not handled the
2993f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            // drop in Workspace
3003f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            mLauncher.exitSpringLoadedDragModeDelayed(true,
3013f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                    Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
3023f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        }
3033f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        mLauncher.unlockScreenOrientation(false);
3043f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
3053f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // Display an error message if the drag failed due to there not being enough space on the
3063f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        // target layout we were dropping on.
3073f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        if (!success) {
3083f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            boolean showOutOfSpaceMessage = false;
3093f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            if (target instanceof Workspace) {
3103f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                int currentScreen = mLauncher.getCurrentWorkspaceScreen();
3113f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                Workspace workspace = (Workspace) target;
3123f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
313aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                ItemInfo itemInfo = d.dragInfo;
3143f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                if (layout != null) {
3153f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                    showOutOfSpaceMessage =
3163f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                            !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
3173f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                }
3183f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            }
3193f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            if (showOutOfSpaceMessage) {
3203f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song                mLauncher.showOutOfSpaceMessage(false);
3213f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            }
3223f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            d.deferDragViewCleanupPostAnimation = false;
3233f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        }
3243f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
3253f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
3263f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    /**
3273f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song     * Initialize the widget data model.
3283f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song     */
3292bd3d7d1cb5e4d8d826982d11b456739fed6b817Hyunyoung Song    public void addWidgets(WidgetsModel model) {
330a6194d3d9205488d6f5400193f8d4d6aaf140218Sunny Goyal        mRecyclerView.setWidgets(model);
3312bd3d7d1cb5e4d8d826982d11b456739fed6b817Hyunyoung Song        mAdapter.setWidgetsModel(model);
3322bd3d7d1cb5e4d8d826982d11b456739fed6b817Hyunyoung Song        mAdapter.notifyDataSetChanged();
3339600d20110861db08f0db21358161dc0ae6b2037Sunny Goyal
3349600d20110861db08f0db21358161dc0ae6b2037Sunny Goyal        View loader = getContentView().findViewById(R.id.loader);
3359600d20110861db08f0db21358161dc0ae6b2037Sunny Goyal        if (loader != null) {
3369600d20110861db08f0db21358161dc0ae6b2037Sunny Goyal            ((ViewGroup) getContentView()).removeView(loader);
3379600d20110861db08f0db21358161dc0ae6b2037Sunny Goyal        }
3383f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
3393f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song
3402e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal    public boolean isEmpty() {
3412e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        return mAdapter.getItemCount() == 0;
3422e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal    }
3432e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal
3443f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    private WidgetPreviewLoader getWidgetPreviewLoader() {
3453f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        if (mWidgetPreviewLoader == null) {
3463f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song            mWidgetPreviewLoader = LauncherAppState.getInstance().getWidgetCache();
3473f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        }
3483f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song        return mWidgetPreviewLoader;
3493f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song    }
3503f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Song}