2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.ui
.preview
;
23 import com
.owncloud
.android
.R
;
24 import com
.owncloud
.android
.datamodel
.OCFile
;
25 import com
.owncloud
.android
.media
.MediaService
;
26 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
28 import android
.accounts
.Account
;
29 import android
.support
.v7
.app
.AlertDialog
;
30 import android
.content
.DialogInterface
;
31 import android
.content
.Intent
;
32 import android
.media
.MediaPlayer
;
33 import android
.media
.MediaPlayer
.OnCompletionListener
;
34 import android
.media
.MediaPlayer
.OnErrorListener
;
35 import android
.media
.MediaPlayer
.OnPreparedListener
;
36 import android
.net
.Uri
;
37 import android
.os
.Bundle
;
38 import android
.widget
.MediaController
;
39 import android
.widget
.VideoView
;
41 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
;
42 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
;
43 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
46 * Activity implementing a basic video player.
48 * Used as an utility to preview video files contained in an ownCloud account.
50 * Currently, it always plays in landscape mode, full screen. When the playback ends,
51 * the activity is finished.
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 int mSavedPlaybackPosition
; // in the unit time handled by MediaPlayer.getCurrentPosition()
64 private boolean mAutoplay
; // when 'true', the playback starts immediately with the activity
65 private VideoView mVideoPlayer
; // view to play the file; both performs and show the playback
66 private MediaController mMediaController
; // panel control used by the user to control the playback
69 * Called when the activity is first created.
71 * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
73 * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
74 * try to stream the remote file - TODO get the streaming works
79 public void onCreate(Bundle savedInstanceState
) {
80 super.onCreate(savedInstanceState
);
81 Log_OC
.e(TAG
, "ACTIVITY\t\tonCreate");
83 setContentView(R
.layout
.video_layout
);
85 if (savedInstanceState
== null
) {
86 Bundle extras
= getIntent().getExtras();
87 mSavedPlaybackPosition
= extras
.getInt(EXTRA_START_POSITION
);
88 mAutoplay
= extras
.getBoolean(EXTRA_AUTOPLAY
);
91 mSavedPlaybackPosition
= savedInstanceState
.getInt(EXTRA_START_POSITION
);
92 mAutoplay
= savedInstanceState
.getBoolean(EXTRA_AUTOPLAY
);
95 mVideoPlayer
= (VideoView
) findViewById(R
.id
.videoPlayer
);
97 // set listeners to get more contol on the playback
98 mVideoPlayer
.setOnPreparedListener(this);
99 mVideoPlayer
.setOnCompletionListener(this);
100 mVideoPlayer
.setOnErrorListener(this);
102 // keep the screen on while the playback is performed (prevents screen off by battery save)
103 mVideoPlayer
.setKeepScreenOn(true
);
111 public void onSaveInstanceState(Bundle outState
) {
112 super.onSaveInstanceState(outState
);
113 Log_OC
.e(TAG
, "ACTIVITY\t\tonSaveInstanceState");
114 outState
.putInt(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
115 outState
.putBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
120 public void onBackPressed() {
121 Log_OC
.e(TAG
, "ACTIVTIY\t\tonBackPressed");
122 Intent i
= new Intent();
123 i
.putExtra(EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying());
124 i
.putExtra(EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition());
125 setResult(RESULT_OK
, i
);
126 super.onBackPressed();
131 * Called when the file is ready to be played.
133 * Just starts the playback.
135 * @param mp {@link MediaPlayer} instance performing the playback.
138 public void onPrepared(MediaPlayer mp
) {
139 Log_OC
.e(TAG
, "ACTIVITY\t\tonPrepare");
140 mVideoPlayer
.seekTo(mSavedPlaybackPosition
);
142 mVideoPlayer
.start();
144 mMediaController
.show(5000);
149 * Called when the file is finished playing.
153 * @param mp {@link MediaPlayer} instance performing the playback.
156 public void onCompletion(MediaPlayer mp
) {
157 mVideoPlayer
.seekTo(0);
162 * Called when an error in playback occurs.
164 * @param mp {@link MediaPlayer} instance performing the playback.
165 * @param what Type of error
166 * @param extra Extra code specific to the error
169 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
170 Log_OC
.e(TAG
, "Error in video playback, what = " + what
+ ", extra = " + extra
);
172 if (mMediaController
!= null
) {
173 mMediaController
.hide();
176 if (mVideoPlayer
.getWindowToken() != null
) {
177 String message
= MediaService
.getMessageForMediaError(this, what
, extra
);
178 new AlertDialog
.Builder(this)
180 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
181 new DialogInterface
.OnClickListener() {
182 public void onClick(DialogInterface dialog
, int whichButton
) {
183 PreviewVideoActivity
.this.onCompletion(null
);
186 .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
);