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 com
.owncloud
.android
.R
;
21 import com
.owncloud
.android
.datamodel
.OCFile
;
22 import com
.owncloud
.android
.media
.MediaService
;
23 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
24 import com
.owncloud
.android
.utils
.Log_OC
;
26 import android
.accounts
.Account
;
27 import android
.app
.AlertDialog
;
28 import android
.content
.DialogInterface
;
29 import android
.content
.Intent
;
30 import android
.media
.MediaPlayer
;
31 import android
.media
.MediaPlayer
.OnCompletionListener
;
32 import android
.media
.MediaPlayer
.OnErrorListener
;
33 import android
.media
.MediaPlayer
.OnPreparedListener
;
34 import android
.net
.Uri
;
35 import android
.os
.Bundle
;
36 import android
.widget
.MediaController
;
37 import android
.widget
.VideoView
;
39 import com
.owncloud
.android
.lib
.accounts
.AccountUtils
;
40 import com
.owncloud
.android
.lib
.accounts
.AccountUtils
.AccountNotFoundException
;
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 FileActivity
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
54 /** Key to receive a flag signaling if the video should be started immediately */
55 public static final String EXTRA_AUTOPLAY
= "AUTOPLAY";
57 /** Key to receive the position of the playback where the video should be put at start */
58 public static final String EXTRA_START_POSITION
= "START_POSITION";
60 private static final String TAG
= PreviewVideoActivity
.class.getSimpleName();
62 private int mSavedPlaybackPosition
; // in the unit time handled by MediaPlayer.getCurrentPosition()
63 private boolean mAutoplay
; // when 'true', the playback starts immediately with the activity
64 private VideoView mVideoPlayer
; // view to play the file; both performs and show the playback
65 private MediaController mMediaController
; // panel control used by the user to control the playback
68 * Called when the activity is first created.
70 * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
72 * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
73 * try to stream the remote file - TODO get the streaming works
78 public void onCreate(Bundle savedInstanceState
) {
79 super.onCreate(savedInstanceState
);
80 Log_OC
.e(TAG
, "ACTIVITY\t\tonCreate");
82 setContentView(R
.layout
.video_layout
);
84 if (savedInstanceState
== null
) {
85 Bundle extras
= getIntent().getExtras();
86 mSavedPlaybackPosition
= extras
.getInt(EXTRA_START_POSITION
);
87 mAutoplay
= extras
.getBoolean(EXTRA_AUTOPLAY
);
90 mSavedPlaybackPosition
= savedInstanceState
.getInt(EXTRA_START_POSITION
);
91 mAutoplay
= savedInstanceState
.getBoolean(EXTRA_AUTOPLAY
);
94 mVideoPlayer
= (VideoView
) findViewById(R
.id
.videoPlayer
);
96 // set listeners to get more contol on the playback
97 mVideoPlayer
.setOnPreparedListener(this);
98 mVideoPlayer
.setOnCompletionListener(this);
99 mVideoPlayer
.setOnErrorListener(this);
101 // keep the screen on while the playback is performed (prevents screen off by battery save)
102 mVideoPlayer
.setKeepScreenOn(true
);
110 public void onSaveInstanceState(Bundle outState
) {
111 super.onSaveInstanceState(outState
);
112 Log_OC
.e(TAG
, "ACTIVITY\t\tonSaveInstanceState");
113 outState
.putInt(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
114 outState
.putBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
119 public void onBackPressed() {
120 Log_OC
.e(TAG
, "ACTIVTIY\t\tonBackPressed");
121 Intent i
= new Intent();
122 i
.putExtra(EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
123 i
.putExtra(EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
124 setResult(RESULT_OK
, i
);
125 super.onBackPressed();
130 * Called when the file is ready to be played.
132 * Just starts the playback.
134 * @param mp {@link MediaPlayer} instance performing the playback.
137 public void onPrepared(MediaPlayer mp
) {
138 Log_OC
.e(TAG
, "ACTIVITY\t\tonPrepare");
139 mVideoPlayer
.seekTo(mSavedPlaybackPosition
);
141 mVideoPlayer
.start();
143 mMediaController
.show(5000);
148 * Called when the file is finished playing.
152 * @param mp {@link MediaPlayer} instance performing the playback.
155 public void onCompletion(MediaPlayer mp
) {
156 mVideoPlayer
.seekTo(0);
161 * Called when an error in playback occurs.
163 * @param mp {@link MediaPlayer} instance performing the playback.
164 * @param what Type of error
165 * @param extra Extra code specific to the error
168 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
169 Log_OC
.e(TAG
, "Error in video playback, what = " + what
+ ", extra = " + extra
);
171 if (mMediaController
!= null
) {
172 mMediaController
.hide();
175 if (mVideoPlayer
.getWindowToken() != null
) {
176 String message
= MediaService
.getMessageForMediaError(this, what
, extra
);
177 new AlertDialog
.Builder(this)
179 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
180 new DialogInterface
.OnClickListener() {
181 public void onClick(DialogInterface dialog
, int whichButton
) {
182 PreviewVideoActivity
.this.onCompletion(null
);
185 .setCancelable(false
)
193 protected void onAccountSet(boolean stateWasRecovered
) {
194 super.onAccountSet(stateWasRecovered
);
195 if (getAccount() != null
) {
196 OCFile file
= getFile();
197 /// Validate handled file (first image to preview)
199 throw new IllegalStateException("Instanced with a NULL OCFile");
201 if (!file
.isVideo()) {
202 throw new IllegalArgumentException("Non-video file passed as argument");
204 file
= getStorageManager().getFileById(file
.getFileId());
207 mVideoPlayer
.setVideoPath(file
.getStoragePath());
213 url
= AccountUtils
.constructFullURLForAccount(this, getAccount()) + file
.getRemotePath();
214 mVideoPlayer
.setVideoURI(Uri
.parse(url
));
215 } catch (AccountNotFoundException e
) {
216 onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_no_account
);
220 // create and prepare control panel for the user
221 mMediaController
= new MediaController(this);
222 mMediaController
.setMediaPlayer(mVideoPlayer
);
223 mMediaController
.setAnchorView(mVideoPlayer
);
224 mVideoPlayer
.setMediaController(mMediaController
);