cee0c6098bf3aa12c5c781f83c03050b0e6decc9
[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 // create and prepare control panel for the user
333 mMediaController.setMediaPlayer(mVideoPreview);
334
335 // load the video file in the video player ; when done, VideoHelper#onPrepared() will be called
336 mVideoPreview.setVideoPath(mFile.getStoragePath());
337
338 //mVideoPreview.setMediaController(mMediaController);
339 }
340
341
342 private class VideoHelper implements OnCompletionListener, OnPreparedListener, OnErrorListener {
343
344 /**
345 * Called when the file is ready to be played.
346 *
347 * Just starts the playback.
348 *
349 * @param mp {@link MediaPlayer} instance performing the playback.
350 */
351 @Override
352 public void onPrepared(MediaPlayer vp) {
353 mVideoPreview.seekTo(mSavedPlaybackPosition);
354 mVideoPreview.start();
355 mMediaController.setEnabled(true);
356 mMediaController.updatePausePlay();
357 //mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE);
358 }
359
360
361 /**
362 * Called when the file is finished playing.
363 *
364 * Finishes the activity.
365 *
366 * @param mp {@link MediaPlayer} instance performing the playback.
367 */
368 @Override
369 public void onCompletion(MediaPlayer mp) {
370 // nothing, right now
371 }
372
373
374 /**
375 * Called when an error in playback occurs.
376 *
377 * @param mp {@link MediaPlayer} instance performing the playback.
378 * @param what Type of error
379 * @param extra Extra code specific to the error
380 */
381 @Override
382 public boolean onError(MediaPlayer mp, int what, int extra) {
383 Log.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
384
385 /*
386 if (mMediaController != null) {
387 mMediaController.hide();
388 }
389 */
390
391 if (mVideoPreview.getWindowToken() != null) {
392 String message = MediaService.getMessageForMediaError(getActivity(), what, extra);
393 new AlertDialog.Builder(getActivity())
394 .setMessage(message)
395 .setPositiveButton(android.R.string.VideoView_error_button,
396 new DialogInterface.OnClickListener() {
397 public void onClick(DialogInterface dialog, int whichButton) {
398 dialog.dismiss();
399 VideoHelper.this.onCompletion(null);
400 }
401 })
402 .setCancelable(false)
403 .show();
404 }
405 return true;
406 }
407
408 }
409
410
411 @Override
412 public void onResume() {
413 super.onResume();
414 }
415
416
417 @Override
418 public void onPause() {
419 super.onPause();
420 }
421
422
423 @Override
424 public void onStop() {
425 super.onStop();
426 if (mMediaServiceConnection != null) {
427 Log.d(TAG, "Unbinding from MediaService ...");
428 if (mMediaServiceBinder != null && mMediaController != null) {
429 //mMediaServiceBinder.unregisterMediaController(mMediaController);
430 }
431 getActivity().unbindService(mMediaServiceConnection);
432 mMediaServiceConnection = null;
433 mMediaServiceBinder = null;
434 /*
435 if (mMediaController != null) {
436 mMediaController.hide();
437 mMediaController = null;
438 }
439 */
440 }
441 }
442
443 @Override
444 public void onDestroy() {
445 super.onDestroy();
446 }
447
448
449 @Override
450 public boolean onTouch(View v, MotionEvent event) {
451 if (event.getAction() == MotionEvent.ACTION_DOWN) {
452 if (v == mImagePreview &&
453 mMediaServiceBinder != null && mFile.isAudio() && mMediaServiceBinder.isPlaying(mFile)) {
454 toggleMediaController(MediaService.MEDIA_CONTROL_PERMANENT);
455 return true;
456
457 } else if (v == mVideoPreview) {
458 toggleMediaController(MediaService.MEDIA_CONTROL_SHORT_LIFE);
459 return true;
460 }
461 }
462 return false;
463 }
464
465
466 private void toggleMediaController(int time) {
467 /*
468 if (mMediaController.isShowing()) {
469 mMediaController.hide();
470 } else {
471 mMediaController.show(time);
472 }
473 */
474 }
475
476
477 private void playAudio() {
478 if (!mMediaServiceBinder.isPlaying(mFile)) {
479 Log.d(TAG, "starting playback of " + mFile.getStoragePath());
480 mMediaServiceBinder.start(mAccount, mFile);
481
482 } else {
483 if (!mMediaServiceBinder.isPlaying()) {
484 mMediaServiceBinder.start();
485 }
486 /*
487 if (!mMediaController.isShowing() && isVisible()) {
488 //mMediaController.show(MediaService.MEDIA_CONTROL_PERMANENT);
489 // TODO - fix strange bug; steps to trigger :
490 // 1. remove the "isVisible()" control
491 // 2. start the app and preview an audio file
492 // 3. exit from the app (home button, for instance) while the audio file is still being played
493 // 4. go to notification bar and click on the "ownCloud music app" notification
494 // PUM!
495 }
496 */
497 }
498 }
499
500
501 private void bindMediaService() {
502 Log.d(TAG, "Binding to MediaService...");
503 if (mMediaServiceConnection == null) {
504 mMediaServiceConnection = new MediaServiceConnection();
505 }
506 getActivity().bindService( new Intent(getActivity(),
507 MediaService.class),
508 mMediaServiceConnection,
509 Context.BIND_AUTO_CREATE);
510 // follow the flow in MediaServiceConnection#onServiceConnected(...)
511 }
512
513 /** Defines callbacks for service binding, passed to bindService() */
514 private class MediaServiceConnection implements ServiceConnection {
515
516 @Override
517 public void onServiceConnected(ComponentName component, IBinder service) {
518 if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
519 Log.d(TAG, "Media service connected");
520 mMediaServiceBinder = (MediaServiceBinder) service;
521 if (mMediaServiceBinder != null) {
522 /*
523 if (mMediaController == null) {
524 mMediaController = new MediaController(getSherlockActivity());
525 }
526 */
527 prepareMediaController();
528 playAudio(); // do not wait for the touch of nobody to play audio
529
530 Log.d(TAG, "Successfully bound to MediaService, MediaController ready");
531
532 } else {
533 Log.e(TAG, "Unexpected response from MediaService while binding");
534 }
535 }
536 }
537
538 private void prepareMediaController() {
539 //mMediaServiceBinder.registerMediaController(mMediaController);
540 if (mMediaController != null) {
541 mMediaController.setMediaPlayer(mMediaServiceBinder);
542 mMediaController.setEnabled(true);
543 mMediaController.updatePausePlay();
544 }
545 }
546
547 @Override
548 public void onServiceDisconnected(ComponentName component) {
549 if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
550 Log.e(TAG, "Media service suddenly disconnected");
551 if (mMediaController != null) {
552 //mMediaController.hide();
553 mMediaController.setMediaPlayer(null);
554 //mMediaController = null;
555 } else {
556 Toast.makeText(getActivity(), "No media controller to release when disconnected from media service", Toast.LENGTH_SHORT).show();
557 }
558 mMediaServiceBinder = null;
559 mMediaServiceConnection = null;
560 }
561 }
562 }
563
564
565
566 /**
567 * Opens the previewed file with an external application.
568 *
569 * TODO - improve this; instead of prioritize the actions available for the MIME type in the server,
570 * we should get a list of available apps for MIME tpye in the server and join it with the list of
571 * available apps for the MIME type known from the file extension, to let the user choose
572 */
573 private void openFile() {
574 stopPreview(true);
575 String storagePath = mFile.getStoragePath();
576 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
577 try {
578 Intent i = new Intent(Intent.ACTION_VIEW);
579 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());
580 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
581 startActivity(i);
582
583 } catch (Throwable t) {
584 Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());
585 boolean toastIt = true;
586 String mimeType = "";
587 try {
588 Intent i = new Intent(Intent.ACTION_VIEW);
589 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
590 if (mimeType == null || !mimeType.equals(mFile.getMimetype())) {
591 if (mimeType != null) {
592 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);
593 } else {
594 // desperate try
595 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*-/*");
596 }
597 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
598 startActivity(i);
599 toastIt = false;
600 }
601
602 } catch (IndexOutOfBoundsException e) {
603 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
604
605 } catch (ActivityNotFoundException e) {
606 Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
607
608 } catch (Throwable th) {
609 Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);
610
611 } finally {
612 if (toastIt) {
613 Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
614 }
615 }
616
617 }
618 finish();
619 }
620
621 /**
622 * Starts a the removal of the previewed file.
623 *
624 * Shows a confirmation dialog. The action continues in {@link #onConfirmation(String)} , {@link #onNeutral(String)} or {@link #onCancel(String)},
625 * depending upon the user selection in the dialog.
626 */
627 private void removeFile() {
628 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
629 R.string.confirmation_remove_alert,
630 new String[]{mFile.getFileName()},
631 R.string.confirmation_remove_remote_and_local,
632 R.string.confirmation_remove_local,
633 R.string.common_cancel);
634 confDialog.setOnConfirmationListener(this);
635 confDialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
636 }
637
638
639 /**
640 * Performs the removal of the previewed file, both locally and in the server.
641 */
642 @Override
643 public void onConfirmation(String callerTag) {
644 if (mStorageManager.getFileById(mFile.getFileId()) != null) { // check that the file is still there;
645 stopPreview(true);
646 mLastRemoteOperation = new RemoveFileOperation( mFile, // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters
647 true,
648 mStorageManager);
649 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());
650 mLastRemoteOperation.execute(wc, this, mHandler);
651
652 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
653 getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
654 }
655 }
656
657
658 /**
659 * Removes the file from local storage
660 */
661 @Override
662 public void onNeutral(String callerTag) {
663 // TODO this code should be made in a secondary thread,
664 if (mFile.isDown()) { // checks it is still there
665 stopPreview(true);
666 File f = new File(mFile.getStoragePath());
667 f.delete();
668 mFile.setStoragePath(null);
669 mStorageManager.saveFile(mFile);
670 finish();
671 }
672 }
673
674 /**
675 * User cancelled the removal action.
676 */
677 @Override
678 public void onCancel(String callerTag) {
679 // nothing to do here
680 }
681
682
683 /**
684 * {@inheritDoc}
685 */
686 public OCFile getFile(){
687 return mFile;
688 }
689
690 /*
691 /**
692 * Use this method to signal this Activity that it shall update its view.
693 *
694 * @param file : An {@link OCFile}
695 *-/
696 public void updateFileDetails(OCFile file, Account ocAccount) {
697 mFile = file;
698 if (ocAccount != null && (
699 mStorageManager == null ||
700 (mAccount != null && !mAccount.equals(ocAccount))
701 )) {
702 mStorageManager = new FileDataStorageManager(ocAccount, getActivity().getApplicationContext().getContentResolver());
703 }
704 mAccount = ocAccount;
705 updateFileDetails(false);
706 }
707 */
708
709
710 /**
711 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed.
712 *
713 * @param file File to test if can be previewed.
714 * @return 'True' if the file can be handled by the fragment.
715 */
716 public static boolean canBePreviewed(OCFile file) {
717 return (file != null && (file.isAudio() || file.isVideo()));
718 }
719
720 /**
721 * {@inheritDoc}
722 */
723 @Override
724 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
725 if (operation.equals(mLastRemoteOperation)) {
726 if (operation instanceof RemoveFileOperation) {
727 onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
728 }
729 }
730 }
731
732 private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
733 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
734 getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
735
736 if (result.isSuccess()) {
737 Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
738 msg.show();
739 finish();
740
741 } else {
742 Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG);
743 msg.show();
744 if (result.isSslRecoverableException()) {
745 // TODO show the SSL warning dialog
746 }
747 }
748 }
749
750 private void stopPreview(boolean stopAudio) {
751 if (mMediaController != null) {
752 //mMediaController.hide();
753 }
754 if (mFile.isAudio() && stopAudio) {
755 mMediaServiceBinder.pause();
756
757 } else if (mFile.isVideo()) {
758 mVideoPreview.stopPlayback();
759 }
760 }
761
762
763
764 /**
765 * Finishes the preview
766 */
767 private void finish() {
768 Activity container = getActivity();
769 if (container instanceof FileDisplayActivity) {
770 // double pane
771 FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
772 transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment
773 transaction.commit();
774 ((FileFragment.ContainerActivity)container).onFileStateChanged();
775 } else {
776 container.finish();
777 }
778 }
779
780 }