Improvementes in full screen video: tap on video shows and hides controls; full scree...
[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
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 outState.putParcelable(PreviewVideoActivity.EXTRA_FILE, mFile);
147 outState.putParcelable(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount);
148 outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
149 outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY , mVideoPlayer.isPlaying());
150 }
151
152
153 @Override
154 public void onBackPressed() {
155 Log.e(TAG, "onBackPressed");
156 Intent i = new Intent();
157 i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
158 i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
159 setResult(RESULT_OK, i);
160 super.onBackPressed();
161 }
162
163
164 /**
165 * Called when the file is ready to be played.
166 *
167 * Just starts the playback.
168 *
169 * @param mp {@link MediaPlayer} instance performing the playback.
170 */
171 @Override
172 public void onPrepared(MediaPlayer vp) {
173 mVideoPlayer.seekTo(mSavedPlaybackPosition);
174 if (mAutoplay) {
175 mVideoPlayer.start();
176 }
177 mMediaController.show(5000);
178 }
179
180
181 /**
182 * Called when the file is finished playing.
183 *
184 * Rewinds the video
185 *
186 * @param mp {@link MediaPlayer} instance performing the playback.
187 */
188 @Override
189 public void onCompletion(MediaPlayer mp) {
190 mVideoPlayer.seekTo(0);
191 }
192
193
194 /**
195 * Called when an error in playback occurs.
196 *
197 * @param mp {@link MediaPlayer} instance performing the playback.
198 * @param what Type of error
199 * @param extra Extra code specific to the error
200 */
201 @Override
202 public boolean onError(MediaPlayer mp, int what, int extra) {
203 Log.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
204
205 if (mMediaController != null) {
206 mMediaController.hide();
207 }
208
209 if (mVideoPlayer.getWindowToken() != null) {
210 String message = MediaService.getMessageForMediaError(this, what, extra);
211 new AlertDialog.Builder(this)
212 .setMessage(message)
213 .setPositiveButton(android.R.string.VideoView_error_button,
214 new DialogInterface.OnClickListener() {
215 public void onClick(DialogInterface dialog, int whichButton) {
216 PreviewVideoActivity.this.onCompletion(null);
217 }
218 })
219 .setCancelable(false)
220 .show();
221 }
222 return true;
223 }
224
225
226 /**
227 * Screen touches trigger the appearance of the control panel for a limited time.
228 *
229 * {@inheritDoc}
230 */
231 @Override
232 public boolean onTouchEvent (MotionEvent ev){
233 /*if (ev.getAction() == MotionEvent.ACTION_DOWN) {
234 if (mMediaController.isShowing()) {
235 mMediaController.hide();
236 } else {
237 mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE);
238 }
239 return true;
240 } else {
241 return false;
242 }*/
243 return false;
244 }
245
246
247 }