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
.Log_OC
;
36 import com
.owncloud
.android
.R
;
37 import com
.owncloud
.android
.authentication
.AccountUtils
;
38 import com
.owncloud
.android
.authentication
.AccountUtils
.AccountNotFoundException
;
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_OC
.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
) {
124 url
= AccountUtils
.constructFullURLForAccount(this, mAccount
) + mFile
.getRemotePath();
125 mVideoPlayer
.setVideoURI(Uri
.parse(url
));
126 } catch (AccountNotFoundException e
) {
127 onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_no_account
);
131 onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_no_account
);
134 // create and prepare control panel for the user
135 mMediaController
= new MediaController(this);
136 mMediaController
.setMediaPlayer(mVideoPlayer
);
137 mMediaController
.setAnchorView(mVideoPlayer
);
138 mVideoPlayer
.setMediaController(mMediaController
);
141 onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_nothing_to_play
);
150 public void onSaveInstanceState(Bundle outState
) {
151 super.onSaveInstanceState(outState
);
152 Log_OC
.e(TAG
, "ACTIVITY\t\tonSaveInstanceState");
153 outState
.putParcelable(PreviewVideoActivity
.EXTRA_FILE
, mFile
);
154 outState
.putParcelable(PreviewVideoActivity
.EXTRA_ACCOUNT
, mAccount
);
155 outState
.putInt(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
156 outState
.putBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
161 public void onBackPressed() {
162 Log_OC
.e(TAG
, "ACTIVTIY\t\tonBackPressed");
163 Intent i
= new Intent();
164 i
.putExtra(EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
165 i
.putExtra(EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
166 setResult(RESULT_OK
, i
);
167 super.onBackPressed();
172 public void onResume() {
174 Log_OC
.e(TAG
, "ACTIVTIY\t\tonResume");
179 public void onStart() {
181 Log_OC
.e(TAG
, "ACTIVTIY\t\tonStart");
185 public void onDestroy() {
187 Log_OC
.e(TAG
, "ACTIVITY\t\tonDestroy");
191 public void onStop() {
193 Log_OC
.e(TAG
, "ACTIVTIY\t\tonStop");
198 public void onPause() {
200 Log_OC
.e(TAG
, "ACTIVTIY\t\tonPause");
205 * Called when the file is ready to be played.
207 * Just starts the playback.
209 * @param mp {@link MediaPlayer} instance performing the playback.
212 public void onPrepared(MediaPlayer mp
) {
213 Log_OC
.e(TAG
, "ACTIVITY\t\tonPrepare");
214 mVideoPlayer
.seekTo(mSavedPlaybackPosition
);
216 mVideoPlayer
.start();
218 mMediaController
.show(5000);
223 * Called when the file is finished playing.
227 * @param mp {@link MediaPlayer} instance performing the playback.
230 public void onCompletion(MediaPlayer mp
) {
231 mVideoPlayer
.seekTo(0);
236 * Called when an error in playback occurs.
238 * @param mp {@link MediaPlayer} instance performing the playback.
239 * @param what Type of error
240 * @param extra Extra code specific to the error
243 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
244 Log_OC
.e(TAG
, "Error in video playback, what = " + what
+ ", extra = " + extra
);
246 if (mMediaController
!= null
) {
247 mMediaController
.hide();
250 if (mVideoPlayer
.getWindowToken() != null
) {
251 String message
= MediaService
.getMessageForMediaError(this, what
, extra
);
252 new AlertDialog
.Builder(this)
254 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
255 new DialogInterface
.OnClickListener() {
256 public void onClick(DialogInterface dialog
, int whichButton
) {
257 PreviewVideoActivity
.this.onCompletion(null
);
260 .setCancelable(false
)
268 * Screen touches trigger the appearance of the control panel for a limited time.
273 public boolean onTouchEvent (MotionEvent ev
){
274 /*if (ev.getAction() == MotionEvent.ACTION_DOWN) {
275 if (mMediaController.isShowing()) {
276 mMediaController.hide();
278 mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE);