SlideshowActivity.java revision 8eed706474910ccb978acda03e85d3261037da6e
1/*
2 * Copyright (C) 2008 Esmertec AG.
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mms.ui;
19
20import com.android.mms.R;
21import com.android.mms.dom.smil.SmilDocumentImpl;
22import com.android.mms.dom.smil.SmilPlayer;
23import com.android.mms.dom.smil.parser.SmilXmlSerializer;
24import com.android.mms.model.SlideshowModel;
25import com.android.mms.model.SmilHelper;
26import com.google.android.mms.MmsException;
27
28import org.w3c.dom.events.Event;
29import org.w3c.dom.events.EventListener;
30import org.w3c.dom.events.EventTarget;
31import org.w3c.dom.smil.SMILDocument;
32
33import android.app.Activity;
34import android.content.Intent;
35import android.graphics.PixelFormat;
36import android.net.Uri;
37import android.os.Bundle;
38import android.os.Handler;
39import android.util.Config;
40import android.util.Log;
41import android.view.KeyEvent;
42import android.view.MotionEvent;
43import android.view.Window;
44import android.widget.MediaController;
45import android.widget.MediaController.MediaPlayerControl;
46
47import java.io.ByteArrayOutputStream;
48
49/**
50 * Plays the given slideshow in full-screen mode with a common controller.
51 */
52public class SlideshowActivity extends Activity implements EventListener {
53    private static final String TAG = "SlideshowActivity";
54    private static final boolean DEBUG = false;
55    private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
56
57    private MediaController mMediaController;
58    private SmilPlayer mSmilPlayer;
59
60    private Handler mHandler;
61
62    private SMILDocument mSmilDoc;
63
64    @Override
65    public void onCreate(Bundle icicle) {
66        super.onCreate(icicle);
67        mHandler = new Handler();
68
69        // Play slide-show in full-screen mode.
70        requestWindowFeature(Window.FEATURE_NO_TITLE);
71        getWindow().setFormat(PixelFormat.TRANSLUCENT);
72        setContentView(R.layout.slideshow);
73
74        Intent intent = getIntent();
75        Uri msg = intent.getData();
76        final SlideshowModel model;
77
78        try {
79            model = SlideshowModel.createFromMessageUri(this, msg);
80        } catch (MmsException e) {
81            Log.e(TAG, "Cannot present the slide show.", e);
82            finish();
83            return;
84        }
85
86        SlideView view = (SlideView) findViewById(R.id.slide_view);
87        PresenterFactory.getPresenter("SlideshowPresenter", this, view, model);
88
89        mHandler.post(new Runnable() {
90            private boolean isRotating() {
91                return mSmilPlayer.isPausedState()
92                        || mSmilPlayer.isPlayingState()
93                        || mSmilPlayer.isPlayedState();
94            }
95
96            public void run() {
97                mSmilPlayer = SmilPlayer.getPlayer();
98                initMediaController();
99
100                // Use SmilHelper.getDocument() to ensure rebuilding the
101                // entire SMIL document.
102                mSmilDoc = SmilHelper.getDocument(model);
103                if (DEBUG) {
104                    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
105                    SmilXmlSerializer.serialize(mSmilDoc, ostream);
106                    if (LOCAL_LOGV) {
107                        Log.v(TAG, ostream.toString());
108                    }
109                }
110
111                // Add event listener.
112                ((EventTarget) mSmilDoc).addEventListener(
113                        SmilDocumentImpl.SMIL_DOCUMENT_END_EVENT,
114                        SlideshowActivity.this, false);
115
116                mSmilPlayer.init(mSmilDoc);
117                if (isRotating()) {
118                    mSmilPlayer.reload();
119                } else {
120                    mSmilPlayer.play();
121                }
122            }
123        });
124    }
125
126    private void initMediaController() {
127        mMediaController = new MediaController(SlideshowActivity.this, false);
128        mMediaController.setMediaPlayer(new SmilPlayerController(mSmilPlayer));
129        mMediaController.setAnchorView(findViewById(R.id.slide_view));
130    }
131
132    @Override
133    public boolean onTouchEvent(MotionEvent ev) {
134        if ((mSmilPlayer != null) && (mMediaController != null)) {
135            mMediaController.show();
136        }
137        return false;
138    }
139
140    @Override
141    protected void onPause() {
142        super.onPause();
143        if (mSmilDoc != null) {
144            ((EventTarget) mSmilDoc).removeEventListener(
145                    SmilDocumentImpl.SMIL_DOCUMENT_END_EVENT, this, false);
146        }
147    }
148
149    @Override
150    protected void onStop() {
151        super.onStop();
152        if ((null != mSmilPlayer)) {
153            if (isFinishing()) {
154                mSmilPlayer.stop();
155            } else {
156                mSmilPlayer.stopWhenReload();
157            }
158        }
159    }
160
161    @Override
162    public boolean onKeyDown(int keyCode, KeyEvent event) {
163        switch (keyCode) {
164            case KeyEvent.KEYCODE_VOLUME_DOWN:
165            case KeyEvent.KEYCODE_VOLUME_UP:
166            case KeyEvent.KEYCODE_BACK:
167            case KeyEvent.KEYCODE_MENU:
168            case KeyEvent.KEYCODE_DPAD_UP:
169            case KeyEvent.KEYCODE_DPAD_DOWN:
170            case KeyEvent.KEYCODE_DPAD_LEFT:
171            case KeyEvent.KEYCODE_DPAD_RIGHT:
172                break;
173            default:
174                if ((mSmilPlayer != null) && (mMediaController != null)) {
175                    mMediaController.show();
176                }
177        }
178        return super.onKeyDown(keyCode, event);
179    }
180
181    private class SmilPlayerController implements MediaPlayerControl {
182        private final SmilPlayer mPlayer;
183
184        public SmilPlayerController(SmilPlayer player) {
185            mPlayer = player;
186        }
187
188        public int getBufferPercentage() {
189            // We don't need to buffer data, always return 100%.
190            return 100;
191        }
192
193        public int getCurrentPosition() {
194            return mPlayer.getCurrentPosition();
195        }
196
197        public int getDuration() {
198            return mPlayer.getDuration();
199        }
200
201        public boolean isPlaying() {
202            return mPlayer != null ? mPlayer.isPlayingState() : false;
203        }
204
205        public void pause() {
206            if (mPlayer != null) {
207                mPlayer.pause();
208            }
209        }
210
211        public void seekTo(int pos) {
212            // Don't need to support.
213        }
214
215        public void start() {
216            if (mPlayer != null) {
217                mPlayer.start();
218            }
219        }
220    }
221
222    public void handleEvent(Event evt) {
223        final Event event = evt;
224        mHandler.post(new Runnable() {
225            public void run() {
226                String type = event.getType();
227                if(type.equals(SmilDocumentImpl.SMIL_DOCUMENT_END_EVENT)) {
228                    finish();
229                }
230            }
231        });
232    }
233}
234