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.animation.Animator;
     20 import android.animation.AnimatorListenerAdapter;
     21 import android.animation.ObjectAnimator;
     22 import android.content.Context;
     23 import android.graphics.Rect;
     24 import android.graphics.drawable.Drawable;
     25 import android.util.AttributeSet;
     26 import android.view.View;
     27 import android.view.animation.AccelerateInterpolator;
     28 import android.widget.FrameLayout;
     29 
     30 import com.android.launcher.R;
     31 
     32 /*
     33  * Ths bar will manage the transition between the QSB search bar and the delete drop
     34  * targets so that each of the individual IconDropTargets don't have to.
     35  */
     36 public class SearchDropTargetBar extends FrameLayout implements DragController.DragListener {
     37 
     38     private static final int sTransitionInDuration = 200;
     39     private static final int sTransitionOutDuration = 175;
     40 
     41     private ObjectAnimator mDropTargetBarAnim;
     42     private ObjectAnimator mQSBSearchBarAnim;
     43     private static final AccelerateInterpolator sAccelerateInterpolator =
     44             new AccelerateInterpolator();
     45 
     46     private boolean mIsSearchBarHidden;
     47     private View mQSBSearchBar;
     48     private View mDropTargetBar;
     49     private ButtonDropTarget mInfoDropTarget;
     50     private ButtonDropTarget mDeleteDropTarget;
     51     private int mBarHeight;
     52     private boolean mDeferOnDragEnd = false;
     53 
     54     private Drawable mPreviousBackground;
     55     private boolean mEnableDropDownDropTargets;
     56 
     57     public SearchDropTargetBar(Context context, AttributeSet attrs) {
     58         this(context, attrs, 0);
     59     }
     60 
     61     public SearchDropTargetBar(Context context, AttributeSet attrs, int defStyle) {
     62         super(context, attrs, defStyle);
     63     }
     64 
     65     public void setup(Launcher launcher, DragController dragController) {
     66         dragController.addDragListener(this);
     67         dragController.addDragListener(mInfoDropTarget);
     68         dragController.addDragListener(mDeleteDropTarget);
     69         dragController.addDropTarget(mInfoDropTarget);
     70         dragController.addDropTarget(mDeleteDropTarget);
     71         dragController.setFlingToDeleteDropTarget(mDeleteDropTarget);
     72         mInfoDropTarget.setLauncher(launcher);
     73         mDeleteDropTarget.setLauncher(launcher);
     74     }
     75 
     76     private void prepareStartAnimation(View v) {
     77         // Enable the hw layers before the animation starts (will be disabled in the onAnimationEnd
     78         // callback below)
     79         v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
     80     }
     81 
     82     private void setupAnimation(ObjectAnimator anim, final View v) {
     83         anim.setInterpolator(sAccelerateInterpolator);
     84         anim.setDuration(sTransitionInDuration);
     85         anim.addListener(new AnimatorListenerAdapter() {
     86             @Override
     87             public void onAnimationEnd(Animator animation) {
     88                 v.setLayerType(View.LAYER_TYPE_NONE, null);
     89             }
     90         });
     91     }
     92 
     93     @Override
     94     protected void onFinishInflate() {
     95         super.onFinishInflate();
     96 
     97         // Get the individual components
     98         mQSBSearchBar = findViewById(R.id.qsb_search_bar);
     99         mDropTargetBar = findViewById(R.id.drag_target_bar);
    100         mInfoDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.info_target_text);
    101         mDeleteDropTarget = (ButtonDropTarget) mDropTargetBar.findViewById(R.id.delete_target_text);
    102         mBarHeight = getResources().getDimensionPixelSize(R.dimen.qsb_bar_height);
    103 
    104         mInfoDropTarget.setSearchDropTargetBar(this);
    105         mDeleteDropTarget.setSearchDropTargetBar(this);
    106 
    107         mEnableDropDownDropTargets =
    108             getResources().getBoolean(R.bool.config_useDropTargetDownTransition);
    109 
    110         // Create the various fade animations
    111         if (mEnableDropDownDropTargets) {
    112             mDropTargetBar.setTranslationY(-mBarHeight);
    113             mDropTargetBarAnim = LauncherAnimUtils.ofFloat(mDropTargetBar, "translationY",
    114                     -mBarHeight, 0f);
    115             mQSBSearchBarAnim = LauncherAnimUtils.ofFloat(mQSBSearchBar, "translationY", 0,
    116                     -mBarHeight);
    117         } else {
    118             mDropTargetBar.setAlpha(0f);
    119             mDropTargetBarAnim = LauncherAnimUtils.ofFloat(mDropTargetBar, "alpha", 0f, 1f);
    120             mQSBSearchBarAnim = LauncherAnimUtils.ofFloat(mQSBSearchBar, "alpha", 1f, 0f);
    121         }
    122         setupAnimation(mDropTargetBarAnim, mDropTargetBar);
    123         setupAnimation(mQSBSearchBarAnim, mQSBSearchBar);
    124     }
    125 
    126     public void finishAnimations() {
    127         prepareStartAnimation(mDropTargetBar);
    128         mDropTargetBarAnim.reverse();
    129         prepareStartAnimation(mQSBSearchBar);
    130         mQSBSearchBarAnim.reverse();
    131     }
    132 
    133     /*
    134      * Shows and hides the search bar.
    135      */
    136     public void showSearchBar(boolean animated) {
    137         if (!mIsSearchBarHidden) return;
    138         if (animated) {
    139             prepareStartAnimation(mQSBSearchBar);
    140             mQSBSearchBarAnim.reverse();
    141         } else {
    142             mQSBSearchBarAnim.cancel();
    143             if (mEnableDropDownDropTargets) {
    144                 mQSBSearchBar.setTranslationY(0);
    145             } else {
    146                 mQSBSearchBar.setAlpha(1f);
    147             }
    148         }
    149         mIsSearchBarHidden = false;
    150     }
    151     public void hideSearchBar(boolean animated) {
    152         if (mIsSearchBarHidden) return;
    153         if (animated) {
    154             prepareStartAnimation(mQSBSearchBar);
    155             mQSBSearchBarAnim.start();
    156         } else {
    157             mQSBSearchBarAnim.cancel();
    158             if (mEnableDropDownDropTargets) {
    159                 mQSBSearchBar.setTranslationY(-mBarHeight);
    160             } else {
    161                 mQSBSearchBar.setAlpha(0f);
    162             }
    163         }
    164         mIsSearchBarHidden = true;
    165     }
    166 
    167     /*
    168      * Gets various transition durations.
    169      */
    170     public int getTransitionInDuration() {
    171         return sTransitionInDuration;
    172     }
    173     public int getTransitionOutDuration() {
    174         return sTransitionOutDuration;
    175     }
    176 
    177     /*
    178      * DragController.DragListener implementation
    179      */
    180     @Override
    181     public void onDragStart(DragSource source, Object info, int dragAction) {
    182         // Animate out the QSB search bar, and animate in the drop target bar
    183         prepareStartAnimation(mDropTargetBar);
    184         mDropTargetBarAnim.start();
    185         if (!mIsSearchBarHidden) {
    186             prepareStartAnimation(mQSBSearchBar);
    187             mQSBSearchBarAnim.start();
    188         }
    189     }
    190 
    191     public void deferOnDragEnd() {
    192         mDeferOnDragEnd = true;
    193     }
    194 
    195     @Override
    196     public void onDragEnd() {
    197         if (!mDeferOnDragEnd) {
    198             // Restore the QSB search bar, and animate out the drop target bar
    199             prepareStartAnimation(mDropTargetBar);
    200             mDropTargetBarAnim.reverse();
    201             if (!mIsSearchBarHidden) {
    202                 prepareStartAnimation(mQSBSearchBar);
    203                 mQSBSearchBarAnim.reverse();
    204             }
    205         } else {
    206             mDeferOnDragEnd = false;
    207         }
    208     }
    209 
    210     public void onSearchPackagesChanged(boolean searchVisible, boolean voiceVisible) {
    211         if (mQSBSearchBar != null) {
    212             Drawable bg = mQSBSearchBar.getBackground();
    213             if (bg != null && (!searchVisible && !voiceVisible)) {
    214                 // Save the background and disable it
    215                 mPreviousBackground = bg;
    216                 mQSBSearchBar.setBackgroundResource(0);
    217             } else if (mPreviousBackground != null && (searchVisible || voiceVisible)) {
    218                 // Restore the background
    219                 mQSBSearchBar.setBackground(mPreviousBackground);
    220             }
    221         }
    222     }
    223 
    224     public Rect getSearchBarBounds() {
    225         if (mQSBSearchBar != null) {
    226             final int[] pos = new int[2];
    227             mQSBSearchBar.getLocationOnScreen(pos);
    228 
    229             final Rect rect = new Rect();
    230             rect.left = pos[0];
    231             rect.top = pos[1];
    232             rect.right = pos[0] + mQSBSearchBar.getWidth();
    233             rect.bottom = pos[1] + mQSBSearchBar.getHeight();
    234             return rect;
    235         } else {
    236             return null;
    237         }
    238     }
    239 }
    240