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 version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.preview
;
20 import android
.accounts
.Account
;
21 import android
.app
.Activity
;
22 import android
.app
.AlertDialog
;
23 import android
.content
.DialogInterface
;
24 import android
.content
.Intent
;
25 import android
.media
.MediaPlayer
;
26 import android
.media
.MediaPlayer
.OnCompletionListener
;
27 import android
.media
.MediaPlayer
.OnErrorListener
;
28 import android
.media
.MediaPlayer
.OnPreparedListener
;
29 import android
.net
.Uri
;
30 import android
.os
.Bundle
;
31 import android
.view
.MotionEvent
;
32 import android
.widget
.MediaController
;
33 import android
.widget
.VideoView
;
35 import com
.owncloud
.android
.AccountUtils
;
36 import com
.owncloud
.android
.Log_OC
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.datamodel
.OCFile
;
39 import com
.owncloud
.android
.media
.MediaService
;
42 * Activity implementing a basic video player.
44 * Used as an utility to preview video files contained in an ownCloud account.
46 * Currently, it always plays in landscape mode, full screen. When the playback ends,
47 * the activity is finished.
49 * @author David A. Velasco
51 public class PreviewVideoActivity
extends Activity
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
53 /** Key to receive an {@link OCFile} to play as an extra value in an {@link Intent} */
54 public static final String EXTRA_FILE
= "FILE";
56 /** Key to receive the ownCloud {@link Account} where the file to play is saved as an extra value in an {@link Intent} */
57 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
59 /** Key to receive a flag signaling if the video should be started immediately */
60 public static final String EXTRA_AUTOPLAY
= "AUTOPLAY";
62 /** Key to receive the position of the playback where the video should be put at start */
63 public static final String EXTRA_START_POSITION
= "START_POSITION";
65 private static final String TAG
= PreviewVideoActivity
.class.getSimpleName();
67 private OCFile mFile
; // video file to play
68 private Account mAccount
; // ownCloud account holding mFile
69 private int mSavedPlaybackPosition
; // in the unit time handled by MediaPlayer.getCurrentPosition()
70 private boolean mAutoplay
; // when 'true', the playback starts immediately with the activity
71 private VideoView mVideoPlayer
; // view to play the file; both performs and show the playback
72 private MediaController mMediaController
; // panel control used by the user to control the playback
75 * Called when the activity is first created.
77 * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
79 * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
80 * try to stream the remote file - TODO get the streaming works
85 public void onCreate(Bundle savedInstanceState
) {
86 super.onCreate(savedInstanceState
);
87 Log_OC
.e(TAG
, "ACTIVITY\t\tonCreate");
89 setContentView(R
.layout
.video_layout
);
91 if (savedInstanceState
== null
) {
92 Bundle extras
= getIntent().getExtras();
93 mFile
= extras
.getParcelable(EXTRA_FILE
);
94 mAccount
= extras
.getParcelable(EXTRA_ACCOUNT
);
95 mSavedPlaybackPosition
= extras
.getInt(EXTRA_START_POSITION
);
96 mAutoplay
= extras
.getBoolean(EXTRA_AUTOPLAY
);
99 mFile
= savedInstanceState
.getParcelable(EXTRA_FILE
);
100 mAccount
= savedInstanceState
.getParcelable(EXTRA_ACCOUNT
);
101 mSavedPlaybackPosition
= savedInstanceState
.getInt(EXTRA_START_POSITION
);
102 mAutoplay
= savedInstanceState
.getBoolean(EXTRA_AUTOPLAY
);
105 mVideoPlayer
= (VideoView
) findViewById(R
.id
.videoPlayer
);
107 // set listeners to get more contol on the playback
108 mVideoPlayer
.setOnPreparedListener(this);
109 mVideoPlayer
.setOnCompletionListener(this);
110 mVideoPlayer
.setOnErrorListener(this);
112 // keep the screen on while the playback is performed (prevents screen off by battery save)
113 mVideoPlayer
.setKeepScreenOn(true
);
116 if (mFile
.isDown()) {
117 mVideoPlayer
.setVideoPath(mFile
.getStoragePath());
119 } else if (mAccount
!= null
) {
121 String url
= AccountUtils
.constructFullURLForAccount(this, mAccount
) + mFile
.getRemotePath();
122 mVideoPlayer
.setVideoURI(Uri
.parse(url
));
125 onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_no_account
);
128 // create and prepare control panel for the user
129 mMediaController
= new MediaController(this);
130 mMediaController
.setMediaPlayer(mVideoPlayer
);
131 mMediaController
.setAnchorView(mVideoPlayer
);
132 mVideoPlayer
.setMediaController(mMediaController
);
135 onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_nothing_to_play
);
144 public void onSaveInstanceState(Bundle outState
) {
145 super.onSaveInstanceState(outState
);
146 Log_OC
.e(TAG
, "ACTIVITY\t\tonSaveInstanceState");
147 outState
.putParcelable(PreviewVideoActivity
.EXTRA_FILE
, mFile
);
148 outState
.putParcelable(PreviewVideoActivity
.EXTRA_ACCOUNT
, mAccount
);
149 outState
.putInt(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
150 outState
.putBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
155 public void onBackPressed() {
156 Log_OC
.e(TAG
, "ACTIVTIY\t\tonBackPressed");
157 Intent i
= new Intent();
158 i
.putExtra(EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
159 i
.putExtra(EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
160 setResult(RESULT_OK
, i
);
161 super.onBackPressed();
166 public void onResume() {
168 Log_OC
.e(TAG
, "ACTIVTIY\t\tonResume");
173 public void onStart() {
175 Log_OC
.e(TAG
, "ACTIVTIY\t\tonStart");
179 public void onDestroy() {
181 Log_OC
.e(TAG
, "ACTIVITY\t\tonDestroy");
185 public void onStop() {
187 Log_OC
.e(TAG
, "ACTIVTIY\t\tonStop");
192 public void onPause() {
194 Log_OC
.e(TAG
, "ACTIVTIY\t\tonPause");
199 * Called when the file is ready to be played.
201 * Just starts the playback.
203 * @param mp {@link MediaPlayer} instance performing the playback.
206 public void onPrepared(MediaPlayer mp
) {
207 Log_OC
.e(TAG
, "ACTIVITY\t\tonPrepare");
208 mVideoPlayer
.seekTo(mSavedPlaybackPosition
);
210 mVideoPlayer
.start();
212 mMediaController
.show(5000);
217 * Called when the file is finished playing.
221 * @param mp {@link MediaPlayer} instance performing the playback.
224 public void onCompletion(MediaPlayer mp
) {
225 mVideoPlayer
.seekTo(0);
230 * Called when an error in playback occurs.
232 * @param mp {@link MediaPlayer} instance performing the playback.
233 * @param what Type of error
234 * @param extra Extra code specific to the error
237 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
238 Log_OC
.e(TAG
, "Error in video playback, what = " + what
+ ", extra = " + extra
);
240 if (mMediaController
!= null
) {
241 mMediaController
.hide();
244 if (mVideoPlayer
.getWindowToken() != null
) {
245 String message
= MediaService
.getMessageForMediaError(this, what
, extra
);
246 new AlertDialog
.Builder(this)
248 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
249 new DialogInterface
.OnClickListener() {
250 public void onClick(DialogInterface dialog
, int whichButton
) {
251 PreviewVideoActivity
.this.onCompletion(null
);
254 .setCancelable(false
)
262 * Screen touches trigger the appearance of the control panel for a limited time.
267 public boolean onTouchEvent (MotionEvent ev
){
268 /*if (ev.getAction() == MotionEvent.ACTION_DOWN) {
269 if (mMediaController.isShowing()) {
270 mMediaController.hide();
272 mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE);