Home | History | Annotate | Download | only in launcher3
      1 /*
      2  * Copyright (C) 2011 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.launcher3;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.res.ColorStateList;
     22 import android.content.res.Configuration;
     23 import android.content.res.Resources;
     24 import android.graphics.drawable.TransitionDrawable;
     25 import android.util.AttributeSet;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 
     29 public class InfoDropTarget extends ButtonDropTarget {
     30 
     31     private ColorStateList mOriginalTextColor;
     32     private TransitionDrawable mDrawable;
     33 
     34     public InfoDropTarget(Context context, AttributeSet attrs) {
     35         this(context, attrs, 0);
     36     }
     37 
     38     public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
     39         super(context, attrs, defStyle);
     40     }
     41 
     42     @Override
     43     protected void onFinishInflate() {
     44         super.onFinishInflate();
     45 
     46         mOriginalTextColor = getTextColors();
     47 
     48         // Get the hover color
     49         Resources r = getResources();
     50         mHoverColor = r.getColor(R.color.info_target_hover_tint);
     51         mDrawable = (TransitionDrawable) getCurrentDrawable();
     52         if (null != mDrawable) {
     53             mDrawable.setCrossFadeEnabled(true);
     54         }
     55 
     56         // Remove the text in the Phone UI in landscape
     57         int orientation = getResources().getConfiguration().orientation;
     58         if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
     59             if (!LauncherAppState.getInstance().isScreenLarge()) {
     60                 setText("");
     61             }
     62         }
     63     }
     64 
     65     @Override
     66     public boolean acceptDrop(DragObject d) {
     67         // acceptDrop is called just before onDrop. We do the work here, rather than
     68         // in onDrop, because it allows us to reject the drop (by returning false)
     69         // so that the object being dragged isn't removed from the drag source.
     70         ComponentName componentName = null;
     71         if (d.dragInfo instanceof AppInfo) {
     72             componentName = ((AppInfo) d.dragInfo).componentName;
     73         } else if (d.dragInfo instanceof ShortcutInfo) {
     74             componentName = ((ShortcutInfo) d.dragInfo).intent.getComponent();
     75         } else if (d.dragInfo instanceof PendingAddItemInfo) {
     76             componentName = ((PendingAddItemInfo) d.dragInfo).componentName;
     77         }
     78         if (componentName != null) {
     79             mLauncher.startApplicationDetailsActivity(componentName);
     80         }
     81 
     82         // There is no post-drop animation, so clean up the DragView now
     83         d.deferDragViewCleanupPostAnimation = false;
     84         return false;
     85     }
     86 
     87     @Override
     88     public void onDragStart(DragSource source, Object info, int dragAction) {
     89         boolean isVisible = true;
     90 
     91         // Hide this button unless we are dragging something from AllApps
     92         if (!source.supportsAppInfoDropTarget()) {
     93             isVisible = false;
     94         }
     95 
     96         mActive = isVisible;
     97         mDrawable.resetTransition();
     98         setTextColor(mOriginalTextColor);
     99         ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
    100     }
    101 
    102     @Override
    103     public void onDragEnd() {
    104         super.onDragEnd();
    105         mActive = false;
    106     }
    107 
    108     public void onDragEnter(DragObject d) {
    109         super.onDragEnter(d);
    110 
    111         mDrawable.startTransition(mTransitionDuration);
    112         setTextColor(mHoverColor);
    113     }
    114 
    115     public void onDragExit(DragObject d) {
    116         super.onDragExit(d);
    117 
    118         if (!d.dragComplete) {
    119             mDrawable.resetTransition();
    120             setTextColor(mOriginalTextColor);
    121         }
    122     }
    123 }
    124