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;
236478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liuimport android.content.res.Configuration;
24f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolbimport android.graphics.Canvas;
25f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolbimport android.graphics.drawable.Drawable;
26d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.util.AttributeSet;
27a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport android.view.LayoutInflater;
285da800f7a1427066a310a41964d780924c1f1a10Michael Kolbimport android.view.MotionEvent;
29d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.View;
30a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport android.view.View.OnClickListener;
31a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport android.view.View.OnTouchListener;
32d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.view.ViewGroup;
33d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolbimport android.widget.LinearLayout;
34d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
35a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbimport com.android.camera.R;
36ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reckimport com.android.gallery3d.common.ApiHelper;
37d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
38a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolbpublic class CameraSwitcher extends RotateImageView
39a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        implements OnClickListener, OnTouchListener {
40d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
41a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private static final String TAG = "CAM_Switcher";
426478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu    private static final int SWITCHER_POPUP_ANIM_DURATION = 200;
43d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
44d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public interface CameraSwitchListener {
45d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        public void onCameraSelected(int i);
46561af39348f34db4d67bc954536d6f6cbf296054John Reck        public void onShowSwitcherPopup();
47d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
48d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
49d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private CameraSwitchListener mListener;
50a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private int mCurrentIndex;
5154424d9c722b6d40be2eb50ae24e6d8dd17ddcabAngus Kong    private int[] mModuleIds;
52a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private int[] mDrawIds;
53a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private int mItemSize;
54a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private View mPopup;
55a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private View mParent;
56ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean mShowingPopup;
57ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean mNeedsAnimationSetup;
58f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    private Drawable mIndicator;
59ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
606478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu    private float mTranslationX = 0;
616478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu    private float mTranslationY = 0;
626478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu
63ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private AnimatorListener mHideAnimationListener;
64ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private AnimatorListener mShowAnimationListener;
65d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
66d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public CameraSwitcher(Context context) {
67d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        super(context);
68d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        init(context);
69d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
70d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
71d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public CameraSwitcher(Context context, AttributeSet attrs) {
72d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        super(context, attrs);
73d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        init(context);
74d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
75d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
76d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    private void init(Context context) {
77a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mItemSize = context.getResources().getDimensionPixelSize(R.dimen.switcher_size);
78a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        setOnClickListener(this);
79f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        mIndicator = context.getResources().getDrawable(R.drawable.ic_switcher_menu_indicator);
80d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
81d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
8254424d9c722b6d40be2eb50ae24e6d8dd17ddcabAngus Kong    public void setIds(int[] moduleids, int[] drawids) {
83a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mDrawIds = drawids;
8454424d9c722b6d40be2eb50ae24e6d8dd17ddcabAngus Kong        mModuleIds = moduleids;
85df0dca9dd26b95a424a5712f16a43413a3e8ebe1Michael Kolb    }
86df0dca9dd26b95a424a5712f16a43413a3e8ebe1Michael Kolb
87a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void setCurrentIndex(int i) {
88a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mCurrentIndex = i;
89a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        setImageResource(mDrawIds[i]);
90d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
91d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
92d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    public void setSwitchListener(CameraSwitchListener l) {
93d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        mListener = l;
94d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
95d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
96a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    @Override
97a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void onClick(View v) {
98a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        showSwitcher();
99561af39348f34db4d67bc954536d6f6cbf296054John Reck        mListener.onShowSwitcherPopup();
1001da18852b3cddf3e2f288636a9ef9d8d002f496bJohn Reck    }
1011da18852b3cddf3e2f288636a9ef9d8d002f496bJohn Reck
102a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void onCameraSelected(int ix) {
103a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        hidePopup();
104a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if ((ix != mCurrentIndex) && (mListener != null)) {
105a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            setCurrentIndex(ix);
10654424d9c722b6d40be2eb50ae24e6d8dd17ddcabAngus Kong            mListener.onCameraSelected(mModuleIds[ix]);
1071da18852b3cddf3e2f288636a9ef9d8d002f496bJohn Reck        }
108d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
109d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
110f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    @Override
111f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    protected void onDraw(Canvas canvas) {
112f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        super.onDraw(canvas);
113f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        mIndicator.setBounds(getDrawable().getBounds());
114f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb        mIndicator.draw(canvas);
115f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb    }
116f16332047e1c7627c20cdf847520a51f2a89d7b0Michael Kolb
117a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void initPopup() {
118a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mParent = LayoutInflater.from(getContext()).inflate(R.layout.switcher_popup,
119a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                (ViewGroup) getParent());
120a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        LinearLayout content = (LinearLayout) mParent.findViewById(R.id.content);
121a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mPopup = content;
122ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setVisibility(View.INVISIBLE);
123ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mNeedsAnimationSetup = true;
124b1be74d6833cd55429cc5c34885eddf613d08302Doris Liu        for (int i = mDrawIds.length - 1; i >= 0; i--) {
125a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            RotateImageView item = new RotateImageView(getContext());
126a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            item.setImageResource(mDrawIds[i]);
127da04f24a205d92c72c32d7768f0d3b4b1f93512dMichael Kolb            item.setBackgroundResource(R.drawable.bg_pressed);
128a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            final int index = i;
129a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            item.setOnClickListener(new OnClickListener() {
130a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                @Override
131a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                public void onClick(View v) {
132a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                    onCameraSelected(index);
133a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb                }
134a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            });
135d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu            switch (mDrawIds[i]) {
136d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                case R.drawable.ic_switch_camera:
137d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
138d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                            R.string.accessibility_switch_to_camera));
139d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
140d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                case R.drawable.ic_switch_video:
141d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
142d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                            R.string.accessibility_switch_to_video));
143d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
144d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                case R.drawable.ic_switch_pan:
145d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
146d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                            R.string.accessibility_switch_to_panorama));
147d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
148ebf2df0f2696dd287a8235cf4e7c1cac0103a3deMichael Kolb                case R.drawable.ic_switch_photosphere:
149d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    item.setContentDescription(getContext().getResources().getString(
1501ae07ad4e5594fea931db5e9cc9acc24f3a4b7bbDoris Liu                            R.string.accessibility_switch_to_new_panorama));
151d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
152d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                default:
153d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu                    break;
154d9a36946b24f4ccd697aa0139aa978c962eb95e1Doris Liu            }
155a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            content.addView(item, new LinearLayout.LayoutParams(mItemSize, mItemSize));
156d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
157d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
158d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
159a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public boolean showsPopup() {
160ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return mShowingPopup;
161d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
162d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
163a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public boolean isInsidePopup(MotionEvent evt) {
164ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!showsPopup()) return false;
165ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return evt.getX() >= mPopup.getLeft()
166ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                && evt.getX() < mPopup.getRight()
167ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                && evt.getY() >= mPopup.getTop()
168ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                && evt.getY() < mPopup.getBottom();
16978711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu    }
17078711c990e57ce7c6228c5446cfb6902f2a47cb6Doris Liu
171a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void hidePopup() {
172ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mShowingPopup = false;
173ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        setVisibility(View.VISIBLE);
174ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mPopup != null && !animateHidePopup()) {
175ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            mPopup.setVisibility(View.INVISIBLE);
176d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
177a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mParent.setOnTouchListener(null);
178d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
179d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
180a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    private void showSwitcher() {
181ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mShowingPopup = true;
182a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if (mPopup == null) {
183a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            initPopup();
184a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        }
185a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mPopup.setVisibility(View.VISIBLE);
186ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!animateShowPopup()) {
187ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            setVisibility(View.INVISIBLE);
188ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
189a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        mParent.setOnTouchListener(this);
1908566e6d534634965a53b536571008bad882b969dMichael Kolb    }
1918566e6d534634965a53b536571008bad882b969dMichael Kolb
1928566e6d534634965a53b536571008bad882b969dMichael Kolb    @Override
193a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public boolean onTouch(View v, MotionEvent event) {
194b8317ef78fa41224a0a8d43431c3eac8e10772f1George Mount        closePopup();
195b8317ef78fa41224a0a8d43431c3eac8e10772f1George Mount        return true;
196b8317ef78fa41224a0a8d43431c3eac8e10772f1George Mount    }
197b8317ef78fa41224a0a8d43431c3eac8e10772f1George Mount
198b8317ef78fa41224a0a8d43431c3eac8e10772f1George Mount    public void closePopup() {
199a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if (showsPopup()) {
200a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            hidePopup();
201d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
202d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
203d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb
204a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    @Override
205a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb    public void setOrientation(int degree, boolean animate) {
206a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        super.setOrientation(degree, animate);
207a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        ViewGroup content = (ViewGroup) mPopup;
208a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        if (content == null) return;
209a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb        for (int i = 0; i < content.getChildCount(); i++) {
210a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            RotateImageView iv = (RotateImageView) content.getChildAt(i);
211a5e23d9eb2bc96e7da4c021df2e11c1c43d379cdMichael Kolb            iv.setOrientation(degree, animate);
212d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb        }
213d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb    }
214ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
2156478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu    private void updateInitialTranslations() {
2166478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        if (getResources().getConfiguration().orientation
2176478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                == Configuration.ORIENTATION_PORTRAIT) {
21854424d9c722b6d40be2eb50ae24e6d8dd17ddcabAngus Kong            mTranslationX = -getWidth() / 2;
2196478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu            mTranslationY = getHeight();
2206478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        } else {
2216478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu            mTranslationX = getWidth();
2226478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu            mTranslationY = getHeight() / 2;
2236478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        }
2246478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu    }
225ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private void popupAnimationSetup() {
226ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!ApiHelper.HAS_VIEW_PROPERTY_ANIMATOR) {
227ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            return;
228ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
2296478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        updateInitialTranslations();
230ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setScaleX(0.3f);
231ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.setScaleY(0.3f);
2326478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        mPopup.setTranslationX(mTranslationX);
2336478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        mPopup.setTranslationY(mTranslationY);
234ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mNeedsAnimationSetup = false;
235ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    }
236ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
237ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean animateHidePopup() {
238ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!ApiHelper.HAS_VIEW_PROPERTY_ANIMATOR) {
239ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            return false;
240ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
241ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mHideAnimationListener == null) {
242ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            mHideAnimationListener = new AnimatorListenerAdapter() {
243ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                @Override
244ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                public void onAnimationEnd(Animator animation) {
245ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    // Verify that we weren't canceled
246ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    if (!showsPopup()) {
247ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                        mPopup.setVisibility(View.INVISIBLE);
248ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    }
249ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                }
250ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            };
251ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
252ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.animate()
253ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .alpha(0f)
254ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .scaleX(0.3f).scaleY(0.3f)
2556478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                .translationX(mTranslationX)
2566478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                .translationY(mTranslationY)
2576478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                .setDuration(SWITCHER_POPUP_ANIM_DURATION)
258ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .setListener(mHideAnimationListener);
2596478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        animate().alpha(1f).setDuration(SWITCHER_POPUP_ANIM_DURATION)
2606478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                .setListener(null);
261ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return true;
262ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    }
263ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck
264ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    private boolean animateShowPopup() {
265ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (!ApiHelper.HAS_VIEW_PROPERTY_ANIMATOR) {
266ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            return false;
267ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
268ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mNeedsAnimationSetup) {
269ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            popupAnimationSetup();
270ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
271ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        if (mShowAnimationListener == null) {
272ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            mShowAnimationListener = new AnimatorListenerAdapter() {
273ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                @Override
274ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                public void onAnimationEnd(Animator animation) {
275ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    // Verify that we weren't canceled
276ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    if (showsPopup()) {
277ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                        setVisibility(View.INVISIBLE);
278ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                    }
279ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                }
280ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck            };
281ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        }
282ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        mPopup.animate()
283ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .alpha(1f)
284ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .scaleX(1f).scaleY(1f)
285ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .translationX(0)
286ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .translationY(0)
2876478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu                .setDuration(SWITCHER_POPUP_ANIM_DURATION)
288ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .setListener(null);
2896478e5e71f631781e5c0e3f30560da4ca74901bbDoris Liu        animate().alpha(0f).setDuration(SWITCHER_POPUP_ANIM_DURATION)
290ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck                .setListener(mShowAnimationListener);
291ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck        return true;
292ecf756f3d43e50a27a09c7b8df30b250ceec0279John Reck    }
293d3a252bb71d119cbaeff55aeaa27dd1f8adadba2Michael Kolb}
294