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