1 /*
2  * Copyright (C) 2017 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.documentsui.sidebar;
18 
19 import android.app.Activity;
20 import android.util.Log;
21 import android.view.View;
22 
23 import com.android.documentsui.AbstractDragHost;
24 import com.android.documentsui.ActionHandler;
25 import com.android.documentsui.DragAndDropManager;
26 import com.android.documentsui.base.DocumentInfo;
27 import com.android.documentsui.base.Lookup;
28 
29 /**
30  * Drag host for items in {@link RootsFragment}.
31  */
32 class DragHost extends AbstractDragHost {
33 
34     private static final String TAG = "RootsDragHost";
35     private static final int DRAG_LOAD_TIME_OUT = 500;
36 
37     private final Activity mActivity;
38     private final Lookup<View, Item> mDestinationLookup;
39     private final ActionHandler mActions;
40 
DragHost( Activity activity, DragAndDropManager dragAndDropManager, Lookup<View, Item> destinationLookup, ActionHandler actions)41     DragHost(
42             Activity activity,
43             DragAndDropManager dragAndDropManager,
44             Lookup<View, Item> destinationLookup,
45             ActionHandler actions) {
46         super(dragAndDropManager);
47         mActivity = activity;
48         mDragAndDropManager = dragAndDropManager;
49         mDestinationLookup = destinationLookup;
50         mActions = actions;
51     }
52 
53     @Override
runOnUiThread(Runnable runnable)54     public void runOnUiThread(Runnable runnable) {
55         mActivity.runOnUiThread(runnable);
56     }
57 
58     @Override
canHandleDragEvent(View v)59     public boolean canHandleDragEvent(View v) {
60         return v instanceof RootItemView;
61     }
62 
63     @Override
setDropTargetHighlight(View v, boolean highlight)64     public void setDropTargetHighlight(View v, boolean highlight) {
65         // SpacerView doesn't have DragListener so this view is guaranteed to be a RootItemView.
66         RootItemView itemView = (RootItemView) v;
67         itemView.setHighlight(highlight);
68     }
69 
70     @Override
onViewHovered(View v)71     public void onViewHovered(View v) {
72         // SpacerView doesn't have DragListener so this view is guaranteed to be a RootItemView.
73         RootItemView itemView = (RootItemView) v;
74         itemView.drawRipple();
75 
76         mDestinationLookup.lookup(v).open();
77     }
78 
79     @Override
onDragEntered(View v)80     public void onDragEntered(View v) {
81         final Item item = mDestinationLookup.lookup(v);
82 
83         // If a read-only root, no need to see if top level is writable (it's not)
84         if (!item.isDropTarget()) {
85             mDragAndDropManager.updateStateToNotAllowed(v);
86             return;
87         }
88 
89         final RootItem rootItem = (RootItem) item;
90         if (mDragAndDropManager.updateState(v, rootItem.root, null)
91                 == DragAndDropManager.STATE_UNKNOWN) {
92             mActions.getRootDocument(
93                     rootItem.root,
94                     DRAG_LOAD_TIME_OUT,
95                     (DocumentInfo doc) -> {
96                         updateDropShadow(v, rootItem, doc);
97                     });
98         }
99     }
100 
updateDropShadow( View v, RootItem rootItem, DocumentInfo rootDoc)101     private void updateDropShadow(
102             View v, RootItem rootItem, DocumentInfo rootDoc) {
103         if (rootDoc == null) {
104             Log.e(TAG, "Root DocumentInfo is null. Defaulting to unknown.");
105         } else {
106             rootItem.docInfo = rootDoc;
107             mDragAndDropManager.updateState(v, rootItem.root, rootDoc);
108         }
109     }
110 }
111