Home | History | Annotate | Download | only in launcher2
      1 /*
      2  * Copyright (C) 2010 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.launcher2;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.graphics.PointF;
     22 import android.graphics.Rect;
     23 import android.graphics.drawable.Drawable;
     24 import android.util.AttributeSet;
     25 import android.widget.TextView;
     26 
     27 import com.android.launcher.R;
     28 
     29 
     30 /**
     31  * Implements a DropTarget.
     32  */
     33 public class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener {
     34 
     35     protected final int mTransitionDuration;
     36 
     37     protected Launcher mLauncher;
     38     private int mBottomDragPadding;
     39     protected TextView mText;
     40     protected SearchDropTargetBar mSearchDropTargetBar;
     41 
     42     /** Whether this drop target is active for the current drag */
     43     protected boolean mActive;
     44 
     45     /** The paint applied to the drag view on hover */
     46     protected int mHoverColor = 0;
     47 
     48     public ButtonDropTarget(Context context, AttributeSet attrs) {
     49         this(context, attrs, 0);
     50     }
     51 
     52     public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
     53         super(context, attrs, defStyle);
     54 
     55         Resources r = getResources();
     56         mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration);
     57         mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
     58     }
     59 
     60     void setLauncher(Launcher launcher) {
     61         mLauncher = launcher;
     62     }
     63 
     64     public boolean acceptDrop(DragObject d) {
     65         return false;
     66     }
     67 
     68     public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
     69         mSearchDropTargetBar = searchDropTargetBar;
     70     }
     71 
     72     protected Drawable getCurrentDrawable() {
     73         Drawable[] drawables = getCompoundDrawables();
     74         for (int i = 0; i < drawables.length; ++i) {
     75             if (drawables[i] != null) {
     76                 return drawables[i];
     77             }
     78         }
     79         return null;
     80     }
     81 
     82     public void onDrop(DragObject d) {
     83     }
     84 
     85     public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
     86         // Do nothing
     87     }
     88 
     89     public void onDragEnter(DragObject d) {
     90         d.dragView.setColor(mHoverColor);
     91     }
     92 
     93     public void onDragOver(DragObject d) {
     94         // Do nothing
     95     }
     96 
     97     public void onDragExit(DragObject d) {
     98         d.dragView.setColor(0);
     99     }
    100 
    101     public void onDragStart(DragSource source, Object info, int dragAction) {
    102         // Do nothing
    103     }
    104 
    105     public boolean isDropEnabled() {
    106         return mActive;
    107     }
    108 
    109     public void onDragEnd() {
    110         // Do nothing
    111     }
    112 
    113     @Override
    114     public void getHitRect(android.graphics.Rect outRect) {
    115         super.getHitRect(outRect);
    116         outRect.bottom += mBottomDragPadding;
    117     }
    118 
    119     Rect getIconRect(int itemWidth, int itemHeight, int drawableWidth, int drawableHeight) {
    120         DragLayer dragLayer = mLauncher.getDragLayer();
    121 
    122         // Find the rect to animate to (the view is center aligned)
    123         Rect to = new Rect();
    124         dragLayer.getViewRectRelativeToSelf(this, to);
    125         int width = drawableWidth;
    126         int height = drawableHeight;
    127         int left = to.left + getPaddingLeft();
    128         int top = to.top + (getMeasuredHeight() - height) / 2;
    129         to.set(left, top, left + width, top + height);
    130 
    131         // Center the destination rect about the trash icon
    132         int xOffset = (int) -(itemWidth - width) / 2;
    133         int yOffset = (int) -(itemHeight - height) / 2;
    134         to.offset(xOffset, yOffset);
    135 
    136         return to;
    137     }
    138 
    139     @Override
    140     public DropTarget getDropTargetDelegate(DragObject d) {
    141         return null;
    142     }
    143 
    144     public void getLocationInDragLayer(int[] loc) {
    145         mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
    146     }
    147 }
    148