Home | History | Annotate | Download | only in list
      1 package com.android.dialer.list;
      2 
      3 import android.animation.Animator;
      4 
      5 import android.animation.ValueAnimator;
      6 import android.content.Context;
      7 import android.content.SharedPreferences;
      8 import android.content.res.Resources;
      9 import android.util.AttributeSet;
     10 import android.util.Log;
     11 import android.view.View;
     12 import android.view.animation.DecelerateInterpolator;
     13 import android.widget.FrameLayout;
     14 import android.widget.ImageView;
     15 import android.widget.LinearLayout;
     16 import android.widget.TextView;
     17 
     18 import com.android.dialer.DialtactsActivity;
     19 import com.android.dialer.R;
     20 
     21 /**
     22  * A teaser to introduce people to the contact photo check boxes
     23  */
     24 public class TileInteractionTeaserView extends FrameLayout {
     25     private static int sShrinkAnimationDuration;
     26 
     27     private static final String KEY_TILE_INTERACTION_TEASER_SHOWN =
     28             "key_tile_interaction_teaser_shown";
     29 
     30     private boolean mNeedLayout;
     31     private int mTextTop;
     32     private int mAnimatedHeight = -1;
     33 
     34     private PhoneFavoriteMergedAdapter mAdapter;
     35 
     36     public TileInteractionTeaserView(final Context context) {
     37         this(context, null);
     38     }
     39 
     40     public TileInteractionTeaserView(final Context context, final AttributeSet attrs) {
     41         super(context, attrs);
     42         final Resources resources = context.getResources();
     43 
     44         mNeedLayout = true;
     45         sShrinkAnimationDuration = resources.getInteger(R.integer.escape_animation_duration);
     46     }
     47 
     48     @Override
     49     protected void onFinishInflate() {
     50         findViewById(R.id.dismiss_button).setOnClickListener(new OnClickListener() {
     51             @Override
     52             public void onClick(View v) {
     53                 startDestroyAnimation();
     54             }
     55         });
     56     }
     57 
     58     @Override
     59     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
     60         super.onLayout(changed, left, top, right, bottom);
     61 
     62         final TextView text = (TextView) findViewById(R.id.text);
     63         final ImageView arrow = (ImageView) findViewById(R.id.arrow);
     64 
     65         // We post to avoid calling layout within layout
     66         arrow.post(new Runnable() {
     67             @Override
     68             public void run() {
     69 
     70                 // The text top is changed when we move the arrow, so we need to
     71                 // do multiple passes
     72                 int textTop = text.getTop();
     73                 if (mNeedLayout || textTop != mTextTop) {
     74                     mNeedLayout = false;
     75                     mTextTop = textTop;
     76 
     77                     final int lineHeight = text.getLineHeight();
     78                     final LinearLayout.LayoutParams arrowParams = (LinearLayout.LayoutParams) arrow
     79                             .getLayoutParams();
     80                     arrowParams.topMargin = mTextTop + lineHeight / 2;
     81                     arrow.setLayoutParams(arrowParams);
     82                 }
     83                 arrow.setVisibility(View.VISIBLE);
     84             }
     85         });
     86     }
     87 
     88     public boolean getShouldDisplayInList() {
     89         final SharedPreferences prefs = getContext().getSharedPreferences(
     90                 DialtactsActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
     91         return prefs.getBoolean(KEY_TILE_INTERACTION_TEASER_SHOWN, true);
     92     }
     93 
     94     public void setAdapter(PhoneFavoriteMergedAdapter adapter) {
     95         mAdapter = adapter;
     96     }
     97 
     98     private void startDestroyAnimation() {
     99         final int start = getHeight();
    100         final int end = 0;
    101         mAnimatedHeight = start;
    102         Log.v("Interaction", "Start from" + start);
    103 
    104         ValueAnimator heightAnimator = ValueAnimator.ofInt(start, end);
    105         heightAnimator.setDuration(sShrinkAnimationDuration);
    106         heightAnimator.setInterpolator(new DecelerateInterpolator(2.0f));
    107         heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    108             public void onAnimationUpdate(ValueAnimator animation) {
    109                 mAnimatedHeight = (Integer) animation.getAnimatedValue();
    110                 requestLayout();
    111             }
    112         });
    113         heightAnimator.addListener(new Animator.AnimatorListener() {
    114             @Override
    115             public void onAnimationStart(Animator animator) {
    116             }
    117 
    118             @Override
    119             public void onAnimationEnd(Animator animator) {
    120                 setVisibility(GONE);
    121                 setDismissed();
    122                 if (mAdapter != null) {
    123                     mAdapter.notifyDataSetChanged();
    124                 }
    125             }
    126 
    127             @Override
    128             public void onAnimationCancel(Animator animator) {
    129             }
    130 
    131             @Override
    132             public void onAnimationRepeat(Animator animator) {
    133             }
    134         });
    135 
    136         heightAnimator.start();
    137     }
    138 
    139     private void setDismissed() {
    140         final SharedPreferences prefs = getContext().getSharedPreferences(
    141                 DialtactsActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
    142         prefs.edit().putBoolean(KEY_TILE_INTERACTION_TEASER_SHOWN, false).apply();
    143     }
    144 
    145     @Override
    146     protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    147         if (mAnimatedHeight == -1) {
    148             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    149         } else {
    150             setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mAnimatedHeight);
    151         }
    152     }
    153 }
    154