106f918a8ff47982f39090672b8cc9b13f1b7256
[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 as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.ui.preview;
20
21 import android.accounts.Account;
22 import android.app.Activity;
23 import android.app.AlertDialog;
24 import android.content.DialogInterface;
25 import android.content.Intent;
26 import android.media.MediaPlayer;
27 import android.media.MediaPlayer.OnCompletionListener;
28 import android.media.MediaPlayer.OnErrorListener;
29 import android.media.MediaPlayer.OnPreparedListener;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.util.Log;
33 import android.view.MotionEvent;
34 import android.widget.MediaController;
35 import android.widget.VideoView;
36
37 import com.owncloud.android.AccountUtils;
38 import com.owncloud.android.R;
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.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 now
122 String url = AccountUtils.constructFullURLForAccount(this, mAccount) + mFile.getRemotePath();
123 mVideoPlayer.setVideoURI(Uri.parse(url));
124
125 } else {
126 onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
127 }
128
129 // create and prepare control panel for the user
130 mMediaController = new MediaController(this);
131 mMediaController.setMediaPlayer(mVideoPlayer);
132 mMediaController.setAnchorView(mVideoPlayer);
133 mVideoPlayer.setMediaController(mMediaController);
134
135 } else {
136 onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_nothing_to_play);
137 }
138 }
139
140
141 /**
142 * {@inheritDoc}
143 */
144 @Override
145 public void onSaveInstanceState(Bundle outState) {
146 super.onSaveInstanceState(outState);
147 Log.e(TAG, "ACTIVITY\t\tonSaveInstanceState");
148 outState.putParcelable(PreviewVideoActivity.EXTRA_FILE, mFile);
149 outState.putParcelable(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount);
150 outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
151 outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY , mVideoPlayer.isPlaying());
152 }
153
154
155 @Override
156 public void onBackPressed() {
157 Log.e(TAG, "ACTIVTIY\t\tonBackPressed");
158 Intent i = new Intent();
159 i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
160 i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
161 setResult(RESULT_OK, i);
162 super.onBackPressed();
163 }
164
165
166 @Override
167 public void onResume() {
168 super.onResume();
169 Log.e(TAG, "ACTIVTIY\t\tonResume");
170 }
171
172
173 @Override
174 public void onStart() {
175 super.onStart();
176 Log.e(TAG, "ACTIVTIY\t\tonStart");
177 }
178
179 @Override
180 public void onDestroy() {
181 super.onDestroy();
182 Log.e(TAG, "ACTIVITY\t\tonDestroy");
183 }
184
185 @Override
186 public void onStop() {
187 super.onStop();
188 Log.e(TAG, "ACTIVTIY\t\tonStop");
189 }
190
191
192 @Override
193 public void onPause() {
194 super.onPause();
195 Log.e(TAG, "ACTIVTIY\t\tonPause");
196 }
197
198
199 /**
200 * Called when the file is ready to be played.
201 *
202 * Just starts the playback.
203 *
204 * @param mp {@link MediaPlayer} instance performing the playback.
205 */
206 @Override
207 public void onPrepared(MediaPlayer mp) {
208 Log.e(TAG, "ACTIVITY\t\tonPrepare");
209 mVideoPlayer.seekTo(mSavedPlaybackPosition);
210 if (mAutoplay) {
211 mVideoPlayer.start();
212 }
213 mMediaController.show(5000);
214 }
215
216
217 /**
218 * Called when the file is finished playing.
219 *
220 * Rewinds the video
221 *
222 * @param mp {@link MediaPlayer} instance performing the playback.
223 */
224 @Override
225 public void onCompletion(MediaPlayer mp) {
226 mVideoPlayer.seekTo(0);
227 }
228
229
230 /**
231 * Called when an error in playback occurs.
232 *
233 * @param mp {@link MediaPlayer} instance performing the playback.
234 * @param what Type of error
235 * @param extra Extra code specific to the error
236 */
237 @Override
238 public boolean onError(MediaPlayer mp, int what, int extra) {
239 Log.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
240
241 if (mMediaController != null) {
242 mMediaController.hide();
243 }
244
245 if (mVideoPlayer.getWindowToken() != null) {
246 String message = MediaService.getMessageForMediaError(this, what, extra);
247 new AlertDialog.Builder(this)
248 .setMessage(message)
249 .setPositiveButton(android.R.string.VideoView_error_button,
250 new DialogInterface.OnClickListener() {
251 public void onClick(DialogInterface dialog, int whichButton) {
252 PreviewVideoActivity.this.onCompletion(null);
253 }
254 })
255 .setCancelable(false)
256 .show();
257 }
258 return true;
259 }
260
261
262 /**
263 * Screen touches trigger the appearance of the control panel for a limited time.
264 *
265 * {@inheritDoc}
266 */
267 @Override
268 public boolean onTouchEvent (MotionEvent ev){
269 /*if (ev.getAction() == MotionEvent.ACTION_DOWN) {
270 if (mMediaController.isShowing()) {
271 mMediaController.hide();
272 } else {
273 mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE);
274 }
275 return true;
276 } else {
277 return false;
278 }*/
279 return false;
280 }
281
282
283 }