Home | History | Annotate | Download | only in sidebar
      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 
     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
     54     public void runOnUiThread(Runnable runnable) {
     55         mActivity.runOnUiThread(runnable);
     56     }
     57 
     58     @Override
     59     public void setDropTargetHighlight(View v, boolean highlight) {
     60         // SpacerView doesn't have DragListener so this view is guaranteed to be a RootItemView.
     61         RootItemView itemView = (RootItemView) v;
     62         itemView.setHighlight(highlight);
     63     }
     64 
     65     @Override
     66     public void onViewHovered(View v) {
     67         // SpacerView doesn't have DragListener so this view is guaranteed to be a RootItemView.
     68         RootItemView itemView = (RootItemView) v;
     69         itemView.drawRipple();
     70 
     71         mDestinationLookup.lookup(v).open();
     72     }
     73 
     74     @Override
     75     public void onDragEntered(View v) {
     76         final Item item = mDestinationLookup.lookup(v);
     77 
     78         // If a read-only root, no need to see if top level is writable (it's not)
     79         if (!item.isDropTarget()) {
     80             mDragAndDropManager.updateStateToNotAllowed(v);
     81             return;
     82         }
     83 
     84         final RootItem rootItem = (RootItem) item;
     85         if (mDragAndDropManager.updateState(v, rootItem.root, null)
     86                 == DragAndDropManager.STATE_UNKNOWN) {
     87             mActions.getRootDocument(
     88                     rootItem.root,
     89                     DRAG_LOAD_TIME_OUT,
     90                     (DocumentInfo doc) -> {
     91                         updateDropShadow(v, rootItem, doc);
     92                     });
     93         }
     94     }
     95 
     96     private void updateDropShadow(
     97             View v, RootItem rootItem, DocumentInfo rootDoc) {
     98         if (rootDoc == null) {
     99             Log.e(TAG, "Root DocumentInfo is null. Defaulting to unknown.");
    100         } else {
    101             rootItem.docInfo = rootDoc;
    102             mDragAndDropManager.updateState(v, rootItem.root, rootDoc);
    103         }
    104     }
    105 }
    106