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