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