CameraSwitcher.java revision f16332047e1c7627c20cdf847520a51f2a89d7b0
1d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb/*
2d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * Copyright (C) 2012 The Android Open Source Project
3d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *
4d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * Licensed under the Apache License, Version 2.0 (the "License");
5d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * you may not use this file except in compliance with the License.
6d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * You may obtain a copy of the License at
7d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *
8d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *      http://www.apache.org/licenses/LICENSE-2.0
9d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb *
10d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * Unless required by applicable law or agreed to in writing, software
11d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * distributed under the License is distributed on an "AS IS" BASIS,
12d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * See the License for the specific language governing permissions and
14d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb * limitations under the License.
15d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb */
16d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
17d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbpackage com.android.camera.ui;
18d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
19ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reckimport android.animation.Animator;
20ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reckimport android.animation.Animator.AnimatorListener;
21ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reckimport android.animation.AnimatorListenerAdapter;
22d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.content.Context;
23f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolbimport android.graphics.Canvas;
24f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolbimport android.graphics.drawable.Drawable;
25d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.util.AttributeSet;
26a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport android.view.LayoutInflater;
275da800f7a1427066a310a41964d780924c1f1a10Michael Kolbimport android.view.MotionEvent;
28d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.View;
29a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport android.view.View.OnClickListener;
30a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport android.view.View.OnTouchListener;
31d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.ViewGroup;
32d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.widget.LinearLayout;
33d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
34a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport com.android.camera.R;
35ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reckimport com.android.gallery3d.common.ApiHelper;
36d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
37a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbpublic class CameraSwitcher extends RotateImageView
38a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        implements OnClickListener, OnTouchListener {
39d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
40a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private static final String TAG = "CAM_Switcher";
41d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
42d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public interface CameraSwitchListener {
43d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onCameraSelected(int i);
44561af39348f34db4d67bc954536d6f6cbf296054John Reck        public void onShowSwitcherPopup();
45d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
46d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
47d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private CameraSwitchListener mListener;
48a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private int mCurrentIndex;
49a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private int[] mDrawIds;
50a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private int mItemSize;
51a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private View mPopup;
52a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private View mParent;
53ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean mShowingPopup;
54ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean mNeedsAnimationSetup;
55f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    private Drawable mIndicator;
56ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
57ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private AnimatorListener mHideAnimationListener;
58ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private AnimatorListener mShowAnimationListener;
59d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
60d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public CameraSwitcher(Context context) {
61d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        super(context);
62d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        init(context);
63d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
64d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
65d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public CameraSwitcher(Context context, AttributeSet attrs) {
66d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        super(context, attrs);
67d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        init(context);
68d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
69d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
70d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void init(Context context) {
71a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mItemSize = context.getResources().getDimensionPixelSize(R.dimen.switcher_size);
72a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        setOnClickListener(this);
73f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        mIndicator = context.getResources().getDrawable(R.drawable.ic_switcher_menu_indicator);
74d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
75d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
76a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void setDrawIds(int[] drawids) {
77a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mDrawIds = drawids;
78df0dca9dd26b95a424a5712f16a43413a3e8ebe1Michael Kolb    }
79df0dca9dd26b95a424a5712f16a43413a3e8ebe1Michael Kolb
80a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void setCurrentIndex(int i) {
81a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mCurrentIndex = i;
82a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        setImageResource(mDrawIds[i]);
83d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
84d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
85d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void setSwitchListener(CameraSwitchListener l) {
86d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mListener = l;
87d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
88d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
89a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    @Override
90a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void onClick(View v) {
91a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        showSwitcher();
92561af39348f34db4d67bc954536d6f6cbf296054John Reck        mListener.onShowSwitcherPopup();
931da18852b3cddf3e2f288636a9ef9d8d002f496bJohn Reck    }
941da18852b3cddf3e2f288636a9ef9d8d002f496bJohn Reck
95a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void onCameraSelected(int ix) {
96a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        hidePopup();
97a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if ((ix != mCurrentIndex) && (mListener != null)) {
98a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            setCurrentIndex(ix);
99a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            mListener.onCameraSelected(ix);
1001da18852b3cddf3e2f288636a9ef9d8d002f496bJohn Reck        }
101d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
102d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
103f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    @Override
104f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    protected void onDraw(Canvas canvas) {
105f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        super.onDraw(canvas);
106f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        mIndicator.setBounds(getDrawable().getBounds());
107f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        mIndicator.draw(canvas);
108f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    }
109f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb
110a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void initPopup() {
111a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mParent = LayoutInflater.from(getContext()).inflate(R.layout.switcher_popup,
112a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                (ViewGroup) getParent());
113a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        LinearLayout content = (LinearLayout) mParent.findViewById(R.id.content);
114a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mPopup = content;
115ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setVisibility(View.INVISIBLE);
116ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mNeedsAnimationSetup = true;
117b1be74d6833cd55429cc5c34885eddf613d08302Doris Liu        for (int i = mDrawIds.length - 1; i >= 0; i--) {
118a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            RotateImageView item = new RotateImageView(getContext());
119a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            item.setImageResource(mDrawIds[i]);
120da04f24a205d92c72c32d7768f0d3b4b1f93512dMichael Kolb            item.setBackgroundResource(R.drawable.bg_pressed);
121a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            final int index = i;
122a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            item.setOnClickListener(new OnClickListener() {
123a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                @Override
124a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                public void onClick(View v) {
125a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                    onCameraSelected(index);
126a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                }
127a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            });
128d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu            switch (mDrawIds[i]) {
129d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                case R.drawable.ic_switch_camera:
130d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
131d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                            R.string.accessibility_switch_to_camera));
132d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
133d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                case R.drawable.ic_switch_video:
134d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
135d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                            R.string.accessibility_switch_to_video));
136d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
137d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                case R.drawable.ic_switch_pan:
138d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
139d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                            R.string.accessibility_switch_to_panorama));
140d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
141d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                case com.android.gallery3d.R.drawable.ic_menu_photosphere:
142d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
14321bb06827d9fec3554ee97c8a2c7b2f00dda7778Doris Liu                            R.string.accessibility_switch_to_photosphere));
144d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
145d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                default:
146d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
147d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu            }
148a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            content.addView(item, new LinearLayout.LayoutParams(mItemSize, mItemSize));
149d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
150d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
151d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
152a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public boolean showsPopup() {
153ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return mShowingPopup;
154d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
155d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
156a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public boolean isInsidePopup(MotionEvent evt) {
157ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!showsPopup()) return false;
158ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return evt.getX() >= mPopup.getLeft()
159ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                && evt.getX() < mPopup.getRight()
160ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                && evt.getY() >= mPopup.getTop()
161ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                && evt.getY() < mPopup.getBottom();
16278711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu    }
16378711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu
164a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void hidePopup() {
165ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mShowingPopup = false;
166ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        setVisibility(View.VISIBLE);
167ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mPopup != null && !animateHidePopup()) {
168ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            mPopup.setVisibility(View.INVISIBLE);
169d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
170a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mParent.setOnTouchListener(null);
171d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
172d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
173a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void showSwitcher() {
174ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mShowingPopup = true;
175a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if (mPopup == null) {
176a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            initPopup();
177a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        }
178a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mPopup.setVisibility(View.VISIBLE);
179ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!animateShowPopup()) {
180ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            setVisibility(View.INVISIBLE);
181ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
182a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mParent.setOnTouchListener(this);
1838566e6d534634965a53b536571008bad882b969dMichael Kolb    }
1848566e6d534634965a53b536571008bad882b969dMichael Kolb
1858566e6d534634965a53b536571008bad882b969dMichael Kolb    @Override
186a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public boolean onTouch(View v, MotionEvent event) {
187a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if (showsPopup()) {
188a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            hidePopup();
189d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
190a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        return true;
191d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
192d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
193a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    @Override
194a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void setOrientation(int degree, boolean animate) {
195a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        super.setOrientation(degree, animate);
196a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        ViewGroup content = (ViewGroup) mPopup;
197a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if (content == null) return;
198a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        for (int i = 0; i < content.getChildCount(); i++) {
199a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            RotateImageView iv = (RotateImageView) content.getChildAt(i);
200a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            iv.setOrientation(degree, animate);
201d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
202d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
203ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
204ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private void popupAnimationSetup() {
205ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!ApiHelper.HAS_VIEW_PROPERTY_ANIMATOR) {
206ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            return;
207ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
208ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setScaleX(0.3f);
209ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setScaleY(0.3f);
210ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setTranslationX(-getWidth() / 2);
211ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setTranslationY(getHeight());
212ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mNeedsAnimationSetup = false;
213ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    }
214ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
215ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean animateHidePopup() {
216ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!ApiHelper.HAS_VIEW_PROPERTY_ANIMATOR) {
217ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            return false;
218ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
219ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mHideAnimationListener == null) {
220ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            mHideAnimationListener = new AnimatorListenerAdapter() {
221ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                @Override
222ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                public void onAnimationEnd(Animator animation) {
223ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    // Verify that we weren't canceled
224ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    if (!showsPopup()) {
225ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                        mPopup.setVisibility(View.INVISIBLE);
226ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    }
227ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                }
228ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            };
229ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
230ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.animate()
231ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .alpha(0f)
232ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .scaleX(0.3f).scaleY(0.3f)
233ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .translationX(-getWidth() / 2)
234ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .translationY(getHeight())
235ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .setListener(mHideAnimationListener);
236ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        animate().alpha(1f).setListener(null);
237ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return true;
238ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    }
239ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
240ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean animateShowPopup() {
241ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!ApiHelper.HAS_VIEW_PROPERTY_ANIMATOR) {
242ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            return false;
243ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
244ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mNeedsAnimationSetup) {
245ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            popupAnimationSetup();
246ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
247ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mShowAnimationListener == null) {
248ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            mShowAnimationListener = new AnimatorListenerAdapter() {
249ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                @Override
250ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                public void onAnimationEnd(Animator animation) {
251ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    // Verify that we weren't canceled
252ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    if (showsPopup()) {
253ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                        setVisibility(View.INVISIBLE);
254ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    }
255ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                }
256ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            };
257ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
258ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.animate()
259ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .alpha(1f)
260ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .scaleX(1f).scaleY(1f)
261ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .translationX(0)
262ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .translationY(0)
263ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .setListener(null);
264ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        animate().alpha(0f)
265ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .setListener(mShowAnimationListener);
266ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return true;
267ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    }
268d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb}
269