1f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu/*
2f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu * Copyright (C) 2012 The Android Open Source Project
3f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu *
4f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu * Licensed under the Apache License, Version 2.0 (the "License");
5f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu * you may not use this file except in compliance with the License.
6f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu * You may obtain a copy of the License at
7f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu *
8f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu *      http://www.apache.org/licenses/LICENSE-2.0
9f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu *
10f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu * Unless required by applicable law or agreed to in writing, software
11f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu * distributed under the License is distributed on an "AS IS" BASIS,
12f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu * See the License for the specific language governing permissions and
14f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu * limitations under the License.
15f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu */
16f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
17f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescupackage com.android.gallery3d.app;
18f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
19f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport android.content.Context;
20f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport android.view.LayoutInflater;
21f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport android.view.View;
22f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport android.view.View.OnClickListener;
23f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport android.view.ViewGroup;
24f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport android.view.animation.AlphaAnimation;
25f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport android.view.animation.Animation;
26f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport android.widget.RelativeLayout;
27f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
28f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport com.android.gallery3d.R;
29f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
30f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport java.util.HashMap;
31f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescuimport java.util.Map;
32f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
33f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescupublic class PhotoPageBottomControls implements OnClickListener {
34f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    public interface Delegate {
35f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        public boolean canDisplayBottomControls();
36a680a0c32ae3321c632288b36083f2782af2c55bDoris Liu        public boolean canDisplayBottomControl(int control);
37f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        public void onBottomControlClicked(int control);
384b4dbd225685502f4249c2bf25bf74f7ce526645George Mount        public void refreshBottomControlsWhenReady();
39f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    }
40f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
41f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private Delegate mDelegate;
42f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private ViewGroup mParentLayout;
43f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private ViewGroup mContainer;
44f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
45f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private boolean mContainerVisible = false;
46f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private Map<View, Boolean> mControlsVisible = new HashMap<View, Boolean>();
47f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
48f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private Animation mContainerAnimIn = new AlphaAnimation(0f, 1f);
49f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private Animation mContainerAnimOut = new AlphaAnimation(1f, 0f);
50f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private static final int CONTAINER_ANIM_DURATION_MS = 200;
51f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
52f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private static final int CONTROL_ANIM_DURATION_MS = 150;
53f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private static Animation getControlAnimForVisibility(boolean visible) {
540aaf8fb8b86cbf6353937c12f377ad524661c553Bobby Georgescu        Animation anim = visible ? new AlphaAnimation(0f, 1f)
550aaf8fb8b86cbf6353937c12f377ad524661c553Bobby Georgescu                : new AlphaAnimation(1f, 0f);
56f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        anim.setDuration(CONTROL_ANIM_DURATION_MS);
57f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        return anim;
58f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    }
59f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
60f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    public PhotoPageBottomControls(Delegate delegate, Context context, RelativeLayout layout) {
61f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mDelegate = delegate;
62f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mParentLayout = layout;
63f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
64f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        LayoutInflater inflater = (LayoutInflater) context
65f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
66f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainer = (ViewGroup) inflater
67f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu                .inflate(R.layout.photopage_bottom_controls, mParentLayout, false);
68f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mParentLayout.addView(mContainer);
69f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
70f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        for (int i = mContainer.getChildCount() - 1; i >= 0; i--) {
71f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            View child = mContainer.getChildAt(i);
72f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            child.setOnClickListener(this);
73f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            mControlsVisible.put(child, false);
74f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        }
75f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
76f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainerAnimIn.setDuration(CONTAINER_ANIM_DURATION_MS);
77f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainerAnimOut.setDuration(CONTAINER_ANIM_DURATION_MS);
78f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
794b4dbd225685502f4249c2bf25bf74f7ce526645George Mount        mDelegate.refreshBottomControlsWhenReady();
80f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    }
81f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
82f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private void hide() {
83f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainer.clearAnimation();
84f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainerAnimOut.reset();
85f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainer.startAnimation(mContainerAnimOut);
86f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainer.setVisibility(View.INVISIBLE);
87f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    }
88f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
89f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    private void show() {
90f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainer.clearAnimation();
91f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainerAnimIn.reset();
92f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainer.startAnimation(mContainerAnimIn);
93f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mContainer.setVisibility(View.VISIBLE);
94f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    }
95f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
96a680a0c32ae3321c632288b36083f2782af2c55bDoris Liu    public void refresh() {
97f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        boolean visible = mDelegate.canDisplayBottomControls();
98f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        boolean containerVisibilityChanged = (visible != mContainerVisible);
99f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        if (containerVisibilityChanged) {
100f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            if (visible) {
101f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu                show();
102f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            } else {
103f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu                hide();
104f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            }
105f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            mContainerVisible = visible;
106f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        }
107f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        if (!mContainerVisible) {
108f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            return;
109f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        }
110f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        for (View control : mControlsVisible.keySet()) {
111f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            Boolean prevVisibility = mControlsVisible.get(control);
112a680a0c32ae3321c632288b36083f2782af2c55bDoris Liu            boolean curVisibility = mDelegate.canDisplayBottomControl(control.getId());
113f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            if (prevVisibility.booleanValue() != curVisibility) {
114f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu                if (!containerVisibilityChanged) {
115f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu                    control.clearAnimation();
116f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu                    control.startAnimation(getControlAnimForVisibility(curVisibility));
117f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu                }
118f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu                control.setVisibility(curVisibility ? View.VISIBLE : View.INVISIBLE);
119f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu                mControlsVisible.put(control, curVisibility);
120f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            }
121f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        }
122c4384338502ed85618095f0672b5926659acab20Doris Liu        // Force a layout change
123c4384338502ed85618095f0672b5926659acab20Doris Liu        mContainer.requestLayout(); // Kick framework to draw the control.
124f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    }
125f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
126f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    public void cleanup() {
127f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mParentLayout.removeView(mContainer);
128f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        mControlsVisible.clear();
129f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    }
130f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu
131f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    @Override
132f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    public void onClick(View view) {
133f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        if (mContainerVisible && mControlsVisible.get(view).booleanValue()) {
134f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu            mDelegate.onBottomControlClicked(view.getId());
135f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu        }
136f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu    }
137f44499dda32783cc74643723ed01ce636e81b186Bobby Georgescu}
138