Fixed (tablet landscape): video playback is resumed in the moment it was left when...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewMediaFragment.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 package com.owncloud.android.ui.preview;
19
20 import java.io.File;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import android.accounts.Account;
25 import android.app.Activity;
26 import android.app.AlertDialog;
27 import android.content.ActivityNotFoundException;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.DialogInterface;
31 import android.content.Intent;
32 import android.content.ServiceConnection;
33 import android.media.MediaPlayer;
34 import android.media.MediaPlayer.OnCompletionListener;
35 import android.media.MediaPlayer.OnErrorListener;
36 import android.media.MediaPlayer.OnPreparedListener;
37 import android.net.Uri;
38 import android.os.Build;
39 import android.os.Bundle;
40 import android.os.Handler;
41 import android.os.IBinder;
42 import android.support.v4.app.FragmentTransaction;
43 import android.util.Log;
44 import android.view.LayoutInflater;
45 import android.view.MotionEvent;
46 import android.view.View;
47 import android.view.View.OnTouchListener;
48 import android.view.ViewGroup;
49 import android.webkit.MimeTypeMap;
50 import android.widget.ImageView;
51 import android.widget.Toast;
52 import android.widget.VideoView;
53
54 import com.actionbarsherlock.app.SherlockFragment;
55 import com.actionbarsherlock.view.Menu;
56 import com.actionbarsherlock.view.MenuInflater;
57 import com.actionbarsherlock.view.MenuItem;
58 import com.owncloud.android.datamodel.FileDataStorageManager;
59 import com.owncloud.android.datamodel.OCFile;
60 import com.owncloud.android.media.MediaControlView;
61 import com.owncloud.android.media.MediaService;
62 import com.owncloud.android.media.MediaServiceBinder;
63 import com.owncloud.android.network.OwnCloudClientUtils;
64 import com.owncloud.android.operations.OnRemoteOperationListener;
65 import com.owncloud.android.operations.RemoteOperation;
66 import com.owncloud.android.operations.RemoteOperationResult;
67 import com.owncloud.android.operations.RemoveFileOperation;
68 import com.owncloud.android.ui.activity.FileDetailActivity;
69 import com.owncloud.android.ui.activity.FileDisplayActivity;
70 import com.owncloud.android.ui.fragment.ConfirmationDialogFragment;
71 import com.owncloud.android.ui.fragment.FileDetailFragment;
72 import com.owncloud.android.ui.fragment.FileFragment;
73
74 import com.owncloud.android.R;
75 import eu.alefzero.webdav.WebdavClient;
76 import eu.alefzero.webdav.WebdavUtils;
77
78 /**
79 * This fragment shows a preview of a downloaded media file (audio or video).
80 *
81 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}.
82 *
83 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
84 *
85 * @author David A. Velasco
86 */
87 public class PreviewMediaFragment extends SherlockFragment implements
88 OnTouchListener , FileFragment,
89 ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener {
90
91 public static final String EXTRA_FILE = "FILE";
92 public static final String EXTRA_ACCOUNT = "ACCOUNT";
93 private static final String EXTRA_PLAY_POSITION = "PLAY_POSITION";
94 private static final String EXTRA_PLAYING = "PLAYING";
95
96 private View mView;
97 private OCFile mFile;
98 private Account mAccount;
99 private FileDataStorageManager mStorageManager;
100 private ImageView mImagePreview;
101 private VideoView mVideoPreview;
102 private int mSavedPlaybackPosition;
103
104 private Handler mHandler;
105 private RemoteOperation mLastRemoteOperation;
106
107 private MediaServiceBinder mMediaServiceBinder = null;
108 private MediaControlView mMediaController = null;
109 private MediaServiceConnection mMediaServiceConnection = null;
110 private VideoHelper mVideoHelper;
111 private boolean mAutoplay;
112
113 private static final String TAG = PreviewMediaFragment.class.getSimpleName();
114
115
116 /**
117 * Creates a fragment to preview a file.
118 *
119 * When 'fileToDetail' or 'ocAccount' are null
120 *
121 * @param fileToDetail An {@link OCFile} to preview in the fragment
122 * @param ocAccount An ownCloud account; needed to start downloads
123 */
124 public PreviewMediaFragment(OCFile fileToDetail, Account ocAccount) {
125 mFile = fileToDetail;
126 mAccount = ocAccount;
127 mSavedPlaybackPosition = 0;
128 mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment
129 mAutoplay = true;
130 }
131
132
133 /**
134 * Creates an empty fragment for previews.
135 *
136 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically (for instance, when the device is turned a aside).
137 *
138 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction
139 */
140 public PreviewMediaFragment() {
141 mFile = null;
142 mAccount = null;
143 mSavedPlaybackPosition = 0;
144 mStorageManager = null;
145 mAutoplay = true;
146 }
147
148
149 /**
150 * {@inheritDoc}
151 */
152 @Override
153 public void onCreate(Bundle savedInstanceState) {
154 super.onCreate(savedInstanceState);
155 mHandler = new Handler();
156 setHasOptionsMenu(true);
157 }
158
159
160 /**
161 * {@inheritDoc}
162 */
163 @Override
164 public View onCreateView(LayoutInflater inflater, ViewGroup container,
165 Bundle savedInstanceState) {
166 super.onCreateView(inflater, container, savedInstanceState);
167
168 mView = inflater.inflate(R.layout.file_preview, container, false);
169
170 mImagePreview = (ImageView)mView.findViewById(R.id.image_preview);
171 mVideoPreview = (VideoView)mView.findViewById(R.id.video_preview);
172 mVideoPreview.setOnTouchListener(this);
173
174 mMediaController = (MediaControlView)mView.findViewById(R.id.media_controller);
175
176 return mView;
177 }
178
179
180 /**
181 * {@inheritDoc}
182 */
183 @Override
184 public void onAttach(Activity activity) {
185 super.onAttach(activity);
186 if (!(activity instanceof FileFragment.ContainerActivity))
187 throw new ClassCastException(activity.toString() + " must implement " + FileFragment.ContainerActivity.class.getSimpleName());
188 }
189
190
191 /**
192 * {@inheritDoc}
193 */
194 @Override
195 public void onActivityCreated(Bundle savedInstanceState) {
196 super.onActivityCreated(savedInstanceState);
197
198 mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());
199 if (savedInstanceState != null) {
200 mFile = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE);
201 mAccount = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_ACCOUNT);
202 mSavedPlaybackPosition = savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION);
203 mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING);
204
205 }
206 if (mFile == null) {
207 throw new IllegalStateException("Instanced with a NULL OCFile");
208 }
209 if (mAccount == null) {
210 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
211 }
212 if (!mFile.isDown()) {
213 throw new IllegalStateException("There is no local file to preview");
214 }
215 if (mFile.isVideo()) {
216 mVideoPreview.setVisibility(View.VISIBLE);
217 mImagePreview.setVisibility(View.GONE);
218 prepareVideo();
219
220 } else {
221 mVideoPreview.setVisibility(View.GONE);
222 mImagePreview.setVisibility(View.VISIBLE);
223 }
224
225 }
226
227
228 /**
229 * {@inheritDoc}
230 */
231 @Override
232 public void onSaveInstanceState(Bundle outState) {
233 super.onSaveInstanceState(outState);
234 outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, mFile);
235 outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount);
236
237 if (mFile.isVideo()) {
238 mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
239 mAutoplay = mVideoPreview.isPlaying();
240 outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mSavedPlaybackPosition);
241 outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mAutoplay);
242 } else {
243 outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mMediaServiceBinder.getCurrentPosition());
244 outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mMediaServiceBinder.isPlaying());
245 }
246 }
247
248
249 @Override
250 public void onStart() {
251 super.onStart();
252
253 if (mFile != null) {
254 if (mFile.isAudio()) {
255 bindMediaService();
256
257 } else if (mFile.isVideo()) {
258 stopAudio();
259 playVideo();
260 }
261 }
262 }
263
264
265 private void stopAudio() {
266 Intent i = new Intent(getSherlockActivity(), MediaService.class);
267 i.setAction(MediaService.ACTION_STOP_ALL);
268 getSherlockActivity().startService(i);
269 }
270
271
272 /**
273 * {@inheritDoc}
274 */
275 @Override
276 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
277 super.onCreateOptionsMenu(menu, inflater);
278
279 inflater.inflate(R.menu.file_actions_menu, menu);
280 List<Integer> toHide = new ArrayList<Integer>();
281
282 MenuItem item = null;
283 toHide.add(R.id.action_cancel_download);
284 toHide.add(R.id.action_cancel_upload);
285 toHide.add(R.id.action_download_file);
286 toHide.add(R.id.action_rename_file); // by now
287
288 for (int i : toHide) {
289 item = menu.findItem(i);
290 if (item != null) {
291 item.setVisible(false);
292 item.setEnabled(false);
293 }
294 }
295
296 }
297
298
299 /**
300 * {@inheritDoc}
301 */
302 @Override
303 public boolean onOptionsItemSelected(MenuItem item) {
304 switch (item.getItemId()) {
305 case R.id.action_open_file_with: {
306 openFile();
307 return true;
308 }
309 case R.id.action_remove_file: {
310 removeFile();
311 return true;
312 }
313 case R.id.action_see_details: {
314 seeDetails();
315 return true;
316 }
317
318 default:
319 return false;
320 }
321 }
322
323
324 private void seeDetails() {
325 stopPreview(false);
326 ((FileFragment.ContainerActivity)getActivity()).showFragmentWithDetails(mFile);
327 }
328
329
330 private void prepareVideo() {
331 // create helper to get more control on the playback
332 mVideoHelper = new VideoHelper();
333 mVideoPreview.setOnPreparedListener(mVideoHelper);
334 mVideoPreview.setOnCompletionListener(mVideoHelper);
335 mVideoPreview.setOnErrorListener(mVideoHelper);
336 }
337
338 private void playVideo() {
339 // create and prepare control panel for the user
340 mMediaController.setMediaPlayer(mVideoPreview);
341
342 // load the video file in the video player ; when done, VideoHelper#onPrepared() will be called
343 mVideoPreview.setVideoPath(mFile.getStoragePath());
344 }
345
346
347 private class VideoHelper implements OnCompletionListener, OnPreparedListener, OnErrorListener {
348
349 /**
350 * Called when the file is ready to be played.
351 *
352 * Just starts the playback.
353 *
354 * @param mp {@link MediaPlayer} instance performing the playback.
355 */
356 @Override
357 public void onPrepared(MediaPlayer vp) {
358 Log.e(TAG, "onPrepared");
359 mVideoPreview.seekTo(mSavedPlaybackPosition);
360 if (mAutoplay) {
361 mVideoPreview.start();
362 }
363 mMediaController.setEnabled(true);
364 mMediaController.updatePausePlay();
365 }
366
367
368 /**
369 * Called when the file is finished playing.
370 *
371 * Finishes the activity.
372 *
373 * @param mp {@link MediaPlayer} instance performing the playback.
374 */
375 @Override
376 public void onCompletion(MediaPlayer mp) {
377 Log.e(TAG, "completed");
378 if (mp != null) {
379 mVideoPreview.seekTo(0);
380 // next lines are necessary to work around undesired video loops
381 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD) {
382 mVideoPreview.pause();
383
384 } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD_MR1) {
385 // mVideePreview.pause() is not enough
386
387 mMediaController.setEnabled(false);
388 mVideoPreview.stopPlayback();
389 mAutoplay = false;
390 mSavedPlaybackPosition = 0;
391 mVideoPreview.setVideoPath(mFile.getStoragePath());
392 }
393 } // else : called from onError()
394 mMediaController.updatePausePlay();
395 }
396
397
398 /**
399 * Called when an error in playback occurs.
400 *
401 * @param mp {@link MediaPlayer} instance performing the playback.
402 * @param what Type of error
403 * @param extra Extra code specific to the error
404 */
405 @Override
406 public boolean onError(MediaPlayer mp, int what, int extra) {
407 if (mVideoPreview.getWindowToken() != null) {
408 String message = MediaService.getMessageForMediaError(getActivity(), what, extra);
409 new AlertDialog.Builder(getActivity())
410 .setMessage(message)
411 .setPositiveButton(android.R.string.VideoView_error_button,
412 new DialogInterface.OnClickListener() {
413 public void onClick(DialogInterface dialog, int whichButton) {
414 dialog.dismiss();
415 VideoHelper.this.onCompletion(null);
416 }
417 })
418 .setCancelable(false)
419 .show();
420 }
421 return true;
422 }
423
424 }
425
426
427 @Override
428 public void onStop() {
429 super.onStop();
430
431 if (mMediaServiceConnection != null) {
432 Log.d(TAG, "Unbinding from MediaService ...");
433 if (mMediaServiceBinder != null && mMediaController != null) {
434 mMediaServiceBinder.unregisterMediaController(mMediaController);
435 }
436 getActivity().unbindService(mMediaServiceConnection);
437 mMediaServiceConnection = null;
438 mMediaServiceBinder = null;
439 }
440 }
441
442 @Override
443 public boolean onTouch(View v, MotionEvent event) {
444 if (event.getAction() == MotionEvent.ACTION_DOWN && v == mVideoPreview) {
445 startFullScreenVideo();
446 return true;
447 }
448 return false;
449 }
450
451
452 private void startFullScreenVideo() {
453 Intent i = new Intent(getActivity(), PreviewVideoActivity.class);
454 i.putExtra(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount);
455 i.putExtra(PreviewVideoActivity.EXTRA_FILE, mFile);
456 i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying());
457 mVideoPreview.pause();
458 i.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPreview.getCurrentPosition());
459 startActivityForResult(i, 0);
460 }
461
462
463 @Override
464 public void onActivityResult (int requestCode, int resultCode, Intent data) {
465 super.onActivityResult(requestCode, resultCode, data);
466 if (resultCode == Activity.RESULT_OK) {
467 mSavedPlaybackPosition = data.getExtras().getInt(PreviewVideoActivity.EXTRA_START_POSITION);
468 mAutoplay = data.getExtras().getBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY);
469 }
470 }
471
472
473 private void playAudio() {
474 if (!mMediaServiceBinder.isPlaying(mFile)) {
475 Log.d(TAG, "starting playback of " + mFile.getStoragePath());
476 mMediaServiceBinder.start(mAccount, mFile, mAutoplay, mSavedPlaybackPosition);
477
478 } else {
479 if (!mMediaServiceBinder.isPlaying() && mAutoplay) {
480 mMediaServiceBinder.start();
481 mMediaController.updatePausePlay();
482 }
483 }
484 }
485
486
487 private void bindMediaService() {
488 Log.d(TAG, "Binding to MediaService...");
489 if (mMediaServiceConnection == null) {
490 mMediaServiceConnection = new MediaServiceConnection();
491 }
492 getActivity().bindService( new Intent(getActivity(),
493 MediaService.class),
494 mMediaServiceConnection,
495 Context.BIND_AUTO_CREATE);
496 // follow the flow in MediaServiceConnection#onServiceConnected(...)
497 }
498
499 /** Defines callbacks for service binding, passed to bindService() */
500 private class MediaServiceConnection implements ServiceConnection {
501
502 @Override
503 public void onServiceConnected(ComponentName component, IBinder service) {
504 if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
505 Log.d(TAG, "Media service connected");
506 mMediaServiceBinder = (MediaServiceBinder) service;
507 if (mMediaServiceBinder != null) {
508 prepareMediaController();
509 playAudio(); // do not wait for the touch of nobody to play audio
510
511 Log.d(TAG, "Successfully bound to MediaService, MediaController ready");
512
513 } else {
514 Log.e(TAG, "Unexpected response from MediaService while binding");
515 }
516 }
517 }
518
519 private void prepareMediaController() {
520 mMediaServiceBinder.registerMediaController(mMediaController);
521 if (mMediaController != null) {
522 mMediaController.setMediaPlayer(mMediaServiceBinder);
523 mMediaController.setEnabled(true);
524 mMediaController.updatePausePlay();
525 }
526 }
527
528 @Override
529 public void onServiceDisconnected(ComponentName component) {
530 if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
531 Log.e(TAG, "Media service suddenly disconnected");
532 if (mMediaController != null) {
533 mMediaController.setMediaPlayer(null);
534 } else {
535 Toast.makeText(getActivity(), "No media controller to release when disconnected from media service", Toast.LENGTH_SHORT).show();
536 }
537 mMediaServiceBinder = null;
538 mMediaServiceConnection = null;
539 }
540 }
541 }
542
543
544
545 /**
546 * Opens the previewed file with an external application.
547 *
548 * TODO - improve this; instead of prioritize the actions available for the MIME type in the server,
549 * we should get a list of available apps for MIME tpye in the server and join it with the list of
550 * available apps for the MIME type known from the file extension, to let the user choose
551 */
552 private void openFile() {
553 stopPreview(true);
554 String storagePath = mFile.getStoragePath();
555 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
556 try {
557 Intent i = new Intent(Intent.ACTION_VIEW);
558 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());
559 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
560 startActivity(i);
561
562 } catch (Throwable t) {
563 Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());
564 boolean toastIt = true;
565 String mimeType = "";
566 try {
567 Intent i = new Intent(Intent.ACTION_VIEW);
568 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
569 if (mimeType == null || !mimeType.equals(mFile.getMimetype())) {
570 if (mimeType != null) {
571 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);
572 } else {
573 // desperate try
574 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*-/*");
575 }
576 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
577 startActivity(i);
578 toastIt = false;
579 }
580
581 } catch (IndexOutOfBoundsException e) {
582 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
583
584 } catch (ActivityNotFoundException e) {
585 Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
586
587 } catch (Throwable th) {
588 Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);
589
590 } finally {
591 if (toastIt) {
592 Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
593 }
594 }
595
596 }
597 finish();
598 }
599
600 /**
601 * Starts a the removal of the previewed file.
602 *
603 * Shows a confirmation dialog. The action continues in {@link #onConfirmation(String)} , {@link #onNeutral(String)} or {@link #onCancel(String)},
604 * depending upon the user selection in the dialog.
605 */
606 private void removeFile() {
607 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
608 R.string.confirmation_remove_alert,
609 new String[]{mFile.getFileName()},
610 R.string.confirmation_remove_remote_and_local,
611 R.string.confirmation_remove_local,
612 R.string.common_cancel);
613 confDialog.setOnConfirmationListener(this);
614 confDialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
615 }
616
617
618 /**
619 * Performs the removal of the previewed file, both locally and in the server.
620 */
621 @Override
622 public void onConfirmation(String callerTag) {
623 if (mStorageManager.getFileById(mFile.getFileId()) != null) { // check that the file is still there;
624 stopPreview(true);
625 mLastRemoteOperation = new RemoveFileOperation( mFile, // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters
626 true,
627 mStorageManager);
628 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());
629 mLastRemoteOperation.execute(wc, this, mHandler);
630
631 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
632 getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
633 }
634 }
635
636
637 /**
638 * Removes the file from local storage
639 */
640 @Override
641 public void onNeutral(String callerTag) {
642 // TODO this code should be made in a secondary thread,
643 if (mFile.isDown()) { // checks it is still there
644 stopPreview(true);
645 File f = new File(mFile.getStoragePath());
646 f.delete();
647 mFile.setStoragePath(null);
648 mStorageManager.saveFile(mFile);
649 finish();
650 }
651 }
652
653 /**
654 * User cancelled the removal action.
655 */
656 @Override
657 public void onCancel(String callerTag) {
658 // nothing to do here
659 }
660
661
662 /**
663 * {@inheritDoc}
664 */
665 public OCFile getFile(){
666 return mFile;
667 }
668
669 /*
670 /**
671 * Use this method to signal this Activity that it shall update its view.
672 *
673 * @param file : An {@link OCFile}
674 *-/
675 public void updateFileDetails(OCFile file, Account ocAccount) {
676 mFile = file;
677 if (ocAccount != null && (
678 mStorageManager == null ||
679 (mAccount != null && !mAccount.equals(ocAccount))
680 )) {
681 mStorageManager = new FileDataStorageManager(ocAccount, getActivity().getApplicationContext().getContentResolver());
682 }
683 mAccount = ocAccount;
684 updateFileDetails(false);
685 }
686 */
687
688
689 /**
690 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed.
691 *
692 * @param file File to test if can be previewed.
693 * @return 'True' if the file can be handled by the fragment.
694 */
695 public static boolean canBePreviewed(OCFile file) {
696 return (file != null && (file.isAudio() || file.isVideo()));
697 }
698
699 /**
700 * {@inheritDoc}
701 */
702 @Override
703 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
704 if (operation.equals(mLastRemoteOperation)) {
705 if (operation instanceof RemoveFileOperation) {
706 onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
707 }
708 }
709 }
710
711 private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
712 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
713 getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
714
715 if (result.isSuccess()) {
716 Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
717 msg.show();
718 finish();
719
720 } else {
721 Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG);
722 msg.show();
723 if (result.isSslRecoverableException()) {
724 // TODO show the SSL warning dialog
725 }
726 }
727 }
728
729 private void stopPreview(boolean stopAudio) {
730 if (mFile.isAudio() && stopAudio) {
731 mMediaServiceBinder.pause();
732
733 } else if (mFile.isVideo()) {
734 mVideoPreview.stopPlayback();
735 }
736 }
737
738
739
740 /**
741 * Finishes the preview
742 */
743 private void finish() {
744 Activity container = getActivity();
745 if (container instanceof FileDisplayActivity) {
746 // double pane
747 FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
748 transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment
749 transaction.commit();
750 ((FileFragment.ContainerActivity)container).onFileStateChanged();
751 } else {
752 container.finish();
753 }
754 }
755
756 }