Merge branch 'master' of github.com:owncloud/android into setAsWallpaper
[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.e(TAG, "ACTIVITY\t\tonCreate");
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 Log_OC.e(TAG, "ACTIVITY\t\tonSaveInstanceState");
114 outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
115 outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY , mVideoPlayer.isPlaying());
116 }
117
118
119 @Override
120 public void onBackPressed() {
121 Log_OC.e(TAG, "ACTIVTIY\t\tonBackPressed");
122 Intent i = new Intent();
123 i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
124 i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
125 setResult(RESULT_OK, i);
126 super.onBackPressed();
127 }
128
129
130 /**
131 * Called when the file is ready to be played.
132 *
133 * Just starts the playback.
134 *
135 * @param mp {@link MediaPlayer} instance performing the playback.
136 */
137 @Override
138 public void onPrepared(MediaPlayer mp) {
139 Log_OC.e(TAG, "ACTIVITY\t\tonPrepare");
140 mVideoPlayer.seekTo(mSavedPlaybackPosition);
141 if (mAutoplay) {
142 mVideoPlayer.start();
143 }
144 mMediaController.show(5000);
145 }
146
147
148 /**
149 * Called when the file is finished playing.
150 *
151 * Rewinds the video
152 *
153 * @param mp {@link MediaPlayer} instance performing the playback.
154 */
155 @Override
156 public void onCompletion(MediaPlayer mp) {
157 mVideoPlayer.seekTo(0);
158 }
159
160
161 /**
162 * Called when an error in playback occurs.
163 *
164 * @param mp {@link MediaPlayer} instance performing the playback.
165 * @param what Type of error
166 * @param extra Extra code specific to the error
167 */
168 @Override
169 public boolean onError(MediaPlayer mp, int what, int extra) {
170 Log_OC.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
171
172 if (mMediaController != null) {
173 mMediaController.hide();
174 }
175
176 if (mVideoPlayer.getWindowToken() != null) {
177 String message = MediaService.getMessageForMediaError(this, what, extra);
178 new AlertDialog.Builder(this)
179 .setMessage(message)
180 .setPositiveButton(android.R.string.VideoView_error_button,
181 new DialogInterface.OnClickListener() {
182 public void onClick(DialogInterface dialog, int whichButton) {
183 PreviewVideoActivity.this.onCompletion(null);
184 }
185 })
186 .setCancelable(false)
187 .show();
188 }
189 return true;
190 }
191
192 @Override
193 protected void onAccountSet(boolean stateWasRecovered) {
194 super.onAccountSet(stateWasRecovered);
195 if (getAccount() != null) {
196 OCFile file = getFile();
197 /// Validate handled file (first image to preview)
198 if (file == null) {
199 throw new IllegalStateException("Instanced with a NULL OCFile");
200 }
201 if (!file.isVideo()) {
202 throw new IllegalArgumentException("Non-video file passed as argument");
203 }
204 file = getStorageManager().getFileById(file.getFileId());
205 if (file != null) {
206 if (file.isDown()) {
207 mVideoPlayer.setVideoPath(file.getStoragePath());
208
209 } else {
210 // not working yet
211 String url;
212 try {
213 url = AccountUtils.constructFullURLForAccount(this, getAccount()) + file.getRemotePath();
214 mVideoPlayer.setVideoURI(Uri.parse(url));
215 } catch (AccountNotFoundException e) {
216 onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
217 }
218 }
219
220 // create and prepare control panel for the user
221 mMediaController = new MediaController(this);
222 mMediaController.setMediaPlayer(mVideoPlayer);
223 mMediaController.setAnchorView(mVideoPlayer);
224 mVideoPlayer.setMediaController(mMediaController);
225
226 } else {
227 finish();
228 }
229 } else {
230 finish();
231 }
232 }
233
234
235 }