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