Home | History | Annotate | Download | only in droptarget
      1 /*
      2 * Copyright 2016, 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.example.android.droptarget;
     18 
     19 import android.content.ClipData;
     20 import android.net.Uri;
     21 import android.view.DragEvent;
     22 import android.view.View;
     23 import android.widget.ImageView;
     24 
     25 public class ImageDragListener implements View.OnDragListener {
     26 
     27     private static final int COLOR_INACTIVE = 0xFF888888;
     28 
     29     private static final int COLOR_ACTIVE = 0xFFCCCCCC;
     30 
     31     private static final int COLOR_HOVER = 0xFFEEEEEE;
     32 
     33     @Override
     34     public boolean onDrag(View view, DragEvent event) {
     35         switch (event.getAction()) {
     36             case DragEvent.ACTION_DRAG_STARTED:
     37                 setTargetColor(view, COLOR_ACTIVE);
     38                 return true;
     39 
     40             case DragEvent.ACTION_DRAG_ENTERED:
     41                 setTargetColor(view, COLOR_HOVER);
     42                 return true;
     43 
     44             case DragEvent.ACTION_DRAG_LOCATION:
     45                 processLocation(event.getX(), event.getY());
     46                 return true;
     47 
     48             case DragEvent.ACTION_DRAG_EXITED:
     49                 setTargetColor(view, COLOR_ACTIVE);
     50                 return true;
     51 
     52             case DragEvent.ACTION_DROP:
     53                 return processDrop(view, event);
     54 
     55             case DragEvent.ACTION_DRAG_ENDED:
     56                 setTargetColor(view, COLOR_INACTIVE);
     57                 return true;
     58 
     59             default:
     60                 break;
     61         }
     62 
     63         return false;
     64     }
     65 
     66     private void setTargetColor(View view, int color) {
     67         view.setBackgroundColor(color);
     68     }
     69 
     70     private boolean processDrop(View view, DragEvent event) {
     71         ClipData clipData = event.getClipData();
     72         if (clipData == null || clipData.getItemCount() == 0) {
     73             return false;
     74         }
     75         ClipData.Item item = clipData.getItemAt(0);
     76         if (item == null) {
     77             return false;
     78         }
     79         Uri uri = item.getUri();
     80         if (uri == null) {
     81             return false;
     82         }
     83         return setImageUri(view, event, uri);
     84     }
     85 
     86     protected void processLocation(float x, float y) {
     87     }
     88 
     89     protected boolean setImageUri(View view, DragEvent event, Uri uri) {
     90         if (!(view instanceof ImageView)) {
     91             return false;
     92         }
     93         ((ImageView) view).setImageURI(uri);
     94         return true;
     95     }
     96 }
     97