OC-3079: (fix bug) Two send options appear after send and mail with a ownCloud file
[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 com.owncloud.android.R;
21 import com.owncloud.android.datamodel.OCFile;
22 import com.owncloud.android.media.MediaService;
23 import com.owncloud.android.ui.activity.FileActivity;
24 import com.owncloud.android.utils.Log_OC;
25
26 import android.accounts.Account;
27 import android.app.AlertDialog;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.content.res.Configuration;
31 import android.media.MediaPlayer;
32 import android.media.MediaPlayer.OnCompletionListener;
33 import android.media.MediaPlayer.OnErrorListener;
34 import android.media.MediaPlayer.OnPreparedListener;
35 import android.net.Uri;
36 import android.os.Bundle;
37 import android.widget.MediaController;
38 import android.widget.VideoView;
39
40 import com.owncloud.android.lib.common.accounts.AccountUtils;
41 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
42
43 /**
44 * Activity implementing a basic video player.
45 *
46 * Used as an utility to preview video files contained in an ownCloud account.
47 *
48 * Currently, it always plays in landscape mode, full screen. When the playback ends,
49 * the activity is finished.
50 *
51 * @author David A. Velasco
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 public void onConfigurationChanged(Configuration newConfig) {
194 super.onConfigurationChanged(newConfig);
195 }
196
197 @Override
198 protected void onAccountSet(boolean stateWasRecovered) {
199 super.onAccountSet(stateWasRecovered);
200 if (getAccount() != null) {
201 OCFile file = getFile();
202 /// Validate handled file (first image to preview)
203 if (file == null) {
204 throw new IllegalStateException("Instanced with a NULL OCFile");
205 }
206 if (!file.isVideo()) {
207 throw new IllegalArgumentException("Non-video file passed as argument");
208 }
209 file = getStorageManager().getFileById(file.getFileId());
210 if (file != null) {
211 if (file.isDown()) {
212 mVideoPlayer.setVideoPath(file.getStoragePath());
213
214 } else {
215 // not working yet
216 String url;
217 try {
218 url = AccountUtils.constructFullURLForAccount(this, getAccount()) + file.getRemotePath();
219 mVideoPlayer.setVideoURI(Uri.parse(url));
220 } catch (AccountNotFoundException e) {
221 onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
222 }
223 }
224
225 // create and prepare control panel for the user
226 mMediaController = new MediaController(this);
227 mMediaController.setMediaPlayer(mVideoPlayer);
228 mMediaController.setAnchorView(mVideoPlayer);
229 mVideoPlayer.setMediaController(mMediaController);
230
231 } else {
232 finish();
233 }
234 } else {
235 finish();
236 }
237 }
238
239
240 }