47441ffb88b6441729217be831bb3389f7954e35
[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.AlertDialog;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.media.MediaPlayer;
25 import android.media.MediaPlayer.OnCompletionListener;
26 import android.media.MediaPlayer.OnErrorListener;
27 import android.media.MediaPlayer.OnPreparedListener;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.widget.MediaController;
31 import android.widget.VideoView;
32
33 import com.owncloud.android.Log_OC;
34 import com.owncloud.android.R;
35 import com.owncloud.android.datamodel.FileDataStorageManager;
36 import com.owncloud.android.authentication.AccountUtils;
37 import com.owncloud.android.authentication.AccountUtils.AccountNotFoundException;
38 import com.owncloud.android.datamodel.OCFile;
39 import com.owncloud.android.media.MediaService;
40 import com.owncloud.android.ui.activity.FileActivity;
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 FileActivity implements OnCompletionListener, OnPreparedListener, OnErrorListener {
53
54 /** Key to receive a flag signaling if the video should be started immediately */
55 public static final String EXTRA_AUTOPLAY = "AUTOPLAY";
56
57 /** Key to receive the position of the playback where the video should be put at start */
58 public static final String EXTRA_START_POSITION = "START_POSITION";
59
60 private static final String TAG = PreviewVideoActivity.class.getSimpleName();
61
62 private FileDataStorageManager mStorageManager;
63
64 private int mSavedPlaybackPosition; // in the unit time handled by MediaPlayer.getCurrentPosition()
65 private boolean mAutoplay; // when 'true', the playback starts immediately with the activity
66 private VideoView mVideoPlayer; // view to play the file; both performs and show the playback
67 private MediaController mMediaController; // panel control used by the user to control the playback
68
69 /**
70 * Called when the activity is first created.
71 *
72 * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
73 *
74 * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
75 * try to stream the remote file - TODO get the streaming works
76 *
77 * {@inheritDoc}
78 */
79 @Override
80 public void onCreate(Bundle savedInstanceState) {
81 super.onCreate(savedInstanceState);
82 Log_OC.e(TAG, "ACTIVITY\t\tonCreate");
83
84 setContentView(R.layout.video_layout);
85
86 if (savedInstanceState == null) {
87 Bundle extras = getIntent().getExtras();
88 mSavedPlaybackPosition = extras.getInt(EXTRA_START_POSITION);
89 mAutoplay = extras.getBoolean(EXTRA_AUTOPLAY);
90
91 } else {
92 mSavedPlaybackPosition = savedInstanceState.getInt(EXTRA_START_POSITION);
93 mAutoplay = savedInstanceState.getBoolean(EXTRA_AUTOPLAY);
94 }
95
96 mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer);
97
98 // set listeners to get more contol on the playback
99 mVideoPlayer.setOnPreparedListener(this);
100 mVideoPlayer.setOnCompletionListener(this);
101 mVideoPlayer.setOnErrorListener(this);
102
103 // keep the screen on while the playback is performed (prevents screen off by battery save)
104 mVideoPlayer.setKeepScreenOn(true);
105 }
106
107
108 /**
109 * {@inheritDoc}
110 */
111 @Override
112 public void onSaveInstanceState(Bundle outState) {
113 super.onSaveInstanceState(outState);
114 Log_OC.e(TAG, "ACTIVITY\t\tonSaveInstanceState");
115 outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
116 outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY , mVideoPlayer.isPlaying());
117 }
118
119
120 @Override
121 public void onBackPressed() {
122 Log_OC.e(TAG, "ACTIVTIY\t\tonBackPressed");
123 Intent i = new Intent();
124 i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
125 i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
126 setResult(RESULT_OK, i);
127 super.onBackPressed();
128 }
129
130
131 /**
132 * Called when the file is ready to be played.
133 *
134 * Just starts the playback.
135 *
136 * @param mp {@link MediaPlayer} instance performing the playback.
137 */
138 @Override
139 public void onPrepared(MediaPlayer mp) {
140 Log_OC.e(TAG, "ACTIVITY\t\tonPrepare");
141 mVideoPlayer.seekTo(mSavedPlaybackPosition);
142 if (mAutoplay) {
143 mVideoPlayer.start();
144 }
145 mMediaController.show(5000);
146 }
147
148
149 /**
150 * Called when the file is finished playing.
151 *
152 * Rewinds the video
153 *
154 * @param mp {@link MediaPlayer} instance performing the playback.
155 */
156 @Override
157 public void onCompletion(MediaPlayer mp) {
158 mVideoPlayer.seekTo(0);
159 }
160
161
162 /**
163 * Called when an error in playback occurs.
164 *
165 * @param mp {@link MediaPlayer} instance performing the playback.
166 * @param what Type of error
167 * @param extra Extra code specific to the error
168 */
169 @Override
170 public boolean onError(MediaPlayer mp, int what, int extra) {
171 Log_OC.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
172
173 if (mMediaController != null) {
174 mMediaController.hide();
175 }
176
177 if (mVideoPlayer.getWindowToken() != null) {
178 String message = MediaService.getMessageForMediaError(this, what, extra);
179 new AlertDialog.Builder(this)
180 .setMessage(message)
181 .setPositiveButton(android.R.string.VideoView_error_button,
182 new DialogInterface.OnClickListener() {
183 public void onClick(DialogInterface dialog, int whichButton) {
184 PreviewVideoActivity.this.onCompletion(null);
185 }
186 })
187 .setCancelable(false)
188 .show();
189 }
190 return true;
191 }
192
193
194 @Override
195 protected void onAccountSet(boolean stateWasRecovered) {
196 if (getAccount() != null) {
197 OCFile file = getFile();
198 /// Validate handled file (first image to preview)
199 if (file == null) {
200 throw new IllegalStateException("Instanced with a NULL OCFile");
201 }
202 if (!file.isVideo()) {
203 throw new IllegalArgumentException("Non-video file passed as argument");
204 }
205 mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
206 file = mStorageManager.getFileById(file.getFileId());
207 if (file != null) {
208 if (file.isDown()) {
209 mVideoPlayer.setVideoPath(file.getStoragePath());
210
211 } else {
212 // not working yet
213 String url;
214 try {
215 url = AccountUtils.constructFullURLForAccount(this, getAccount()) + file.getRemotePath();
216 mVideoPlayer.setVideoURI(Uri.parse(url));
217 } catch (AccountNotFoundException e) {
218 onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
219 }
220 }
221
222 // create and prepare control panel for the user
223 mMediaController = new MediaController(this);
224 mMediaController.setMediaPlayer(mVideoPlayer);
225 mMediaController.setAnchorView(mVideoPlayer);
226 mVideoPlayer.setMediaController(mMediaController);
227
228 } else {
229 finish();
230 }
231 } else {
232 Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
233 finish();
234 }
235 }
236
237
238 }