2013-02-11 14:33:50 +04:00
|
|
|
/* ownCloud Android client application
|
|
|
|
* Copyright (C) 2012-2013 ownCloud Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2013-04-17 14:26:13 +04:00
|
|
|
* it under the terms of the GNU General Public License version 2,
|
|
|
|
* as published by the Free Software Foundation.
|
2013-02-11 14:33:50 +04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-03-14 19:55:57 +04:00
|
|
|
package com.owncloud.android.ui.preview;
|
2013-02-07 14:09:28 +04:00
|
|
|
|
2013-02-11 14:33:50 +04:00
|
|
|
import android.accounts.Account;
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
2013-02-07 14:09:28 +04:00
|
|
|
import android.media.MediaPlayer;
|
|
|
|
import android.media.MediaPlayer.OnCompletionListener;
|
2013-02-11 14:33:50 +04:00
|
|
|
import android.media.MediaPlayer.OnErrorListener;
|
2013-02-07 14:09:28 +04:00
|
|
|
import android.media.MediaPlayer.OnPreparedListener;
|
2013-02-11 14:33:50 +04:00
|
|
|
import android.net.Uri;
|
2013-02-07 14:09:28 +04:00
|
|
|
import android.os.Bundle;
|
2013-02-11 14:33:50 +04:00
|
|
|
import android.widget.MediaController;
|
2013-02-07 14:09:28 +04:00
|
|
|
import android.widget.VideoView;
|
|
|
|
|
2013-04-25 21:39:22 +04:00
|
|
|
import com.owncloud.android.Log_OC;
|
2013-02-07 14:09:28 +04:00
|
|
|
import com.owncloud.android.R;
|
2013-06-14 18:59:38 +04:00
|
|
|
import com.owncloud.android.datamodel.DataStorageManager;
|
|
|
|
import com.owncloud.android.datamodel.FileDataStorageManager;
|
2013-06-18 13:34:08 +04:00
|
|
|
import com.owncloud.android.authentication.AccountUtils;
|
2013-06-18 16:04:51 +04:00
|
|
|
import com.owncloud.android.authentication.AccountUtils.AccountNotFoundException;
|
2013-02-11 14:33:50 +04:00
|
|
|
import com.owncloud.android.datamodel.OCFile;
|
2013-02-12 14:26:38 +04:00
|
|
|
import com.owncloud.android.media.MediaService;
|
2013-06-14 18:59:38 +04:00
|
|
|
import com.owncloud.android.ui.activity.FileActivity;
|
2013-02-11 14:33:50 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Activity implementing a basic video player.
|
|
|
|
*
|
|
|
|
* Used as an utility to preview video files contained in an ownCloud account.
|
|
|
|
*
|
|
|
|
* Currently, it always plays in landscape mode, full screen. When the playback ends,
|
|
|
|
* the activity is finished.
|
|
|
|
*
|
|
|
|
* @author David A. Velasco
|
|
|
|
*/
|
2013-06-14 18:59:38 +04:00
|
|
|
public class PreviewVideoActivity extends FileActivity implements OnCompletionListener, OnPreparedListener, OnErrorListener {
|
2013-02-11 14:33:50 +04:00
|
|
|
|
2013-03-14 19:55:57 +04:00
|
|
|
/** Key to receive a flag signaling if the video should be started immediately */
|
|
|
|
public static final String EXTRA_AUTOPLAY = "AUTOPLAY";
|
|
|
|
|
|
|
|
/** Key to receive the position of the playback where the video should be put at start */
|
|
|
|
public static final String EXTRA_START_POSITION = "START_POSITION";
|
|
|
|
|
|
|
|
private static final String TAG = PreviewVideoActivity.class.getSimpleName();
|
2013-02-11 14:33:50 +04:00
|
|
|
|
2013-06-14 18:59:38 +04:00
|
|
|
private DataStorageManager mStorageManager;
|
|
|
|
|
2013-03-14 19:55:57 +04:00
|
|
|
private int mSavedPlaybackPosition; // in the unit time handled by MediaPlayer.getCurrentPosition()
|
|
|
|
private boolean mAutoplay; // when 'true', the playback starts immediately with the activity
|
2013-02-11 14:33:50 +04:00
|
|
|
private VideoView mVideoPlayer; // view to play the file; both performs and show the playback
|
|
|
|
private MediaController mMediaController; // panel control used by the user to control the playback
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the activity is first created.
|
|
|
|
*
|
|
|
|
* Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
|
|
|
|
*
|
|
|
|
* The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
|
|
|
|
* try to stream the remote file - TODO get the streaming works
|
|
|
|
*
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2013-04-25 21:39:22 +04:00
|
|
|
Log_OC.e(TAG, "ACTIVITY\t\tonCreate");
|
2013-02-11 14:33:50 +04:00
|
|
|
|
|
|
|
setContentView(R.layout.video_layout);
|
|
|
|
|
2013-03-21 18:49:10 +04:00
|
|
|
if (savedInstanceState == null) {
|
|
|
|
Bundle extras = getIntent().getExtras();
|
|
|
|
mSavedPlaybackPosition = extras.getInt(EXTRA_START_POSITION);
|
|
|
|
mAutoplay = extras.getBoolean(EXTRA_AUTOPLAY);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
mSavedPlaybackPosition = savedInstanceState.getInt(EXTRA_START_POSITION);
|
|
|
|
mAutoplay = savedInstanceState.getBoolean(EXTRA_AUTOPLAY);
|
|
|
|
}
|
2013-02-11 14:33:50 +04:00
|
|
|
|
|
|
|
mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer);
|
|
|
|
|
|
|
|
// set listeners to get more contol on the playback
|
|
|
|
mVideoPlayer.setOnPreparedListener(this);
|
|
|
|
mVideoPlayer.setOnCompletionListener(this);
|
|
|
|
mVideoPlayer.setOnErrorListener(this);
|
|
|
|
|
|
|
|
// keep the screen on while the playback is performed (prevents screen off by battery save)
|
|
|
|
mVideoPlayer.setKeepScreenOn(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-21 18:49:10 +04:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
2013-04-25 21:39:22 +04:00
|
|
|
Log_OC.e(TAG, "ACTIVITY\t\tonSaveInstanceState");
|
2013-03-21 18:49:10 +04:00
|
|
|
outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
|
|
|
|
outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY , mVideoPlayer.isPlaying());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-14 19:55:57 +04:00
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
2013-04-25 21:39:22 +04:00
|
|
|
Log_OC.e(TAG, "ACTIVTIY\t\tonBackPressed");
|
2013-03-14 19:55:57 +04:00
|
|
|
Intent i = new Intent();
|
|
|
|
i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
|
|
|
|
i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
|
|
|
|
setResult(RESULT_OK, i);
|
|
|
|
super.onBackPressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-11 14:33:50 +04:00
|
|
|
/**
|
|
|
|
* Called when the file is ready to be played.
|
|
|
|
*
|
|
|
|
* Just starts the playback.
|
|
|
|
*
|
|
|
|
* @param mp {@link MediaPlayer} instance performing the playback.
|
|
|
|
*/
|
|
|
|
@Override
|
2013-03-22 16:04:57 +04:00
|
|
|
public void onPrepared(MediaPlayer mp) {
|
2013-04-25 21:39:22 +04:00
|
|
|
Log_OC.e(TAG, "ACTIVITY\t\tonPrepare");
|
2013-03-14 19:55:57 +04:00
|
|
|
mVideoPlayer.seekTo(mSavedPlaybackPosition);
|
|
|
|
if (mAutoplay) {
|
|
|
|
mVideoPlayer.start();
|
|
|
|
}
|
2013-02-12 14:26:38 +04:00
|
|
|
mMediaController.show(5000);
|
2013-02-11 14:33:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the file is finished playing.
|
|
|
|
*
|
2013-03-14 19:55:57 +04:00
|
|
|
* Rewinds the video
|
2013-02-11 14:33:50 +04:00
|
|
|
*
|
|
|
|
* @param mp {@link MediaPlayer} instance performing the playback.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onCompletion(MediaPlayer mp) {
|
2013-03-14 19:55:57 +04:00
|
|
|
mVideoPlayer.seekTo(0);
|
2013-02-11 14:33:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when an error in playback occurs.
|
|
|
|
*
|
|
|
|
* @param mp {@link MediaPlayer} instance performing the playback.
|
|
|
|
* @param what Type of error
|
|
|
|
* @param extra Extra code specific to the error
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean onError(MediaPlayer mp, int what, int extra) {
|
2013-04-25 21:39:22 +04:00
|
|
|
Log_OC.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
|
2013-02-11 14:33:50 +04:00
|
|
|
|
|
|
|
if (mMediaController != null) {
|
|
|
|
mMediaController.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVideoPlayer.getWindowToken() != null) {
|
2013-02-12 15:07:42 +04:00
|
|
|
String message = MediaService.getMessageForMediaError(this, what, extra);
|
2013-02-11 14:33:50 +04:00
|
|
|
new AlertDialog.Builder(this)
|
2013-02-12 15:07:42 +04:00
|
|
|
.setMessage(message)
|
2013-02-11 14:33:50 +04:00
|
|
|
.setPositiveButton(android.R.string.VideoView_error_button,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int whichButton) {
|
2013-02-25 15:24:14 +04:00
|
|
|
PreviewVideoActivity.this.onCompletion(null);
|
2013-02-11 14:33:50 +04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.setCancelable(false)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-06-14 18:59:38 +04:00
|
|
|
protected void onAccountSet(boolean stateWasRecovered) {
|
|
|
|
if (getAccount() != null) {
|
|
|
|
OCFile file = getFile();
|
|
|
|
/// Validate handled file (first image to preview)
|
|
|
|
if (file == null) {
|
|
|
|
throw new IllegalStateException("Instanced with a NULL OCFile");
|
|
|
|
}
|
|
|
|
if (!file.isVideo()) {
|
|
|
|
throw new IllegalArgumentException("Non-video file passed as argument");
|
|
|
|
}
|
|
|
|
mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
|
|
|
|
file = mStorageManager.getFileById(file.getFileId());
|
|
|
|
if (file != null) {
|
|
|
|
if (file.isDown()) {
|
|
|
|
mVideoPlayer.setVideoPath(file.getStoragePath());
|
|
|
|
|
|
|
|
} else {
|
2013-06-28 13:58:43 +04:00
|
|
|
// not working yet
|
|
|
|
String url;
|
|
|
|
try {
|
|
|
|
url = AccountUtils.constructFullURLForAccount(this, getAccount()) + file.getRemotePath();
|
|
|
|
mVideoPlayer.setVideoURI(Uri.parse(url));
|
|
|
|
} catch (AccountNotFoundException e) {
|
|
|
|
onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
|
|
|
|
}
|
2013-06-14 18:59:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// create and prepare control panel for the user
|
|
|
|
mMediaController = new MediaController(this);
|
|
|
|
mMediaController.setMediaPlayer(mVideoPlayer);
|
|
|
|
mMediaController.setAnchorView(mVideoPlayer);
|
|
|
|
mVideoPlayer.setMediaController(mMediaController);
|
|
|
|
|
2013-03-21 18:49:10 +04:00
|
|
|
} else {
|
2013-06-14 18:59:38 +04:00
|
|
|
finish();
|
2013-03-21 18:49:10 +04:00
|
|
|
}
|
2013-02-11 14:33:50 +04:00
|
|
|
} else {
|
2013-06-14 18:59:38 +04:00
|
|
|
Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
2013-02-11 14:33:50 +04:00
|
|
|
|
2013-02-07 14:09:28 +04:00
|
|
|
|
|
|
|
}
|