4a45975b79be3911c580b93af94d44405953b1b4
[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 mVideoPreview = (VideoView)mView.findViewById(R.id.video_preview);
171 mVideoPreview.setOnTouchListener(this);
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 outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, mFile);
234 outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount);
235
236 if (mFile.isVideo()) {
237 outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mVideoPreview.getCurrentPosition());
238 outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mVideoPreview.isPlaying());
239 } else {
240 outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mMediaServiceBinder.getCurrentPosition());
241 outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mMediaServiceBinder.isPlaying());
242 }
243 }
244
245
246 @Override
247 public void onStart() {
248 super.onStart();
249
250 if (mFile != null) {
251 if (mFile.isAudio()) {
252 bindMediaService();
253
254 } else if (mFile.isVideo()) {
255 stopAudio();
256 playVideo();
257 }
258 }
259 }
260
261
262 private void stopAudio() {
263 Intent i = new Intent(getSherlockActivity(), MediaService.class);
264 i.setAction(MediaService.ACTION_STOP_ALL);
265 getSherlockActivity().startService(i);
266 }
267
268
269 /**
270 * {@inheritDoc}
271 */
272 @Override
273 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
274 super.onCreateOptionsMenu(menu, inflater);
275
276 inflater.inflate(R.menu.file_actions_menu, menu);
277 List<Integer> toHide = new ArrayList<Integer>();
278
279 MenuItem item = null;
280 toHide.add(R.id.action_cancel_download);
281 toHide.add(R.id.action_cancel_upload);
282 toHide.add(R.id.action_download_file);
283 toHide.add(R.id.action_rename_file); // by now
284
285 for (int i : toHide) {
286 item = menu.findItem(i);
287 if (item != null) {
288 item.setVisible(false);
289 item.setEnabled(false);
290 }
291 }
292
293 }
294
295
296 /**
297 * {@inheritDoc}
298 */
299 @Override
300 public boolean onOptionsItemSelected(MenuItem item) {
301 switch (item.getItemId()) {
302 case R.id.action_open_file_with: {
303 openFile();
304 return true;
305 }
306 case R.id.action_remove_file: {
307 removeFile();
308 return true;
309 }
310 case R.id.action_see_details: {
311 seeDetails();
312 return true;
313 }
314
315 default:
316 return false;
317 }
318 }
319
320
321 private void seeDetails() {
322 stopPreview(false);
323 ((FileFragment.ContainerActivity)getActivity()).showFragmentWithDetails(mFile);
324 }
325
326
327 private void prepareVideo() {
328 // create helper to get more control on the playback
329 mVideoHelper = new VideoHelper();
330 mVideoPreview.setOnPreparedListener(mVideoHelper);
331 mVideoPreview.setOnCompletionListener(mVideoHelper);
332 mVideoPreview.setOnErrorListener(mVideoHelper);
333 }
334
335 private void playVideo() {
336 // create and prepare control panel for the user
337 mMediaController.setMediaPlayer(mVideoPreview);
338
339 // load the video file in the video player ; when done, VideoHelper#onPrepared() will be called
340 mVideoPreview.setVideoPath(mFile.getStoragePath());
341 }
342
343
344 private class VideoHelper implements OnCompletionListener, OnPreparedListener, OnErrorListener {
345
346 /**
347 * Called when the file is ready to be played.
348 *
349 * Just starts the playback.
350 *
351 * @param mp {@link MediaPlayer} instance performing the playback.
352 */
353 @Override
354 public void onPrepared(MediaPlayer vp) {
355 mVideoPreview.seekTo(mSavedPlaybackPosition);
356 if (mAutoplay) {
357 mVideoPreview.start();
358 }
359 mMediaController.setEnabled(true);
360 mMediaController.updatePausePlay();
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 mVideoPreview.seekTo(0);
374 mMediaController.updatePausePlay();
375 }
376
377
378 /**
379 * Called when an error in playback occurs.
380 *
381 * @param mp {@link MediaPlayer} instance performing the playback.
382 * @param what Type of error
383 * @param extra Extra code specific to the error
384 */
385 @Override
386 public boolean onError(MediaPlayer mp, int what, int extra) {
387 if (mVideoPreview.getWindowToken() != null) {
388 String message = MediaService.getMessageForMediaError(getActivity(), what, extra);
389 new AlertDialog.Builder(getActivity())
390 .setMessage(message)
391 .setPositiveButton(android.R.string.VideoView_error_button,
392 new DialogInterface.OnClickListener() {
393 public void onClick(DialogInterface dialog, int whichButton) {
394 dialog.dismiss();
395 VideoHelper.this.onCompletion(null);
396 }
397 })
398 .setCancelable(false)
399 .show();
400 }
401 return true;
402 }
403
404 }
405
406
407 @Override
408 public void onStop() {
409 super.onStop();
410
411 if (mMediaServiceConnection != null) {
412 Log.d(TAG, "Unbinding from MediaService ...");
413 if (mMediaServiceBinder != null && mMediaController != null) {
414 mMediaServiceBinder.unregisterMediaController(mMediaController);
415 }
416 getActivity().unbindService(mMediaServiceConnection);
417 mMediaServiceConnection = null;
418 mMediaServiceBinder = null;
419 }
420 }
421
422 @Override
423 public boolean onTouch(View v, MotionEvent event) {
424 if (event.getAction() == MotionEvent.ACTION_DOWN && v == mVideoPreview) {
425 startFullScreenVideo();
426 return true;
427 }
428 return false;
429 }
430
431
432 private void startFullScreenVideo() {
433 Intent i = new Intent(getActivity(), PreviewVideoActivity.class);
434 i.putExtra(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount);
435 i.putExtra(PreviewVideoActivity.EXTRA_FILE, mFile);
436 i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying());
437 mVideoPreview.pause();
438 i.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPreview.getCurrentPosition());
439 startActivityForResult(i, 0);
440 }
441
442
443 @Override
444 public void onActivityResult (int requestCode, int resultCode, Intent data) {
445 super.onActivityResult(requestCode, resultCode, data);
446 if (resultCode == Activity.RESULT_OK) {
447 mSavedPlaybackPosition = data.getExtras().getInt(PreviewVideoActivity.EXTRA_START_POSITION);
448 mAutoplay = data.getExtras().getBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY);
449 }
450 }
451
452
453 private void playAudio() {
454 if (!mMediaServiceBinder.isPlaying(mFile)) {
455 Log.d(TAG, "starting playback of " + mFile.getStoragePath());
456 mMediaServiceBinder.start(mAccount, mFile, mAutoplay, mSavedPlaybackPosition);
457
458 } else {
459 if (!mMediaServiceBinder.isPlaying() && mAutoplay) {
460 mMediaServiceBinder.start();
461 mMediaController.updatePausePlay();
462 }
463 }
464 }
465
466
467 private void bindMediaService() {
468 Log.d(TAG, "Binding to MediaService...");
469 if (mMediaServiceConnection == null) {
470 mMediaServiceConnection = new MediaServiceConnection();
471 }
472 getActivity().bindService( new Intent(getActivity(),
473 MediaService.class),
474 mMediaServiceConnection,
475 Context.BIND_AUTO_CREATE);
476 // follow the flow in MediaServiceConnection#onServiceConnected(...)
477 }
478
479 /** Defines callbacks for service binding, passed to bindService() */
480 private class MediaServiceConnection implements ServiceConnection {
481
482 @Override
483 public void onServiceConnected(ComponentName component, IBinder service) {
484 if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
485 Log.d(TAG, "Media service connected");
486 mMediaServiceBinder = (MediaServiceBinder) service;
487 if (mMediaServiceBinder != null) {
488 prepareMediaController();
489 playAudio(); // do not wait for the touch of nobody to play audio
490
491 Log.d(TAG, "Successfully bound to MediaService, MediaController ready");
492
493 } else {
494 Log.e(TAG, "Unexpected response from MediaService while binding");
495 }
496 }
497 }
498
499 private void prepareMediaController() {
500 mMediaServiceBinder.registerMediaController(mMediaController);
501 if (mMediaController != null) {
502 mMediaController.setMediaPlayer(mMediaServiceBinder);
503 mMediaController.setEnabled(true);
504 mMediaController.updatePausePlay();
505 }
506 }
507
508 @Override
509 public void onServiceDisconnected(ComponentName component) {
510 if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
511 Log.e(TAG, "Media service suddenly disconnected");
512 if (mMediaController != null) {
513 mMediaController.setMediaPlayer(null);
514 } else {
515 Toast.makeText(getActivity(), "No media controller to release when disconnected from media service", Toast.LENGTH_SHORT).show();
516 }
517 mMediaServiceBinder = null;
518 mMediaServiceConnection = null;
519 }
520 }
521 }
522
523
524
525 /**
526 * Opens the previewed file with an external application.
527 *
528 * TODO - improve this; instead of prioritize the actions available for the MIME type in the server,
529 * we should get a list of available apps for MIME tpye in the server and join it with the list of
530 * available apps for the MIME type known from the file extension, to let the user choose
531 */
532 private void openFile() {
533 stopPreview(true);
534 String storagePath = mFile.getStoragePath();
535 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
536 try {
537 Intent i = new Intent(Intent.ACTION_VIEW);
538 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());
539 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
540 startActivity(i);
541
542 } catch (Throwable t) {
543 Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());
544 boolean toastIt = true;
545 String mimeType = "";
546 try {
547 Intent i = new Intent(Intent.ACTION_VIEW);
548 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
549 if (mimeType == null || !mimeType.equals(mFile.getMimetype())) {
550 if (mimeType != null) {
551 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);
552 } else {
553 // desperate try
554 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), "*-/*");
555 }
556 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
557 startActivity(i);
558 toastIt = false;
559 }
560
561 } catch (IndexOutOfBoundsException e) {
562 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
563
564 } catch (ActivityNotFoundException e) {
565 Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
566
567 } catch (Throwable th) {
568 Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);
569
570 } finally {
571 if (toastIt) {
572 Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
573 }
574 }
575
576 }
577 finish();
578 }
579
580 /**
581 * Starts a the removal of the previewed file.
582 *
583 * Shows a confirmation dialog. The action continues in {@link #onConfirmation(String)} , {@link #onNeutral(String)} or {@link #onCancel(String)},
584 * depending upon the user selection in the dialog.
585 */
586 private void removeFile() {
587 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
588 R.string.confirmation_remove_alert,
589 new String[]{mFile.getFileName()},
590 R.string.confirmation_remove_remote_and_local,
591 R.string.confirmation_remove_local,
592 R.string.common_cancel);
593 confDialog.setOnConfirmationListener(this);
594 confDialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
595 }
596
597
598 /**
599 * Performs the removal of the previewed file, both locally and in the server.
600 */
601 @Override
602 public void onConfirmation(String callerTag) {
603 if (mStorageManager.getFileById(mFile.getFileId()) != null) { // check that the file is still there;
604 stopPreview(true);
605 mLastRemoteOperation = new RemoveFileOperation( mFile, // TODO we need to review the interface with RemoteOperations, and use OCFile IDs instead of OCFile objects as parameters
606 true,
607 mStorageManager);
608 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());
609 mLastRemoteOperation.execute(wc, this, mHandler);
610
611 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
612 getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
613 }
614 }
615
616
617 /**
618 * Removes the file from local storage
619 */
620 @Override
621 public void onNeutral(String callerTag) {
622 // TODO this code should be made in a secondary thread,
623 if (mFile.isDown()) { // checks it is still there
624 stopPreview(true);
625 File f = new File(mFile.getStoragePath());
626 f.delete();
627 mFile.setStoragePath(null);
628 mStorageManager.saveFile(mFile);
629 finish();
630 }
631 }
632
633 /**
634 * User cancelled the removal action.
635 */
636 @Override
637 public void onCancel(String callerTag) {
638 // nothing to do here
639 }
640
641
642 /**
643 * {@inheritDoc}
644 */
645 public OCFile getFile(){
646 return mFile;
647 }
648
649 /*
650 /**
651 * Use this method to signal this Activity that it shall update its view.
652 *
653 * @param file : An {@link OCFile}
654 *-/
655 public void updateFileDetails(OCFile file, Account ocAccount) {
656 mFile = file;
657 if (ocAccount != null && (
658 mStorageManager == null ||
659 (mAccount != null && !mAccount.equals(ocAccount))
660 )) {
661 mStorageManager = new FileDataStorageManager(ocAccount, getActivity().getApplicationContext().getContentResolver());
662 }
663 mAccount = ocAccount;
664 updateFileDetails(false);
665 }
666 */
667
668
669 /**
670 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed.
671 *
672 * @param file File to test if can be previewed.
673 * @return 'True' if the file can be handled by the fragment.
674 */
675 public static boolean canBePreviewed(OCFile file) {
676 return (file != null && (file.isAudio() || file.isVideo()));
677 }
678
679 /**
680 * {@inheritDoc}
681 */
682 @Override
683 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
684 if (operation.equals(mLastRemoteOperation)) {
685 if (operation instanceof RemoveFileOperation) {
686 onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
687 }
688 }
689 }
690
691 private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
692 boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
693 getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
694
695 if (result.isSuccess()) {
696 Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
697 msg.show();
698 finish();
699
700 } else {
701 Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG);
702 msg.show();
703 if (result.isSslRecoverableException()) {
704 // TODO show the SSL warning dialog
705 }
706 }
707 }
708
709 private void stopPreview(boolean stopAudio) {
710 if (mFile.isAudio() && stopAudio) {
711 mMediaServiceBinder.pause();
712
713 } else if (mFile.isVideo()) {
714 mVideoPreview.stopPlayback();
715 }
716 }
717
718
719
720 /**
721 * Finishes the preview
722 */
723 private void finish() {
724 Activity container = getActivity();
725 if (container instanceof FileDisplayActivity) {
726 // double pane
727 FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
728 transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FileDetailFragment.FTAG); // empty FileDetailFragment
729 transaction.commit();
730 ((FileFragment.ContainerActivity)container).onFileStateChanged();
731 } else {
732 container.finish();
733 }
734 }
735
736 }