1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.photoeditor;
18
19import android.os.Handler;
20import android.os.Message;
21import android.view.View;
22import android.view.ViewGroup;
23import android.view.animation.Animation;
24import android.view.animation.AnimationUtils;
25
26import java.util.ArrayList;
27import java.util.List;
28
29/**
30 * Handler that controls idle/awake behaviors of toolbar's child views.
31 */
32class ToolbarIdleHandler {
33
34    private static final String IDLE_VIEW_TAG = "fadeOnIdle";
35    private static final int MAKE_IDLE = 1;
36    private static final int TIMEOUT_IDLE = 8000;
37
38    private final List<View> childViews = new ArrayList<View>();
39    private final Handler mainHandler;
40    private final Animation fadeIn;
41    private final Animation fadeOut;
42    private boolean idle;
43
44    /**
45     * Constructor should only be invoked after toolbar has done inflation and added all its child
46     * views; then its child views could be found by findViewById calls.
47     */
48    public ToolbarIdleHandler(ViewGroup toolbar) {
49        mainHandler = new Handler() {
50
51            @Override
52            public void handleMessage(Message msg) {
53                switch (msg.what) {
54                    case MAKE_IDLE:
55                        if (!idle) {
56                            idle = true;
57                            for (View view : childViews) {
58                                makeIdle(view);
59                            }
60                        }
61                        break;
62                }
63            }
64        };
65
66        fadeIn = AnimationUtils.loadAnimation(toolbar.getContext(), R.anim.fade_in);
67        fadeOut = AnimationUtils.loadAnimation(toolbar.getContext(), R.anim.fade_out);
68
69        for (int i = 0; i < toolbar.getChildCount(); i++) {
70            View view = toolbar.getChildAt(i);
71            String tag = (String) view.getTag();
72            if ((tag != null) && tag.equals(IDLE_VIEW_TAG)) {
73                childViews.add(view);
74            }
75        }
76        // Alpha animations don't work well on scroll-view; apply them on container linear-layout.
77        childViews.add(toolbar.findViewById(R.id.effects_container));
78    }
79
80    public void killIdle() {
81        mainHandler.removeMessages(MAKE_IDLE);
82        if (idle) {
83            idle = false;
84            for (View view : childViews) {
85                makeAwake(view);
86            }
87        }
88        mainHandler.sendEmptyMessageDelayed(MAKE_IDLE, TIMEOUT_IDLE);
89    }
90
91    private void makeAwake(View view) {
92        if (view.getVisibility() == View.VISIBLE) {
93            view.startAnimation(fadeIn);
94        }
95    }
96
97    private void makeIdle(View view) {
98        if (view.getVisibility() == View.VISIBLE) {
99            view.startAnimation(fadeOut);
100        }
101    }
102}
103