Code cleanups
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewVideoActivity.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.ui.preview;
22
23 import com.owncloud.android.R;
24 import com.owncloud.android.datamodel.OCFile;
25 import com.owncloud.android.media.MediaService;
26 import com.owncloud.android.ui.activity.FileActivity;
27
28 import android.accounts.Account;
29 import android.support.v7.app.AlertDialog;
30 import android.content.DialogInterface;
31 import android.content.Intent;
32 import android.media.MediaPlayer;
33 import android.media.MediaPlayer.OnCompletionListener;
34 import android.media.MediaPlayer.OnErrorListener;
35 import android.media.MediaPlayer.OnPreparedListener;
36 import android.net.Uri;
37 import android.os.Bundle;
38 import android.widget.MediaController;
39 import android.widget.VideoView;
40
41 import com.owncloud.android.lib.common.accounts.AccountUtils;
42 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
43 import com.owncloud.android.lib.common.utils.Log_OC;
44
45 /**
46 * Activity implementing a basic video player.
47 *
48 * Used as an utility to preview video files contained in an ownCloud account.
49 *
50 * Currently, it always plays in landscape mode, full screen. When the playback ends,
51 * the activity is finished.
52 */
53 public class PreviewVideoActivity extends FileActivity implements OnCompletionListener, OnPreparedListener, OnErrorListener {
54
55 /** Key to receive a flag signaling if the video should be started immediately */
56 public static final String EXTRA_AUTOPLAY = "AUTOPLAY";
57
58 /** Key to receive the position of the playback where the video should be put at start */
59 public static final String EXTRA_START_POSITION = "START_POSITION";
60
61 private static final String TAG = PreviewVideoActivity.class.getSimpleName();
62
63 private int mSavedPlaybackPosition; // in the unit time handled by MediaPlayer.getCurrentPosition()
64 private boolean mAutoplay; // when 'true', the playback starts immediately with the activity
65 private VideoView mVideoPlayer; // view to play the file; both performs and show the playback
66 private MediaController mMediaController; // panel control used by the user to control the playback
67
68 /**
69 * Called when the activity is first created.
70 *
71 * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}.
72 *
73 * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to
74 * try to stream the remote file - TODO get the streaming works
75 *
76 * {@inheritDoc}
77 */
78 @Override
79 public void onCreate(Bundle savedInstanceState) {
80 super.onCreate(savedInstanceState);
81 Log_OC.v(TAG, "onCreate");
82
83 setContentView(R.layout.video_layout);
84
85 if (savedInstanceState == null) {
86 Bundle extras = getIntent().getExtras();
87 mSavedPlaybackPosition = extras.getInt(EXTRA_START_POSITION);
88 mAutoplay = extras.getBoolean(EXTRA_AUTOPLAY);
89
90 } else {
91 mSavedPlaybackPosition = savedInstanceState.getInt(EXTRA_START_POSITION);
92 mAutoplay = savedInstanceState.getBoolean(EXTRA_AUTOPLAY);
93 }
94
95 mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer);
96
97 // set listeners to get more contol on the playback
98 mVideoPlayer.setOnPreparedListener(this);
99 mVideoPlayer.setOnCompletionListener(this);
100 mVideoPlayer.setOnErrorListener(this);
101
102 // keep the screen on while the playback is performed (prevents screen off by battery save)
103 mVideoPlayer.setKeepScreenOn(true);
104 }
105
106
107 /**
108 * {@inheritDoc}
109 */
110 @Override
111 public void onSaveInstanceState(Bundle outState) {
112 super.onSaveInstanceState(outState);
113 outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
114 outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY , mVideoPlayer.isPlaying());
115 }
116
117
118 @Override
119 public void onBackPressed() {
120 Log_OC.v(TAG, "onBackPressed");
121 Intent i = new Intent();
122 i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
123 i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
124 setResult(RESULT_OK, i);
125 super.onBackPressed();
126 }
127
128
129 /**
130 * Called when the file is ready to be played.
131 *
132 * Just starts the playback.
133 *
134 * @param mp {@link MediaPlayer} instance performing the playback.
135 */
136 @Override
137 public void onPrepared(MediaPlayer mp) {
138 Log_OC.v(TAG, "onPrepare");
139 mVideoPlayer.seekTo(mSavedPlaybackPosition);
140 if (mAutoplay) {
141 mVideoPlayer.start();
142 }
143 mMediaController.show(5000);
144 }
145
146
147 /**
148 * Called when the file is finished playing.
149 *
150 * Rewinds the video
151 *
152 * @param mp {@link MediaPlayer} instance performing the playback.
153 */
154 @Override
155 public void onCompletion(MediaPlayer mp) {
156 mVideoPlayer.seekTo(0);
157 }
158
159
160 /**
161 * Called when an error in playback occurs.
162 *
163 * @param mp {@link MediaPlayer} instance performing the playback.
164 * @param what Type of error
165 * @param extra Extra code specific to the error
166 */
167 @Override
168 public boolean onError(MediaPlayer mp, int what, int extra) {
169 Log_OC.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
170
171 if (mMediaController != null) {
172 mMediaController.hide();
173 }
174
175 if (mVideoPlayer.getWindowToken() != null) {
176 String message = MediaService.getMessageForMediaError(this, what, extra);
177 new AlertDialog.Builder(this)
178 .setMessage(message)
179 .setPositiveButton(android.R.string.VideoView_error_button,
180 new DialogInterface.OnClickListener() {
181 public void onClick(DialogInterface dialog, int whichButton) {
182 PreviewVideoActivity.this.onCompletion(null);
183 }
184 })
185 .setCancelable(false)
186 .show();
187 }
188 return true;
189 }
190
191 @Override
192 protected void onAccountSet(boolean stateWasRecovered) {
193 super.onAccountSet(stateWasRecovered);
194 if (getAccount() != null) {
195 OCFile file = getFile();
196 /// Validate handled file (first image to preview)
197 if (file == null) {
198 throw new IllegalStateException("Instanced with a NULL OCFile");
199 }
200 if (!file.isVideo()) {
201 throw new IllegalArgumentException("Non-video file passed as argument");
202 }
203 file = getStorageManager().getFileById(file.getFileId());
204 if (file != null) {
205 if (file.isDown()) {
206 mVideoPlayer.setVideoURI(file.getStorageUri());
207
208 } else {
209 // not working yet
210 String url;
211 try {
212 url = AccountUtils.constructFullURLForAccount(this, getAccount()) + file.getRemotePath();
213 mVideoPlayer.setVideoURI(Uri.parse(url));
214 } catch (AccountNotFoundException e) {
215 onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
216 }
217 }
218
219 // create and prepare control panel for the user
220 mMediaController = new MediaController(this);
221 mMediaController.setMediaPlayer(mVideoPlayer);
222 mMediaController.setAnchorView(mVideoPlayer);
223 mVideoPlayer.setMediaController(mMediaController);
224
225 } else {
226 finish();
227 }
228 } else {
229 finish();
230 }
231 }
232
233
234 }