1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.preview
;
21 import android
.accounts
.Account
;
22 import android
.app
.Activity
;
23 import android
.app
.AlertDialog
;
24 import android
.content
.DialogInterface
;
25 import android
.content
.Intent
;
26 import android
.media
.MediaPlayer
;
27 import android
.media
.MediaPlayer
.OnCompletionListener
;
28 import android
.media
.MediaPlayer
.OnErrorListener
;
29 import android
.media
.MediaPlayer
.OnPreparedListener
;
30 import android
.net
.Uri
;
31 import android
.os
.Bundle
;
32 import android
.util
.Log
;
33 import android
.view
.MotionEvent
;
34 import android
.widget
.MediaController
;
35 import android
.widget
.VideoView
;
37 import com
.owncloud
.android
.AccountUtils
;
38 import com
.owncloud
.android
.R
;
39 import com
.owncloud
.android
.datamodel
.OCFile
;
40 import com
.owncloud
.android
.media
.MediaService
;
43 * Activity implementing a basic video player.
45 * Used as an utility to preview video files contained in an ownCloud account.
47 * Currently, it always plays in landscape mode, full screen. When the playback ends,
48 * the activity is finished.
50 * @author David A. Velasco
52 public class PreviewVideoActivity
extends Activity
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
54 /** Key to receive an {@link OCFile} to play as an extra value in an {@link Intent} */
55 public static final String EXTRA_FILE
= "FILE";
57 /** Key to receive the ownCloud {@link Account} where the file to play is saved as an extra value in an {@link Intent} */
58 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
60 /** Key to receive a flag signaling if the video should be started immediately */
61 public static final String EXTRA_AUTOPLAY
= "AUTOPLAY";
63 /** Key to receive the position of the playback where the video should be put at start */
64 public static final String EXTRA_START_POSITION
= "START_POSITION";
66 private static final String TAG
= PreviewVideoActivity
.class.getSimpleName();
68 private OCFile mFile
; // video file to play
69 private Account mAccount
; // ownCloud account holding mFile
70 private int mSavedPlaybackPosition
; // in the unit time handled by MediaPlayer.getCurrentPosition()
71 private boolean mAutoplay
; // when 'true', the playback starts immediately with the activity
72 private VideoView mVideoPlayer
; // view to play the file; both performs and show the playback
73 private MediaController mMediaController
; // panel control used by the user to control the playback
76 * Called when the activity is first created.
78 * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
80 * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
81 * try to stream the remote file - TODO get the streaming works
86 public void onCreate(Bundle savedInstanceState
) {
87 super.onCreate(savedInstanceState
);
88 Log
.e(TAG
, "ACTIVITY\t\tonCreate");
90 setContentView(R
.layout
.video_layout
);
92 if (savedInstanceState
== null
) {
93 Bundle extras
= getIntent().getExtras();
94 mFile
= extras
.getParcelable(EXTRA_FILE
);
95 mAccount
= extras
.getParcelable(EXTRA_ACCOUNT
);
96 mSavedPlaybackPosition
= extras
.getInt(EXTRA_START_POSITION
);
97 mAutoplay
= extras
.getBoolean(EXTRA_AUTOPLAY
);
100 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
101 mAccount
= savedInstanceState
.getParcelable(EXTRA_ACCOUNT
);
102 mSavedPlaybackPosition
= savedInstanceState
.getInt(EXTRA_START_POSITION
);
103 mAutoplay
= savedInstanceState
.getBoolean(EXTRA_AUTOPLAY
);
106 mVideoPlayer
= (VideoView
) findViewById(R
.id
.videoPlayer
);
108 // set listeners to get more contol on the playback
109 mVideoPlayer
.setOnPreparedListener(this);
110 mVideoPlayer
.setOnCompletionListener(this);
111 mVideoPlayer
.setOnErrorListener(this);
113 // keep the screen on while the playback is performed (prevents screen off by battery save)
114 mVideoPlayer
.setKeepScreenOn(true
);
117 if (mFile
.isDown()) {
118 mVideoPlayer
.setVideoPath(mFile
.getStoragePath());
120 } else if (mAccount
!= null
) {
122 String url
= AccountUtils
.constructFullURLForAccount(this, mAccount
) + mFile
.getRemotePath();
123 mVideoPlayer
.setVideoURI(Uri
.parse(url
));
126 onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_no_account
);
129 // create and prepare control panel for the user
130 mMediaController
= new MediaController(this);
131 mMediaController
.setMediaPlayer(mVideoPlayer
);
132 mMediaController
.setAnchorView(mVideoPlayer
);
133 mVideoPlayer
.setMediaController(mMediaController
);
136 onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_nothing_to_play
);
145 public void onSaveInstanceState(Bundle outState
) {
146 super.onSaveInstanceState(outState
);
147 Log
.e(TAG
, "ACTIVITY\t\tonSaveInstanceState");
148 outState
.putParcelable(PreviewVideoActivity
.EXTRA_FILE
, mFile
);
149 outState
.putParcelable(PreviewVideoActivity
.EXTRA_ACCOUNT
, mAccount
);
150 outState
.putInt(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
151 outState
.putBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
156 public void onBackPressed() {
157 Log
.e(TAG
, "ACTIVTIY\t\tonBackPressed");
158 Intent i
= new Intent();
159 i
.putExtra(EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
160 i
.putExtra(EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
161 setResult(RESULT_OK
, i
);
162 super.onBackPressed();
167 public void onResume() {
169 Log
.e(TAG
, "ACTIVTIY\t\tonResume");
174 public void onStart() {
176 Log
.e(TAG
, "ACTIVTIY\t\tonStart");
180 public void onDestroy() {
182 Log
.e(TAG
, "ACTIVITY\t\tonDestroy");
186 public void onStop() {
188 Log
.e(TAG
, "ACTIVTIY\t\tonStop");
193 public void onPause() {
195 Log
.e(TAG
, "ACTIVTIY\t\tonPause");
200 * Called when the file is ready to be played.
202 * Just starts the playback.
204 * @param mp {@link MediaPlayer} instance performing the playback.
207 public void onPrepared(MediaPlayer mp
) {
208 Log
.e(TAG
, "ACTIVITY\t\tonPrepare");
209 mVideoPlayer
.seekTo(mSavedPlaybackPosition
);
211 mVideoPlayer
.start();
213 mMediaController
.show(5000);
218 * Called when the file is finished playing.
222 * @param mp {@link MediaPlayer} instance performing the playback.
225 public void onCompletion(MediaPlayer mp
) {
226 mVideoPlayer
.seekTo(0);
231 * Called when an error in playback occurs.
233 * @param mp {@link MediaPlayer} instance performing the playback.
234 * @param what Type of error
235 * @param extra Extra code specific to the error
238 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
239 Log
.e(TAG
, "Error in video playback, what = " + what
+ ", extra = " + extra
);
241 if (mMediaController
!= null
) {
242 mMediaController
.hide();
245 if (mVideoPlayer
.getWindowToken() != null
) {
246 String message
= MediaService
.getMessageForMediaError(this, what
, extra
);
247 new AlertDialog
.Builder(this)
249 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
250 new DialogInterface
.OnClickListener() {
251 public void onClick(DialogInterface dialog
, int whichButton
) {
252 PreviewVideoActivity
.this.onCompletion(null
);
255 .setCancelable(false
)
263 * Screen touches trigger the appearance of the control panel for a limited time.
268 public boolean onTouchEvent (MotionEvent ev
){
269 /*if (ev.getAction() == MotionEvent.ACTION_DOWN) {
270 if (mMediaController.isShowing()) {
271 mMediaController.hide();
273 mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE);