summaryrefslogtreecommitdiffstats
path: root/src/com/android/dialer/list/TileInteractionTeaserView.java
blob: 6e70fd1865381ebb87083011ce7e022da2e8a1c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.android.dialer.list;

import android.animation.Animator;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.dialer.DialtactsActivity;
import com.android.dialer.R;

/**
 * A teaser to introduce people to the contact photo check boxes
 */
public class TileInteractionTeaserView extends FrameLayout {
    private static int sShrinkAnimationDuration;

    private static final String KEY_TILE_INTERACTION_TEASER_SHOWN =
            "key_tile_interaction_teaser_shown";

    private boolean mNeedLayout;
    private int mTextTop;
    private int mAnimatedHeight = -1;

    private PhoneFavoriteMergedAdapter mAdapter;

    public TileInteractionTeaserView(final Context context) {
        this(context, null);
    }

    public TileInteractionTeaserView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        final Resources resources = context.getResources();

        mNeedLayout = true;
        sShrinkAnimationDuration = resources.getInteger(R.integer.escape_animation_duration);
    }

    @Override
    protected void onFinishInflate() {
        findViewById(R.id.dismiss_button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startDestroyAnimation();
            }
        });
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        final TextView text = (TextView) findViewById(R.id.text);
        final ImageView arrow = (ImageView) findViewById(R.id.arrow);

        // We post to avoid calling layout within layout
        arrow.post(new Runnable() {
            @Override
            public void run() {

                // The text top is changed when we move the arrow, so we need to
                // do multiple passes
                int textTop = text.getTop();
                if (mNeedLayout || textTop != mTextTop) {
                    mNeedLayout = false;
                    mTextTop = textTop;

                    final int lineHeight = text.getLineHeight();
                    final LinearLayout.LayoutParams arrowParams = (LinearLayout.LayoutParams) arrow
                            .getLayoutParams();
                    arrowParams.topMargin = mTextTop + lineHeight / 2;
                    arrow.setLayoutParams(arrowParams);
                }
                arrow.setVisibility(View.VISIBLE);
            }
        });
    }

    public boolean getShouldDisplayInList() {
        final SharedPreferences prefs = getContext().getSharedPreferences(
                DialtactsActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
        return prefs.getBoolean(KEY_TILE_INTERACTION_TEASER_SHOWN, true);
    }

    public void setAdapter(PhoneFavoriteMergedAdapter adapter) {
        mAdapter = adapter;
    }

    private void startDestroyAnimation() {
        final int start = getHeight();
        final int end = 0;
        mAnimatedHeight = start;
        Log.v("Interaction", "Start from" + start);

        ValueAnimator heightAnimator = ValueAnimator.ofInt(start, end);
        heightAnimator.setDuration(sShrinkAnimationDuration);
        heightAnimator.setInterpolator(new DecelerateInterpolator(2.0f));
        heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                mAnimatedHeight = (Integer) animation.getAnimatedValue();
                requestLayout();
            }
        });
        heightAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                setVisibility(GONE);
                setDismissed();
                if (mAdapter != null) {
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onAnimationCancel(Animator animator) {
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
            }
        });

        heightAnimator.start();
    }

    private void setDismissed() {
        final SharedPreferences prefs = getContext().getSharedPreferences(
                DialtactsActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
        prefs.edit().putBoolean(KEY_TILE_INTERACTION_TEASER_SHOWN, false).apply();
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        if (mAnimatedHeight == -1) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        } else {
            setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mAnimatedHeight);
        }
    }
}