1/*
2 * Copyright (C) 2011 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.gallery3d.app;
18
19import android.content.Context;
20import android.os.Handler;
21import android.view.KeyEvent;
22import android.view.MotionEvent;
23import android.view.View;
24import android.view.animation.Animation;
25import android.view.animation.Animation.AnimationListener;
26import android.view.animation.AnimationUtils;
27import com.android.gallery3d.R;
28
29/**
30 * The playback controller for the Movie Player.
31 */
32public class MovieControllerOverlay extends CommonControllerOverlay implements
33        AnimationListener {
34
35    private boolean hidden;
36
37    private final Handler handler;
38    private final Runnable startHidingRunnable;
39    private final Animation hideAnimation;
40
41    public MovieControllerOverlay(Context context) {
42        super(context);
43
44        handler = new Handler();
45        startHidingRunnable = new Runnable() {
46                @Override
47            public void run() {
48                startHiding();
49            }
50        };
51
52        hideAnimation = AnimationUtils.loadAnimation(context, R.anim.player_out);
53        hideAnimation.setAnimationListener(this);
54
55        hide();
56    }
57
58    @Override
59    protected void createTimeBar(Context context) {
60        mTimeBar = new TimeBar(context, this);
61    }
62
63    @Override
64    public void hide() {
65        boolean wasHidden = hidden;
66        hidden = true;
67        super.hide();
68        if (mListener != null && wasHidden != hidden) {
69            mListener.onHidden();
70        }
71    }
72
73
74    @Override
75    public void show() {
76        boolean wasHidden = hidden;
77        hidden = false;
78        super.show();
79        if (mListener != null && wasHidden != hidden) {
80            mListener.onShown();
81        }
82        maybeStartHiding();
83    }
84
85    private void maybeStartHiding() {
86        cancelHiding();
87        if (mState == State.PLAYING) {
88            handler.postDelayed(startHidingRunnable, 2500);
89        }
90    }
91
92    private void startHiding() {
93        startHideAnimation(mBackground);
94        startHideAnimation(mTimeBar);
95        startHideAnimation(mPlayPauseReplayView);
96    }
97
98    private void startHideAnimation(View view) {
99        if (view.getVisibility() == View.VISIBLE) {
100            view.startAnimation(hideAnimation);
101        }
102    }
103
104    private void cancelHiding() {
105        handler.removeCallbacks(startHidingRunnable);
106        mBackground.setAnimation(null);
107        mTimeBar.setAnimation(null);
108        mPlayPauseReplayView.setAnimation(null);
109    }
110
111    @Override
112    public void onAnimationStart(Animation animation) {
113        // Do nothing.
114    }
115
116    @Override
117    public void onAnimationRepeat(Animation animation) {
118        // Do nothing.
119    }
120
121    @Override
122    public void onAnimationEnd(Animation animation) {
123        hide();
124    }
125
126    @Override
127    public boolean onKeyDown(int keyCode, KeyEvent event) {
128        if (hidden) {
129            show();
130        }
131        return super.onKeyDown(keyCode, event);
132    }
133
134    @Override
135    public boolean onTouchEvent(MotionEvent event) {
136        if (super.onTouchEvent(event)) {
137            return true;
138        }
139
140        if (hidden) {
141            show();
142            return true;
143        }
144        switch (event.getAction()) {
145            case MotionEvent.ACTION_DOWN:
146                cancelHiding();
147                if (mState == State.PLAYING || mState == State.PAUSED) {
148                    mListener.onPlayPause();
149                }
150                break;
151            case MotionEvent.ACTION_UP:
152                maybeStartHiding();
153                break;
154        }
155        return true;
156    }
157
158    @Override
159    protected void updateViews() {
160        if (hidden) {
161            return;
162        }
163        super.updateViews();
164    }
165
166    // TimeBar listener
167
168    @Override
169    public void onScrubbingStart() {
170        cancelHiding();
171        super.onScrubbingStart();
172    }
173
174    @Override
175    public void onScrubbingMove(int time) {
176        cancelHiding();
177        super.onScrubbingMove(time);
178    }
179
180    @Override
181    public void onScrubbingEnd(int time, int trimStartTime, int trimEndTime) {
182        maybeStartHiding();
183        super.onScrubbingEnd(time, trimStartTime, trimEndTime);
184    }
185}
186