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
.FileDataStorageManager
;
22 import com
.owncloud
.android
.datamodel
.OCFile
;
23 import com
.owncloud
.android
.media
.MediaService
;
24 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
25 import com
.owncloud
.android
.utils
.Log_OC
;
27 import android
.accounts
.Account
;
28 import android
.app
.AlertDialog
;
29 import android
.content
.DialogInterface
;
30 import android
.content
.Intent
;
31 import android
.media
.MediaPlayer
;
32 import android
.media
.MediaPlayer
.OnCompletionListener
;
33 import android
.media
.MediaPlayer
.OnErrorListener
;
34 import android
.media
.MediaPlayer
.OnPreparedListener
;
35 import android
.net
.Uri
;
36 import android
.os
.Bundle
;
37 import android
.widget
.MediaController
;
38 import android
.widget
.VideoView
;
40 import com
.owncloud
.android
.oc_framework
.accounts
.AccountUtils
;
41 import com
.owncloud
.android
.oc_framework
.accounts
.AccountUtils
.AccountNotFoundException
;
44 * Activity implementing a basic video player.
46 * Used as an utility to preview video files contained in an ownCloud account.
48 * Currently, it always plays in landscape mode, full screen. When the playback ends,
49 * the activity is finished.
51 * @author David A. Velasco
53 public class PreviewVideoActivity
extends FileActivity
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
55 /** Key to receive a flag signaling if the video should be started immediately */
56 public static final String EXTRA_AUTOPLAY
= "AUTOPLAY";
58 /** Key to receive the position of the playback where the video should be put at start */
59 public static final String EXTRA_START_POSITION
= "START_POSITION";
61 private static final String TAG
= PreviewVideoActivity
.class.getSimpleName();
63 private FileDataStorageManager mStorageManager
;
65 private int mSavedPlaybackPosition
; // in the unit time handled by MediaPlayer.getCurrentPosition()
66 private boolean mAutoplay
; // when 'true', the playback starts immediately with the activity
67 private VideoView mVideoPlayer
; // view to play the file; both performs and show the playback
68 private MediaController mMediaController
; // panel control used by the user to control the playback
71 * Called when the activity is first created.
73 * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
75 * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
76 * try to stream the remote file - TODO get the streaming works
81 public void onCreate(Bundle savedInstanceState
) {
82 super.onCreate(savedInstanceState
);
83 Log_OC
.e(TAG
, "ACTIVITY\t\tonCreate");
85 setContentView(R
.layout
.video_layout
);
87 if (savedInstanceState
== null
) {
88 Bundle extras
= getIntent().getExtras();
89 mSavedPlaybackPosition
= extras
.getInt(EXTRA_START_POSITION
);
90 mAutoplay
= extras
.getBoolean(EXTRA_AUTOPLAY
);
93 mSavedPlaybackPosition
= savedInstanceState
.getInt(EXTRA_START_POSITION
);
94 mAutoplay
= savedInstanceState
.getBoolean(EXTRA_AUTOPLAY
);
97 mVideoPlayer
= (VideoView
) findViewById(R
.id
.videoPlayer
);
99 // set listeners to get more contol on the playback
100 mVideoPlayer
.setOnPreparedListener(this);
101 mVideoPlayer
.setOnCompletionListener(this);
102 mVideoPlayer
.setOnErrorListener(this);
104 // keep the screen on while the playback is performed (prevents screen off by battery save)
105 mVideoPlayer
.setKeepScreenOn(true
);
113 public void onSaveInstanceState(Bundle outState
) {
114 super.onSaveInstanceState(outState
);
115 Log_OC
.e(TAG
, "ACTIVITY\t\tonSaveInstanceState");
116 outState
.putInt(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
117 outState
.putBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
122 public void onBackPressed() {
123 Log_OC
.e(TAG
, "ACTIVTIY\t\tonBackPressed");
124 Intent i
= new Intent();
125 i
.putExtra(EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
126 i
.putExtra(EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
127 setResult(RESULT_OK
, i
);
128 super.onBackPressed();
133 * Called when the file is ready to be played.
135 * Just starts the playback.
137 * @param mp {@link MediaPlayer} instance performing the playback.
140 public void onPrepared(MediaPlayer mp
) {
141 Log_OC
.e(TAG
, "ACTIVITY\t\tonPrepare");
142 mVideoPlayer
.seekTo(mSavedPlaybackPosition
);
144 mVideoPlayer
.start();
146 mMediaController
.show(5000);
151 * Called when the file is finished playing.
155 * @param mp {@link MediaPlayer} instance performing the playback.
158 public void onCompletion(MediaPlayer mp
) {
159 mVideoPlayer
.seekTo(0);
164 * Called when an error in playback occurs.
166 * @param mp {@link MediaPlayer} instance performing the playback.
167 * @param what Type of error
168 * @param extra Extra code specific to the error
171 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
172 Log_OC
.e(TAG
, "Error in video playback, what = " + what
+ ", extra = " + extra
);
174 if (mMediaController
!= null
) {
175 mMediaController
.hide();
178 if (mVideoPlayer
.getWindowToken() != null
) {
179 String message
= MediaService
.getMessageForMediaError(this, what
, extra
);
180 new AlertDialog
.Builder(this)
182 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
183 new DialogInterface
.OnClickListener() {
184 public void onClick(DialogInterface dialog
, int whichButton
) {
185 PreviewVideoActivity
.this.onCompletion(null
);
188 .setCancelable(false
)
196 protected void onAccountSet(boolean stateWasRecovered
) {
197 if (getAccount() != null
) {
198 OCFile file
= getFile();
199 /// Validate handled file (first image to preview)
201 throw new IllegalStateException("Instanced with a NULL OCFile");
203 if (!file
.isVideo()) {
204 throw new IllegalArgumentException("Non-video file passed as argument");
206 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
207 file
= mStorageManager
.getFileById(file
.getFileId());
210 mVideoPlayer
.setVideoPath(file
.getStoragePath());
216 url
= AccountUtils
.constructFullURLForAccount(this, getAccount()) + file
.getRemotePath();
217 mVideoPlayer
.setVideoURI(Uri
.parse(url
));
218 } catch (AccountNotFoundException e
) {
219 onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_no_account
);
223 // create and prepare control panel for the user
224 mMediaController
= new MediaController(this);
225 mMediaController
.setMediaPlayer(mVideoPlayer
);
226 mMediaController
.setAnchorView(mVideoPlayer
);
227 mVideoPlayer
.setMediaController(mMediaController
);
233 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");