Merge branch 'develop' into refactor_remote_operation_to_create_folder
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewVideoActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.ui.preview;
19
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;
26
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;
39
40 import com.owncloud.android.oc_framework.accounts.AccountUtils;
41 import com.owncloud.android.oc_framework.accounts.AccountUtils.AccountNotFoundException;
42
43 /**
44 * Activity implementing a basic video player.
45 *
46 * Used as an utility to preview video files contained in an ownCloud account.
47 *
48 * Currently, it always plays in landscape mode, full screen. When the playback ends,
49 * the activity is finished.
50 *
51 * @author David A. Velasco
52 */
53 public class PreviewVideoActivity extends FileActivity implements OnCompletionListener, OnPreparedListener, OnErrorListener {
54
55 /** Key to receive a flag signaling if the video should be started immediately */
56 public static final String EXTRA_AUTOPLAY = "AUTOPLAY";
57
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";
60
61 private static final String TAG = PreviewVideoActivity.class.getSimpleName();
62
63 private FileDataStorageManager mStorageManager;
64
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
69
70 /**
71 * Called when the activity is first created.
72 *
73 * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
74 *
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
77 *
78 * {@inheritDoc}
79 */
80 @Override
81 public void onCreate(Bundle savedInstanceState) {
82 super.onCreate(savedInstanceState);
83 Log_OC.e(TAG, "ACTIVITY\t\tonCreate");
84
85 setContentView(R.layout.video_layout);
86
87 if (savedInstanceState == null) {
88 Bundle extras = getIntent().getExtras();
89 mSavedPlaybackPosition = extras.getInt(EXTRA_START_POSITION);
90 mAutoplay = extras.getBoolean(EXTRA_AUTOPLAY);
91
92 } else {
93 mSavedPlaybackPosition = savedInstanceState.getInt(EXTRA_START_POSITION);
94 mAutoplay = savedInstanceState.getBoolean(EXTRA_AUTOPLAY);
95 }
96
97 mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer);
98
99 // set listeners to get more contol on the playback
100 mVideoPlayer.setOnPreparedListener(this);
101 mVideoPlayer.setOnCompletionListener(this);
102 mVideoPlayer.setOnErrorListener(this);
103
104 // keep the screen on while the playback is performed (prevents screen off by battery save)
105 mVideoPlayer.setKeepScreenOn(true);
106 }
107
108
109 /**
110 * {@inheritDoc}
111 */
112 @Override
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());
118 }
119
120
121 @Override
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();
129 }
130
131
132 /**
133 * Called when the file is ready to be played.
134 *
135 * Just starts the playback.
136 *
137 * @param mp {@link MediaPlayer} instance performing the playback.
138 */
139 @Override
140 public void onPrepared(MediaPlayer mp) {
141 Log_OC.e(TAG, "ACTIVITY\t\tonPrepare");
142 mVideoPlayer.seekTo(mSavedPlaybackPosition);
143 if (mAutoplay) {
144 mVideoPlayer.start();
145 }
146 mMediaController.show(5000);
147 }
148
149
150 /**
151 * Called when the file is finished playing.
152 *
153 * Rewinds the video
154 *
155 * @param mp {@link MediaPlayer} instance performing the playback.
156 */
157 @Override
158 public void onCompletion(MediaPlayer mp) {
159 mVideoPlayer.seekTo(0);
160 }
161
162
163 /**
164 * Called when an error in playback occurs.
165 *
166 * @param mp {@link MediaPlayer} instance performing the playback.
167 * @param what Type of error
168 * @param extra Extra code specific to the error
169 */
170 @Override
171 public boolean onError(MediaPlayer mp, int what, int extra) {
172 Log_OC.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
173
174 if (mMediaController != null) {
175 mMediaController.hide();
176 }
177
178 if (mVideoPlayer.getWindowToken() != null) {
179 String message = MediaService.getMessageForMediaError(this, what, extra);
180 new AlertDialog.Builder(this)
181 .setMessage(message)
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);
186 }
187 })
188 .setCancelable(false)
189 .show();
190 }
191 return true;
192 }
193
194
195 @Override
196 protected void onAccountSet(boolean stateWasRecovered) {
197 if (getAccount() != null) {
198 OCFile file = getFile();
199 /// Validate handled file (first image to preview)
200 if (file == null) {
201 throw new IllegalStateException("Instanced with a NULL OCFile");
202 }
203 if (!file.isVideo()) {
204 throw new IllegalArgumentException("Non-video file passed as argument");
205 }
206 mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
207 file = mStorageManager.getFileById(file.getFileId());
208 if (file != null) {
209 if (file.isDown()) {
210 mVideoPlayer.setVideoPath(file.getStoragePath());
211
212 } else {
213 // not working yet
214 String url;
215 try {
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);
220 }
221 }
222
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);
228
229 } else {
230 finish();
231 }
232 } else {
233 Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
234 finish();
235 }
236 }
237
238
239 }