Merge pull request #197 from owncloud/fixed_crash_due_to_uploads_to_accounts_not_alre...
[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 android.accounts.Account;
21 import android.app.Activity;
22 import android.app.AlertDialog;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.media.MediaPlayer;
26 import android.media.MediaPlayer.OnCompletionListener;
27 import android.media.MediaPlayer.OnErrorListener;
28 import android.media.MediaPlayer.OnPreparedListener;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.view.MotionEvent;
32 import android.widget.MediaController;
33 import android.widget.VideoView;
34
35 import com.owncloud.android.Log_OC;
36 import com.owncloud.android.R;
37 import com.owncloud.android.authentication.AccountUtils;
38 import com.owncloud.android.authentication.AccountUtils.AccountNotFoundException;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.media.MediaService;
41
42 /**
43 * Activity implementing a basic video player.
44 *
45 * Used as an utility to preview video files contained in an ownCloud account.
46 *
47 * Currently, it always plays in landscape mode, full screen. When the playback ends,
48 * the activity is finished.
49 *
50 * @author David A. Velasco
51 */
52 public class PreviewVideoActivity extends Activity implements OnCompletionListener, OnPreparedListener, OnErrorListener {
53
54 /** Key to receive an {@link OCFile} to play as an extra value in an {@link Intent} */
55 public static final String EXTRA_FILE = "FILE";
56
57 /** Key to receive the ownCloud {@link Account} where the file to play is saved as an extra value in an {@link Intent} */
58 public static final String EXTRA_ACCOUNT = "ACCOUNT";
59
60 /** Key to receive a flag signaling if the video should be started immediately */
61 public static final String EXTRA_AUTOPLAY = "AUTOPLAY";
62
63 /** Key to receive the position of the playback where the video should be put at start */
64 public static final String EXTRA_START_POSITION = "START_POSITION";
65
66 private static final String TAG = PreviewVideoActivity.class.getSimpleName();
67
68 private OCFile mFile; // video file to play
69 private Account mAccount; // ownCloud account holding mFile
70 private int mSavedPlaybackPosition; // in the unit time handled by MediaPlayer.getCurrentPosition()
71 private boolean mAutoplay; // when 'true', the playback starts immediately with the activity
72 private VideoView mVideoPlayer; // view to play the file; both performs and show the playback
73 private MediaController mMediaController; // panel control used by the user to control the playback
74
75 /**
76 * Called when the activity is first created.
77 *
78 * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
79 *
80 * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
81 * try to stream the remote file - TODO get the streaming works
82 *
83 * {@inheritDoc}
84 */
85 @Override
86 public void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88 Log_OC.e(TAG, "ACTIVITY\t\tonCreate");
89
90 setContentView(R.layout.video_layout);
91
92 if (savedInstanceState == null) {
93 Bundle extras = getIntent().getExtras();
94 mFile = extras.getParcelable(EXTRA_FILE);
95 mAccount = extras.getParcelable(EXTRA_ACCOUNT);
96 mSavedPlaybackPosition = extras.getInt(EXTRA_START_POSITION);
97 mAutoplay = extras.getBoolean(EXTRA_AUTOPLAY);
98
99 } else {
100 mFile = savedInstanceState.getParcelable(EXTRA_FILE);
101 mAccount = savedInstanceState.getParcelable(EXTRA_ACCOUNT);
102 mSavedPlaybackPosition = savedInstanceState.getInt(EXTRA_START_POSITION);
103 mAutoplay = savedInstanceState.getBoolean(EXTRA_AUTOPLAY);
104 }
105
106 mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer);
107
108 // set listeners to get more contol on the playback
109 mVideoPlayer.setOnPreparedListener(this);
110 mVideoPlayer.setOnCompletionListener(this);
111 mVideoPlayer.setOnErrorListener(this);
112
113 // keep the screen on while the playback is performed (prevents screen off by battery save)
114 mVideoPlayer.setKeepScreenOn(true);
115
116 if (mFile != null) {
117 if (mFile.isDown()) {
118 mVideoPlayer.setVideoPath(mFile.getStoragePath());
119
120 } else if (mAccount != null) {
121 // not working yet
122 String url;
123 try {
124 url = AccountUtils.constructFullURLForAccount(this, mAccount) + mFile.getRemotePath();
125 mVideoPlayer.setVideoURI(Uri.parse(url));
126 } catch (AccountNotFoundException e) {
127 onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
128 }
129
130 } else {
131 onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
132 }
133
134 // create and prepare control panel for the user
135 mMediaController = new MediaController(this);
136 mMediaController.setMediaPlayer(mVideoPlayer);
137 mMediaController.setAnchorView(mVideoPlayer);
138 mVideoPlayer.setMediaController(mMediaController);
139
140 } else {
141 onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_nothing_to_play);
142 }
143 }
144
145
146 /**
147 * {@inheritDoc}
148 */
149 @Override
150 public void onSaveInstanceState(Bundle outState) {
151 super.onSaveInstanceState(outState);
152 Log_OC.e(TAG, "ACTIVITY\t\tonSaveInstanceState");
153 outState.putParcelable(PreviewVideoActivity.EXTRA_FILE, mFile);
154 outState.putParcelable(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount);
155 outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
156 outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY , mVideoPlayer.isPlaying());
157 }
158
159
160 @Override
161 public void onBackPressed() {
162 Log_OC.e(TAG, "ACTIVTIY\t\tonBackPressed");
163 Intent i = new Intent();
164 i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
165 i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
166 setResult(RESULT_OK, i);
167 super.onBackPressed();
168 }
169
170
171 @Override
172 public void onResume() {
173 super.onResume();
174 Log_OC.e(TAG, "ACTIVTIY\t\tonResume");
175 }
176
177
178 @Override
179 public void onStart() {
180 super.onStart();
181 Log_OC.e(TAG, "ACTIVTIY\t\tonStart");
182 }
183
184 @Override
185 public void onDestroy() {
186 super.onDestroy();
187 Log_OC.e(TAG, "ACTIVITY\t\tonDestroy");
188 }
189
190 @Override
191 public void onStop() {
192 super.onStop();
193 Log_OC.e(TAG, "ACTIVTIY\t\tonStop");
194 }
195
196
197 @Override
198 public void onPause() {
199 super.onPause();
200 Log_OC.e(TAG, "ACTIVTIY\t\tonPause");
201 }
202
203
204 /**
205 * Called when the file is ready to be played.
206 *
207 * Just starts the playback.
208 *
209 * @param mp {@link MediaPlayer} instance performing the playback.
210 */
211 @Override
212 public void onPrepared(MediaPlayer mp) {
213 Log_OC.e(TAG, "ACTIVITY\t\tonPrepare");
214 mVideoPlayer.seekTo(mSavedPlaybackPosition);
215 if (mAutoplay) {
216 mVideoPlayer.start();
217 }
218 mMediaController.show(5000);
219 }
220
221
222 /**
223 * Called when the file is finished playing.
224 *
225 * Rewinds the video
226 *
227 * @param mp {@link MediaPlayer} instance performing the playback.
228 */
229 @Override
230 public void onCompletion(MediaPlayer mp) {
231 mVideoPlayer.seekTo(0);
232 }
233
234
235 /**
236 * Called when an error in playback occurs.
237 *
238 * @param mp {@link MediaPlayer} instance performing the playback.
239 * @param what Type of error
240 * @param extra Extra code specific to the error
241 */
242 @Override
243 public boolean onError(MediaPlayer mp, int what, int extra) {
244 Log_OC.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
245
246 if (mMediaController != null) {
247 mMediaController.hide();
248 }
249
250 if (mVideoPlayer.getWindowToken() != null) {
251 String message = MediaService.getMessageForMediaError(this, what, extra);
252 new AlertDialog.Builder(this)
253 .setMessage(message)
254 .setPositiveButton(android.R.string.VideoView_error_button,
255 new DialogInterface.OnClickListener() {
256 public void onClick(DialogInterface dialog, int whichButton) {
257 PreviewVideoActivity.this.onCompletion(null);
258 }
259 })
260 .setCancelable(false)
261 .show();
262 }
263 return true;
264 }
265
266
267 /**
268 * Screen touches trigger the appearance of the control panel for a limited time.
269 *
270 * {@inheritDoc}
271 */
272 @Override
273 public boolean onTouchEvent (MotionEvent ev){
274 /*if (ev.getAction() == MotionEvent.ACTION_DOWN) {
275 if (mMediaController.isShowing()) {
276 mMediaController.hide();
277 } else {
278 mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE);
279 }
280 return true;
281 } else {
282 return false;
283 }*/
284 return false;
285 }
286
287
288 }