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
.Log_OC
; 
  21 import com
.owncloud
.android
.R
; 
  22 import com
.owncloud
.android
.authentication
.AccountUtils
; 
  23 import com
.owncloud
.android
.authentication
.AccountUtils
.AccountNotFoundException
; 
  24 import com
.owncloud
.android
.datamodel
.DataStorageManager
; 
  25 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
; 
  26 import com
.owncloud
.android
.datamodel
.OCFile
; 
  27 import com
.owncloud
.android
.media
.MediaService
; 
  28 import com
.owncloud
.android
.ui
.activity
.FileActivity
; 
  30 import android
.accounts
.Account
; 
  31 import android
.app
.AlertDialog
; 
  32 import android
.content
.DialogInterface
; 
  33 import android
.content
.Intent
; 
  34 import android
.media
.MediaPlayer
; 
  35 import android
.media
.MediaPlayer
.OnCompletionListener
; 
  36 import android
.media
.MediaPlayer
.OnErrorListener
; 
  37 import android
.media
.MediaPlayer
.OnPreparedListener
; 
  38 import android
.net
.Uri
; 
  39 import android
.os
.Bundle
; 
  40 import android
.widget
.MediaController
; 
  41 import android
.widget
.VideoView
; 
  45  *  Activity implementing a basic video player. 
  47  *  Used as an utility to preview video files contained in an ownCloud account. 
  49  *  Currently, it always plays in landscape mode, full screen. When the playback ends, 
  50  *  the activity is finished.  
  52  *  @author David A. Velasco 
  54 public class PreviewVideoActivity 
extends FileActivity 
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener 
{ 
  56     /** Key to receive a flag signaling if the video should be started immediately */ 
  57     public static final String EXTRA_AUTOPLAY 
= "AUTOPLAY"; 
  59     /** Key to receive the position of the playback where the video should be put at start */ 
  60     public static final String EXTRA_START_POSITION 
= "START_POSITION"; 
  62     private static final String TAG 
= PreviewVideoActivity
.class.getSimpleName(); 
  64     private DataStorageManager mStorageManager
; 
  66     private int mSavedPlaybackPosition
;         // in the unit time handled by MediaPlayer.getCurrentPosition() 
  67     private boolean mAutoplay
;                  // when 'true', the playback starts immediately with the activity 
  68     private VideoView mVideoPlayer
;             // view to play the file; both performs and show the playback 
  69     private MediaController mMediaController
;   // panel control used by the user to control the playback 
  72      *  Called when the activity is first created. 
  74      *  Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}. 
  76      *  The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to  
  77      *  try to stream the remote file - TODO get the streaming works 
  82     public void onCreate(Bundle savedInstanceState
) { 
  83         super.onCreate(savedInstanceState
); 
  84         Log_OC
.e(TAG
, "ACTIVITY\t\tonCreate"); 
  86         setContentView(R
.layout
.video_layout
); 
  88         if (savedInstanceState 
== null
) { 
  89             Bundle extras 
= getIntent().getExtras(); 
  90             mSavedPlaybackPosition 
= extras
.getInt(EXTRA_START_POSITION
); 
  91             mAutoplay 
= extras
.getBoolean(EXTRA_AUTOPLAY
); 
  94             mSavedPlaybackPosition 
= savedInstanceState
.getInt(EXTRA_START_POSITION
); 
  95             mAutoplay 
= savedInstanceState
.getBoolean(EXTRA_AUTOPLAY
); 
  98         mVideoPlayer 
= (VideoView
) findViewById(R
.id
.videoPlayer
); 
 100         // set listeners to get more contol on the playback 
 101         mVideoPlayer
.setOnPreparedListener(this); 
 102         mVideoPlayer
.setOnCompletionListener(this); 
 103         mVideoPlayer
.setOnErrorListener(this); 
 105         // keep the screen on while the playback is performed (prevents screen off by battery save) 
 106         mVideoPlayer
.setKeepScreenOn(true
); 
 114     public void onSaveInstanceState(Bundle outState
) { 
 115         super.onSaveInstanceState(outState
); 
 116         Log_OC
.e(TAG
, "ACTIVITY\t\tonSaveInstanceState"); 
 117         outState
.putInt(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition()); 
 118         outState
.putBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY 
, mVideoPlayer
.isPlaying()); 
 123     public void onBackPressed() { 
 124         Log_OC
.e(TAG
, "ACTIVTIY\t\tonBackPressed"); 
 125         Intent i 
= new Intent(); 
 126         i
.putExtra(EXTRA_AUTOPLAY
, mVideoPlayer
.isPlaying()); 
 127         i
.putExtra(EXTRA_START_POSITION
, mVideoPlayer
.getCurrentPosition()); 
 128         setResult(RESULT_OK
, i
); 
 129         super.onBackPressed(); 
 134      * Called when the file is ready to be played. 
 136      * Just starts the playback. 
 138      * @param   mp    {@link MediaPlayer} instance performing the playback. 
 141     public void onPrepared(MediaPlayer mp
) { 
 142         Log_OC
.e(TAG
, "ACTIVITY\t\tonPrepare"); 
 143         mVideoPlayer
.seekTo(mSavedPlaybackPosition
); 
 145             mVideoPlayer
.start(); 
 147         mMediaController
.show(5000);   
 152      * Called when the file is finished playing. 
 156      * @param   mp    {@link MediaPlayer} instance performing the playback. 
 159     public void onCompletion(MediaPlayer  mp
) { 
 160         mVideoPlayer
.seekTo(0); 
 165      * Called when an error in playback occurs. 
 167      * @param   mp      {@link MediaPlayer} instance performing the playback. 
 168      * @param   what    Type of error 
 169      * @param   extra   Extra code specific to the error 
 172     public boolean onError(MediaPlayer mp
, int what
, int extra
) { 
 173         Log_OC
.e(TAG
, "Error in video playback, what = " + what 
+ ", extra = " + extra
); 
 175         if (mMediaController 
!= null
) { 
 176             mMediaController
.hide(); 
 179         if (mVideoPlayer
.getWindowToken() != null
) { 
 180             String message 
= MediaService
.getMessageForMediaError(this, what
, extra
); 
 181             new AlertDialog
.Builder(this) 
 183                     .setPositiveButton(android
.R
.string
.VideoView_error_button
, 
 184                             new DialogInterface
.OnClickListener() { 
 185                                 public void onClick(DialogInterface dialog
, int whichButton
) { 
 186                                     PreviewVideoActivity
.this.onCompletion(null
); 
 189                     .setCancelable(false
) 
 197     protected void onAccountSet(boolean stateWasRecovered
) { 
 198         if (getAccount() != null
) { 
 199             OCFile file 
= getFile(); 
 200             /// Validate handled file  (first image to preview) 
 202                 throw new IllegalStateException("Instanced with a NULL OCFile"); 
 204             if (!file
.isVideo()) { 
 205                 throw new IllegalArgumentException("Non-video file passed as argument"); 
 207             mStorageManager 
= new FileDataStorageManager(getAccount(), getContentResolver()); 
 208             file 
= mStorageManager
.getFileById(file
.getFileId());  
 211                     mVideoPlayer
.setVideoPath(file
.getStoragePath()); 
 217                         url 
= AccountUtils
.constructFullURLForAccount(this, getAccount()) + file
.getRemotePath(); 
 218                         mVideoPlayer
.setVideoURI(Uri
.parse(url
)); 
 219                     } catch (AccountNotFoundException e
) { 
 220                         onError(null
, MediaService
.OC_MEDIA_ERROR
, R
.string
.media_err_no_account
); 
 224                 // create and prepare control panel for the user 
 225                 mMediaController 
= new MediaController(this); 
 226                 mMediaController
.setMediaPlayer(mVideoPlayer
); 
 227                 mMediaController
.setAnchorView(mVideoPlayer
); 
 228                 mVideoPlayer
.setMediaController(mMediaController
); 
 234             Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");