1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.launcher3.dragndrop;
18 
19 import android.graphics.Rect;
20 
21 import androidx.annotation.NonNull;
22 
23 import com.android.launcher3.util.SafeCloseable;
24 
25 /**
26  * Interface defining methods required for drawing and previewing DragViews, drag previews, and
27  * related animations
28  */
29 public interface DraggableView {
30     int DRAGGABLE_ICON = 0;
31     int DRAGGABLE_WIDGET = 1;
32 
33     /**
34      * Static ctr for a simple instance which just returns the view type.
35      */
ofType(int type)36     static DraggableView ofType(int type) {
37         return () -> type;
38     }
39 
40     /**
41      * Certain handling of DragViews depend only on whether this is an Icon Type item or a Widget
42      * Type item.
43      *
44      * @return DRAGGABLE_ICON or DRAGGABLE_WIDGET as appropriate
45      */
getViewType()46     int getViewType();
47 
48     /**
49      * Before rendering as a DragView bitmap, some views need a preparation step. Returns a
50      * callback to clear any preparation work
51      */
prepareDrawDragView()52     @NonNull default SafeCloseable prepareDrawDragView() {
53         return () -> { };
54     }
55 
56     /**
57      * If an actual View subclass, this method returns the rectangle (within the View's coordinates)
58      * of the visual region that should get dragged. This is used to extract exactly that element
59      * as well as to offset that element as appropriate for various animations
60      *
61      * @param bounds Visual bounds in the views coordinates will be written here.
62      */
getWorkspaceVisualDragBounds(Rect bounds)63     default void getWorkspaceVisualDragBounds(Rect bounds) { }
64 
65     /**
66      * Same as above, but accounts for differing icon sizes between source and destination
67      *
68      * @param bounds Visual bounds in the views coordinates will be written here.
69      */
getSourceVisualDragBounds(Rect bounds)70     default void getSourceVisualDragBounds(Rect bounds) {
71         getWorkspaceVisualDragBounds(bounds);
72     }
73 }
74