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