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
.AlertDialog
;
22 import android
.content
.DialogInterface
;
23 import android
.content
.Intent
;
24 import android
.media
.MediaPlayer
;
25 import android
.media
.MediaPlayer
.OnCompletionListener
;
26 import android
.media
.MediaPlayer
.OnErrorListener
;
27 import android
.media
.MediaPlayer
.OnPreparedListener
;
28 import android
.net
.Uri
;
29 import android
.os
.Bundle
;
30 import android
.widget
.MediaController
;
31 import android
.widget
.VideoView
;
33 import com
.owncloud
.android
.Log_OC
;
34 import com
.owncloud
.android
.R
;
35 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
36 import com
.owncloud
.android
.authentication
.AccountUtils
;
37 import com
.owncloud
.android
.authentication
.AccountUtils
.AccountNotFoundException
;
38 import com
.owncloud
.android
.datamodel
.OCFile
;
39 import com
.owncloud
.android
.media
.MediaService
;
40 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
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 FileDataStorageManager mStorageManager
;
64 private int mSavedPlaybackPosition
; // in the unit time handled by MediaPlayer.getCurrentPosition()
65 private boolean mAutoplay
; // when 'true', the playback starts immediately with the activity
66 private VideoView mVideoPlayer
; // view to play the file; both performs and show the playback
67 private MediaController mMediaController
; // panel control used by the user to control the playback
70 * Called when the activity is first created.
72 * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
74 * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
75 * try to stream the remote file - TODO get the streaming works
80 public void onCreate(Bundle savedInstanceState
) {
81 super.onCreate(savedInstanceState
);
82 Log_OC
.e(TAG
, "ACTIVITY\t\tonCreate");
84 setContentView(R
.layout
.video_layout
);
86 if (savedInstanceState
== null
) {
87 Bundle extras
= getIntent().getExtras();
88 mSavedPlaybackPosition
= extras
.getInt(EXTRA_START_POSITION
);
89 mAutoplay
= extras
.getBoolean(EXTRA_AUTOPLAY
);
92 mSavedPlaybackPosition
= savedInstanceState
.getInt(EXTRA_START_POSITION
);
93 mAutoplay
= savedInstanceState
.getBoolean(EXTRA_AUTOPLAY
);
96 mVideoPlayer
= (VideoView
) findViewById(R
.id
.videoPlayer
);
98 // set listeners to get more contol on the playback
99 mVideoPlayer
.setOnPreparedListener(this);
100 mVideoPlayer
.setOnCompletionListener(this);
101 mVideoPlayer
.setOnErrorListener(this);
103 // keep the screen on while the playback is performed (prevents screen off by battery save)
104 mVideoPlayer
.setKeepScreenOn(true
);
112 public void onSaveInstanceState(Bundle outState
) {
113 super.onSaveInstanceState(outState
);
114 Log_OC
.e(TAG
, "ACTIVITY\t\tonSaveInstanceState");
115 outState
.putInt(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
116 outState
.putBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
121 public void onBackPressed() {
122 Log_OC
.e(TAG
, "ACTIVTIY\t\tonBackPressed");
123 Intent i
= new Intent();
124 i
.putExtra(EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
125 i
.putExtra(EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
126 setResult(RESULT_OK
, i
);
127 super.onBackPressed();
132 * Called when the file is ready to be played.
134 * Just starts the playback.
136 * @param mp {@link MediaPlayer} instance performing the playback.
139 public void onPrepared(MediaPlayer mp
) {
140 Log_OC
.e(TAG
, "ACTIVITY\t\tonPrepare");
141 mVideoPlayer
.seekTo(mSavedPlaybackPosition
);
143 mVideoPlayer
.start();
145 mMediaController
.show(5000);
150 * Called when the file is finished playing.
154 * @param mp {@link MediaPlayer} instance performing the playback.
157 public void onCompletion(MediaPlayer mp
) {
158 mVideoPlayer
.seekTo(0);
163 * Called when an error in playback occurs.
165 * @param mp {@link MediaPlayer} instance performing the playback.
166 * @param what Type of error
167 * @param extra Extra code specific to the error
170 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
171 Log_OC
.e(TAG
, "Error in video playback, what = " + what
+ ", extra = " + extra
);
173 if (mMediaController
!= null
) {
174 mMediaController
.hide();
177 if (mVideoPlayer
.getWindowToken() != null
) {
178 String message
= MediaService
.getMessageForMediaError(this, what
, extra
);
179 new AlertDialog
.Builder(this)
181 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
182 new DialogInterface
.OnClickListener() {
183 public void onClick(DialogInterface dialog
, int whichButton
) {
184 PreviewVideoActivity
.this.onCompletion(null
);
187 .setCancelable(false
)
195 protected void onAccountSet(boolean stateWasRecovered
) {
196 if (getAccount() != null
) {
197 OCFile file
= getFile();
198 /// Validate handled file (first image to preview)
200 throw new IllegalStateException("Instanced with a NULL OCFile");
202 if (!file
.isVideo()) {
203 throw new IllegalArgumentException("Non-video file passed as argument");
205 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
206 file
= mStorageManager
.getFileById(file
.getFileId());
209 mVideoPlayer
.setVideoPath(file
.getStoragePath());
215 url
= AccountUtils
.constructFullURLForAccount(this, getAccount()) + file
.getRemotePath();
216 mVideoPlayer
.setVideoURI(Uri
.parse(url
));
217 } catch (AccountNotFoundException e
) {
218 onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_no_account
);
222 // create and prepare control panel for the user
223 mMediaController
= new MediaController(this);
224 mMediaController
.setMediaPlayer(mVideoPlayer
);
225 mMediaController
.setAnchorView(mVideoPlayer
);
226 mVideoPlayer
.setMediaController(mMediaController
);
232 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");